src/Scanner.cpp
changeset 171 023711891116
parent 148 c0740249963c
child 172 48cffae8c9e0
     1.1 --- a/src/Scanner.cpp	Sat Aug 11 12:11:39 2012 -0700
     1.2 +++ b/src/Scanner.cpp	Sun Aug 12 13:22:56 2012 -0700
     1.3 @@ -351,6 +351,54 @@
     1.4      return TOK_FLOAT_LITERAL;
     1.5  }
     1.6  
     1.7 +static inline TokenKind
     1.8 +FindIdentifier(const char *str)
     1.9 +{
    1.10 +    if (!strcmp(str,  "new"))
    1.11 +        return TOK_NEW;
    1.12 +    if (!strcmp(str,  "for"))
    1.13 +        return TOK_FOR;
    1.14 +    if (!strcmp(str,  "return"))
    1.15 +        return TOK_RETURN;
    1.16 +    if (!strcmp(str,  "if"))
    1.17 +        return TOK_IF;
    1.18 +    if (!strcmp(str,  "else"))
    1.19 +        return TOK_ELSE;
    1.20 +    if (!strcmp(str,  "enum"))
    1.21 +        return TOK_ENUM;
    1.22 +    if (!strcmp(str,  "while"))
    1.23 +        return TOK_WHILE;
    1.24 +    if (!strcmp(str,  "do"))
    1.25 +        return TOK_DO;
    1.26 +    if (!strcmp(str,  "break"))
    1.27 +        return TOK_BREAK;
    1.28 +    if (!strcmp(str,  "continue"))
    1.29 +        return TOK_CONTINUE;
    1.30 +    if (!strcmp(str,  "forward"))
    1.31 +        return TOK_FORWARD;
    1.32 +    if (!strcmp(str,  "public"))
    1.33 +        return TOK_PUBLIC;
    1.34 +    if (!strcmp(str,  "const"))
    1.35 +        return TOK_CONST;
    1.36 +    if (!strcmp(str,  "native"))
    1.37 +        return TOK_NATIVE;
    1.38 +    if (!strcmp(str,  "stock"))
    1.39 +        return TOK_STOCK;
    1.40 +    if (!strcmp(str,  "true"))
    1.41 +        return TOK_TRUE;
    1.42 +    if (!strcmp(str,  "false"))
    1.43 +        return TOK_FALSE;
    1.44 +    if (!strcmp(str,  "struct"))
    1.45 +        return TOK_STRUCT;
    1.46 +    if (!strcmp(str,  "switch"))
    1.47 +        return TOK_SWITCH;
    1.48 +    if (!strcmp(str,  "case"))
    1.49 +        return TOK_CASE;
    1.50 +    if (!strcmp(str,  "default"))
    1.51 +        return TOK_DEFAULT;
    1.52 +    return TOK_NONE;
    1.53 +}
    1.54 +
    1.55  TokenKind
    1.56  Scanner::identifier(char first)
    1.57  {
    1.58 @@ -361,11 +409,8 @@
    1.59      char c;
    1.60      for (;;) {
    1.61          c = getChar();
    1.62 -        if (!isascii(c) || (!isalnum(c) && c != '_')) {
    1.63 -            if (c != ':')
    1.64 -                putChar(c);
    1.65 +        if (!isascii(c) || (!isalnum(c) && c != '_'))
    1.66              break;
    1.67 -        }
    1.68          if (!literal_.append(c))
    1.69              return TOK_ERROR;
    1.70      }
    1.71 @@ -373,45 +418,23 @@
    1.72      if (!literal_.append('\0'))
    1.73          return TOK_ERROR;
    1.74  
    1.75 -    if (c != ':' && !strcmp(literal(), "_"))
    1.76 +    // Most keywords do not act as labels.
    1.77 +    TokenKind kind = FindIdentifier(literal());
    1.78 +    if (kind != TOK_NONE) {
    1.79 +        putChar(c);
    1.80 +        return kind;
    1.81 +    }
    1.82 +
    1.83 +    if (c == ':')
    1.84 +        return TOK_LABEL;
    1.85 +
    1.86 +    putChar(c);
    1.87 +
    1.88 +    // _ is a special case which acts as a tag and a keyword.
    1.89 +    if (!strcmp(literal(), "_"))
    1.90          return TOK_UNDERSCORE;
    1.91 -    if (!strcmp(literal(),  "new"))
    1.92 -        return TOK_NEW;
    1.93 -    if (!strcmp(literal(),  "for"))
    1.94 -        return TOK_FOR;
    1.95 -    if (!strcmp(literal(),  "return"))
    1.96 -        return TOK_RETURN;
    1.97 -    if (!strcmp(literal(),  "if"))
    1.98 -        return TOK_IF;
    1.99 -    if (!strcmp(literal(),  "else"))
   1.100 -        return TOK_ELSE;
   1.101 -    if (!strcmp(literal(),  "enum"))
   1.102 -        return TOK_ENUM;
   1.103 -    if (!strcmp(literal(),  "while"))
   1.104 -        return TOK_WHILE;
   1.105 -    if (!strcmp(literal(),  "do"))
   1.106 -        return TOK_DO;
   1.107 -    if (!strcmp(literal(),  "break"))
   1.108 -        return TOK_BREAK;
   1.109 -    if (!strcmp(literal(),  "continue"))
   1.110 -        return TOK_CONTINUE;
   1.111 -    if (!strcmp(literal(),  "forward"))
   1.112 -        return TOK_FORWARD;
   1.113 -    if (!strcmp(literal(),  "public"))
   1.114 -        return TOK_PUBLIC;
   1.115 -    if (!strcmp(literal(),  "const"))
   1.116 -        return TOK_CONST;
   1.117 -    if (!strcmp(literal(),  "native"))
   1.118 -        return TOK_NATIVE;
   1.119 -    if (!strcmp(literal(),  "stock"))
   1.120 -        return TOK_STOCK;
   1.121 -    if (!strcmp(literal(),  "true"))
   1.122 -        return TOK_TRUE;
   1.123 -    if (!strcmp(literal(),  "false"))
   1.124 -        return TOK_FALSE;
   1.125 -    if (!strcmp(literal(), "struct"))
   1.126 -        return TOK_STRUCT;
   1.127 -    return (c == ':') ? TOK_LABEL : TOK_NAME;
   1.128 +
   1.129 +    return TOK_NAME;
   1.130  }
   1.131  
   1.132  TokenKind