OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef V8_TOKEN_H_ | 5 #ifndef V8_TOKEN_H_ |
6 #define V8_TOKEN_H_ | 6 #define V8_TOKEN_H_ |
7 | 7 |
8 #include "src/base/logging.h" | 8 #include "src/base/logging.h" |
9 | 9 |
10 namespace v8 { | 10 namespace v8 { |
11 namespace internal { | 11 namespace internal { |
12 | 12 |
13 // TOKEN_LIST takes a list of 3 macros M, all of which satisfy the | 13 // TOKEN_LIST takes a list of 3 macros M, all of which satisfy the |
14 // same signature M(name, string, precedence), where name is the | 14 // same signature M(name, string, precedence), where name is the |
15 // symbolic token name, string is the corresponding syntactic symbol | 15 // symbolic token name, string is the corresponding syntactic symbol |
16 // (or NULL, for literals), and precedence is the precedence (or 0). | 16 // (or NULL, for literals), and precedence is the precedence (or 0). |
17 // The parameters are invoked for token categories as follows: | 17 // The parameters are invoked for token categories as follows: |
18 // | 18 // |
19 // T: Non-keyword tokens | 19 // T: Non-keyword tokens |
20 // K: Keyword tokens | 20 // K: Keyword tokens |
21 | 21 |
22 // IGNORE_TOKEN is a convenience macro that can be supplied as | 22 // IGNORE_TOKEN is a convenience macro that can be supplied as |
23 // an argument (at any position) for a TOKEN_LIST call. It does | 23 // an argument (at any position) for a TOKEN_LIST call. It does |
24 // nothing with tokens belonging to the respective category. | 24 // nothing with tokens belonging to the respective category. |
25 | 25 |
26 #define IGNORE_TOKEN(name, string, precedence) | 26 #define IGNORE_TOKEN(name, string, precedence) |
27 | 27 |
28 #define TOKEN_LIST(T, K) \ | 28 #define TOKEN_LIST(T, K) \ |
29 /* End of source indicator. */ \ | 29 /* End of source indicator. */ \ |
30 T(EOS, "EOS", 0) \ | 30 T(EOS, "EOS", 0) \ |
31 \ | 31 \ |
32 /* Punctuators (ECMA-262, section 7.7, page 15). */ \ | 32 /* Punctuators (ECMA-262, section 7.7, page 15). */ \ |
33 T(LPAREN, "(", 0) \ | 33 T(LPAREN, "(", 0) \ |
34 T(RPAREN, ")", 0) \ | 34 T(RPAREN, ")", 0) \ |
35 T(LBRACK, "[", 0) \ | 35 T(LBRACK, "[", 0) \ |
36 T(RBRACK, "]", 0) \ | 36 T(RBRACK, "]", 0) \ |
37 T(LBRACE, "{", 0) \ | 37 T(LBRACE, "{", 0) \ |
38 T(RBRACE, "}", 0) \ | 38 T(RBRACE, "}", 0) \ |
39 T(COLON, ":", 0) \ | 39 T(COLON, ":", 0) \ |
40 T(SEMICOLON, ";", 0) \ | 40 T(SEMICOLON, ";", 0) \ |
41 T(PERIOD, ".", 0) \ | 41 T(PERIOD, ".", 0) \ |
42 T(CONDITIONAL, "?", 3) \ | 42 T(CONDITIONAL, "?", 3) \ |
43 T(INC, "++", 0) \ | 43 T(INC, "++", 0) \ |
44 T(DEC, "--", 0) \ | 44 T(DEC, "--", 0) \ |
45 \ | 45 T(ARROW, "=>", 0) \ |
46 /* Assignment operators. */ \ | 46 \ |
47 /* IsAssignmentOp() and Assignment::is_compound() relies on */ \ | 47 /* Assignment operators. */ \ |
48 /* this block of enum values being contiguous and sorted in the */ \ | 48 /* IsAssignmentOp() and Assignment::is_compound() relies on */ \ |
49 /* same order! */ \ | 49 /* this block of enum values being contiguous and sorted in the */ \ |
50 T(INIT_VAR, "=init_var", 2) /* AST-use only. */ \ | 50 /* same order! */ \ |
51 T(INIT_LET, "=init_let", 2) /* AST-use only. */ \ | 51 T(INIT_VAR, "=init_var", 2) /* AST-use only. */ \ |
52 T(INIT_CONST, "=init_const", 2) /* AST-use only. */ \ | 52 T(INIT_LET, "=init_let", 2) /* AST-use only. */ \ |
53 T(INIT_CONST_LEGACY, "=init_const_legacy", 2) /* AST-use only. */ \ | 53 T(INIT_CONST, "=init_const", 2) /* AST-use only. */ \ |
54 T(ASSIGN, "=", 2) \ | 54 T(INIT_CONST_LEGACY, "=init_const_legacy", 2) /* AST-use only. */ \ |
55 T(ASSIGN_BIT_OR, "|=", 2) \ | 55 T(ASSIGN, "=", 2) \ |
56 T(ASSIGN_BIT_XOR, "^=", 2) \ | 56 T(ASSIGN_BIT_OR, "|=", 2) \ |
57 T(ASSIGN_BIT_AND, "&=", 2) \ | 57 T(ASSIGN_BIT_XOR, "^=", 2) \ |
58 T(ASSIGN_SHL, "<<=", 2) \ | 58 T(ASSIGN_BIT_AND, "&=", 2) \ |
59 T(ASSIGN_SAR, ">>=", 2) \ | 59 T(ASSIGN_SHL, "<<=", 2) \ |
60 T(ASSIGN_SHR, ">>>=", 2) \ | 60 T(ASSIGN_SAR, ">>=", 2) \ |
61 T(ASSIGN_ADD, "+=", 2) \ | 61 T(ASSIGN_SHR, ">>>=", 2) \ |
62 T(ASSIGN_SUB, "-=", 2) \ | 62 T(ASSIGN_ADD, "+=", 2) \ |
63 T(ASSIGN_MUL, "*=", 2) \ | 63 T(ASSIGN_SUB, "-=", 2) \ |
64 T(ASSIGN_DIV, "/=", 2) \ | 64 T(ASSIGN_MUL, "*=", 2) \ |
65 T(ASSIGN_MOD, "%=", 2) \ | 65 T(ASSIGN_DIV, "/=", 2) \ |
66 \ | 66 T(ASSIGN_MOD, "%=", 2) \ |
67 /* Binary operators sorted by precedence. */ \ | 67 \ |
68 /* IsBinaryOp() relies on this block of enum values */ \ | 68 /* Binary operators sorted by precedence. */ \ |
69 /* being contiguous and sorted in the same order! */ \ | 69 /* IsBinaryOp() relies on this block of enum values */ \ |
70 T(COMMA, ",", 1) \ | 70 /* being contiguous and sorted in the same order! */ \ |
71 T(OR, "||", 4) \ | 71 T(COMMA, ",", 1) \ |
72 T(AND, "&&", 5) \ | 72 T(OR, "||", 4) \ |
73 T(BIT_OR, "|", 6) \ | 73 T(AND, "&&", 5) \ |
74 T(BIT_XOR, "^", 7) \ | 74 T(BIT_OR, "|", 6) \ |
75 T(BIT_AND, "&", 8) \ | 75 T(BIT_XOR, "^", 7) \ |
76 T(SHL, "<<", 11) \ | 76 T(BIT_AND, "&", 8) \ |
77 T(SAR, ">>", 11) \ | 77 T(SHL, "<<", 11) \ |
78 T(SHR, ">>>", 11) \ | 78 T(SAR, ">>", 11) \ |
79 T(ROR, "rotate right", 11) /* only used by Crankshaft */ \ | 79 T(SHR, ">>>", 11) \ |
80 T(ADD, "+", 12) \ | 80 T(ROR, "rotate right", 11) /* only used by Crankshaft */ \ |
81 T(SUB, "-", 12) \ | 81 T(ADD, "+", 12) \ |
82 T(MUL, "*", 13) \ | 82 T(SUB, "-", 12) \ |
83 T(DIV, "/", 13) \ | 83 T(MUL, "*", 13) \ |
84 T(MOD, "%", 13) \ | 84 T(DIV, "/", 13) \ |
85 \ | 85 T(MOD, "%", 13) \ |
86 /* Compare operators sorted by precedence. */ \ | 86 \ |
87 /* IsCompareOp() relies on this block of enum values */ \ | 87 /* Compare operators sorted by precedence. */ \ |
88 /* being contiguous and sorted in the same order! */ \ | 88 /* IsCompareOp() relies on this block of enum values */ \ |
89 T(EQ, "==", 9) \ | 89 /* being contiguous and sorted in the same order! */ \ |
90 T(NE, "!=", 9) \ | 90 T(EQ, "==", 9) \ |
91 T(EQ_STRICT, "===", 9) \ | 91 T(NE, "!=", 9) \ |
92 T(NE_STRICT, "!==", 9) \ | 92 T(EQ_STRICT, "===", 9) \ |
93 T(LT, "<", 10) \ | 93 T(NE_STRICT, "!==", 9) \ |
94 T(GT, ">", 10) \ | 94 T(LT, "<", 10) \ |
95 T(LTE, "<=", 10) \ | 95 T(GT, ">", 10) \ |
96 T(GTE, ">=", 10) \ | 96 T(LTE, "<=", 10) \ |
97 K(INSTANCEOF, "instanceof", 10) \ | 97 T(GTE, ">=", 10) \ |
98 K(IN, "in", 10) \ | 98 K(INSTANCEOF, "instanceof", 10) \ |
99 \ | 99 K(IN, "in", 10) \ |
100 /* Unary operators. */ \ | 100 \ |
101 /* IsUnaryOp() relies on this block of enum values */ \ | 101 /* Unary operators. */ \ |
102 /* being contiguous and sorted in the same order! */ \ | 102 /* IsUnaryOp() relies on this block of enum values */ \ |
103 T(NOT, "!", 0) \ | 103 /* being contiguous and sorted in the same order! */ \ |
104 T(BIT_NOT, "~", 0) \ | 104 T(NOT, "!", 0) \ |
105 K(DELETE, "delete", 0) \ | 105 T(BIT_NOT, "~", 0) \ |
106 K(TYPEOF, "typeof", 0) \ | 106 K(DELETE, "delete", 0) \ |
107 K(VOID, "void", 0) \ | 107 K(TYPEOF, "typeof", 0) \ |
108 \ | 108 K(VOID, "void", 0) \ |
109 /* Keywords (ECMA-262, section 7.5.2, page 13). */ \ | 109 \ |
110 K(BREAK, "break", 0) \ | 110 /* Keywords (ECMA-262, section 7.5.2, page 13). */ \ |
111 K(CASE, "case", 0) \ | 111 K(BREAK, "break", 0) \ |
112 K(CATCH, "catch", 0) \ | 112 K(CASE, "case", 0) \ |
113 K(CONTINUE, "continue", 0) \ | 113 K(CATCH, "catch", 0) \ |
114 K(DEBUGGER, "debugger", 0) \ | 114 K(CONTINUE, "continue", 0) \ |
115 K(DEFAULT, "default", 0) \ | 115 K(DEBUGGER, "debugger", 0) \ |
116 /* DELETE */ \ | 116 K(DEFAULT, "default", 0) \ |
117 K(DO, "do", 0) \ | 117 /* DELETE */ \ |
118 K(ELSE, "else", 0) \ | 118 K(DO, "do", 0) \ |
119 K(FINALLY, "finally", 0) \ | 119 K(ELSE, "else", 0) \ |
120 K(FOR, "for", 0) \ | 120 K(FINALLY, "finally", 0) \ |
121 K(FUNCTION, "function", 0) \ | 121 K(FOR, "for", 0) \ |
122 K(IF, "if", 0) \ | 122 K(FUNCTION, "function", 0) \ |
123 /* IN */ \ | 123 K(IF, "if", 0) \ |
124 /* INSTANCEOF */ \ | 124 /* IN */ \ |
125 K(NEW, "new", 0) \ | 125 /* INSTANCEOF */ \ |
126 K(RETURN, "return", 0) \ | 126 K(NEW, "new", 0) \ |
127 K(SWITCH, "switch", 0) \ | 127 K(RETURN, "return", 0) \ |
128 K(THIS, "this", 0) \ | 128 K(SWITCH, "switch", 0) \ |
129 K(THROW, "throw", 0) \ | 129 K(THIS, "this", 0) \ |
130 K(TRY, "try", 0) \ | 130 K(THROW, "throw", 0) \ |
131 /* TYPEOF */ \ | 131 K(TRY, "try", 0) \ |
132 K(VAR, "var", 0) \ | 132 /* TYPEOF */ \ |
133 /* VOID */ \ | 133 K(VAR, "var", 0) \ |
134 K(WHILE, "while", 0) \ | 134 /* VOID */ \ |
135 K(WITH, "with", 0) \ | 135 K(WHILE, "while", 0) \ |
136 \ | 136 K(WITH, "with", 0) \ |
137 /* Literals (ECMA-262, section 7.8, page 16). */ \ | 137 \ |
138 K(NULL_LITERAL, "null", 0) \ | 138 /* Literals (ECMA-262, section 7.8, page 16). */ \ |
139 K(TRUE_LITERAL, "true", 0) \ | 139 K(NULL_LITERAL, "null", 0) \ |
140 K(FALSE_LITERAL, "false", 0) \ | 140 K(TRUE_LITERAL, "true", 0) \ |
141 T(NUMBER, NULL, 0) \ | 141 K(FALSE_LITERAL, "false", 0) \ |
142 T(STRING, NULL, 0) \ | 142 T(NUMBER, NULL, 0) \ |
143 \ | 143 T(STRING, NULL, 0) \ |
144 /* Identifiers (not keywords or future reserved words). */ \ | 144 \ |
145 T(IDENTIFIER, NULL, 0) \ | 145 /* Identifiers (not keywords or future reserved words). */ \ |
146 \ | 146 T(IDENTIFIER, NULL, 0) \ |
147 /* Future reserved words (ECMA-262, section 7.6.1.2). */ \ | 147 \ |
148 T(FUTURE_RESERVED_WORD, NULL, 0) \ | 148 /* Future reserved words (ECMA-262, section 7.6.1.2). */ \ |
149 T(FUTURE_STRICT_RESERVED_WORD, NULL, 0) \ | 149 T(FUTURE_RESERVED_WORD, NULL, 0) \ |
150 K(CONST, "const", 0) \ | 150 T(FUTURE_STRICT_RESERVED_WORD, NULL, 0) \ |
151 K(EXPORT, "export", 0) \ | 151 K(CONST, "const", 0) \ |
152 K(IMPORT, "import", 0) \ | 152 K(EXPORT, "export", 0) \ |
153 K(LET, "let", 0) \ | 153 K(IMPORT, "import", 0) \ |
154 K(YIELD, "yield", 0) \ | 154 K(LET, "let", 0) \ |
155 \ | 155 K(YIELD, "yield", 0) \ |
156 /* Illegal token - not able to scan. */ \ | 156 \ |
157 T(ILLEGAL, "ILLEGAL", 0) \ | 157 /* Illegal token - not able to scan. */ \ |
158 \ | 158 T(ILLEGAL, "ILLEGAL", 0) \ |
159 /* Scanner-internal use only. */ \ | 159 \ |
| 160 /* Scanner-internal use only. */ \ |
160 T(WHITESPACE, NULL, 0) | 161 T(WHITESPACE, NULL, 0) |
161 | 162 |
162 | 163 |
163 class Token { | 164 class Token { |
164 public: | 165 public: |
165 // All token values. | 166 // All token values. |
166 #define T(name, string, precedence) name, | 167 #define T(name, string, precedence) name, |
167 enum Value { | 168 enum Value { |
168 TOKEN_LIST(T, T) | 169 TOKEN_LIST(T, T) |
169 NUM_TOKENS | 170 NUM_TOKENS |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
283 private: | 284 private: |
284 static const char* const name_[NUM_TOKENS]; | 285 static const char* const name_[NUM_TOKENS]; |
285 static const char* const string_[NUM_TOKENS]; | 286 static const char* const string_[NUM_TOKENS]; |
286 static const int8_t precedence_[NUM_TOKENS]; | 287 static const int8_t precedence_[NUM_TOKENS]; |
287 static const char token_type[NUM_TOKENS]; | 288 static const char token_type[NUM_TOKENS]; |
288 }; | 289 }; |
289 | 290 |
290 } } // namespace v8::internal | 291 } } // namespace v8::internal |
291 | 292 |
292 #endif // V8_TOKEN_H_ | 293 #endif // V8_TOKEN_H_ |
OLD | NEW |