blob: 7e6c18cfe6402bca329635057292fc3dfcbe46ad [file] [log] [blame]
whesse@chromium.org7b260152011-06-20 15:33:18 +00001// Copyright 2011 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
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000044
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.
48
49#define IGNORE_TOKEN(name, string, precedence)
50
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +000051#define TOKEN_LIST(T, K) \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000052 /* End of source indicator. */ \
53 T(EOS, "EOS", 0) \
54 \
55 /* Punctuators (ECMA-262, section 7.7, page 15). */ \
56 T(LPAREN, "(", 0) \
57 T(RPAREN, ")", 0) \
58 T(LBRACK, "[", 0) \
59 T(RBRACK, "]", 0) \
60 T(LBRACE, "{", 0) \
61 T(RBRACE, "}", 0) \
62 T(COLON, ":", 0) \
63 T(SEMICOLON, ";", 0) \
64 T(PERIOD, ".", 0) \
65 T(CONDITIONAL, "?", 3) \
66 T(INC, "++", 0) \
67 T(DEC, "--", 0) \
68 \
69 /* Assignment operators. */ \
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +000070 /* IsAssignmentOp() and Assignment::is_compound() relies on */ \
71 /* this block of enum values being contiguous and sorted in the */ \
72 /* same order! */ \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000073 T(INIT_VAR, "=init_var", 2) /* AST-use only. */ \
74 T(INIT_CONST, "=init_const", 2) /* AST-use only. */ \
75 T(ASSIGN, "=", 2) \
76 T(ASSIGN_BIT_OR, "|=", 2) \
77 T(ASSIGN_BIT_XOR, "^=", 2) \
78 T(ASSIGN_BIT_AND, "&=", 2) \
79 T(ASSIGN_SHL, "<<=", 2) \
80 T(ASSIGN_SAR, ">>=", 2) \
81 T(ASSIGN_SHR, ">>>=", 2) \
82 T(ASSIGN_ADD, "+=", 2) \
83 T(ASSIGN_SUB, "-=", 2) \
84 T(ASSIGN_MUL, "*=", 2) \
85 T(ASSIGN_DIV, "/=", 2) \
86 T(ASSIGN_MOD, "%=", 2) \
87 \
88 /* Binary operators sorted by precedence. */ \
89 /* IsBinaryOp() relies on this block of enum values */ \
90 /* being contiguous and sorted in the same order! */ \
91 T(COMMA, ",", 1) \
92 T(OR, "||", 4) \
93 T(AND, "&&", 5) \
94 T(BIT_OR, "|", 6) \
95 T(BIT_XOR, "^", 7) \
96 T(BIT_AND, "&", 8) \
97 T(SHL, "<<", 11) \
98 T(SAR, ">>", 11) \
99 T(SHR, ">>>", 11) \
100 T(ADD, "+", 12) \
101 T(SUB, "-", 12) \
102 T(MUL, "*", 13) \
103 T(DIV, "/", 13) \
104 T(MOD, "%", 13) \
105 \
106 /* Compare operators sorted by precedence. */ \
107 /* IsCompareOp() relies on this block of enum values */ \
108 /* being contiguous and sorted in the same order! */ \
109 T(EQ, "==", 9) \
110 T(NE, "!=", 9) \
111 T(EQ_STRICT, "===", 9) \
112 T(NE_STRICT, "!==", 9) \
113 T(LT, "<", 10) \
114 T(GT, ">", 10) \
115 T(LTE, "<=", 10) \
116 T(GTE, ">=", 10) \
117 K(INSTANCEOF, "instanceof", 10) \
118 K(IN, "in", 10) \
119 \
120 /* Unary operators. */ \
121 /* IsUnaryOp() relies on this block of enum values */ \
122 /* being contiguous and sorted in the same order! */ \
123 T(NOT, "!", 0) \
124 T(BIT_NOT, "~", 0) \
125 K(DELETE, "delete", 0) \
126 K(TYPEOF, "typeof", 0) \
127 K(VOID, "void", 0) \
128 \
129 /* Keywords (ECMA-262, section 7.5.2, page 13). */ \
130 K(BREAK, "break", 0) \
131 K(CASE, "case", 0) \
132 K(CATCH, "catch", 0) \
133 K(CONTINUE, "continue", 0) \
134 K(DEBUGGER, "debugger", 0) \
135 K(DEFAULT, "default", 0) \
136 /* DELETE */ \
137 K(DO, "do", 0) \
138 K(ELSE, "else", 0) \
139 K(FINALLY, "finally", 0) \
140 K(FOR, "for", 0) \
141 K(FUNCTION, "function", 0) \
142 K(IF, "if", 0) \
143 /* IN */ \
144 /* INSTANCEOF */ \
145 K(NEW, "new", 0) \
146 K(RETURN, "return", 0) \
147 K(SWITCH, "switch", 0) \
148 K(THIS, "this", 0) \
149 K(THROW, "throw", 0) \
150 K(TRY, "try", 0) \
151 /* TYPEOF */ \
152 K(VAR, "var", 0) \
153 /* VOID */ \
154 K(WHILE, "while", 0) \
155 K(WITH, "with", 0) \
156 \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000157 /* Literals (ECMA-262, section 7.8, page 16). */ \
158 K(NULL_LITERAL, "null", 0) \
159 K(TRUE_LITERAL, "true", 0) \
160 K(FALSE_LITERAL, "false", 0) \
161 T(NUMBER, NULL, 0) \
162 T(STRING, NULL, 0) \
163 \
164 /* Identifiers (not keywords or future reserved words). */ \
165 T(IDENTIFIER, NULL, 0) \
166 \
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000167 /* Future reserved words (ECMA-262, section 7.6.1.2). */ \
168 T(FUTURE_RESERVED_WORD, NULL, 0) \
ager@chromium.org04921a82011-06-27 13:21:41 +0000169 T(FUTURE_STRICT_RESERVED_WORD, NULL, 0) \
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000170 K(CONST, "const", 0) \
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000171 \
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000172 /* Illegal token - not able to scan. */ \
173 T(ILLEGAL, "ILLEGAL", 0) \
174 \
175 /* Scanner-internal use only. */ \
kasperl@chromium.orgb3284ad2009-05-18 06:12:45 +0000176 T(WHITESPACE, NULL, 0)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000177
178
179class Token {
180 public:
181 // All token values.
182#define T(name, string, precedence) name,
183 enum Value {
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +0000184 TOKEN_LIST(T, T)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000185 NUM_TOKENS
186 };
187#undef T
188
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000189 // Returns a string corresponding to the C++ token name
190 // (e.g. "LT" for the token LT).
191 static const char* Name(Value tok) {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000192 ASSERT(tok < NUM_TOKENS); // tok is unsigned
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000193 return name_[tok];
194 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000195
196 // Predicates
sgjesse@chromium.orgd3b3be02010-08-06 08:09:27 +0000197 static bool IsKeyword(Value tok) {
198 return token_type[tok] == 'K';
199 }
200
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000201 static bool IsAssignmentOp(Value tok) {
202 return INIT_VAR <= tok && tok <= ASSIGN_MOD;
203 }
204
205 static bool IsBinaryOp(Value op) {
206 return COMMA <= op && op <= MOD;
207 }
208
209 static bool IsCompareOp(Value op) {
210 return EQ <= op && op <= IN;
211 }
212
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000213 static bool IsOrderedCompareOp(Value op) {
214 return op == LT || op == LTE || op == GT || op == GTE;
215 }
216
217 static Value NegateCompareOp(Value op) {
218 ASSERT(IsCompareOp(op));
219 switch (op) {
220 case EQ: return NE;
221 case NE: return EQ;
222 case EQ_STRICT: return NE_STRICT;
223 case LT: return GTE;
224 case GT: return LTE;
225 case LTE: return GT;
226 case GTE: return LT;
227 default:
228 return op;
229 }
230 }
231
232 static Value InvertCompareOp(Value op) {
233 ASSERT(IsCompareOp(op));
234 switch (op) {
235 case EQ: return NE;
236 case NE: return EQ;
237 case EQ_STRICT: return NE_STRICT;
238 case LT: return GT;
239 case GT: return LT;
240 case LTE: return GTE;
241 case GTE: return LTE;
242 default:
243 return op;
244 }
245 }
246
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000247 static bool IsBitOp(Value op) {
248 return (BIT_OR <= op && op <= SHR) || op == BIT_NOT;
249 }
250
251 static bool IsUnaryOp(Value op) {
252 return (NOT <= op && op <= VOID) || op == ADD || op == SUB;
253 }
254
255 static bool IsCountOp(Value op) {
256 return op == INC || op == DEC;
257 }
258
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000259 static bool IsShiftOp(Value op) {
260 return (SHL <= op) && (op <= SHR);
261 }
262
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000263 // Returns a string corresponding to the JS token string
264 // (.e., "<" for the token LT) or NULL if the token doesn't
265 // have a (unique) string (e.g. an IDENTIFIER).
266 static const char* String(Value tok) {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000267 ASSERT(tok < NUM_TOKENS); // tok is unsigned.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000268 return string_[tok];
269 }
270
271 // Returns the precedence > 0 for binary and compare
272 // operators; returns 0 otherwise.
273 static int Precedence(Value tok) {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000274 ASSERT(tok < NUM_TOKENS); // tok is unsigned.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000275 return precedence_[tok];
276 }
277
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000278 private:
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000279 static const char* const name_[NUM_TOKENS];
280 static const char* const string_[NUM_TOKENS];
281 static const int8_t precedence_[NUM_TOKENS];
sgjesse@chromium.orgd3b3be02010-08-06 08:09:27 +0000282 static const char token_type[NUM_TOKENS];
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000283};
284
285} } // namespace v8::internal
286
287#endif // V8_TOKEN_H_