Skip to content

Commit

Permalink
scanner: change GetNumber to call GetIdent if needed
Browse files Browse the repository at this point in the history
... instead of replicating the logic for parsing identifiers,
but slightly differently (i.e., with different escaping rules)
  • Loading branch information
fingolfin committed Apr 2, 2018
1 parent 50e68a0 commit 8649af6
Showing 1 changed file with 14 additions and 28 deletions.
42 changes: 14 additions & 28 deletions src/scanner.c
Original file line number Diff line number Diff line change
Expand Up @@ -191,15 +191,12 @@ void Match (
*/
static void GetIdent(Int i)
{
Int i;
Int isQuoted;

/* initially it could be a keyword */
isQuoted = 0;
// initially it could be a keyword
Int isQuoted = 0;

/* read all characters into 'STATE(Value)' */
Char c = PEEK_CURR_CHAR();
for ( i=0; IsIdent(c) || IsDigit(c) || c=='\\'; i++ ) {
for ( ; IsIdent(c) || IsDigit(c) || c=='\\'; i++ ) {

/* handle escape sequences */
/* we ignore '\ newline' by decrementing i, except at the
Expand Down Expand Up @@ -344,29 +341,18 @@ static void GetNumber ( const UInt StartingStatus )

c = PEEK_CURR_CHAR();
if (StartingStatus < 2) {
/* read initial sequence of digits into 'Value' */
for (i = 0; !wasEscaped && IsDigit(c) && i < SAFE_VALUE_SIZE-1; i++) {
STATE(Value)[i] = c;
// read initial sequence of digits into 'Value'
while (IsDigit(c) && i < SAFE_VALUE_SIZE-1) {
STATE(Value)[i++] = c;
seenADigit = 1;
c = GetCleanedChar(&wasEscaped);
c = GET_NEXT_CHAR();
}

/* So why did we run off the end of that loop */
/* maybe we saw an identifier character and realised that this is an identifier we are reading */
if (wasEscaped || IsIdent(c)) {
/* Now we know we have an identifier read the rest of it */
STATE(Value)[i++] = c;
c = GetCleanedChar(&wasEscaped);
for (; wasEscaped || IsIdent(c) || IsDigit(c); i++) {
if (i < SAFE_VALUE_SIZE -1)
STATE(Value)[i] = c;
c = GetCleanedChar(&wasEscaped);
}
if (i < SAFE_VALUE_SIZE -1)
STATE(Value)[i] = '\0';
else
STATE(Value)[SAFE_VALUE_SIZE-1] = '\0';
STATE(Symbol) = S_IDENT;
if (IsIdent(c) || c == '\\') {
// Now we know we have an identifier, read the rest of it
GetIdent(i);
return;
}

Expand Down Expand Up @@ -936,7 +922,7 @@ void GetSymbol ( void )

// switch according to the character
if (IsAlpha(c)) {
GetIdent();
GetIdent(0);
return;
}

Expand Down Expand Up @@ -991,9 +977,9 @@ void GetSymbol ( void )
case '?': STATE(Symbol) = S_HELP; GetHelp(); break;
case '"': GetMaybeTripStr(); break;
case '\'': GetChar(); break;
case '\\': GetIdent(); break;
case '_': GetIdent(); break;
case '@': GetIdent(); break;
case '\\': GetIdent(0); break;
case '_': GetIdent(0); break;
case '@': GetIdent(0); break;

case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9': GetNumber(0); break;
Expand Down

0 comments on commit 8649af6

Please sign in to comment.