1 // Copyright 2011 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided
11 // with the distribution.
12 // * Neither the name of Google Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived
14 // from this software without specific prior written permission.
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 // TOKEN_LIST takes a list of 3 macros M, all of which satisfy the
37 // same signature M(name, string, precedence), where name is the
38 // symbolic token name, string is the corresponding syntactic symbol
39 // (or NULL, for literals), and precedence is the precedence (or 0).
40 // The parameters are invoked for token categories as follows:
42 // T: Non-keyword tokens
45 // IGNORE_TOKEN is a convenience macro that can be supplied as
46 // an argument (at any position) for a TOKEN_LIST call. It does
47 // nothing with tokens belonging to the respective category.
49 #define IGNORE_TOKEN(name, string, precedence)
51 #define TOKEN_LIST(T, K) \
52 /* End of source indicator. */ \
55 /* Punctuators (ECMA-262, section 7.7, page 15). */ \
63 T(SEMICOLON, ";", 0) \
65 T(CONDITIONAL, "?", 3) \
69 /* Assignment operators. */ \
70 /* IsAssignmentOp() and Assignment::is_compound() relies on */ \
71 /* this block of enum values being contiguous and sorted in the */ \
73 T(INIT_VAR, "=init_var", 2) /* AST-use only. */ \
74 T(INIT_LET, "=init_let", 2) /* AST-use only. */ \
75 T(INIT_CONST, "=init_const", 2) /* AST-use only. */ \
76 T(INIT_CONST_HARMONY, "=init_const_harmony", 2) /* AST-use only. */ \
78 T(ASSIGN_BIT_OR, "|=", 2) \
79 T(ASSIGN_BIT_XOR, "^=", 2) \
80 T(ASSIGN_BIT_AND, "&=", 2) \
81 T(ASSIGN_SHL, "<<=", 2) \
82 T(ASSIGN_SAR, ">>=", 2) \
83 T(ASSIGN_SHR, ">>>=", 2) \
84 T(ASSIGN_ADD, "+=", 2) \
85 T(ASSIGN_SUB, "-=", 2) \
86 T(ASSIGN_MUL, "*=", 2) \
87 T(ASSIGN_DIV, "/=", 2) \
88 T(ASSIGN_MOD, "%=", 2) \
90 /* Binary operators sorted by precedence. */ \
91 /* IsBinaryOp() relies on this block of enum values */ \
92 /* being contiguous and sorted in the same order! */ \
108 /* Compare operators sorted by precedence. */ \
109 /* IsCompareOp() relies on this block of enum values */ \
110 /* being contiguous and sorted in the same order! */ \
113 T(EQ_STRICT, "===", 9) \
114 T(NE_STRICT, "!==", 9) \
119 K(INSTANCEOF, "instanceof", 10) \
122 /* Unary operators. */ \
123 /* IsUnaryOp() relies on this block of enum values */ \
124 /* being contiguous and sorted in the same order! */ \
127 K(DELETE, "delete", 0) \
128 K(TYPEOF, "typeof", 0) \
131 /* Keywords (ECMA-262, section 7.5.2, page 13). */ \
132 K(BREAK, "break", 0) \
134 K(CATCH, "catch", 0) \
135 K(CONTINUE, "continue", 0) \
136 K(DEBUGGER, "debugger", 0) \
137 K(DEFAULT, "default", 0) \
141 K(FINALLY, "finally", 0) \
143 K(FUNCTION, "function", 0) \
148 K(RETURN, "return", 0) \
149 K(SWITCH, "switch", 0) \
151 K(THROW, "throw", 0) \
156 K(WHILE, "while", 0) \
159 /* Literals (ECMA-262, section 7.8, page 16). */ \
160 K(NULL_LITERAL, "null", 0) \
161 K(TRUE_LITERAL, "true", 0) \
162 K(FALSE_LITERAL, "false", 0) \
166 /* Identifiers (not keywords or future reserved words). */ \
167 T(IDENTIFIER, NULL, 0) \
169 /* Future reserved words (ECMA-262, section 7.6.1.2). */ \
170 T(FUTURE_RESERVED_WORD, NULL, 0) \
171 T(FUTURE_STRICT_RESERVED_WORD, NULL, 0) \
172 K(CONST, "const", 0) \
175 /* Illegal token - not able to scan. */ \
176 T(ILLEGAL, "ILLEGAL", 0) \
178 /* Scanner-internal use only. */ \
179 T(WHITESPACE, NULL, 0)
185 #define T(name, string, precedence) name,
192 // Returns a string corresponding to the C++ token name
193 // (e.g. "LT" for the token LT).
194 static const char* Name(Value tok) {
195 ASSERT(tok < NUM_TOKENS); // tok is unsigned
200 static bool IsKeyword(Value tok) {
201 return token_type[tok] == 'K';
204 static bool IsAssignmentOp(Value tok) {
205 return INIT_VAR <= tok && tok <= ASSIGN_MOD;
208 static bool IsBinaryOp(Value op) {
209 return COMMA <= op && op <= MOD;
212 static bool IsCompareOp(Value op) {
213 return EQ <= op && op <= IN;
216 static bool IsOrderedCompareOp(Value op) {
217 return op == LT || op == LTE || op == GT || op == GTE;
220 static bool IsEqualityOp(Value op) {
221 return op == EQ || op == EQ_STRICT;
224 static Value NegateCompareOp(Value op) {
225 ASSERT(IsCompareOp(op));
229 case EQ_STRICT: return NE_STRICT;
239 static Value InvertCompareOp(Value op) {
240 ASSERT(IsCompareOp(op));
244 case EQ_STRICT: return NE_STRICT;
247 case LTE: return GTE;
248 case GTE: return LTE;
254 static bool IsBitOp(Value op) {
255 return (BIT_OR <= op && op <= SHR) || op == BIT_NOT;
258 static bool IsUnaryOp(Value op) {
259 return (NOT <= op && op <= VOID) || op == ADD || op == SUB;
262 static bool IsCountOp(Value op) {
263 return op == INC || op == DEC;
266 static bool IsShiftOp(Value op) {
267 return (SHL <= op) && (op <= SHR);
270 // Returns a string corresponding to the JS token string
271 // (.e., "<" for the token LT) or NULL if the token doesn't
272 // have a (unique) string (e.g. an IDENTIFIER).
273 static const char* String(Value tok) {
274 ASSERT(tok < NUM_TOKENS); // tok is unsigned.
278 // Returns the precedence > 0 for binary and compare
279 // operators; returns 0 otherwise.
280 static int Precedence(Value tok) {
281 ASSERT(tok < NUM_TOKENS); // tok is unsigned.
282 return precedence_[tok];
286 static const char* const name_[NUM_TOKENS];
287 static const char* const string_[NUM_TOKENS];
288 static const int8_t precedence_[NUM_TOKENS];
289 static const char token_type[NUM_TOKENS];
292 } } // namespace v8::internal
294 #endif // V8_TOKEN_H_