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