OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 329 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
340 Type type_; | 340 Type type_; |
341 | 341 |
342 friend class Expression; | 342 friend class Expression; |
343 }; | 343 }; |
344 | 344 |
345 // Bits 0 and 1 are used to identify the type of expression: | 345 // Bits 0 and 1 are used to identify the type of expression: |
346 // If bit 0 is set, it's an identifier. | 346 // If bit 0 is set, it's an identifier. |
347 // if bit 1 is set, it's a string literal. | 347 // if bit 1 is set, it's a string literal. |
348 // If neither is set, it's no particular type, and both set isn't | 348 // If neither is set, it's no particular type, and both set isn't |
349 // use yet. | 349 // use yet. |
| 350 // Bit 2 is used to mark the expression as being parenthesized, |
| 351 // so "(foo)" isn't recognized as a pure identifier (and possible label). |
350 class Expression { | 352 class Expression { |
351 public: | 353 public: |
352 static Expression Default() { | 354 static Expression Default() { |
353 return Expression(kUnknownExpression); | 355 return Expression(kUnknownExpression); |
354 } | 356 } |
355 | 357 |
356 static Expression FromIdentifier(Identifier id) { | 358 static Expression FromIdentifier(Identifier id) { |
357 return Expression(kIdentifierFlag | (id.type_ << kIdentifierShift)); | 359 return Expression(kIdentifierFlag | (id.type_ << kIdentifierShift)); |
358 } | 360 } |
359 | 361 |
(...skipping 20 matching lines...) Expand all Loading... |
380 bool IsIdentifier() { | 382 bool IsIdentifier() { |
381 return (code_ & kIdentifierFlag) != 0; | 383 return (code_ & kIdentifierFlag) != 0; |
382 } | 384 } |
383 | 385 |
384 // Only works corretly if it is actually an identifier expression. | 386 // Only works corretly if it is actually an identifier expression. |
385 PreParser::Identifier AsIdentifier() { | 387 PreParser::Identifier AsIdentifier() { |
386 return PreParser::Identifier( | 388 return PreParser::Identifier( |
387 static_cast<PreParser::Identifier::Type>(code_ >> kIdentifierShift)); | 389 static_cast<PreParser::Identifier::Type>(code_ >> kIdentifierShift)); |
388 } | 390 } |
389 | 391 |
| 392 bool IsParenthesized() { |
| 393 // If bit 0 or 1 is set, we interpret bit 2 as meaning parenthesized. |
| 394 return (code_ & 7) > 4; |
| 395 } |
| 396 |
| 397 bool IsRawIdentifier() { |
| 398 return !IsParenthesized() && IsIdentifier(); |
| 399 } |
| 400 |
390 bool IsStringLiteral() { return (code_ & kStringLiteralFlag) != 0; } | 401 bool IsStringLiteral() { return (code_ & kStringLiteralFlag) != 0; } |
391 | 402 |
| 403 bool IsRawStringLiteral() { |
| 404 return !IsParenthesized() && IsStringLiteral(); |
| 405 } |
| 406 |
392 bool IsUseStrictLiteral() { | 407 bool IsUseStrictLiteral() { |
393 return (code_ & kStringLiteralMask) == kUseStrictString; | 408 return (code_ & kStringLiteralMask) == kUseStrictString; |
394 } | 409 } |
395 | 410 |
396 bool IsThis() { | 411 bool IsThis() { |
397 return code_ == kThisExpression; | 412 return code_ == kThisExpression; |
398 } | 413 } |
399 | 414 |
400 bool IsThisProperty() { | 415 bool IsThisProperty() { |
401 return code_ == kThisPropertyExpression; | 416 return code_ == kThisPropertyExpression; |
402 } | 417 } |
403 | 418 |
404 bool IsStrictFunction() { | 419 bool IsStrictFunction() { |
405 return code_ == kStrictFunctionExpression; | 420 return code_ == kStrictFunctionExpression; |
406 } | 421 } |
407 | 422 |
| 423 Expression Parenthesize() { |
| 424 int type = code_ & 3; |
| 425 if (type != 0) { |
| 426 // Identifiers and string literals can be parenthesized. |
| 427 // They no longer work as labels or directive prologues, |
| 428 // but are still recognized in other contexts. |
| 429 return Expression(code_ | kParenthesizedExpressionFlag); |
| 430 } |
| 431 // For other types of expressions, it's not important to remember |
| 432 // the parentheses. |
| 433 return *this; |
| 434 } |
| 435 |
408 private: | 436 private: |
409 // First two/three bits are used as flags. | 437 // First two/three bits are used as flags. |
410 // Bit 0 and 1 represent identifiers or strings literals, and are | 438 // Bit 0 and 1 represent identifiers or strings literals, and are |
411 // mutually exclusive, but can both be absent. | 439 // mutually exclusive, but can both be absent. |
| 440 // If bit 0 or 1 are set, bit 2 marks that the expression has |
| 441 // been wrapped in parentheses (a string literal can no longer |
| 442 // be a directive prologue, and an identifier can no longer be |
| 443 // a label. |
412 enum { | 444 enum { |
413 kUnknownExpression = 0, | 445 kUnknownExpression = 0, |
414 // Identifiers | 446 // Identifiers |
415 kIdentifierFlag = 1, // Used to detect labels. | 447 kIdentifierFlag = 1, // Used to detect labels. |
416 kIdentifierShift = 3, | 448 kIdentifierShift = 3, |
417 | 449 |
418 kStringLiteralFlag = 2, // Used to detect directive prologue. | 450 kStringLiteralFlag = 2, // Used to detect directive prologue. |
419 kUnknownStringLiteral = kStringLiteralFlag, | 451 kUnknownStringLiteral = kStringLiteralFlag, |
420 kUseStrictString = kStringLiteralFlag | 8, | 452 kUseStrictString = kStringLiteralFlag | 8, |
421 kStringLiteralMask = kUseStrictString, | 453 kStringLiteralMask = kUseStrictString, |
422 | 454 |
| 455 // Only if identifier or string literal. |
| 456 kParenthesizedExpressionFlag = 4, |
| 457 |
423 // Below here applies if neither identifier nor string literal. | 458 // Below here applies if neither identifier nor string literal. |
424 kThisExpression = 4, | 459 kThisExpression = 4, |
425 kThisPropertyExpression = 8, | 460 kThisPropertyExpression = 8, |
426 kStrictFunctionExpression = 12 | 461 kStrictFunctionExpression = 12 |
427 }; | 462 }; |
428 | 463 |
429 explicit Expression(int expression_code) : code_(expression_code) { } | 464 explicit Expression(int expression_code) : code_(expression_code) { } |
430 | 465 |
431 int code_; | 466 int code_; |
432 }; | 467 }; |
433 | 468 |
434 class Statement { | 469 class Statement { |
435 public: | 470 public: |
436 static Statement Default() { | 471 static Statement Default() { |
437 return Statement(kUnknownStatement); | 472 return Statement(kUnknownStatement); |
438 } | 473 } |
439 | 474 |
440 static Statement FunctionDeclaration() { | 475 static Statement FunctionDeclaration() { |
441 return Statement(kFunctionDeclaration); | 476 return Statement(kFunctionDeclaration); |
442 } | 477 } |
443 | 478 |
444 // Creates expression statement from expression. | 479 // Creates expression statement from expression. |
445 // Preserves being an unparenthesized string literal, possibly | 480 // Preserves being an unparenthesized string literal, possibly |
446 // "use strict". | 481 // "use strict". |
447 static Statement ExpressionStatement(Expression expression) { | 482 static Statement ExpressionStatement(Expression expression) { |
448 if (expression.IsUseStrictLiteral()) { | 483 if (!expression.IsParenthesized()) { |
449 return Statement(kUseStrictExpressionStatement); | 484 if (expression.IsUseStrictLiteral()) { |
450 } | 485 return Statement(kUseStrictExpressionStatement); |
451 if (expression.IsStringLiteral()) { | 486 } |
452 return Statement(kStringLiteralExpressionStatement); | 487 if (expression.IsStringLiteral()) { |
| 488 return Statement(kStringLiteralExpressionStatement); |
| 489 } |
453 } | 490 } |
454 return Default(); | 491 return Default(); |
455 } | 492 } |
456 | 493 |
457 bool IsStringLiteral() { | 494 bool IsStringLiteral() { |
458 return code_ == kStringLiteralExpressionStatement; | 495 return code_ == kStringLiteralExpressionStatement; |
459 } | 496 } |
460 | 497 |
461 bool IsUseStrictLiteral() { | 498 bool IsUseStrictLiteral() { |
462 return code_ == kUseStrictExpressionStatement; | 499 return code_ == kUseStrictExpressionStatement; |
(...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
650 ParserRecorder* log_; | 687 ParserRecorder* log_; |
651 Scope* scope_; | 688 Scope* scope_; |
652 Scanner::Location strict_mode_violation_location_; | 689 Scanner::Location strict_mode_violation_location_; |
653 const char* strict_mode_violation_type_; | 690 const char* strict_mode_violation_type_; |
654 bool parenthesized_function_; | 691 bool parenthesized_function_; |
655 }; | 692 }; |
656 | 693 |
657 } } // v8::internal | 694 } } // v8::internal |
658 | 695 |
659 #endif // V8_PREPARSER_H | 696 #endif // V8_PREPARSER_H |
OLD | NEW |