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_PARSING_TOKEN_H_ | 5 #ifndef V8_PARSING_TOKEN_H_ |
6 #define V8_PARSING_TOKEN_H_ | 6 #define V8_PARSING_TOKEN_H_ |
7 | 7 |
8 #include "src/base/logging.h" | 8 #include "src/base/logging.h" |
9 #include "src/globals.h" | 9 #include "src/globals.h" |
10 | 10 |
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
167 T(WHITESPACE, NULL, 0) \ | 167 T(WHITESPACE, NULL, 0) \ |
168 T(UNINITIALIZED, NULL, 0) \ | 168 T(UNINITIALIZED, NULL, 0) \ |
169 \ | 169 \ |
170 /* ES6 Template Literals */ \ | 170 /* ES6 Template Literals */ \ |
171 T(TEMPLATE_SPAN, NULL, 0) \ | 171 T(TEMPLATE_SPAN, NULL, 0) \ |
172 T(TEMPLATE_TAIL, NULL, 0) | 172 T(TEMPLATE_TAIL, NULL, 0) |
173 | 173 |
174 | 174 |
175 class Token { | 175 class Token { |
176 public: | 176 public: |
177 // All token values. | 177 // All token values. |
178 #define T(name, string, precedence) name, | 178 #define T(name, string, precedence) name, |
179 enum Value { | 179 enum Value { TOKEN_LIST(T, T) NUM_TOKENS }; |
180 TOKEN_LIST(T, T) | |
181 NUM_TOKENS | |
182 }; | |
183 #undef T | 180 #undef T |
184 | 181 |
185 // Returns a string corresponding to the C++ token name | 182 // Returns a string corresponding to the C++ token name |
186 // (e.g. "LT" for the token LT). | 183 // (e.g. "LT" for the token LT). |
187 static const char* Name(Value tok) { | 184 static const char* Name(Value tok) { |
188 DCHECK(tok < NUM_TOKENS); // tok is unsigned | 185 DCHECK(tok < NUM_TOKENS); // tok is unsigned |
189 return name_[tok]; | 186 return name_[tok]; |
190 } | 187 } |
191 | 188 |
192 // Predicates | 189 // Predicates |
193 static bool IsKeyword(Value tok) { | 190 static bool IsKeyword(Value tok) { return token_type[tok] == 'K'; } |
194 return token_type[tok] == 'K'; | |
195 } | |
196 | 191 |
197 static bool IsIdentifier(Value tok, LanguageMode language_mode, | 192 static bool IsIdentifier(Value tok, LanguageMode language_mode, |
198 bool is_generator) { | 193 bool is_generator) { |
199 switch (tok) { | 194 switch (tok) { |
200 case IDENTIFIER: | 195 case IDENTIFIER: |
201 return true; | 196 return true; |
202 case ESCAPED_STRICT_RESERVED_WORD: | 197 case ESCAPED_STRICT_RESERVED_WORD: |
203 case FUTURE_STRICT_RESERVED_WORD: | 198 case FUTURE_STRICT_RESERVED_WORD: |
204 case LET: | 199 case LET: |
205 case STATIC: | 200 case STATIC: |
206 return is_sloppy(language_mode); | 201 return is_sloppy(language_mode); |
207 case YIELD: | 202 case YIELD: |
208 return !is_generator && is_sloppy(language_mode); | 203 return !is_generator && is_sloppy(language_mode); |
209 default: | 204 default: |
210 return false; | 205 return false; |
211 } | 206 } |
212 UNREACHABLE(); | 207 UNREACHABLE(); |
213 return false; | 208 return false; |
214 } | 209 } |
215 | 210 |
216 static bool IsAssignmentOp(Value tok) { | 211 static bool IsAssignmentOp(Value tok) { |
217 return INIT <= tok && tok <= ASSIGN_MOD; | 212 return INIT <= tok && tok <= ASSIGN_MOD; |
218 } | 213 } |
219 | 214 |
220 static bool IsBinaryOp(Value op) { | 215 static bool IsBinaryOp(Value op) { return COMMA <= op && op <= MOD; } |
221 return COMMA <= op && op <= MOD; | |
222 } | |
223 | 216 |
224 static bool IsTruncatingBinaryOp(Value op) { | 217 static bool IsTruncatingBinaryOp(Value op) { |
225 return BIT_OR <= op && op <= ROR; | 218 return BIT_OR <= op && op <= ROR; |
226 } | 219 } |
227 | 220 |
228 static bool IsCompareOp(Value op) { | 221 static bool IsCompareOp(Value op) { return EQ <= op && op <= IN; } |
229 return EQ <= op && op <= IN; | |
230 } | |
231 | 222 |
232 static bool IsOrderedRelationalCompareOp(Value op) { | 223 static bool IsOrderedRelationalCompareOp(Value op) { |
233 return op == LT || op == LTE || op == GT || op == GTE; | 224 return op == LT || op == LTE || op == GT || op == GTE; |
234 } | 225 } |
235 | 226 |
236 static bool IsEqualityOp(Value op) { | 227 static bool IsEqualityOp(Value op) { return op == EQ || op == EQ_STRICT; } |
237 return op == EQ || op == EQ_STRICT; | |
238 } | |
239 | 228 |
240 static bool IsInequalityOp(Value op) { | 229 static bool IsInequalityOp(Value op) { return op == NE || op == NE_STRICT; } |
241 return op == NE || op == NE_STRICT; | |
242 } | |
243 | 230 |
244 static bool IsArithmeticCompareOp(Value op) { | 231 static bool IsArithmeticCompareOp(Value op) { |
245 return IsOrderedRelationalCompareOp(op) || | 232 return IsOrderedRelationalCompareOp(op) || IsEqualityOp(op) || |
246 IsEqualityOp(op) || IsInequalityOp(op); | 233 IsInequalityOp(op); |
247 } | 234 } |
248 | 235 |
249 static Value NegateCompareOp(Value op) { | 236 static Value NegateCompareOp(Value op) { |
250 DCHECK(IsArithmeticCompareOp(op)); | 237 DCHECK(IsArithmeticCompareOp(op)); |
251 switch (op) { | 238 switch (op) { |
252 case EQ: return NE; | 239 case EQ: |
253 case NE: return EQ; | 240 return NE; |
254 case EQ_STRICT: return NE_STRICT; | 241 case NE: |
255 case NE_STRICT: return EQ_STRICT; | 242 return EQ; |
256 case LT: return GTE; | 243 case EQ_STRICT: |
257 case GT: return LTE; | 244 return NE_STRICT; |
258 case LTE: return GT; | 245 case NE_STRICT: |
259 case GTE: return LT; | 246 return EQ_STRICT; |
| 247 case LT: |
| 248 return GTE; |
| 249 case GT: |
| 250 return LTE; |
| 251 case LTE: |
| 252 return GT; |
| 253 case GTE: |
| 254 return LT; |
260 default: | 255 default: |
261 UNREACHABLE(); | 256 UNREACHABLE(); |
262 return op; | 257 return op; |
263 } | 258 } |
264 } | 259 } |
265 | 260 |
266 static Value ReverseCompareOp(Value op) { | 261 static Value ReverseCompareOp(Value op) { |
267 DCHECK(IsArithmeticCompareOp(op)); | 262 DCHECK(IsArithmeticCompareOp(op)); |
268 switch (op) { | 263 switch (op) { |
269 case EQ: return EQ; | 264 case EQ: |
270 case NE: return NE; | 265 return EQ; |
271 case EQ_STRICT: return EQ_STRICT; | 266 case NE: |
272 case NE_STRICT: return NE_STRICT; | 267 return NE; |
273 case LT: return GT; | 268 case EQ_STRICT: |
274 case GT: return LT; | 269 return EQ_STRICT; |
275 case LTE: return GTE; | 270 case NE_STRICT: |
276 case GTE: return LTE; | 271 return NE_STRICT; |
| 272 case LT: |
| 273 return GT; |
| 274 case GT: |
| 275 return LT; |
| 276 case LTE: |
| 277 return GTE; |
| 278 case GTE: |
| 279 return LTE; |
277 default: | 280 default: |
278 UNREACHABLE(); | 281 UNREACHABLE(); |
279 return op; | 282 return op; |
280 } | 283 } |
281 } | 284 } |
282 | 285 |
283 static bool IsBitOp(Value op) { | 286 static bool IsBitOp(Value op) { |
284 return (BIT_OR <= op && op <= SHR) || op == BIT_NOT; | 287 return (BIT_OR <= op && op <= SHR) || op == BIT_NOT; |
285 } | 288 } |
286 | 289 |
287 static bool IsUnaryOp(Value op) { | 290 static bool IsUnaryOp(Value op) { |
288 return (NOT <= op && op <= VOID) || op == ADD || op == SUB; | 291 return (NOT <= op && op <= VOID) || op == ADD || op == SUB; |
289 } | 292 } |
290 | 293 |
291 static bool IsCountOp(Value op) { | 294 static bool IsCountOp(Value op) { return op == INC || op == DEC; } |
292 return op == INC || op == DEC; | |
293 } | |
294 | 295 |
295 static bool IsShiftOp(Value op) { | 296 static bool IsShiftOp(Value op) { return (SHL <= op) && (op <= SHR); } |
296 return (SHL <= op) && (op <= SHR); | |
297 } | |
298 | 297 |
299 // Returns a string corresponding to the JS token string | 298 // Returns a string corresponding to the JS token string |
300 // (.e., "<" for the token LT) or NULL if the token doesn't | 299 // (.e., "<" for the token LT) or NULL if the token doesn't |
301 // have a (unique) string (e.g. an IDENTIFIER). | 300 // have a (unique) string (e.g. an IDENTIFIER). |
302 static const char* String(Value tok) { | 301 static const char* String(Value tok) { |
303 DCHECK(tok < NUM_TOKENS); // tok is unsigned. | 302 DCHECK(tok < NUM_TOKENS); // tok is unsigned. |
304 return string_[tok]; | 303 return string_[tok]; |
305 } | 304 } |
306 | 305 |
307 // Returns the precedence > 0 for binary and compare | 306 // Returns the precedence > 0 for binary and compare |
308 // operators; returns 0 otherwise. | 307 // operators; returns 0 otherwise. |
309 static int Precedence(Value tok) { | 308 static int Precedence(Value tok) { |
310 DCHECK(tok < NUM_TOKENS); // tok is unsigned. | 309 DCHECK(tok < NUM_TOKENS); // tok is unsigned. |
311 return precedence_[tok]; | 310 return precedence_[tok]; |
312 } | 311 } |
313 | 312 |
314 private: | 313 private: |
315 static const char* const name_[NUM_TOKENS]; | 314 static const char* const name_[NUM_TOKENS]; |
316 static const char* const string_[NUM_TOKENS]; | 315 static const char* const string_[NUM_TOKENS]; |
317 static const int8_t precedence_[NUM_TOKENS]; | 316 static const int8_t precedence_[NUM_TOKENS]; |
318 static const char token_type[NUM_TOKENS]; | 317 static const char token_type[NUM_TOKENS]; |
319 }; | 318 }; |
320 | 319 |
321 } // namespace internal | 320 } // namespace internal |
322 } // namespace v8 | 321 } // namespace v8 |
323 | 322 |
324 #endif // V8_PARSING_TOKEN_H_ | 323 #endif // V8_PARSING_TOKEN_H_ |
OLD | NEW |