token.h revision bb769b257e753aafcbd96767abb2abc645eaa20c
1// Copyright 2006-2008 the V8 project authors. All rights reserved. 2// Redistribution and use in source and binary forms, with or without 3// modification, are permitted provided that the following conditions are 4// met: 5// 6// * Redistributions of source code must retain the above copyright 7// notice, this list of conditions and the following disclaimer. 8// * Redistributions in binary form must reproduce the above 9// copyright notice, this list of conditions and the following 10// disclaimer in the documentation and/or other materials provided 11// with the distribution. 12// * Neither the name of Google Inc. nor the names of its 13// contributors may be used to endorse or promote products derived 14// from this software without specific prior written permission. 15// 16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 28#ifndef V8_TOKEN_H_ 29#define V8_TOKEN_H_ 30 31namespace v8 { 32namespace internal { 33 34// TOKEN_LIST takes a list of 3 macros M, all of which satisfy the 35// same signature M(name, string, precedence), where name is the 36// symbolic token name, string is the corresponding syntactic symbol 37// (or NULL, for literals), and precedence is the precedence (or 0). 38// The parameters are invoked for token categories as follows: 39// 40// T: Non-keyword tokens 41// K: Keyword tokens 42// F: Future (reserved) keyword tokens 43 44// IGNORE_TOKEN is a convenience macro that can be supplied as 45// an argument (at any position) for a TOKEN_LIST call. It does 46// nothing with tokens belonging to the respective category. 47 48#define IGNORE_TOKEN(name, string, precedence) 49 50#define TOKEN_LIST(T, K, F) \ 51 /* End of source indicator. */ \ 52 T(EOS, "EOS", 0) \ 53 \ 54 /* Punctuators (ECMA-262, section 7.7, page 15). */ \ 55 T(LPAREN, "(", 0) \ 56 T(RPAREN, ")", 0) \ 57 T(LBRACK, "[", 0) \ 58 T(RBRACK, "]", 0) \ 59 T(LBRACE, "{", 0) \ 60 T(RBRACE, "}", 0) \ 61 T(COLON, ":", 0) \ 62 T(SEMICOLON, ";", 0) \ 63 T(PERIOD, ".", 0) \ 64 T(CONDITIONAL, "?", 3) \ 65 T(INC, "++", 0) \ 66 T(DEC, "--", 0) \ 67 \ 68 /* Assignment operators. */ \ 69 /* IsAssignmentOp() and Assignment::is_compound() relies on */ \ 70 /* this block of enum values being contiguous and sorted in the */ \ 71 /* same order! */ \ 72 T(INIT_VAR, "=init_var", 2) /* AST-use only. */ \ 73 T(INIT_CONST, "=init_const", 2) /* AST-use only. */ \ 74 T(ASSIGN, "=", 2) \ 75 T(ASSIGN_BIT_OR, "|=", 2) \ 76 T(ASSIGN_BIT_XOR, "^=", 2) \ 77 T(ASSIGN_BIT_AND, "&=", 2) \ 78 T(ASSIGN_SHL, "<<=", 2) \ 79 T(ASSIGN_SAR, ">>=", 2) \ 80 T(ASSIGN_SHR, ">>>=", 2) \ 81 T(ASSIGN_ADD, "+=", 2) \ 82 T(ASSIGN_SUB, "-=", 2) \ 83 T(ASSIGN_MUL, "*=", 2) \ 84 T(ASSIGN_DIV, "/=", 2) \ 85 T(ASSIGN_MOD, "%=", 2) \ 86 \ 87 /* Binary operators sorted by precedence. */ \ 88 /* IsBinaryOp() relies on this block of enum values */ \ 89 /* being contiguous and sorted in the same order! */ \ 90 T(COMMA, ",", 1) \ 91 T(OR, "||", 4) \ 92 T(AND, "&&", 5) \ 93 T(BIT_OR, "|", 6) \ 94 T(BIT_XOR, "^", 7) \ 95 T(BIT_AND, "&", 8) \ 96 T(SHL, "<<", 11) \ 97 T(SAR, ">>", 11) \ 98 T(SHR, ">>>", 11) \ 99 T(ADD, "+", 12) \ 100 T(SUB, "-", 12) \ 101 T(MUL, "*", 13) \ 102 T(DIV, "/", 13) \ 103 T(MOD, "%", 13) \ 104 \ 105 /* Compare operators sorted by precedence. */ \ 106 /* IsCompareOp() relies on this block of enum values */ \ 107 /* being contiguous and sorted in the same order! */ \ 108 T(EQ, "==", 9) \ 109 T(NE, "!=", 9) \ 110 T(EQ_STRICT, "===", 9) \ 111 T(NE_STRICT, "!==", 9) \ 112 T(LT, "<", 10) \ 113 T(GT, ">", 10) \ 114 T(LTE, "<=", 10) \ 115 T(GTE, ">=", 10) \ 116 K(INSTANCEOF, "instanceof", 10) \ 117 K(IN, "in", 10) \ 118 \ 119 /* Unary operators. */ \ 120 /* IsUnaryOp() relies on this block of enum values */ \ 121 /* being contiguous and sorted in the same order! */ \ 122 T(NOT, "!", 0) \ 123 T(BIT_NOT, "~", 0) \ 124 K(DELETE, "delete", 0) \ 125 K(TYPEOF, "typeof", 0) \ 126 K(VOID, "void", 0) \ 127 \ 128 /* Keywords (ECMA-262, section 7.5.2, page 13). */ \ 129 K(BREAK, "break", 0) \ 130 K(CASE, "case", 0) \ 131 K(CATCH, "catch", 0) \ 132 K(CONTINUE, "continue", 0) \ 133 K(DEBUGGER, "debugger", 0) \ 134 K(DEFAULT, "default", 0) \ 135 /* DELETE */ \ 136 K(DO, "do", 0) \ 137 K(ELSE, "else", 0) \ 138 K(FINALLY, "finally", 0) \ 139 K(FOR, "for", 0) \ 140 K(FUNCTION, "function", 0) \ 141 K(IF, "if", 0) \ 142 /* IN */ \ 143 /* INSTANCEOF */ \ 144 K(NEW, "new", 0) \ 145 K(RETURN, "return", 0) \ 146 K(SWITCH, "switch", 0) \ 147 K(THIS, "this", 0) \ 148 K(THROW, "throw", 0) \ 149 K(TRY, "try", 0) \ 150 /* TYPEOF */ \ 151 K(VAR, "var", 0) \ 152 /* VOID */ \ 153 K(WHILE, "while", 0) \ 154 K(WITH, "with", 0) \ 155 \ 156 /* Future reserved words (ECMA-262, section 7.5.3, page 14). */ \ 157 F(ABSTRACT, "abstract", 0) \ 158 F(BOOLEAN, "boolean", 0) \ 159 F(BYTE, "byte", 0) \ 160 F(CHAR, "char", 0) \ 161 F(CLASS, "class", 0) \ 162 K(CONST, "const", 0) \ 163 F(DOUBLE, "double", 0) \ 164 F(ENUM, "enum", 0) \ 165 F(EXPORT, "export", 0) \ 166 F(EXTENDS, "extends", 0) \ 167 F(FINAL, "final", 0) \ 168 F(FLOAT, "float", 0) \ 169 F(GOTO, "goto", 0) \ 170 F(IMPLEMENTS, "implements", 0) \ 171 F(IMPORT, "import", 0) \ 172 F(INT, "int", 0) \ 173 F(INTERFACE, "interface", 0) \ 174 F(LONG, "long", 0) \ 175 K(NATIVE, "native", 0) \ 176 F(PACKAGE, "package", 0) \ 177 F(PRIVATE, "private", 0) \ 178 F(PROTECTED, "protected", 0) \ 179 F(PUBLIC, "public", 0) \ 180 F(SHORT, "short", 0) \ 181 F(STATIC, "static", 0) \ 182 F(SUPER, "super", 0) \ 183 F(SYNCHRONIZED, "synchronized", 0) \ 184 F(THROWS, "throws", 0) \ 185 F(TRANSIENT, "transient", 0) \ 186 F(VOLATILE, "volatile", 0) \ 187 \ 188 /* Literals (ECMA-262, section 7.8, page 16). */ \ 189 K(NULL_LITERAL, "null", 0) \ 190 K(TRUE_LITERAL, "true", 0) \ 191 K(FALSE_LITERAL, "false", 0) \ 192 T(NUMBER, NULL, 0) \ 193 T(STRING, NULL, 0) \ 194 \ 195 /* Identifiers (not keywords or future reserved words). */ \ 196 T(IDENTIFIER, NULL, 0) \ 197 \ 198 /* Illegal token - not able to scan. */ \ 199 T(ILLEGAL, "ILLEGAL", 0) \ 200 \ 201 /* Scanner-internal use only. */ \ 202 T(WHITESPACE, NULL, 0) 203 204 205class Token { 206 public: 207 // All token values. 208#define T(name, string, precedence) name, 209 enum Value { 210 TOKEN_LIST(T, T, IGNORE_TOKEN) 211 NUM_TOKENS 212 }; 213#undef T 214 215 // Returns a string corresponding to the C++ token name 216 // (e.g. "LT" for the token LT). 217 static const char* Name(Value tok) { 218 ASSERT(0 <= tok && tok < NUM_TOKENS); 219 return name_[tok]; 220 } 221 222 // Predicates 223 static bool IsKeyword(Value tok) { 224 return token_type[tok] == 'K'; 225 } 226 227 static bool IsAssignmentOp(Value tok) { 228 return INIT_VAR <= tok && tok <= ASSIGN_MOD; 229 } 230 231 static bool IsBinaryOp(Value op) { 232 return COMMA <= op && op <= MOD; 233 } 234 235 static bool IsCompareOp(Value op) { 236 return EQ <= op && op <= IN; 237 } 238 239 static bool IsBitOp(Value op) { 240 return (BIT_OR <= op && op <= SHR) || op == BIT_NOT; 241 } 242 243 static bool IsUnaryOp(Value op) { 244 return (NOT <= op && op <= VOID) || op == ADD || op == SUB; 245 } 246 247 static bool IsCountOp(Value op) { 248 return op == INC || op == DEC; 249 } 250 251 // Returns a string corresponding to the JS token string 252 // (.e., "<" for the token LT) or NULL if the token doesn't 253 // have a (unique) string (e.g. an IDENTIFIER). 254 static const char* String(Value tok) { 255 ASSERT(0 <= tok && tok < NUM_TOKENS); 256 return string_[tok]; 257 } 258 259 // Returns the precedence > 0 for binary and compare 260 // operators; returns 0 otherwise. 261 static int Precedence(Value tok) { 262 ASSERT(0 <= tok && tok < NUM_TOKENS); 263 return precedence_[tok]; 264 } 265 266 private: 267 static const char* name_[NUM_TOKENS]; 268 static const char* string_[NUM_TOKENS]; 269 static int8_t precedence_[NUM_TOKENS]; 270 static const char token_type[NUM_TOKENS]; 271}; 272 273} } // namespace v8::internal 274 275#endif // V8_TOKEN_H_ 276