1 // Copyright 2012 the V8 project authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef V8_PARSING_TOKEN_H_ 6 #define V8_PARSING_TOKEN_H_ 7 8 #include "src/base/logging.h" 9 #include "src/globals.h" 10 11 namespace v8 { 12 namespace internal { 13 14 // TOKEN_LIST takes a list of 3 macros M, all of which satisfy the 15 // same signature M(name, string, precedence), where name is the 16 // symbolic token name, string is the corresponding syntactic symbol 17 // (or NULL, for literals), and precedence is the precedence (or 0). 18 // The parameters are invoked for token categories as follows: 19 // 20 // T: Non-keyword tokens 21 // K: Keyword tokens 22 23 // IGNORE_TOKEN is a convenience macro that can be supplied as 24 // an argument (at any position) for a TOKEN_LIST call. It does 25 // nothing with tokens belonging to the respective category. 26 27 #define IGNORE_TOKEN(name, string, precedence) 28 29 #define TOKEN_LIST(T, K) \ 30 /* End of source indicator. */ \ 31 T(EOS, "EOS", 0) \ 32 \ 33 /* Punctuators (ECMA-262, section 7.7, page 15). */ \ 34 T(LPAREN, "(", 0) \ 35 T(RPAREN, ")", 0) \ 36 T(LBRACK, "[", 0) \ 37 T(RBRACK, "]", 0) \ 38 T(LBRACE, "{", 0) \ 39 T(RBRACE, "}", 0) \ 40 T(COLON, ":", 0) \ 41 T(SEMICOLON, ";", 0) \ 42 T(PERIOD, ".", 0) \ 43 T(ELLIPSIS, "...", 0) \ 44 T(CONDITIONAL, "?", 3) \ 45 T(INC, "++", 0) \ 46 T(DEC, "--", 0) \ 47 T(ARROW, "=>", 0) \ 48 \ 49 /* Assignment operators. */ \ 50 /* IsAssignmentOp() and Assignment::is_compound() relies on */ \ 51 /* this block of enum values being contiguous and sorted in the */ \ 52 /* same order! */ \ 53 T(INIT, "=init", 2) /* AST-use only. */ \ 54 T(ASSIGN, "=", 2) \ 55 T(ASSIGN_BIT_OR, "|=", 2) \ 56 T(ASSIGN_BIT_XOR, "^=", 2) \ 57 T(ASSIGN_BIT_AND, "&=", 2) \ 58 T(ASSIGN_SHL, "<<=", 2) \ 59 T(ASSIGN_SAR, ">>=", 2) \ 60 T(ASSIGN_SHR, ">>>=", 2) \ 61 T(ASSIGN_ADD, "+=", 2) \ 62 T(ASSIGN_SUB, "-=", 2) \ 63 T(ASSIGN_MUL, "*=", 2) \ 64 T(ASSIGN_DIV, "/=", 2) \ 65 T(ASSIGN_MOD, "%=", 2) \ 66 T(ASSIGN_EXP, "**=", 2) \ 67 \ 68 /* Binary operators sorted by precedence. */ \ 69 /* IsBinaryOp() relies on this block of enum values */ \ 70 /* being contiguous and sorted in the same order! */ \ 71 T(COMMA, ",", 1) \ 72 T(OR, "||", 4) \ 73 T(AND, "&&", 5) \ 74 T(BIT_OR, "|", 6) \ 75 T(BIT_XOR, "^", 7) \ 76 T(BIT_AND, "&", 8) \ 77 T(SHL, "<<", 11) \ 78 T(SAR, ">>", 11) \ 79 T(SHR, ">>>", 11) \ 80 T(ROR, "rotate right", 11) /* only used by Crankshaft */ \ 81 T(ADD, "+", 12) \ 82 T(SUB, "-", 12) \ 83 T(MUL, "*", 13) \ 84 T(DIV, "/", 13) \ 85 T(MOD, "%", 13) \ 86 T(EXP, "**", 14) \ 87 \ 88 /* Compare operators sorted by precedence. */ \ 89 /* IsCompareOp() relies on this block of enum values */ \ 90 /* being contiguous and sorted in the same order! */ \ 91 T(EQ, "==", 9) \ 92 T(NE, "!=", 9) \ 93 T(EQ_STRICT, "===", 9) \ 94 T(NE_STRICT, "!==", 9) \ 95 T(LT, "<", 10) \ 96 T(GT, ">", 10) \ 97 T(LTE, "<=", 10) \ 98 T(GTE, ">=", 10) \ 99 K(INSTANCEOF, "instanceof", 10) \ 100 K(IN, "in", 10) \ 101 \ 102 /* Unary operators. */ \ 103 /* IsUnaryOp() relies on this block of enum values */ \ 104 /* being contiguous and sorted in the same order! */ \ 105 T(NOT, "!", 0) \ 106 T(BIT_NOT, "~", 0) \ 107 K(DELETE, "delete", 0) \ 108 K(TYPEOF, "typeof", 0) \ 109 K(VOID, "void", 0) \ 110 \ 111 /* Keywords (ECMA-262, section 7.5.2, page 13). */ \ 112 K(BREAK, "break", 0) \ 113 K(CASE, "case", 0) \ 114 K(CATCH, "catch", 0) \ 115 K(CONTINUE, "continue", 0) \ 116 K(DEBUGGER, "debugger", 0) \ 117 K(DEFAULT, "default", 0) \ 118 /* DELETE */ \ 119 K(DO, "do", 0) \ 120 K(ELSE, "else", 0) \ 121 K(FINALLY, "finally", 0) \ 122 K(FOR, "for", 0) \ 123 K(FUNCTION, "function", 0) \ 124 K(IF, "if", 0) \ 125 /* IN */ \ 126 /* INSTANCEOF */ \ 127 K(NEW, "new", 0) \ 128 K(RETURN, "return", 0) \ 129 K(SWITCH, "switch", 0) \ 130 K(THIS, "this", 0) \ 131 K(THROW, "throw", 0) \ 132 K(TRY, "try", 0) \ 133 /* TYPEOF */ \ 134 K(VAR, "var", 0) \ 135 /* VOID */ \ 136 K(WHILE, "while", 0) \ 137 K(WITH, "with", 0) \ 138 \ 139 /* Literals (ECMA-262, section 7.8, page 16). */ \ 140 K(NULL_LITERAL, "null", 0) \ 141 K(TRUE_LITERAL, "true", 0) \ 142 K(FALSE_LITERAL, "false", 0) \ 143 T(NUMBER, NULL, 0) \ 144 T(SMI, NULL, 0) \ 145 T(STRING, NULL, 0) \ 146 \ 147 /* Identifiers (not keywords or future reserved words). */ \ 148 T(IDENTIFIER, NULL, 0) \ 149 \ 150 /* Future reserved words (ECMA-262, section 7.6.1.2). */ \ 151 T(FUTURE_STRICT_RESERVED_WORD, NULL, 0) \ 152 K(ASYNC, "async", 0) \ 153 /* `await` is a reserved word in module code only */ \ 154 K(AWAIT, "await", 0) \ 155 K(CLASS, "class", 0) \ 156 K(CONST, "const", 0) \ 157 K(ENUM, "enum", 0) \ 158 K(EXPORT, "export", 0) \ 159 K(EXTENDS, "extends", 0) \ 160 K(IMPORT, "import", 0) \ 161 K(LET, "let", 0) \ 162 K(STATIC, "static", 0) \ 163 K(YIELD, "yield", 0) \ 164 K(SUPER, "super", 0) \ 165 \ 166 /* Illegal token - not able to scan. */ \ 167 T(ILLEGAL, "ILLEGAL", 0) \ 168 T(ESCAPED_KEYWORD, NULL, 0) \ 169 T(ESCAPED_STRICT_RESERVED_WORD, NULL, 0) \ 170 \ 171 /* Scanner-internal use only. */ \ 172 T(WHITESPACE, NULL, 0) \ 173 T(UNINITIALIZED, NULL, 0) \ 174 \ 175 /* ES6 Template Literals */ \ 176 T(TEMPLATE_SPAN, NULL, 0) \ 177 T(TEMPLATE_TAIL, NULL, 0) 178 179 class Token { 180 public: 181 // All token values. 182 #define T(name, string, precedence) name, 183 enum Value { 184 TOKEN_LIST(T, T) 185 NUM_TOKENS 186 }; 187 #undef T 188 189 // Returns a string corresponding to the C++ token name 190 // (e.g. "LT" for the token LT). Name(Value tok)191 static const char* Name(Value tok) { 192 DCHECK(tok < NUM_TOKENS); // tok is unsigned 193 return name_[tok]; 194 } 195 196 // Predicates IsKeyword(Value tok)197 static bool IsKeyword(Value tok) { 198 return token_type[tok] == 'K'; 199 } 200 IsIdentifier(Value tok,LanguageMode language_mode,bool is_generator,bool is_module)201 static bool IsIdentifier(Value tok, LanguageMode language_mode, 202 bool is_generator, bool is_module) { 203 switch (tok) { 204 case IDENTIFIER: 205 case ASYNC: 206 return true; 207 case ESCAPED_STRICT_RESERVED_WORD: 208 case FUTURE_STRICT_RESERVED_WORD: 209 case LET: 210 case STATIC: 211 return is_sloppy(language_mode); 212 case YIELD: 213 return !is_generator && is_sloppy(language_mode); 214 case AWAIT: 215 return !is_module; 216 default: 217 return false; 218 } 219 UNREACHABLE(); 220 return false; 221 } 222 IsAssignmentOp(Value tok)223 static bool IsAssignmentOp(Value tok) { 224 return INIT <= tok && tok <= ASSIGN_EXP; 225 } 226 IsBinaryOp(Value op)227 static bool IsBinaryOp(Value op) { return COMMA <= op && op <= EXP; } 228 IsTruncatingBinaryOp(Value op)229 static bool IsTruncatingBinaryOp(Value op) { 230 return BIT_OR <= op && op <= ROR; 231 } 232 IsCompareOp(Value op)233 static bool IsCompareOp(Value op) { 234 return EQ <= op && op <= IN; 235 } 236 IsOrderedRelationalCompareOp(Value op)237 static bool IsOrderedRelationalCompareOp(Value op) { 238 return op == LT || op == LTE || op == GT || op == GTE; 239 } 240 IsEqualityOp(Value op)241 static bool IsEqualityOp(Value op) { 242 return op == EQ || op == EQ_STRICT; 243 } 244 IsInequalityOp(Value op)245 static bool IsInequalityOp(Value op) { 246 return op == NE || op == NE_STRICT; 247 } 248 IsArithmeticCompareOp(Value op)249 static bool IsArithmeticCompareOp(Value op) { 250 return IsOrderedRelationalCompareOp(op) || 251 IsEqualityOp(op) || IsInequalityOp(op); 252 } 253 NegateCompareOp(Value op)254 static Value NegateCompareOp(Value op) { 255 DCHECK(IsArithmeticCompareOp(op)); 256 switch (op) { 257 case EQ: return NE; 258 case NE: return EQ; 259 case EQ_STRICT: return NE_STRICT; 260 case NE_STRICT: return EQ_STRICT; 261 case LT: return GTE; 262 case GT: return LTE; 263 case LTE: return GT; 264 case GTE: return LT; 265 default: 266 UNREACHABLE(); 267 return op; 268 } 269 } 270 ReverseCompareOp(Value op)271 static Value ReverseCompareOp(Value op) { 272 DCHECK(IsArithmeticCompareOp(op)); 273 switch (op) { 274 case EQ: return EQ; 275 case NE: return NE; 276 case EQ_STRICT: return EQ_STRICT; 277 case NE_STRICT: return NE_STRICT; 278 case LT: return GT; 279 case GT: return LT; 280 case LTE: return GTE; 281 case GTE: return LTE; 282 default: 283 UNREACHABLE(); 284 return op; 285 } 286 } 287 EvalComparison(Value op,double op1,double op2)288 static bool EvalComparison(Value op, double op1, double op2) { 289 DCHECK(IsArithmeticCompareOp(op)); 290 switch (op) { 291 case Token::EQ: 292 case Token::EQ_STRICT: return (op1 == op2); 293 case Token::NE: return (op1 != op2); 294 case Token::LT: return (op1 < op2); 295 case Token::GT: return (op1 > op2); 296 case Token::LTE: return (op1 <= op2); 297 case Token::GTE: return (op1 >= op2); 298 default: 299 UNREACHABLE(); 300 return false; 301 } 302 } 303 IsBitOp(Value op)304 static bool IsBitOp(Value op) { 305 return (BIT_OR <= op && op <= SHR) || op == BIT_NOT; 306 } 307 IsUnaryOp(Value op)308 static bool IsUnaryOp(Value op) { 309 return (NOT <= op && op <= VOID) || op == ADD || op == SUB; 310 } 311 IsCountOp(Value op)312 static bool IsCountOp(Value op) { 313 return op == INC || op == DEC; 314 } 315 IsShiftOp(Value op)316 static bool IsShiftOp(Value op) { 317 return (SHL <= op) && (op <= SHR); 318 } 319 320 // Returns a string corresponding to the JS token string 321 // (.e., "<" for the token LT) or NULL if the token doesn't 322 // have a (unique) string (e.g. an IDENTIFIER). String(Value tok)323 static const char* String(Value tok) { 324 DCHECK(tok < NUM_TOKENS); // tok is unsigned. 325 return string_[tok]; 326 } 327 328 // Returns the precedence > 0 for binary and compare 329 // operators; returns 0 otherwise. Precedence(Value tok)330 static int Precedence(Value tok) { 331 DCHECK(tok < NUM_TOKENS); // tok is unsigned. 332 return precedence_[tok]; 333 } 334 335 private: 336 static const char* const name_[NUM_TOKENS]; 337 static const char* const string_[NUM_TOKENS]; 338 static const int8_t precedence_[NUM_TOKENS]; 339 static const char token_type[NUM_TOKENS]; 340 }; 341 342 } // namespace internal 343 } // namespace v8 344 345 #endif // V8_PARSING_TOKEN_H_ 346