Skip to content

Commit

Permalink
kernel: clang-format GetNumber
Browse files Browse the repository at this point in the history
The code formatting in this function is way off and that makes it hard
to read (granted, it used to be even worse before recent refactoring).
Let's clang-format it -- tools like `tig blame` easily dig through this.
  • Loading branch information
fingolfin committed May 22, 2020
1 parent 9ec66cf commit 0d6f7ad
Showing 1 changed file with 105 additions and 99 deletions.
204 changes: 105 additions & 99 deletions src/scanner.c
Original file line number Diff line number Diff line change
Expand Up @@ -385,138 +385,144 @@ static UInt AddCharToValue(ScannerState * s, UInt pos, Char c)

static UInt GetNumber(ScannerState * s, Int readDecimalPoint, Char c)
{
UInt symbol = S_ILLEGAL;
UInt i = 0;
BOOL seenADigit = FALSE;
UInt symbol = S_ILLEGAL;
UInt i = 0;
BOOL seenADigit = FALSE;

s->ValueObj = 0;
s->ValueObj = 0;

if (readDecimalPoint) {
s->Value[i++] = '.';
}
else {
// read initial sequence of digits into 'Value'
while (IsDigit(c)) {
i = AddCharToValue(s, i, c);
seenADigit = TRUE;
c = GET_NEXT_CHAR();
if (readDecimalPoint) {
s->Value[i++] = '.';
}
else {
// read initial sequence of digits into 'Value'
while (IsDigit(c)) {
i = AddCharToValue(s, i, c);
seenADigit = TRUE;
c = GET_NEXT_CHAR();
}

// maybe we saw an identifier character and realised that this is an
// identifier we are reading
if (IsIdent(c) || c == '\\') {
// if necessary, copy back from s->ValueObj to s->Value
if (s->ValueObj) {
i = GET_LEN_STRING(s->ValueObj);
GAP_ASSERT(i >= MAX_VALUE_LEN - 1);
memcpy(s->Value, CONST_CSTR_STRING(s->ValueObj), MAX_VALUE_LEN);
s->ValueObj = 0;
}
// this looks like an identifier, scan the rest of it
return GetIdent(s, i, c);
}

// Or maybe we saw a '.' which could indicate one of three things:
// - a float literal: 12.345
// - S_DOT, i.e., '.' used to access a record entry: r.12.345
// - S_DDOT, i.e., '..' in a range expression: [12..345]
if (c == '.') {
GAP_ASSERT(i < MAX_VALUE_LEN - 1);

// If the symbol before this integer was S_DOT then we must be in
// a nested record element expression, so don't look for a float.
// This is a bit fragile
if (s->Symbol == S_DOT || s->Symbol == S_BDOT) {
symbol = S_INT;
goto finish;
}
// maybe we saw an identifier character and realised that this is an
// identifier we are reading
if (IsIdent(c) || c == '\\') {
// if necessary, copy back from s->ValueObj to s->Value
if (s->ValueObj) {
i = GET_LEN_STRING(s->ValueObj);
GAP_ASSERT(i >= MAX_VALUE_LEN - 1);
memcpy(s->Value, CONST_CSTR_STRING(s->ValueObj),
MAX_VALUE_LEN);
s->ValueObj = 0;
}
// this looks like an identifier, scan the rest of it
return GetIdent(s, i, c);
}

// peek ahead to decide if we are looking at a range expression
if (PEEK_NEXT_CHAR() == '.') {
// we are looking at '..' and are probably inside a range expression
symbol = S_INT;
goto finish;
}
// Or maybe we saw a '.' which could indicate one of three things:
// - a float literal: 12.345
// - S_DOT, i.e., '.' used to access a record entry: r.12.345
// - S_DDOT, i.e., '..' in a range expression: [12..345]
if (c == '.') {
GAP_ASSERT(i < MAX_VALUE_LEN - 1);

// If the symbol before this integer was S_DOT then we must be in
// a nested record element expression, so don't look for a float.
// This is a bit fragile
if (s->Symbol == S_DOT || s->Symbol == S_BDOT) {
symbol = S_INT;
goto finish;
}

// Now the '.' must be part of our number; store it and move on
i = AddCharToValue(s, i, '.');
c = GET_NEXT_CHAR();
}
else {
// Anything else we see tells us that the token is done
symbol = S_INT;
goto finish;
// peek ahead to decide if we are looking at a range expression
if (PEEK_NEXT_CHAR() == '.') {
// we are looking at '..' and are probably inside a range
// expression
symbol = S_INT;
goto finish;
}

// Now the '.' must be part of our number; store it and move on
i = AddCharToValue(s, i, '.');
c = GET_NEXT_CHAR();
}
else {
// Anything else we see tells us that the token is done
symbol = S_INT;
goto finish;
}
}
}

// When we get here we have read possibly some digits, a . and possibly
// some more digits, but not an e,E,d,D,q or Q
// In any case, from now on, we know we are dealing with a float literal
symbol = S_FLOAT;
// When we get here we have read possibly some digits, a . and possibly
// some more digits, but not an e,E,d,D,q or Q
// In any case, from now on, we know we are dealing with a float literal
symbol = S_FLOAT;

// read digits
while (IsDigit(c)) {
i = AddCharToValue(s, i, c);
seenADigit = TRUE;
c = GET_NEXT_CHAR();
i = AddCharToValue(s, i, c);
seenADigit = TRUE;
c = GET_NEXT_CHAR();
}
if (!seenADigit)
SyntaxError(s, "Badly formed number: need a digit before or after the "
"decimal point");
SyntaxError(s,
"Badly formed number: need a digit before or after the "
"decimal point");
if (c == '\\')
SyntaxError(s, "Badly formed number");
SyntaxError(s, "Badly formed number");

// If the next thing is the start of the exponential notation, read it now.
if (c == 'e' || c == 'E' || c == 'd' || c == 'D' || c == 'q' || c == 'Q') {
i = AddCharToValue(s, i, c);
c = GET_NEXT_CHAR();
if (c == '+' || c == '-') {
// If the next thing is the start of the exponential notation, read it
// now.
if (c == 'e' || c == 'E' || c == 'd' || c == 'D' || c == 'q' ||
c == 'Q') {
i = AddCharToValue(s, i, c);
c = GET_NEXT_CHAR();
}
if (c == '+' || c == '-') {
i = AddCharToValue(s, i, c);
c = GET_NEXT_CHAR();
}

// Here we are into the unsigned exponent of a number in scientific
// notation, so we just read digits
if (!IsDigit(c))
SyntaxError(s,
"Badly formed number: need at least one digit in the exponent");
while (IsDigit(c)) {
i = AddCharToValue(s, i, c);
c = GET_NEXT_CHAR();
}
// Here we are into the unsigned exponent of a number in scientific
// notation, so we just read digits
if (!IsDigit(c))
SyntaxError(s, "Badly formed number: need at least one digit in "
"the exponent");
while (IsDigit(c)) {
i = AddCharToValue(s, i, c);
c = GET_NEXT_CHAR();
}
}

// Allow one letter at the end of the number, which is a conversion
// marker; e.g. an `i` as in C99, to indicate an imaginary value.
if (IsAlpha(c)) {
// Allow one letter at the end of the number, which is a conversion
// marker; e.g. an `i` as in C99, to indicate an imaginary value.
if (IsAlpha(c)) {
i = AddCharToValue(s, i, c);
c = GET_NEXT_CHAR();
}
}

// independently of that, we allow an _ signalling immediate or "eager" conversion
if (c == '_') {
// independently of that, we allow an _ signalling immediate or "eager"
// conversion
if (c == '_') {
i = AddCharToValue(s, i, c);
c = GET_NEXT_CHAR();
// After which there may be one character signifying the
// conversion styles
if (IsAlpha(c)) {
i = AddCharToValue(s, i, c);
c = GET_NEXT_CHAR();
i = AddCharToValue(s, i, c);
c = GET_NEXT_CHAR();
}
}
}

// Now if the next character is an identifier symbol then we have an error
if (IsIdent(c)) {
// Now if the next character is an identifier symbol then we have an error
if (IsIdent(c)) {
SyntaxError(s, "Badly formed number");
}
}

finish:
i = AddCharToValue(s, i, '\0');
if (s->ValueObj) {
// flush buffer
AppendBufToString(s->ValueObj, s->Value, i - 1);
}
return symbol;
i = AddCharToValue(s, i, '\0');
if (s->ValueObj) {
// flush buffer
AppendBufToString(s->ValueObj, s->Value, i - 1);
}
return symbol;
}


Expand Down

0 comments on commit 0d6f7ad

Please sign in to comment.