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