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