0f46b118cd58035444d2338896bec214b3cc5d63
[platform/upstream/nodejs.git] / deps / v8 / src / token.h
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_TOKEN_H_
6 #define V8_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_VAR, "=init_var", 2)                   /* AST-use only. */  \
54   T(INIT_LET, "=init_let", 2)                   /* AST-use only. */  \
55   T(INIT_CONST, "=init_const", 2)               /* AST-use only. */  \
56   T(INIT_CONST_LEGACY, "=init_const_legacy", 2) /* AST-use only. */  \
57   T(ASSIGN, "=", 2)                                                  \
58   T(ASSIGN_BIT_OR, "|=", 2)                                          \
59   T(ASSIGN_BIT_XOR, "^=", 2)                                         \
60   T(ASSIGN_BIT_AND, "&=", 2)                                         \
61   T(ASSIGN_SHL, "<<=", 2)                                            \
62   T(ASSIGN_SAR, ">>=", 2)                                            \
63   T(ASSIGN_SHR, ">>>=", 2)                                           \
64   T(ASSIGN_ADD, "+=", 2)                                             \
65   T(ASSIGN_SUB, "-=", 2)                                             \
66   T(ASSIGN_MUL, "*=", 2)                                             \
67   T(ASSIGN_DIV, "/=", 2)                                             \
68   T(ASSIGN_MOD, "%=", 2)                                             \
69                                                                      \
70   /* Binary operators sorted by precedence. */                       \
71   /* IsBinaryOp() relies on this block of enum values */             \
72   /* being contiguous and sorted in the same order! */               \
73   T(COMMA, ",", 1)                                                   \
74   T(OR, "||", 4)                                                     \
75   T(AND, "&&", 5)                                                    \
76   T(BIT_OR, "|", 6)                                                  \
77   T(BIT_XOR, "^", 7)                                                 \
78   T(BIT_AND, "&", 8)                                                 \
79   T(SHL, "<<", 11)                                                   \
80   T(SAR, ">>", 11)                                                   \
81   T(SHR, ">>>", 11)                                                  \
82   T(ROR, "rotate right", 11) /* only used by Crankshaft */           \
83   T(ADD, "+", 12)                                                    \
84   T(SUB, "-", 12)                                                    \
85   T(MUL, "*", 13)                                                    \
86   T(DIV, "/", 13)                                                    \
87   T(MOD, "%", 13)                                                    \
88                                                                      \
89   /* Compare operators sorted by precedence. */                      \
90   /* IsCompareOp() relies on this block of enum values */            \
91   /* being contiguous and sorted in the same order! */               \
92   T(EQ, "==", 9)                                                     \
93   T(NE, "!=", 9)                                                     \
94   T(EQ_STRICT, "===", 9)                                             \
95   T(NE_STRICT, "!==", 9)                                             \
96   T(LT, "<", 10)                                                     \
97   T(GT, ">", 10)                                                     \
98   T(LTE, "<=", 10)                                                   \
99   T(GTE, ">=", 10)                                                   \
100   K(INSTANCEOF, "instanceof", 10)                                    \
101   K(IN, "in", 10)                                                    \
102                                                                      \
103   /* Unary operators. */                                             \
104   /* IsUnaryOp() relies on this block of enum values */              \
105   /* being contiguous and sorted in the same order! */               \
106   T(NOT, "!", 0)                                                     \
107   T(BIT_NOT, "~", 0)                                                 \
108   K(DELETE, "delete", 0)                                             \
109   K(TYPEOF, "typeof", 0)                                             \
110   K(VOID, "void", 0)                                                 \
111                                                                      \
112   /* Keywords (ECMA-262, section 7.5.2, page 13). */                 \
113   K(BREAK, "break", 0)                                               \
114   K(CASE, "case", 0)                                                 \
115   K(CATCH, "catch", 0)                                               \
116   K(CONTINUE, "continue", 0)                                         \
117   K(DEBUGGER, "debugger", 0)                                         \
118   K(DEFAULT, "default", 0)                                           \
119   /* DELETE */                                                       \
120   K(DO, "do", 0)                                                     \
121   K(ELSE, "else", 0)                                                 \
122   K(FINALLY, "finally", 0)                                           \
123   K(FOR, "for", 0)                                                   \
124   K(FUNCTION, "function", 0)                                         \
125   K(IF, "if", 0)                                                     \
126   /* IN */                                                           \
127   /* INSTANCEOF */                                                   \
128   K(NEW, "new", 0)                                                   \
129   K(RETURN, "return", 0)                                             \
130   K(SWITCH, "switch", 0)                                             \
131   K(THIS, "this", 0)                                                 \
132   K(THROW, "throw", 0)                                               \
133   K(TRY, "try", 0)                                                   \
134   /* TYPEOF */                                                       \
135   K(VAR, "var", 0)                                                   \
136   /* VOID */                                                         \
137   K(WHILE, "while", 0)                                               \
138   K(WITH, "with", 0)                                                 \
139                                                                      \
140   /* Literals (ECMA-262, section 7.8, page 16). */                   \
141   K(NULL_LITERAL, "null", 0)                                         \
142   K(TRUE_LITERAL, "true", 0)                                         \
143   K(FALSE_LITERAL, "false", 0)                                       \
144   T(NUMBER, 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_RESERVED_WORD, NULL, 0)                                   \
152   T(FUTURE_STRICT_RESERVED_WORD, NULL, 0)                            \
153   K(CLASS, "class", 0)                                               \
154   K(CONST, "const", 0)                                               \
155   K(EXPORT, "export", 0)                                             \
156   K(EXTENDS, "extends", 0)                                           \
157   K(IMPORT, "import", 0)                                             \
158   K(LET, "let", 0)                                                   \
159   K(STATIC, "static", 0)                                             \
160   K(YIELD, "yield", 0)                                               \
161   K(SUPER, "super", 0)                                               \
162                                                                      \
163   /* Illegal token - not able to scan. */                            \
164   T(ILLEGAL, "ILLEGAL", 0)                                           \
165                                                                      \
166   /* Scanner-internal use only. */                                   \
167   T(WHITESPACE, NULL, 0)                                             \
168                                                                      \
169   /* ES6 Template Literals */                                        \
170   T(TEMPLATE_SPAN, NULL, 0)                                          \
171   T(TEMPLATE_TAIL, NULL, 0)
172
173
174 class Token {
175  public:
176   // All token values.
177 #define T(name, string, precedence) name,
178   enum Value {
179     TOKEN_LIST(T, T)
180     NUM_TOKENS
181   };
182 #undef T
183
184   // Returns a string corresponding to the C++ token name
185   // (e.g. "LT" for the token LT).
186   static const char* Name(Value tok) {
187     DCHECK(tok < NUM_TOKENS);  // tok is unsigned
188     return name_[tok];
189   }
190
191   // Predicates
192   static bool IsKeyword(Value tok) {
193     return token_type[tok] == 'K';
194   }
195
196   static bool IsIdentifier(Value tok, LanguageMode language_mode,
197                            bool is_generator) {
198     switch (tok) {
199       case IDENTIFIER:
200         return true;
201       case FUTURE_STRICT_RESERVED_WORD:
202       case LET:
203       case STATIC:
204         return is_sloppy(language_mode);
205       case YIELD:
206         return !is_generator && is_sloppy(language_mode);
207       default:
208         return false;
209     }
210     UNREACHABLE();
211     return false;
212   }
213
214   static bool IsAssignmentOp(Value tok) {
215     return INIT_VAR <= tok && tok <= ASSIGN_MOD;
216   }
217
218   static bool IsBinaryOp(Value op) {
219     return COMMA <= op && op <= MOD;
220   }
221
222   static bool IsTruncatingBinaryOp(Value op) {
223     return BIT_OR <= op && op <= ROR;
224   }
225
226   static bool IsCompareOp(Value op) {
227     return EQ <= op && op <= IN;
228   }
229
230   static bool IsOrderedRelationalCompareOp(Value op) {
231     return op == LT || op == LTE || op == GT || op == GTE;
232   }
233
234   static bool IsEqualityOp(Value op) {
235     return op == EQ || op == EQ_STRICT;
236   }
237
238   static bool IsInequalityOp(Value op) {
239     return op == NE || op == NE_STRICT;
240   }
241
242   static bool IsArithmeticCompareOp(Value op) {
243     return IsOrderedRelationalCompareOp(op) ||
244         IsEqualityOp(op) || IsInequalityOp(op);
245   }
246
247   static Value NegateCompareOp(Value op) {
248     DCHECK(IsArithmeticCompareOp(op));
249     switch (op) {
250       case EQ: return NE;
251       case NE: return EQ;
252       case EQ_STRICT: return NE_STRICT;
253       case NE_STRICT: return EQ_STRICT;
254       case LT: return GTE;
255       case GT: return LTE;
256       case LTE: return GT;
257       case GTE: return LT;
258       default:
259         UNREACHABLE();
260         return op;
261     }
262   }
263
264   static Value ReverseCompareOp(Value op) {
265     DCHECK(IsArithmeticCompareOp(op));
266     switch (op) {
267       case EQ: return EQ;
268       case NE: return NE;
269       case EQ_STRICT: return EQ_STRICT;
270       case NE_STRICT: return NE_STRICT;
271       case LT: return GT;
272       case GT: return LT;
273       case LTE: return GTE;
274       case GTE: return LTE;
275       default:
276         UNREACHABLE();
277         return op;
278     }
279   }
280
281   static bool IsBitOp(Value op) {
282     return (BIT_OR <= op && op <= SHR) || op == BIT_NOT;
283   }
284
285   static bool IsUnaryOp(Value op) {
286     return (NOT <= op && op <= VOID) || op == ADD || op == SUB;
287   }
288
289   static bool IsCountOp(Value op) {
290     return op == INC || op == DEC;
291   }
292
293   static bool IsShiftOp(Value op) {
294     return (SHL <= op) && (op <= SHR);
295   }
296
297   // Returns a string corresponding to the JS token string
298   // (.e., "<" for the token LT) or NULL if the token doesn't
299   // have a (unique) string (e.g. an IDENTIFIER).
300   static const char* String(Value tok) {
301     DCHECK(tok < NUM_TOKENS);  // tok is unsigned.
302     return string_[tok];
303   }
304
305   // Returns the precedence > 0 for binary and compare
306   // operators; returns 0 otherwise.
307   static int Precedence(Value tok) {
308     DCHECK(tok < NUM_TOKENS);  // tok is unsigned.
309     return precedence_[tok];
310   }
311
312  private:
313   static const char* const name_[NUM_TOKENS];
314   static const char* const string_[NUM_TOKENS];
315   static const int8_t precedence_[NUM_TOKENS];
316   static const char token_type[NUM_TOKENS];
317 };
318
319 } }  // namespace v8::internal
320
321 #endif  // V8_TOKEN_H_