blob: 0f194a3c94dc25bd99d5e4bee55463c8f77c0e51 [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
31namespace v8 { namespace internal {
32
33// TOKEN_LIST takes a list of 3 macros M, all of which satisfy the
34// same signature M(name, string, precedence), where name is the
35// symbolic token name, string is the corresponding syntactic symbol
36// (or NULL, for literals), and precedence is the precedence (or 0).
37// The parameters are invoked for token categories as follows:
38//
39// T: Non-keyword tokens
40// K: Keyword tokens
41// F: Future (reserved) keyword tokens
42
43// IGNORE_TOKEN is a convenience macro that can be supplied as
44// an argument (at any position) for a TOKEN_LIST call. It does
45// nothing with tokens belonging to the respective category.
46
47#define IGNORE_TOKEN(name, string, precedence)
48
49#define TOKEN_LIST(T, K, F) \
50 /* End of source indicator. */ \
51 T(EOS, "EOS", 0) \
52 \
53 /* Punctuators (ECMA-262, section 7.7, page 15). */ \
54 T(LPAREN, "(", 0) \
55 T(RPAREN, ")", 0) \
56 T(LBRACK, "[", 0) \
57 T(RBRACK, "]", 0) \
58 T(LBRACE, "{", 0) \
59 T(RBRACE, "}", 0) \
60 T(COLON, ":", 0) \
61 T(SEMICOLON, ";", 0) \
62 T(PERIOD, ".", 0) \
63 T(CONDITIONAL, "?", 3) \
64 T(INC, "++", 0) \
65 T(DEC, "--", 0) \
66 \
67 /* Assignment operators. */ \
68 /* IsAssignmentOp() relies on this block of enum values */ \
69 /* being contiguous and sorted in the same order! */ \
70 T(INIT_VAR, "=init_var", 2) /* AST-use only. */ \
71 T(INIT_CONST, "=init_const", 2) /* AST-use only. */ \
72 T(ASSIGN, "=", 2) \
73 T(ASSIGN_BIT_OR, "|=", 2) \
74 T(ASSIGN_BIT_XOR, "^=", 2) \
75 T(ASSIGN_BIT_AND, "&=", 2) \
76 T(ASSIGN_SHL, "<<=", 2) \
77 T(ASSIGN_SAR, ">>=", 2) \
78 T(ASSIGN_SHR, ">>>=", 2) \
79 T(ASSIGN_ADD, "+=", 2) \
80 T(ASSIGN_SUB, "-=", 2) \
81 T(ASSIGN_MUL, "*=", 2) \
82 T(ASSIGN_DIV, "/=", 2) \
83 T(ASSIGN_MOD, "%=", 2) \
84 \
85 /* Binary operators sorted by precedence. */ \
86 /* IsBinaryOp() relies on this block of enum values */ \
87 /* being contiguous and sorted in the same order! */ \
88 T(COMMA, ",", 1) \
89 T(OR, "||", 4) \
90 T(AND, "&&", 5) \
91 T(BIT_OR, "|", 6) \
92 T(BIT_XOR, "^", 7) \
93 T(BIT_AND, "&", 8) \
94 T(SHL, "<<", 11) \
95 T(SAR, ">>", 11) \
96 T(SHR, ">>>", 11) \
97 T(ADD, "+", 12) \
98 T(SUB, "-", 12) \
99 T(MUL, "*", 13) \
100 T(DIV, "/", 13) \
101 T(MOD, "%", 13) \
102 \
103 /* Compare operators sorted by precedence. */ \
104 /* IsCompareOp() relies on this block of enum values */ \
105 /* being contiguous and sorted in the same order! */ \
106 T(EQ, "==", 9) \
107 T(NE, "!=", 9) \
108 T(EQ_STRICT, "===", 9) \
109 T(NE_STRICT, "!==", 9) \
110 T(LT, "<", 10) \
111 T(GT, ">", 10) \
112 T(LTE, "<=", 10) \
113 T(GTE, ">=", 10) \
114 K(INSTANCEOF, "instanceof", 10) \
115 K(IN, "in", 10) \
116 \
117 /* Unary operators. */ \
118 /* IsUnaryOp() relies on this block of enum values */ \
119 /* being contiguous and sorted in the same order! */ \
120 T(NOT, "!", 0) \
121 T(BIT_NOT, "~", 0) \
122 K(DELETE, "delete", 0) \
123 K(TYPEOF, "typeof", 0) \
124 K(VOID, "void", 0) \
125 \
126 /* Keywords (ECMA-262, section 7.5.2, page 13). */ \
127 K(BREAK, "break", 0) \
128 K(CASE, "case", 0) \
129 K(CATCH, "catch", 0) \
130 K(CONTINUE, "continue", 0) \
131 K(DEBUGGER, "debugger", 0) \
132 K(DEFAULT, "default", 0) \
133 /* DELETE */ \
134 K(DO, "do", 0) \
135 K(ELSE, "else", 0) \
136 K(FINALLY, "finally", 0) \
137 K(FOR, "for", 0) \
138 K(FUNCTION, "function", 0) \
139 K(IF, "if", 0) \
140 /* IN */ \
141 /* INSTANCEOF */ \
142 K(NEW, "new", 0) \
143 K(RETURN, "return", 0) \
144 K(SWITCH, "switch", 0) \
145 K(THIS, "this", 0) \
146 K(THROW, "throw", 0) \
147 K(TRY, "try", 0) \
148 /* TYPEOF */ \
149 K(VAR, "var", 0) \
150 /* VOID */ \
151 K(WHILE, "while", 0) \
152 K(WITH, "with", 0) \
153 \
154 /* Future reserved words (ECMA-262, section 7.5.3, page 14). */ \
155 F(ABSTRACT, "abstract", 0) \
156 F(BOOLEAN, "boolean", 0) \
157 F(BYTE, "byte", 0) \
158 F(CHAR, "char", 0) \
159 F(CLASS, "class", 0) \
160 K(CONST, "const", 0) \
161 F(DOUBLE, "double", 0) \
162 F(ENUM, "enum", 0) \
163 F(EXPORT, "export", 0) \
164 F(EXTENDS, "extends", 0) \
165 F(FINAL, "final", 0) \
166 F(FLOAT, "float", 0) \
167 F(GOTO, "goto", 0) \
168 F(IMPLEMENTS, "implements", 0) \
169 F(IMPORT, "import", 0) \
170 F(INT, "int", 0) \
171 F(INTERFACE, "interface", 0) \
172 F(LONG, "long", 0) \
173 K(NATIVE, "native", 0) \
174 F(PACKAGE, "package", 0) \
175 F(PRIVATE, "private", 0) \
176 F(PROTECTED, "protected", 0) \
177 F(PUBLIC, "public", 0) \
178 F(SHORT, "short", 0) \
179 F(STATIC, "static", 0) \
180 F(SUPER, "super", 0) \
181 F(SYNCHRONIZED, "synchronized", 0) \
182 F(THROWS, "throws", 0) \
183 F(TRANSIENT, "transient", 0) \
184 F(VOLATILE, "volatile", 0) \
185 \
186 /* Literals (ECMA-262, section 7.8, page 16). */ \
187 K(NULL_LITERAL, "null", 0) \
188 K(TRUE_LITERAL, "true", 0) \
189 K(FALSE_LITERAL, "false", 0) \
190 T(NUMBER, NULL, 0) \
191 T(STRING, NULL, 0) \
192 \
193 /* Identifiers (not keywords or future reserved words). */ \
194 T(IDENTIFIER, NULL, 0) \
195 \
196 /* Illegal token - not able to scan. */ \
197 T(ILLEGAL, "ILLEGAL", 0) \
198 \
199 /* Scanner-internal use only. */ \
200 T(COMMENT, NULL, 0)
201
202
203class Token {
204 public:
205 // All token values.
206#define T(name, string, precedence) name,
207 enum Value {
208 TOKEN_LIST(T, T, IGNORE_TOKEN)
209 NUM_TOKENS
210 };
211#undef T
212
213#ifdef DEBUG
214 // Returns a string corresponding to the C++ token name
215 // (e.g. "LT" for the token LT).
216 static const char* Name(Value tok) {
217 ASSERT(0 <= tok && tok < NUM_TOKENS);
218 return name_[tok];
219 }
220#endif
221
222 // Predicates
223 static bool IsAssignmentOp(Value tok) {
224 return INIT_VAR <= tok && tok <= ASSIGN_MOD;
225 }
226
227 static bool IsBinaryOp(Value op) {
228 return COMMA <= op && op <= MOD;
229 }
230
231 static bool IsCompareOp(Value op) {
232 return EQ <= op && op <= IN;
233 }
234
235 static bool IsBitOp(Value op) {
236 return (BIT_OR <= op && op <= SHR) || op == BIT_NOT;
237 }
238
239 static bool IsUnaryOp(Value op) {
240 return (NOT <= op && op <= VOID) || op == ADD || op == SUB;
241 }
242
243 static bool IsCountOp(Value op) {
244 return op == INC || op == DEC;
245 }
246
247 // Returns a string corresponding to the JS token string
248 // (.e., "<" for the token LT) or NULL if the token doesn't
249 // have a (unique) string (e.g. an IDENTIFIER).
250 static const char* String(Value tok) {
251 ASSERT(0 <= tok && tok < NUM_TOKENS);
252 return string_[tok];
253 }
254
255 // Returns the precedence > 0 for binary and compare
256 // operators; returns 0 otherwise.
257 static int Precedence(Value tok) {
258 ASSERT(0 <= tok && tok < NUM_TOKENS);
259 return precedence_[tok];
260 }
261
262 // Returns the keyword value if str is a keyword;
263 // returns IDENTIFIER otherwise. The class must
264 // have been initialized.
265 static Value Lookup(const char* str);
266
267 // Must be called once to initialize the class.
268 // Multiple calls are ignored.
269 static void Initialize();
270
271 private:
272#ifdef DEBUG
273 static const char* name_[NUM_TOKENS];
274#endif
275 static const char* string_[NUM_TOKENS];
276 static int8_t precedence_[NUM_TOKENS];
277};
278
279} } // namespace v8::internal
280
281#endif // V8_TOKEN_H_