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