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.
8 #include "src/base/logging.h"
9 #include "src/globals.h"
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:
20 // T: Non-keyword tokens
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.
27 #define IGNORE_TOKEN(name, string, precedence)
29 #define TOKEN_LIST(T, K) \
30 /* End of source indicator. */ \
33 /* Punctuators (ECMA-262, section 7.7, page 15). */ \
41 T(SEMICOLON, ";", 0) \
43 T(CONDITIONAL, "?", 3) \
48 /* Assignment operators. */ \
49 /* IsAssignmentOp() and Assignment::is_compound() relies on */ \
50 /* this block of enum values being contiguous and sorted in the */ \
52 T(INIT_VAR, "=init_var", 2) /* AST-use only. */ \
53 T(INIT_LET, "=init_let", 2) /* AST-use only. */ \
54 T(INIT_CONST, "=init_const", 2) /* AST-use only. */ \
55 T(INIT_CONST_LEGACY, "=init_const_legacy", 2) /* AST-use only. */ \
57 T(ASSIGN_BIT_OR, "|=", 2) \
58 T(ASSIGN_BIT_XOR, "^=", 2) \
59 T(ASSIGN_BIT_AND, "&=", 2) \
60 T(ASSIGN_SHL, "<<=", 2) \
61 T(ASSIGN_SAR, ">>=", 2) \
62 T(ASSIGN_SHR, ">>>=", 2) \
63 T(ASSIGN_ADD, "+=", 2) \
64 T(ASSIGN_SUB, "-=", 2) \
65 T(ASSIGN_MUL, "*=", 2) \
66 T(ASSIGN_DIV, "/=", 2) \
67 T(ASSIGN_MOD, "%=", 2) \
69 /* Binary operators sorted by precedence. */ \
70 /* IsBinaryOp() relies on this block of enum values */ \
71 /* being contiguous and sorted in the same order! */ \
81 T(ROR, "rotate right", 11) /* only used by Crankshaft */ \
88 /* Compare operators sorted by precedence. */ \
89 /* IsCompareOp() relies on this block of enum values */ \
90 /* being contiguous and sorted in the same order! */ \
93 T(EQ_STRICT, "===", 9) \
94 T(NE_STRICT, "!==", 9) \
99 K(INSTANCEOF, "instanceof", 10) \
102 /* Unary operators. */ \
103 /* IsUnaryOp() relies on this block of enum values */ \
104 /* being contiguous and sorted in the same order! */ \
107 K(DELETE, "delete", 0) \
108 K(TYPEOF, "typeof", 0) \
111 /* Keywords (ECMA-262, section 7.5.2, page 13). */ \
112 K(BREAK, "break", 0) \
114 K(CATCH, "catch", 0) \
115 K(CONTINUE, "continue", 0) \
116 K(DEBUGGER, "debugger", 0) \
117 K(DEFAULT, "default", 0) \
121 K(FINALLY, "finally", 0) \
123 K(FUNCTION, "function", 0) \
128 K(RETURN, "return", 0) \
129 K(SWITCH, "switch", 0) \
131 K(THROW, "throw", 0) \
136 K(WHILE, "while", 0) \
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) \
146 /* Identifiers (not keywords or future reserved words). */ \
147 T(IDENTIFIER, NULL, 0) \
149 /* Future reserved words (ECMA-262, section 7.6.1.2). */ \
150 T(FUTURE_RESERVED_WORD, NULL, 0) \
151 T(FUTURE_STRICT_RESERVED_WORD, NULL, 0) \
152 K(CLASS, "class", 0) \
153 K(CONST, "const", 0) \
154 K(EXPORT, "export", 0) \
155 K(EXTENDS, "extends", 0) \
156 K(IMPORT, "import", 0) \
158 K(STATIC, "static", 0) \
159 K(YIELD, "yield", 0) \
160 K(SUPER, "super", 0) \
162 /* Illegal token - not able to scan. */ \
163 T(ILLEGAL, "ILLEGAL", 0) \
165 /* Scanner-internal use only. */ \
166 T(WHITESPACE, NULL, 0)
172 #define T(name, string, precedence) name,
179 // Returns a string corresponding to the C++ token name
180 // (e.g. "LT" for the token LT).
181 static const char* Name(Value tok) {
182 DCHECK(tok < NUM_TOKENS); // tok is unsigned
187 static bool IsKeyword(Value tok) {
188 return token_type[tok] == 'K';
191 static bool IsIdentifier(Value tok, StrictMode strict_mode,
196 case FUTURE_STRICT_RESERVED_WORD:
199 return strict_mode == SLOPPY;
201 return !is_generator && strict_mode == SLOPPY;
209 static bool IsAssignmentOp(Value tok) {
210 return INIT_VAR <= tok && tok <= ASSIGN_MOD;
213 static bool IsBinaryOp(Value op) {
214 return COMMA <= op && op <= MOD;
217 static bool IsTruncatingBinaryOp(Value op) {
218 return BIT_OR <= op && op <= ROR;
221 static bool IsCompareOp(Value op) {
222 return EQ <= op && op <= IN;
225 static bool IsOrderedRelationalCompareOp(Value op) {
226 return op == LT || op == LTE || op == GT || op == GTE;
229 static bool IsEqualityOp(Value op) {
230 return op == EQ || op == EQ_STRICT;
233 static bool IsInequalityOp(Value op) {
234 return op == NE || op == NE_STRICT;
237 static bool IsArithmeticCompareOp(Value op) {
238 return IsOrderedRelationalCompareOp(op) ||
239 IsEqualityOp(op) || IsInequalityOp(op);
242 static Value NegateCompareOp(Value op) {
243 DCHECK(IsArithmeticCompareOp(op));
247 case EQ_STRICT: return NE_STRICT;
248 case NE_STRICT: return EQ_STRICT;
259 static Value ReverseCompareOp(Value op) {
260 DCHECK(IsArithmeticCompareOp(op));
264 case EQ_STRICT: return EQ_STRICT;
265 case NE_STRICT: return NE_STRICT;
268 case LTE: return GTE;
269 case GTE: return LTE;
276 static bool IsBitOp(Value op) {
277 return (BIT_OR <= op && op <= SHR) || op == BIT_NOT;
280 static bool IsUnaryOp(Value op) {
281 return (NOT <= op && op <= VOID) || op == ADD || op == SUB;
284 static bool IsCountOp(Value op) {
285 return op == INC || op == DEC;
288 static bool IsShiftOp(Value op) {
289 return (SHL <= op) && (op <= SHR);
292 // Returns a string corresponding to the JS token string
293 // (.e., "<" for the token LT) or NULL if the token doesn't
294 // have a (unique) string (e.g. an IDENTIFIER).
295 static const char* String(Value tok) {
296 DCHECK(tok < NUM_TOKENS); // tok is unsigned.
300 // Returns the precedence > 0 for binary and compare
301 // operators; returns 0 otherwise.
302 static int Precedence(Value tok) {
303 DCHECK(tok < NUM_TOKENS); // tok is unsigned.
304 return precedence_[tok];
308 static const char* const name_[NUM_TOKENS];
309 static const char* const string_[NUM_TOKENS];
310 static const int8_t precedence_[NUM_TOKENS];
311 static const char token_type[NUM_TOKENS];
314 } } // namespace v8::internal
316 #endif // V8_TOKEN_H_