• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
10 namespace v8 {
11 namespace internal {
12 
13 // TOKEN_LIST takes a list of 3 macros M, all of which satisfy the
14 // same signature M(name, string, precedence), where name is the
15 // symbolic token name, string is the corresponding syntactic symbol
16 // (or NULL, for literals), and precedence is the precedence (or 0).
17 // The parameters are invoked for token categories as follows:
18 //
19 //   T: Non-keyword tokens
20 //   K: Keyword tokens
21 
22 // IGNORE_TOKEN is a convenience macro that can be supplied as
23 // an argument (at any position) for a TOKEN_LIST call. It does
24 // nothing with tokens belonging to the respective category.
25 
26 #define IGNORE_TOKEN(name, string, precedence)
27 
28 #define TOKEN_LIST(T, K)                                             \
29   /* End of source indicator. */                                     \
30   T(EOS, "EOS", 0)                                                   \
31                                                                      \
32   /* Punctuators (ECMA-262, section 7.7, page 15). */                \
33   T(LPAREN, "(", 0)                                                  \
34   T(RPAREN, ")", 0)                                                  \
35   T(LBRACK, "[", 0)                                                  \
36   T(RBRACK, "]", 0)                                                  \
37   T(LBRACE, "{", 0)                                                  \
38   T(RBRACE, "}", 0)                                                  \
39   T(COLON, ":", 0)                                                   \
40   T(SEMICOLON, ";", 0)                                               \
41   T(PERIOD, ".", 0)                                                  \
42   T(CONDITIONAL, "?", 3)                                             \
43   T(INC, "++", 0)                                                    \
44   T(DEC, "--", 0)                                                    \
45   T(ARROW, "=>", 0)                                                  \
46                                                                      \
47   /* Assignment operators. */                                        \
48   /* IsAssignmentOp() and Assignment::is_compound() relies on */     \
49   /* this block of enum values being contiguous and sorted in the */ \
50   /* same order! */                                                  \
51   T(INIT_VAR, "=init_var", 2)                   /* AST-use only. */  \
52   T(INIT_LET, "=init_let", 2)                   /* AST-use only. */  \
53   T(INIT_CONST, "=init_const", 2)               /* AST-use only. */  \
54   T(INIT_CONST_LEGACY, "=init_const_legacy", 2) /* AST-use only. */  \
55   T(ASSIGN, "=", 2)                                                  \
56   T(ASSIGN_BIT_OR, "|=", 2)                                          \
57   T(ASSIGN_BIT_XOR, "^=", 2)                                         \
58   T(ASSIGN_BIT_AND, "&=", 2)                                         \
59   T(ASSIGN_SHL, "<<=", 2)                                            \
60   T(ASSIGN_SAR, ">>=", 2)                                            \
61   T(ASSIGN_SHR, ">>>=", 2)                                           \
62   T(ASSIGN_ADD, "+=", 2)                                             \
63   T(ASSIGN_SUB, "-=", 2)                                             \
64   T(ASSIGN_MUL, "*=", 2)                                             \
65   T(ASSIGN_DIV, "/=", 2)                                             \
66   T(ASSIGN_MOD, "%=", 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                                                                      \
87   /* Compare operators sorted by precedence. */                      \
88   /* IsCompareOp() relies on this block of enum values */            \
89   /* being contiguous and sorted in the same order! */               \
90   T(EQ, "==", 9)                                                     \
91   T(NE, "!=", 9)                                                     \
92   T(EQ_STRICT, "===", 9)                                             \
93   T(NE_STRICT, "!==", 9)                                             \
94   T(LT, "<", 10)                                                     \
95   T(GT, ">", 10)                                                     \
96   T(LTE, "<=", 10)                                                   \
97   T(GTE, ">=", 10)                                                   \
98   K(INSTANCEOF, "instanceof", 10)                                    \
99   K(IN, "in", 10)                                                    \
100                                                                      \
101   /* Unary operators. */                                             \
102   /* IsUnaryOp() relies on this block of enum values */              \
103   /* being contiguous and sorted in the same order! */               \
104   T(NOT, "!", 0)                                                     \
105   T(BIT_NOT, "~", 0)                                                 \
106   K(DELETE, "delete", 0)                                             \
107   K(TYPEOF, "typeof", 0)                                             \
108   K(VOID, "void", 0)                                                 \
109                                                                      \
110   /* Keywords (ECMA-262, section 7.5.2, page 13). */                 \
111   K(BREAK, "break", 0)                                               \
112   K(CASE, "case", 0)                                                 \
113   K(CATCH, "catch", 0)                                               \
114   K(CONTINUE, "continue", 0)                                         \
115   K(DEBUGGER, "debugger", 0)                                         \
116   K(DEFAULT, "default", 0)                                           \
117   /* DELETE */                                                       \
118   K(DO, "do", 0)                                                     \
119   K(ELSE, "else", 0)                                                 \
120   K(FINALLY, "finally", 0)                                           \
121   K(FOR, "for", 0)                                                   \
122   K(FUNCTION, "function", 0)                                         \
123   K(IF, "if", 0)                                                     \
124   /* IN */                                                           \
125   /* INSTANCEOF */                                                   \
126   K(NEW, "new", 0)                                                   \
127   K(RETURN, "return", 0)                                             \
128   K(SWITCH, "switch", 0)                                             \
129   K(THIS, "this", 0)                                                 \
130   K(THROW, "throw", 0)                                               \
131   K(TRY, "try", 0)                                                   \
132   /* TYPEOF */                                                       \
133   K(VAR, "var", 0)                                                   \
134   /* VOID */                                                         \
135   K(WHILE, "while", 0)                                               \
136   K(WITH, "with", 0)                                                 \
137                                                                      \
138   /* Literals (ECMA-262, section 7.8, page 16). */                   \
139   K(NULL_LITERAL, "null", 0)                                         \
140   K(TRUE_LITERAL, "true", 0)                                         \
141   K(FALSE_LITERAL, "false", 0)                                       \
142   T(NUMBER, NULL, 0)                                                 \
143   T(STRING, NULL, 0)                                                 \
144                                                                      \
145   /* Identifiers (not keywords or future reserved words). */         \
146   T(IDENTIFIER, NULL, 0)                                             \
147                                                                      \
148   /* Future reserved words (ECMA-262, section 7.6.1.2). */           \
149   T(FUTURE_RESERVED_WORD, NULL, 0)                                   \
150   T(FUTURE_STRICT_RESERVED_WORD, NULL, 0)                            \
151   K(CLASS, "class", 0)                                               \
152   K(CONST, "const", 0)                                               \
153   K(EXPORT, "export", 0)                                             \
154   K(EXTENDS, "extends", 0)                                           \
155   K(IMPORT, "import", 0)                                             \
156   K(LET, "let", 0)                                                   \
157   K(STATIC, "static", 0)                                             \
158   K(YIELD, "yield", 0)                                               \
159   K(SUPER, "super", 0)                                               \
160                                                                      \
161   /* Illegal token - not able to scan. */                            \
162   T(ILLEGAL, "ILLEGAL", 0)                                           \
163                                                                      \
164   /* Scanner-internal use only. */                                   \
165   T(WHITESPACE, NULL, 0)
166 
167 
168 class Token {
169  public:
170   // All token values.
171 #define T(name, string, precedence) name,
172   enum Value {
173     TOKEN_LIST(T, T)
174     NUM_TOKENS
175   };
176 #undef T
177 
178   // Returns a string corresponding to the C++ token name
179   // (e.g. "LT" for the token LT).
Name(Value tok)180   static const char* Name(Value tok) {
181     DCHECK(tok < NUM_TOKENS);  // tok is unsigned
182     return name_[tok];
183   }
184 
185   // Predicates
IsKeyword(Value tok)186   static bool IsKeyword(Value tok) {
187     return token_type[tok] == 'K';
188   }
189 
IsAssignmentOp(Value tok)190   static bool IsAssignmentOp(Value tok) {
191     return INIT_VAR <= tok && tok <= ASSIGN_MOD;
192   }
193 
IsBinaryOp(Value op)194   static bool IsBinaryOp(Value op) {
195     return COMMA <= op && op <= MOD;
196   }
197 
IsTruncatingBinaryOp(Value op)198   static bool IsTruncatingBinaryOp(Value op) {
199     return BIT_OR <= op && op <= ROR;
200   }
201 
IsCompareOp(Value op)202   static bool IsCompareOp(Value op) {
203     return EQ <= op && op <= IN;
204   }
205 
IsOrderedRelationalCompareOp(Value op)206   static bool IsOrderedRelationalCompareOp(Value op) {
207     return op == LT || op == LTE || op == GT || op == GTE;
208   }
209 
IsEqualityOp(Value op)210   static bool IsEqualityOp(Value op) {
211     return op == EQ || op == EQ_STRICT;
212   }
213 
IsInequalityOp(Value op)214   static bool IsInequalityOp(Value op) {
215     return op == NE || op == NE_STRICT;
216   }
217 
IsArithmeticCompareOp(Value op)218   static bool IsArithmeticCompareOp(Value op) {
219     return IsOrderedRelationalCompareOp(op) ||
220         IsEqualityOp(op) || IsInequalityOp(op);
221   }
222 
NegateCompareOp(Value op)223   static Value NegateCompareOp(Value op) {
224     DCHECK(IsArithmeticCompareOp(op));
225     switch (op) {
226       case EQ: return NE;
227       case NE: return EQ;
228       case EQ_STRICT: return NE_STRICT;
229       case NE_STRICT: return EQ_STRICT;
230       case LT: return GTE;
231       case GT: return LTE;
232       case LTE: return GT;
233       case GTE: return LT;
234       default:
235         UNREACHABLE();
236         return op;
237     }
238   }
239 
ReverseCompareOp(Value op)240   static Value ReverseCompareOp(Value op) {
241     DCHECK(IsArithmeticCompareOp(op));
242     switch (op) {
243       case EQ: return EQ;
244       case NE: return NE;
245       case EQ_STRICT: return EQ_STRICT;
246       case NE_STRICT: return NE_STRICT;
247       case LT: return GT;
248       case GT: return LT;
249       case LTE: return GTE;
250       case GTE: return LTE;
251       default:
252         UNREACHABLE();
253         return op;
254     }
255   }
256 
IsBitOp(Value op)257   static bool IsBitOp(Value op) {
258     return (BIT_OR <= op && op <= SHR) || op == BIT_NOT;
259   }
260 
IsUnaryOp(Value op)261   static bool IsUnaryOp(Value op) {
262     return (NOT <= op && op <= VOID) || op == ADD || op == SUB;
263   }
264 
IsCountOp(Value op)265   static bool IsCountOp(Value op) {
266     return op == INC || op == DEC;
267   }
268 
IsShiftOp(Value op)269   static bool IsShiftOp(Value op) {
270     return (SHL <= op) && (op <= SHR);
271   }
272 
273   // Returns a string corresponding to the JS token string
274   // (.e., "<" for the token LT) or NULL if the token doesn't
275   // have a (unique) string (e.g. an IDENTIFIER).
String(Value tok)276   static const char* String(Value tok) {
277     DCHECK(tok < NUM_TOKENS);  // tok is unsigned.
278     return string_[tok];
279   }
280 
281   // Returns the precedence > 0 for binary and compare
282   // operators; returns 0 otherwise.
Precedence(Value tok)283   static int Precedence(Value tok) {
284     DCHECK(tok < NUM_TOKENS);  // tok is unsigned.
285     return precedence_[tok];
286   }
287 
288  private:
289   static const char* const name_[NUM_TOKENS];
290   static const char* const string_[NUM_TOKENS];
291   static const int8_t precedence_[NUM_TOKENS];
292   static const char token_type[NUM_TOKENS];
293 };
294 
295 } }  // namespace v8::internal
296 
297 #endif  // V8_TOKEN_H_
298