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 487 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
498 enum PreParseResult { | 498 enum PreParseResult { |
499 kPreParseStackOverflow, | 499 kPreParseStackOverflow, |
500 kPreParseSuccess | 500 kPreParseSuccess |
501 }; | 501 }; |
502 | 502 |
503 PreParser(Scanner* scanner, | 503 PreParser(Scanner* scanner, |
504 ParserRecorder* log, | 504 ParserRecorder* log, |
505 uintptr_t stack_limit) | 505 uintptr_t stack_limit) |
506 : ParserBase<PreParserTraits>(scanner, stack_limit, this), | 506 : ParserBase<PreParserTraits>(scanner, stack_limit, this), |
507 log_(log), | 507 log_(log), |
| 508 function_state_(NULL), |
508 scope_(NULL), | 509 scope_(NULL), |
509 parenthesized_function_(false) { } | 510 parenthesized_function_(false) { } |
510 | 511 |
511 ~PreParser() {} | 512 ~PreParser() {} |
512 | 513 |
513 // Pre-parse the program from the character stream; returns true on | 514 // Pre-parse the program from the character stream; returns true on |
514 // success (even if parsing failed, the pre-parse data successfully | 515 // success (even if parsing failed, the pre-parse data successfully |
515 // captured the syntax error), and false if a stack-overflow happened | 516 // captured the syntax error), and false if a stack-overflow happened |
516 // during parsing. | 517 // during parsing. |
517 PreParseResult PreParseProgram() { | 518 PreParseResult PreParseProgram() { |
518 Scope top_scope(&scope_, kTopLevelScope); | 519 FunctionState top_scope(&function_state_, &scope_, GLOBAL_SCOPE); |
519 bool ok = true; | 520 bool ok = true; |
520 int start_position = scanner()->peek_location().beg_pos; | 521 int start_position = scanner()->peek_location().beg_pos; |
521 ParseSourceElements(Token::EOS, &ok); | 522 ParseSourceElements(Token::EOS, &ok); |
522 if (stack_overflow()) return kPreParseStackOverflow; | 523 if (stack_overflow()) return kPreParseStackOverflow; |
523 if (!ok) { | 524 if (!ok) { |
524 ReportUnexpectedToken(scanner()->current_token()); | 525 ReportUnexpectedToken(scanner()->current_token()); |
525 } else if (!scope_->is_classic_mode()) { | 526 } else if (!scope_->is_classic_mode()) { |
526 CheckOctalLiteral(start_position, scanner()->location().end_pos, &ok); | 527 CheckOctalLiteral(start_position, scanner()->location().end_pos, &ok); |
527 } | 528 } |
528 return kPreParseSuccess; | 529 return kPreParseSuccess; |
(...skipping 12 matching lines...) Expand all Loading... |
541 ParserRecorder* log); | 542 ParserRecorder* log); |
542 | 543 |
543 private: | 544 private: |
544 friend class PreParserTraits; | 545 friend class PreParserTraits; |
545 | 546 |
546 // These types form an algebra over syntactic categories that is just | 547 // These types form an algebra over syntactic categories that is just |
547 // rich enough to let us recognize and propagate the constructs that | 548 // rich enough to let us recognize and propagate the constructs that |
548 // are either being counted in the preparser data, or is important | 549 // are either being counted in the preparser data, or is important |
549 // to throw the correct syntax error exceptions. | 550 // to throw the correct syntax error exceptions. |
550 | 551 |
551 enum ScopeType { | |
552 kTopLevelScope, | |
553 kFunctionScope | |
554 }; | |
555 | |
556 enum VariableDeclarationContext { | 552 enum VariableDeclarationContext { |
557 kSourceElement, | 553 kSourceElement, |
558 kStatement, | 554 kStatement, |
559 kForStatement | 555 kForStatement |
560 }; | 556 }; |
561 | 557 |
562 // If a list of variable declarations includes any initializers. | 558 // If a list of variable declarations includes any initializers. |
563 enum VariableDeclarationProperties { | 559 enum VariableDeclarationProperties { |
564 kHasInitializers, | 560 kHasInitializers, |
565 kHasNoInitializers | 561 kHasNoInitializers |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
613 }; | 609 }; |
614 | 610 |
615 enum SourceElements { | 611 enum SourceElements { |
616 kUnknownSourceElements | 612 kUnknownSourceElements |
617 }; | 613 }; |
618 | 614 |
619 typedef int Arguments; | 615 typedef int Arguments; |
620 | 616 |
621 class Scope { | 617 class Scope { |
622 public: | 618 public: |
623 Scope(Scope** variable, ScopeType type) | 619 explicit Scope(Scope* outer_scope, ScopeType scope_type) |
624 : variable_(variable), | 620 : scope_type_(scope_type) { |
625 prev_(*variable), | 621 if (outer_scope) { |
626 type_(type), | 622 scope_inside_with_ = |
627 materialized_literal_count_(0), | 623 outer_scope->scope_inside_with_ || is_with_scope(); |
628 expected_properties_(0), | 624 language_mode_ = outer_scope->language_mode(); |
629 with_nesting_count_(0), | 625 } else { |
630 language_mode_( | 626 scope_inside_with_ = is_with_scope(); |
631 (prev_ != NULL) ? prev_->language_mode() : CLASSIC_MODE), | 627 language_mode_ = CLASSIC_MODE; |
632 is_generator_(false) { | 628 } |
633 *variable = this; | |
634 } | 629 } |
635 ~Scope() { *variable_ = prev_; } | 630 |
636 int NextMaterializedLiteralIndex() { return materialized_literal_count_++; } | 631 bool is_with_scope() const { return scope_type_ == WITH_SCOPE; } |
637 void AddProperty() { expected_properties_++; } | 632 bool is_classic_mode() const { |
638 ScopeType type() { return type_; } | 633 return language_mode() == CLASSIC_MODE; |
639 int expected_properties() { return expected_properties_; } | |
640 int materialized_literal_count() { return materialized_literal_count_; } | |
641 bool IsInsideWith() { return with_nesting_count_ != 0; } | |
642 bool is_generator() { return is_generator_; } | |
643 void set_is_generator(bool is_generator) { is_generator_ = is_generator; } | |
644 bool is_classic_mode() { | |
645 return language_mode_ == CLASSIC_MODE; | |
646 } | 634 } |
647 LanguageMode language_mode() { | 635 bool is_extended_mode() { |
648 return language_mode_; | 636 return language_mode() == EXTENDED_MODE; |
649 } | 637 } |
650 void set_language_mode(LanguageMode language_mode) { | 638 bool inside_with() const { |
| 639 return scope_inside_with_; |
| 640 } |
| 641 |
| 642 ScopeType type() { return scope_type_; } |
| 643 LanguageMode language_mode() const { return language_mode_; } |
| 644 void SetLanguageMode(LanguageMode language_mode) { |
651 language_mode_ = language_mode; | 645 language_mode_ = language_mode; |
652 } | 646 } |
653 | 647 |
654 class InsideWith { | 648 private: |
655 public: | 649 ScopeType scope_type_; |
656 explicit InsideWith(Scope* scope) : scope_(scope) { | 650 bool scope_inside_with_; |
657 scope->with_nesting_count_++; | 651 LanguageMode language_mode_; |
658 } | 652 }; |
659 | 653 |
660 ~InsideWith() { scope_->with_nesting_count_--; } | 654 class FunctionState { |
661 | 655 public: |
662 private: | 656 FunctionState(FunctionState** function_state_stack, Scope** scope_stack, |
663 Scope* scope_; | 657 ScopeType scope_type) |
664 DISALLOW_COPY_AND_ASSIGN(InsideWith); | 658 : function_state_stack_(function_state_stack), |
665 }; | 659 outer_function_state_(*function_state_stack), |
| 660 scope_stack_(scope_stack), |
| 661 outer_scope_(*scope_stack), |
| 662 scope_(*scope_stack, scope_type), |
| 663 materialized_literal_count_(0), |
| 664 expected_properties_(0), |
| 665 is_generator_(false) { |
| 666 *scope_stack = &scope_; |
| 667 *function_state_stack = this; |
| 668 } |
| 669 ~FunctionState() { |
| 670 *scope_stack_ = outer_scope_; |
| 671 *function_state_stack_ = outer_function_state_; |
| 672 } |
| 673 int NextMaterializedLiteralIndex() { return materialized_literal_count_++; } |
| 674 void AddProperty() { expected_properties_++; } |
| 675 int expected_properties() { return expected_properties_; } |
| 676 int materialized_literal_count() { return materialized_literal_count_; } |
| 677 bool is_generator() { return is_generator_; } |
| 678 void set_is_generator(bool is_generator) { is_generator_ = is_generator; } |
666 | 679 |
667 private: | 680 private: |
668 Scope** const variable_; | 681 FunctionState** const function_state_stack_; |
669 Scope* const prev_; | 682 FunctionState* const outer_function_state_; |
670 const ScopeType type_; | 683 Scope** const scope_stack_; |
| 684 Scope* const outer_scope_; |
| 685 Scope scope_; |
| 686 |
671 int materialized_literal_count_; | 687 int materialized_literal_count_; |
672 int expected_properties_; | 688 int expected_properties_; |
673 int with_nesting_count_; | |
674 LanguageMode language_mode_; | 689 LanguageMode language_mode_; |
675 bool is_generator_; | 690 bool is_generator_; |
676 }; | 691 }; |
677 | 692 |
| 693 class BlockState { |
| 694 public: |
| 695 BlockState(Scope** scope_stack, ScopeType scope_type) |
| 696 : scope_stack_(scope_stack), |
| 697 outer_scope_(*scope_stack), |
| 698 scope_(*scope_stack, scope_type) { |
| 699 *scope_stack_ = &scope_; |
| 700 } |
| 701 |
| 702 ~BlockState() { *scope_stack_ = outer_scope_; } |
| 703 |
| 704 private: |
| 705 Scope** scope_stack_; |
| 706 Scope* outer_scope_; |
| 707 Scope scope_; |
| 708 }; |
| 709 |
678 // All ParseXXX functions take as the last argument an *ok parameter | 710 // All ParseXXX functions take as the last argument an *ok parameter |
679 // which is set to false if parsing failed; it is unchanged otherwise. | 711 // which is set to false if parsing failed; it is unchanged otherwise. |
680 // By making the 'exception handling' explicit, we are forced to check | 712 // By making the 'exception handling' explicit, we are forced to check |
681 // for failure at the call sites. | 713 // for failure at the call sites. |
682 Statement ParseSourceElement(bool* ok); | 714 Statement ParseSourceElement(bool* ok); |
683 SourceElements ParseSourceElements(int end_token, bool* ok); | 715 SourceElements ParseSourceElements(int end_token, bool* ok); |
684 Statement ParseStatement(bool* ok); | 716 Statement ParseStatement(bool* ok); |
685 Statement ParseFunctionDeclaration(bool* ok); | 717 Statement ParseFunctionDeclaration(bool* ok); |
686 Statement ParseBlock(bool* ok); | 718 Statement ParseBlock(bool* ok); |
687 Statement ParseVariableStatement(VariableDeclarationContext var_context, | 719 Statement ParseVariableStatement(VariableDeclarationContext var_context, |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
727 bool name_is_strict_reserved, | 759 bool name_is_strict_reserved, |
728 bool is_generator, | 760 bool is_generator, |
729 bool* ok); | 761 bool* ok); |
730 void ParseLazyFunctionLiteralBody(bool* ok); | 762 void ParseLazyFunctionLiteralBody(bool* ok); |
731 | 763 |
732 // Logs the currently parsed literal as a symbol in the preparser data. | 764 // Logs the currently parsed literal as a symbol in the preparser data. |
733 void LogSymbol(); | 765 void LogSymbol(); |
734 // Log the currently parsed string literal. | 766 // Log the currently parsed string literal. |
735 Expression GetStringSymbol(); | 767 Expression GetStringSymbol(); |
736 | 768 |
737 void set_language_mode(LanguageMode language_mode) { | |
738 scope_->set_language_mode(language_mode); | |
739 } | |
740 | |
741 bool is_extended_mode() { | |
742 return scope_->language_mode() == EXTENDED_MODE; | |
743 } | |
744 | |
745 LanguageMode language_mode() { return scope_->language_mode(); } | |
746 | |
747 bool CheckInOrOf(bool accept_OF); | 769 bool CheckInOrOf(bool accept_OF); |
748 | 770 |
749 ParserRecorder* log_; | 771 ParserRecorder* log_; |
| 772 FunctionState* function_state_; |
750 Scope* scope_; | 773 Scope* scope_; |
751 bool parenthesized_function_; | 774 bool parenthesized_function_; |
752 }; | 775 }; |
753 | 776 |
754 | 777 |
755 template<class Traits> | 778 template<class Traits> |
756 void ParserBase<Traits>::ReportUnexpectedToken(Token::Value token) { | 779 void ParserBase<Traits>::ReportUnexpectedToken(Token::Value token) { |
757 // We don't report stack overflows here, to avoid increasing the | 780 // We don't report stack overflows here, to avoid increasing the |
758 // stack depth even further. Instead we report it after parsing is | 781 // stack depth even further. Instead we report it after parsing is |
759 // over, in ParseProgram. | 782 // over, in ParseProgram. |
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
921 "accessor_get_set"); | 944 "accessor_get_set"); |
922 } | 945 } |
923 *ok = false; | 946 *ok = false; |
924 } | 947 } |
925 } | 948 } |
926 | 949 |
927 | 950 |
928 } } // v8::internal | 951 } } // v8::internal |
929 | 952 |
930 #endif // V8_PREPARSER_H | 953 #endif // V8_PREPARSER_H |
OLD | NEW |