blob: a0afbc14f5f19eb82b192bcf3f30f048c32e6d1a [file] [log] [blame]
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001// Copyright 2006-2008 the V8 project authors. All rights reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
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.
15//
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.
27
28#ifndef V8_TOKEN_H_
29#define V8_TOKEN_H_
30
whesse@chromium.orgf0ac72d2010-11-08 12:47:26 +000031#include "checks.h"
32
kasperl@chromium.org71affb52009-05-26 05:44:31 +000033namespace v8 {
34namespace internal {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000035
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:
41//
42// T: Non-keyword tokens
43// K: Keyword tokens
44// F: Future (reserved) keyword tokens
45
46// IGNORE_TOKEN is a convenience macro that can be supplied as
47// an argument (at any position) for a TOKEN_LIST call. It does
48// nothing with tokens belonging to the respective category.
49
50#define IGNORE_TOKEN(name, string, precedence)
51
52#define TOKEN_LIST(T, K, F) \
53 /* End of source indicator. */ \
54 T(EOS, "EOS", 0) \
55 \
56 /* Punctuators (ECMA-262, section 7.7, page 15). */ \
57 T(LPAREN, "(", 0) \
58 T(RPAREN, ")", 0) \
59 T(LBRACK, "[", 0) \
60 T(RBRACK, "]", 0) \
61 T(LBRACE, "{", 0) \
62 T(RBRACE, "}", 0) \
63 T(COLON, ":", 0) \
64 T(SEMICOLON, ";", 0) \
65 T(PERIOD, ".", 0) \
66 T(CONDITIONAL, "?", 3) \
67 T(INC, "++", 0) \
68 T(DEC, "--", 0) \
69 \
70 /* Assignment operators. */ \
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +000071 /* IsAssignmentOp() and Assignment::is_compound() relies on */ \
72 /* this block of enum values being contiguous and sorted in the */ \
73 /* same order! */ \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000074 T(INIT_VAR, "=init_var", 2) /* AST-use only. */ \
75 T(INIT_CONST, "=init_const", 2) /* AST-use only. */ \
76 T(ASSIGN, "=", 2) \
77 T(ASSIGN_BIT_OR, "|=", 2) \
78 T(ASSIGN_BIT_XOR, "^=", 2) \
79 T(ASSIGN_BIT_AND, "&=", 2) \
80 T(ASSIGN_SHL, "<<=", 2) \
81 T(ASSIGN_SAR, ">>=", 2) \
82 T(ASSIGN_SHR, ">>>=", 2) \
83 T(ASSIGN_ADD, "+=", 2) \
84 T(ASSIGN_SUB, "-=", 2) \
85 T(ASSIGN_MUL, "*=", 2) \
86 T(ASSIGN_DIV, "/=", 2) \
87 T(ASSIGN_MOD, "%=", 2) \
88 \
89 /* Binary operators sorted by precedence. */ \
90 /* IsBinaryOp() relies on this block of enum values */ \
91 /* being contiguous and sorted in the same order! */ \
92 T(COMMA, ",", 1) \
93 T(OR, "||", 4) \
94 T(AND, "&&", 5) \
95 T(BIT_OR, "|", 6) \
96 T(BIT_XOR, "^", 7) \
97 T(BIT_AND, "&", 8) \
98 T(SHL, "<<", 11) \
99 T(SAR, ">>", 11) \
100 T(SHR, ">>>", 11) \
101 T(ADD, "+", 12) \
102 T(SUB, "-", 12) \
103 T(MUL, "*", 13) \
104 T(DIV, "/", 13) \
105 T(MOD, "%", 13) \
106 \
107 /* Compare operators sorted by precedence. */ \
108 /* IsCompareOp() relies on this block of enum values */ \
109 /* being contiguous and sorted in the same order! */ \
110 T(EQ, "==", 9) \
111 T(NE, "!=", 9) \
112 T(EQ_STRICT, "===", 9) \
113 T(NE_STRICT, "!==", 9) \
114 T(LT, "<", 10) \
115 T(GT, ">", 10) \
116 T(LTE, "<=", 10) \
117 T(GTE, ">=", 10) \
118 K(INSTANCEOF, "instanceof", 10) \
119 K(IN, "in", 10) \
120 \
121 /* Unary operators. */ \
122 /* IsUnaryOp() relies on this block of enum values */ \
123 /* being contiguous and sorted in the same order! */ \
124 T(NOT, "!", 0) \
125 T(BIT_NOT, "~", 0) \
126 K(DELETE, "delete", 0) \
127 K(TYPEOF, "typeof", 0) \
128 K(VOID, "void", 0) \
129 \
130 /* Keywords (ECMA-262, section 7.5.2, page 13). */ \
131 K(BREAK, "break", 0) \
132 K(CASE, "case", 0) \
133 K(CATCH, "catch", 0) \
134 K(CONTINUE, "continue", 0) \
135 K(DEBUGGER, "debugger", 0) \
136 K(DEFAULT, "default", 0) \
137 /* DELETE */ \
138 K(DO, "do", 0) \
139 K(ELSE, "else", 0) \
140 K(FINALLY, "finally", 0) \
141 K(FOR, "for", 0) \
142 K(FUNCTION, "function", 0) \
143 K(IF, "if", 0) \
144 /* IN */ \
145 /* INSTANCEOF */ \
146 K(NEW, "new", 0) \
147 K(RETURN, "return", 0) \
148 K(SWITCH, "switch", 0) \
149 K(THIS, "this", 0) \
150 K(THROW, "throw", 0) \
151 K(TRY, "try", 0) \
152 /* TYPEOF */ \
153 K(VAR, "var", 0) \
154 /* VOID */ \
155 K(WHILE, "while", 0) \
156 K(WITH, "with", 0) \
157 \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000158 /* Literals (ECMA-262, section 7.8, page 16). */ \
159 K(NULL_LITERAL, "null", 0) \
160 K(TRUE_LITERAL, "true", 0) \
161 K(FALSE_LITERAL, "false", 0) \
162 T(NUMBER, NULL, 0) \
163 T(STRING, NULL, 0) \
164 \
165 /* Identifiers (not keywords or future reserved words). */ \
166 T(IDENTIFIER, NULL, 0) \
167 \
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000168 /* Future reserved words (ECMA-262, section 7.6.1.2). */ \
169 T(FUTURE_RESERVED_WORD, NULL, 0) \
170 K(CONST, "const", 0) \
171 K(NATIVE, "native", 0) \
172 \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000173 /* Illegal token - not able to scan. */ \
174 T(ILLEGAL, "ILLEGAL", 0) \
175 \
176 /* Scanner-internal use only. */ \
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000177 T(WHITESPACE, NULL, 0)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000178
179
180class Token {
181 public:
182 // All token values.
183#define T(name, string, precedence) name,
184 enum Value {
185 TOKEN_LIST(T, T, IGNORE_TOKEN)
186 NUM_TOKENS
187 };
188#undef T
189
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000190 // Returns a string corresponding to the C++ token name
191 // (e.g. "LT" for the token LT).
192 static const char* Name(Value tok) {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000193 ASSERT(tok < NUM_TOKENS); // tok is unsigned
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000194 return name_[tok];
195 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000196
197 // Predicates
sgjesse@chromium.orgd3b3be02010-08-06 08:09:27 +0000198 static bool IsKeyword(Value tok) {
199 return token_type[tok] == 'K';
200 }
201
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000202 static bool IsAssignmentOp(Value tok) {
203 return INIT_VAR <= tok && tok <= ASSIGN_MOD;
204 }
205
206 static bool IsBinaryOp(Value op) {
207 return COMMA <= op && op <= MOD;
208 }
209
210 static bool IsCompareOp(Value op) {
211 return EQ <= op && op <= IN;
212 }
213
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000214 static bool IsOrderedCompareOp(Value op) {
215 return op == LT || op == LTE || op == GT || op == GTE;
216 }
217
218 static Value NegateCompareOp(Value op) {
219 ASSERT(IsCompareOp(op));
220 switch (op) {
221 case EQ: return NE;
222 case NE: return EQ;
223 case EQ_STRICT: return NE_STRICT;
224 case LT: return GTE;
225 case GT: return LTE;
226 case LTE: return GT;
227 case GTE: return LT;
228 default:
229 return op;
230 }
231 }
232
233 static Value InvertCompareOp(Value op) {
234 ASSERT(IsCompareOp(op));
235 switch (op) {
236 case EQ: return NE;
237 case NE: return EQ;
238 case EQ_STRICT: return NE_STRICT;
239 case LT: return GT;
240 case GT: return LT;
241 case LTE: return GTE;
242 case GTE: return LTE;
243 default:
244 return op;
245 }
246 }
247
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000248 static bool IsBitOp(Value op) {
249 return (BIT_OR <= op && op <= SHR) || op == BIT_NOT;
250 }
251
252 static bool IsUnaryOp(Value op) {
253 return (NOT <= op && op <= VOID) || op == ADD || op == SUB;
254 }
255
256 static bool IsCountOp(Value op) {
257 return op == INC || op == DEC;
258 }
259
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000260 static bool IsShiftOp(Value op) {
261 return (SHL <= op) && (op <= SHR);
262 }
263
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000264 // Returns a string corresponding to the JS token string
265 // (.e., "<" for the token LT) or NULL if the token doesn't
266 // have a (unique) string (e.g. an IDENTIFIER).
267 static const char* String(Value tok) {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000268 ASSERT(tok < NUM_TOKENS); // tok is unsigned.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000269 return string_[tok];
270 }
271
272 // Returns the precedence > 0 for binary and compare
273 // operators; returns 0 otherwise.
274 static int Precedence(Value tok) {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000275 ASSERT(tok < NUM_TOKENS); // tok is unsigned.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000276 return precedence_[tok];
277 }
278
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000279 private:
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000280 static const char* const name_[NUM_TOKENS];
281 static const char* const string_[NUM_TOKENS];
282 static const int8_t precedence_[NUM_TOKENS];
sgjesse@chromium.orgd3b3be02010-08-06 08:09:27 +0000283 static const char token_type[NUM_TOKENS];
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000284};
285
286} } // namespace v8::internal
287
288#endif // V8_TOKEN_H_