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 T(ARROW, "=>", 0) \ | 45 \ |
46 \ | 46 /* Assignment operators. */ \ |
47 /* Assignment operators. */ \ | 47 /* IsAssignmentOp() and Assignment::is_compound() relies on */ \ |
48 /* IsAssignmentOp() and Assignment::is_compound() relies on */ \ | 48 /* this block of enum values being contiguous and sorted in the */ \ |
49 /* this block of enum values being contiguous and sorted in the */ \ | 49 /* same order! */ \ |
50 /* same order! */ \ | 50 T(INIT_VAR, "=init_var", 2) /* AST-use only. */ \ |
51 T(INIT_VAR, "=init_var", 2) /* AST-use only. */ \ | 51 T(INIT_LET, "=init_let", 2) /* AST-use only. */ \ |
52 T(INIT_LET, "=init_let", 2) /* AST-use only. */ \ | 52 T(INIT_CONST, "=init_const", 2) /* AST-use only. */ \ |
53 T(INIT_CONST, "=init_const", 2) /* AST-use only. */ \ | 53 T(INIT_CONST_LEGACY, "=init_const_legacy", 2) /* AST-use only. */ \ |
54 T(INIT_CONST_LEGACY, "=init_const_legacy", 2) /* AST-use only. */ \ | 54 T(ASSIGN, "=", 2) \ |
55 T(ASSIGN, "=", 2) \ | 55 T(ASSIGN_BIT_OR, "|=", 2) \ |
56 T(ASSIGN_BIT_OR, "|=", 2) \ | 56 T(ASSIGN_BIT_XOR, "^=", 2) \ |
57 T(ASSIGN_BIT_XOR, "^=", 2) \ | 57 T(ASSIGN_BIT_AND, "&=", 2) \ |
58 T(ASSIGN_BIT_AND, "&=", 2) \ | 58 T(ASSIGN_SHL, "<<=", 2) \ |
59 T(ASSIGN_SHL, "<<=", 2) \ | 59 T(ASSIGN_SAR, ">>=", 2) \ |
60 T(ASSIGN_SAR, ">>=", 2) \ | 60 T(ASSIGN_SHR, ">>>=", 2) \ |
61 T(ASSIGN_SHR, ">>>=", 2) \ | 61 T(ASSIGN_ADD, "+=", 2) \ |
62 T(ASSIGN_ADD, "+=", 2) \ | 62 T(ASSIGN_SUB, "-=", 2) \ |
63 T(ASSIGN_SUB, "-=", 2) \ | 63 T(ASSIGN_MUL, "*=", 2) \ |
64 T(ASSIGN_MUL, "*=", 2) \ | 64 T(ASSIGN_DIV, "/=", 2) \ |
65 T(ASSIGN_DIV, "/=", 2) \ | 65 T(ASSIGN_MOD, "%=", 2) \ |
66 T(ASSIGN_MOD, "%=", 2) \ | 66 \ |
67 \ | 67 /* Binary operators sorted by precedence. */ \ |
68 /* Binary operators sorted by precedence. */ \ | 68 /* IsBinaryOp() relies on this block of enum values */ \ |
69 /* IsBinaryOp() relies on this block of enum values */ \ | 69 /* being contiguous and sorted in the same order! */ \ |
70 /* being contiguous and sorted in the same order! */ \ | 70 T(COMMA, ",", 1) \ |
71 T(COMMA, ",", 1) \ | 71 T(OR, "||", 4) \ |
72 T(OR, "||", 4) \ | 72 T(AND, "&&", 5) \ |
73 T(AND, "&&", 5) \ | 73 T(BIT_OR, "|", 6) \ |
74 T(BIT_OR, "|", 6) \ | 74 T(BIT_XOR, "^", 7) \ |
75 T(BIT_XOR, "^", 7) \ | 75 T(BIT_AND, "&", 8) \ |
76 T(BIT_AND, "&", 8) \ | 76 T(SHL, "<<", 11) \ |
77 T(SHL, "<<", 11) \ | 77 T(SAR, ">>", 11) \ |
78 T(SAR, ">>", 11) \ | 78 T(SHR, ">>>", 11) \ |
79 T(SHR, ">>>", 11) \ | 79 T(ROR, "rotate right", 11) /* only used by Crankshaft */ \ |
80 T(ROR, "rotate right", 11) /* only used by Crankshaft */ \ | 80 T(ADD, "+", 12) \ |
81 T(ADD, "+", 12) \ | 81 T(SUB, "-", 12) \ |
82 T(SUB, "-", 12) \ | 82 T(MUL, "*", 13) \ |
83 T(MUL, "*", 13) \ | 83 T(DIV, "/", 13) \ |
84 T(DIV, "/", 13) \ | 84 T(MOD, "%", 13) \ |
85 T(MOD, "%", 13) \ | 85 \ |
86 \ | 86 /* Compare operators sorted by precedence. */ \ |
87 /* Compare operators sorted by precedence. */ \ | 87 /* IsCompareOp() relies on this block of enum values */ \ |
88 /* IsCompareOp() relies on this block of enum values */ \ | 88 /* being contiguous and sorted in the same order! */ \ |
89 /* being contiguous and sorted in the same order! */ \ | 89 T(EQ, "==", 9) \ |
90 T(EQ, "==", 9) \ | 90 T(NE, "!=", 9) \ |
91 T(NE, "!=", 9) \ | 91 T(EQ_STRICT, "===", 9) \ |
92 T(EQ_STRICT, "===", 9) \ | 92 T(NE_STRICT, "!==", 9) \ |
93 T(NE_STRICT, "!==", 9) \ | 93 T(LT, "<", 10) \ |
94 T(LT, "<", 10) \ | 94 T(GT, ">", 10) \ |
95 T(GT, ">", 10) \ | 95 T(LTE, "<=", 10) \ |
96 T(LTE, "<=", 10) \ | 96 T(GTE, ">=", 10) \ |
97 T(GTE, ">=", 10) \ | 97 K(INSTANCEOF, "instanceof", 10) \ |
98 K(INSTANCEOF, "instanceof", 10) \ | 98 K(IN, "in", 10) \ |
99 K(IN, "in", 10) \ | 99 \ |
100 \ | 100 /* Unary operators. */ \ |
101 /* Unary operators. */ \ | 101 /* IsUnaryOp() relies on this block of enum values */ \ |
102 /* IsUnaryOp() relies on this block of enum values */ \ | 102 /* being contiguous and sorted in the same order! */ \ |
103 /* being contiguous and sorted in the same order! */ \ | 103 T(NOT, "!", 0) \ |
104 T(NOT, "!", 0) \ | 104 T(BIT_NOT, "~", 0) \ |
105 T(BIT_NOT, "~", 0) \ | 105 K(DELETE, "delete", 0) \ |
106 K(DELETE, "delete", 0) \ | 106 K(TYPEOF, "typeof", 0) \ |
107 K(TYPEOF, "typeof", 0) \ | 107 K(VOID, "void", 0) \ |
108 K(VOID, "void", 0) \ | 108 \ |
109 \ | 109 /* Keywords (ECMA-262, section 7.5.2, page 13). */ \ |
110 /* Keywords (ECMA-262, section 7.5.2, page 13). */ \ | 110 K(BREAK, "break", 0) \ |
111 K(BREAK, "break", 0) \ | 111 K(CASE, "case", 0) \ |
112 K(CASE, "case", 0) \ | 112 K(CATCH, "catch", 0) \ |
113 K(CATCH, "catch", 0) \ | 113 K(CONTINUE, "continue", 0) \ |
114 K(CONTINUE, "continue", 0) \ | 114 K(DEBUGGER, "debugger", 0) \ |
115 K(DEBUGGER, "debugger", 0) \ | 115 K(DEFAULT, "default", 0) \ |
116 K(DEFAULT, "default", 0) \ | 116 /* DELETE */ \ |
117 /* DELETE */ \ | 117 K(DO, "do", 0) \ |
118 K(DO, "do", 0) \ | 118 K(ELSE, "else", 0) \ |
119 K(ELSE, "else", 0) \ | 119 K(FINALLY, "finally", 0) \ |
120 K(FINALLY, "finally", 0) \ | 120 K(FOR, "for", 0) \ |
121 K(FOR, "for", 0) \ | 121 K(FUNCTION, "function", 0) \ |
122 K(FUNCTION, "function", 0) \ | 122 K(IF, "if", 0) \ |
123 K(IF, "if", 0) \ | 123 /* IN */ \ |
124 /* IN */ \ | 124 /* INSTANCEOF */ \ |
125 /* INSTANCEOF */ \ | 125 K(NEW, "new", 0) \ |
126 K(NEW, "new", 0) \ | 126 K(RETURN, "return", 0) \ |
127 K(RETURN, "return", 0) \ | 127 K(SWITCH, "switch", 0) \ |
128 K(SWITCH, "switch", 0) \ | 128 K(THIS, "this", 0) \ |
129 K(THIS, "this", 0) \ | 129 K(THROW, "throw", 0) \ |
130 K(THROW, "throw", 0) \ | 130 K(TRY, "try", 0) \ |
131 K(TRY, "try", 0) \ | 131 /* TYPEOF */ \ |
132 /* TYPEOF */ \ | 132 K(VAR, "var", 0) \ |
133 K(VAR, "var", 0) \ | 133 /* VOID */ \ |
134 /* VOID */ \ | 134 K(WHILE, "while", 0) \ |
135 K(WHILE, "while", 0) \ | 135 K(WITH, "with", 0) \ |
136 K(WITH, "with", 0) \ | 136 \ |
137 \ | 137 /* Literals (ECMA-262, section 7.8, page 16). */ \ |
138 /* Literals (ECMA-262, section 7.8, page 16). */ \ | 138 K(NULL_LITERAL, "null", 0) \ |
139 K(NULL_LITERAL, "null", 0) \ | 139 K(TRUE_LITERAL, "true", 0) \ |
140 K(TRUE_LITERAL, "true", 0) \ | 140 K(FALSE_LITERAL, "false", 0) \ |
141 K(FALSE_LITERAL, "false", 0) \ | 141 T(NUMBER, NULL, 0) \ |
142 T(NUMBER, NULL, 0) \ | 142 T(STRING, NULL, 0) \ |
143 T(STRING, NULL, 0) \ | 143 \ |
144 \ | 144 /* Identifiers (not keywords or future reserved words). */ \ |
145 /* Identifiers (not keywords or future reserved words). */ \ | 145 T(IDENTIFIER, NULL, 0) \ |
146 T(IDENTIFIER, NULL, 0) \ | 146 \ |
147 \ | 147 /* Future reserved words (ECMA-262, section 7.6.1.2). */ \ |
148 /* Future reserved words (ECMA-262, section 7.6.1.2). */ \ | 148 T(FUTURE_RESERVED_WORD, NULL, 0) \ |
149 T(FUTURE_RESERVED_WORD, NULL, 0) \ | 149 T(FUTURE_STRICT_RESERVED_WORD, NULL, 0) \ |
150 T(FUTURE_STRICT_RESERVED_WORD, NULL, 0) \ | 150 K(CONST, "const", 0) \ |
151 K(CONST, "const", 0) \ | 151 K(EXPORT, "export", 0) \ |
152 K(EXPORT, "export", 0) \ | 152 K(IMPORT, "import", 0) \ |
153 K(IMPORT, "import", 0) \ | 153 K(LET, "let", 0) \ |
154 K(LET, "let", 0) \ | 154 K(YIELD, "yield", 0) \ |
155 K(YIELD, "yield", 0) \ | 155 \ |
156 \ | 156 /* Illegal token - not able to scan. */ \ |
157 /* Illegal token - not able to scan. */ \ | 157 T(ILLEGAL, "ILLEGAL", 0) \ |
158 T(ILLEGAL, "ILLEGAL", 0) \ | 158 \ |
159 \ | 159 /* Scanner-internal use only. */ \ |
160 /* Scanner-internal use only. */ \ | |
161 T(WHITESPACE, NULL, 0) | 160 T(WHITESPACE, NULL, 0) |
162 | 161 |
163 | 162 |
164 class Token { | 163 class Token { |
165 public: | 164 public: |
166 // All token values. | 165 // All token values. |
167 #define T(name, string, precedence) name, | 166 #define T(name, string, precedence) name, |
168 enum Value { | 167 enum Value { |
169 TOKEN_LIST(T, T) | 168 TOKEN_LIST(T, T) |
170 NUM_TOKENS | 169 NUM_TOKENS |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
284 private: | 283 private: |
285 static const char* const name_[NUM_TOKENS]; | 284 static const char* const name_[NUM_TOKENS]; |
286 static const char* const string_[NUM_TOKENS]; | 285 static const char* const string_[NUM_TOKENS]; |
287 static const int8_t precedence_[NUM_TOKENS]; | 286 static const int8_t precedence_[NUM_TOKENS]; |
288 static const char token_type[NUM_TOKENS]; | 287 static const char token_type[NUM_TOKENS]; |
289 }; | 288 }; |
290 | 289 |
291 } } // namespace v8::internal | 290 } } // namespace v8::internal |
292 | 291 |
293 #endif // V8_TOKEN_H_ | 292 #endif // V8_TOKEN_H_ |
OLD | NEW |