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_PARSER_H_ | 5 #ifndef V8_PARSER_H_ |
6 #define V8_PARSER_H_ | 6 #define V8_PARSER_H_ |
7 | 7 |
8 #include "src/allocation.h" | 8 #include "src/allocation.h" |
9 #include "src/ast.h" | 9 #include "src/ast.h" |
10 #include "src/compiler.h" // For CachedDataMode | 10 #include "src/compiler.h" // For CachedDataMode |
(...skipping 637 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
648 ~Parser() { | 648 ~Parser() { |
649 delete reusable_preparser_; | 649 delete reusable_preparser_; |
650 reusable_preparser_ = NULL; | 650 reusable_preparser_ = NULL; |
651 delete cached_parse_data_; | 651 delete cached_parse_data_; |
652 cached_parse_data_ = NULL; | 652 cached_parse_data_ = NULL; |
653 } | 653 } |
654 | 654 |
655 // Parses the source code represented by the compilation info and sets its | 655 // Parses the source code represented by the compilation info and sets its |
656 // function literal. Returns false (and deallocates any allocated AST | 656 // function literal. Returns false (and deallocates any allocated AST |
657 // nodes) if parsing failed. | 657 // nodes) if parsing failed. |
658 static bool Parse(CompilationInfo* info, | 658 static bool ParseStatic(CompilationInfo* info, bool allow_lazy = false) { |
659 bool allow_lazy = false) { | |
660 ParseInfo parse_info = {info->isolate()->stack_guard()->real_climit(), | 659 ParseInfo parse_info = {info->isolate()->stack_guard()->real_climit(), |
661 info->isolate()->heap()->HashSeed(), | 660 info->isolate()->heap()->HashSeed(), |
662 info->isolate()->unicode_cache()}; | 661 info->isolate()->unicode_cache()}; |
663 Parser parser(info, &parse_info); | 662 Parser parser(info, &parse_info); |
664 parser.set_allow_lazy(allow_lazy); | 663 parser.set_allow_lazy(allow_lazy); |
665 if (parser.Parse()) { | 664 if (parser.Parse(info)) { |
666 info->SetLanguageMode(info->function()->language_mode()); | 665 info->SetLanguageMode(info->function()->language_mode()); |
667 return true; | 666 return true; |
668 } | 667 } |
669 return false; | 668 return false; |
670 } | 669 } |
671 bool Parse(); | 670 bool Parse(CompilationInfo* info); |
titzer
2015/02/12 10:06:34
What about renaming this ParseSynchronous() and th
| |
672 void ParseOnBackground(); | 671 void ParseOnBackground(CompilationInfo* info); |
673 | 672 |
674 // Handle errors detected during parsing, move statistics to Isolate, | 673 // Handle errors detected during parsing, move statistics to Isolate, |
675 // internalize strings (move them to the heap). | 674 // internalize strings (move them to the heap). |
676 void Internalize(); | 675 void Internalize(CompilationInfo* info); |
677 void HandleSourceURLComments(); | 676 void HandleSourceURLComments(CompilationInfo* info); |
678 | 677 |
679 private: | 678 private: |
680 friend class ParserTraits; | 679 friend class ParserTraits; |
681 | 680 |
682 // Limit the allowed number of local variables in a function. The hard limit | 681 // Limit the allowed number of local variables in a function. The hard limit |
683 // is that offsets computed by FullCodeGenerator::StackOperand and similar | 682 // is that offsets computed by FullCodeGenerator::StackOperand and similar |
684 // functions are ints, and they should not overflow. In addition, accessing | 683 // functions are ints, and they should not overflow. In addition, accessing |
685 // local variables creates user-controlled constants in the generated code, | 684 // local variables creates user-controlled constants in the generated code, |
686 // and we don't want too much user-controlled memory inside the code (this was | 685 // and we don't want too much user-controlled memory inside the code (this was |
687 // the reason why this limit was introduced in the first place; see | 686 // the reason why this limit was introduced in the first place; see |
688 // https://codereview.chromium.org/7003030/ ). | 687 // https://codereview.chromium.org/7003030/ ). |
689 static const int kMaxNumFunctionLocals = 4194303; // 2^22-1 | 688 static const int kMaxNumFunctionLocals = 4194303; // 2^22-1 |
690 | 689 |
691 enum VariableDeclarationContext { | 690 enum VariableDeclarationContext { |
692 kStatementListItem, | 691 kStatementListItem, |
693 kStatement, | 692 kStatement, |
694 kForStatement | 693 kForStatement |
695 }; | 694 }; |
696 | 695 |
697 // If a list of variable declarations includes any initializers. | 696 // If a list of variable declarations includes any initializers. |
698 enum VariableDeclarationProperties { | 697 enum VariableDeclarationProperties { |
699 kHasInitializers, | 698 kHasInitializers, |
700 kHasNoInitializers | 699 kHasNoInitializers |
701 }; | 700 }; |
702 | 701 |
703 // Returns NULL if parsing failed. | 702 // Returns NULL if parsing failed. |
704 FunctionLiteral* ParseProgram(); | 703 FunctionLiteral* ParseProgram(CompilationInfo* info); |
705 | 704 |
706 FunctionLiteral* ParseLazy(); | 705 FunctionLiteral* ParseLazy(CompilationInfo* info); |
707 FunctionLiteral* ParseLazy(Utf16CharacterStream* source); | 706 FunctionLiteral* ParseLazy(CompilationInfo* info, |
708 | 707 Utf16CharacterStream* source); |
709 Isolate* isolate() { return info_->isolate(); } | |
710 CompilationInfo* info() const { return info_; } | |
711 Handle<Script> script() const { return info_->script(); } | |
712 | 708 |
713 // Called by ParseProgram after setting up the scanner. | 709 // Called by ParseProgram after setting up the scanner. |
714 FunctionLiteral* DoParseProgram(CompilationInfo* info, Scope** scope, | 710 FunctionLiteral* DoParseProgram(CompilationInfo* info, Scope** scope, |
715 Scope** ad_hoc_eval_scope); | 711 Scope** ad_hoc_eval_scope); |
716 | 712 |
717 void SetCachedData(); | 713 void SetCachedData(CompilationInfo* info); |
718 | 714 |
719 bool inside_with() const { return scope_->inside_with(); } | 715 bool inside_with() const { return scope_->inside_with(); } |
720 ScriptCompiler::CompileOptions compile_options() const { | 716 ScriptCompiler::CompileOptions compile_options() const { |
721 return info_->compile_options(); | 717 return compile_options_; |
722 } | 718 } |
723 bool consume_cached_parse_data() const { | 719 bool consume_cached_parse_data() const { |
724 return compile_options() == ScriptCompiler::kConsumeParserCache && | 720 return compile_options_ == ScriptCompiler::kConsumeParserCache && |
725 cached_parse_data_ != NULL; | 721 cached_parse_data_ != NULL; |
726 } | 722 } |
727 bool produce_cached_parse_data() const { | 723 bool produce_cached_parse_data() const { |
728 return compile_options() == ScriptCompiler::kProduceParserCache; | 724 return compile_options_ == ScriptCompiler::kProduceParserCache; |
729 } | 725 } |
730 Scope* DeclarationScope(VariableMode mode) { | 726 Scope* DeclarationScope(VariableMode mode) { |
731 return IsLexicalVariableMode(mode) | 727 return IsLexicalVariableMode(mode) |
732 ? scope_ : scope_->DeclarationScope(); | 728 ? scope_ : scope_->DeclarationScope(); |
733 } | 729 } |
734 | 730 |
735 // All ParseXXX functions take as the last argument an *ok parameter | 731 // All ParseXXX functions take as the last argument an *ok parameter |
736 // which is set to false if parsing failed; it is unchanged otherwise. | 732 // which is set to false if parsing failed; it is unchanged otherwise. |
737 // By making the 'exception handling' explicit, we are forced to check | 733 // By making the 'exception handling' explicit, we are forced to check |
738 // for failure at the call sites. | 734 // for failure at the call sites. |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
852 bool* ok); | 848 bool* ok); |
853 | 849 |
854 PreParser::PreParseResult ParseLazyFunctionBodyWithPreParser( | 850 PreParser::PreParseResult ParseLazyFunctionBodyWithPreParser( |
855 SingletonLogger* logger); | 851 SingletonLogger* logger); |
856 | 852 |
857 // Consumes the ending }. | 853 // Consumes the ending }. |
858 ZoneList<Statement*>* ParseEagerFunctionBody( | 854 ZoneList<Statement*>* ParseEagerFunctionBody( |
859 const AstRawString* function_name, int pos, Variable* fvar, | 855 const AstRawString* function_name, int pos, Variable* fvar, |
860 Token::Value fvar_init_op, FunctionKind kind, bool* ok); | 856 Token::Value fvar_init_op, FunctionKind kind, bool* ok); |
861 | 857 |
862 void ThrowPendingError(); | 858 void ThrowPendingError(Isolate* isolate, Handle<Script> script); |
863 | 859 |
864 TemplateLiteralState OpenTemplateLiteral(int pos); | 860 TemplateLiteralState OpenTemplateLiteral(int pos); |
865 void AddTemplateSpan(TemplateLiteralState* state, bool tail); | 861 void AddTemplateSpan(TemplateLiteralState* state, bool tail); |
866 void AddTemplateExpression(TemplateLiteralState* state, | 862 void AddTemplateExpression(TemplateLiteralState* state, |
867 Expression* expression); | 863 Expression* expression); |
868 Expression* CloseTemplateLiteral(TemplateLiteralState* state, int start, | 864 Expression* CloseTemplateLiteral(TemplateLiteralState* state, int start, |
869 Expression* tag); | 865 Expression* tag); |
870 uint32_t ComputeTemplateLiteralHash(const TemplateLiteral* lit); | 866 uint32_t ComputeTemplateLiteralHash(const TemplateLiteral* lit); |
871 | 867 |
872 Scanner scanner_; | 868 Scanner scanner_; |
873 PreParser* reusable_preparser_; | 869 PreParser* reusable_preparser_; |
874 Scope* original_scope_; // for ES5 function declarations in sloppy eval | 870 Scope* original_scope_; // for ES5 function declarations in sloppy eval |
875 Target* target_stack_; // for break, continue statements | 871 Target* target_stack_; // for break, continue statements |
872 ScriptCompiler::CompileOptions compile_options_; | |
876 ParseData* cached_parse_data_; | 873 ParseData* cached_parse_data_; |
877 | 874 |
878 CompilationInfo* info_; | |
879 bool parsing_lazy_arrow_parameters_; // for lazily parsed arrow functions. | 875 bool parsing_lazy_arrow_parameters_; // for lazily parsed arrow functions. |
880 | 876 |
881 // Pending errors. | 877 // Pending errors. |
882 bool has_pending_error_; | 878 bool has_pending_error_; |
883 Scanner::Location pending_error_location_; | 879 Scanner::Location pending_error_location_; |
884 const char* pending_error_message_; | 880 const char* pending_error_message_; |
885 const AstRawString* pending_error_arg_; | 881 const AstRawString* pending_error_arg_; |
886 const char* pending_error_char_arg_; | 882 const char* pending_error_char_arg_; |
887 bool pending_error_is_reference_error_; | 883 bool pending_error_is_reference_error_; |
888 | 884 |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
978 } | 974 } |
979 | 975 |
980 | 976 |
981 Expression* ParserTraits::CloseTemplateLiteral(TemplateLiteralState* state, | 977 Expression* ParserTraits::CloseTemplateLiteral(TemplateLiteralState* state, |
982 int start, Expression* tag) { | 978 int start, Expression* tag) { |
983 return parser_->CloseTemplateLiteral(state, start, tag); | 979 return parser_->CloseTemplateLiteral(state, start, tag); |
984 } | 980 } |
985 } } // namespace v8::internal | 981 } } // namespace v8::internal |
986 | 982 |
987 #endif // V8_PARSER_H_ | 983 #endif // V8_PARSER_H_ |
OLD | NEW |