| 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 407 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 418 bool is_scanned_for_captures_; | 418 bool is_scanned_for_captures_; |
| 419 bool failed_; | 419 bool failed_; |
| 420 }; | 420 }; |
| 421 | 421 |
| 422 // ---------------------------------------------------------------------------- | 422 // ---------------------------------------------------------------------------- |
| 423 // JAVASCRIPT PARSING | 423 // JAVASCRIPT PARSING |
| 424 | 424 |
| 425 // Forward declaration. | 425 // Forward declaration. |
| 426 class SingletonLogger; | 426 class SingletonLogger; |
| 427 | 427 |
| 428 class Parser BASE_EMBEDDED { | 428 class Parser : public ParserBase { |
| 429 public: | 429 public: |
| 430 explicit Parser(CompilationInfo* info); | 430 explicit Parser(CompilationInfo* info); |
| 431 ~Parser() { | 431 ~Parser() { |
| 432 delete reusable_preparser_; | 432 delete reusable_preparser_; |
| 433 reusable_preparser_ = NULL; | 433 reusable_preparser_ = NULL; |
| 434 } | 434 } |
| 435 | 435 |
| 436 bool allow_natives_syntax() const { return allow_natives_syntax_; } | |
| 437 bool allow_lazy() const { return allow_lazy_; } | |
| 438 bool allow_modules() { return scanner().HarmonyModules(); } | |
| 439 bool allow_harmony_scoping() { return scanner().HarmonyScoping(); } | |
| 440 bool allow_generators() const { return allow_generators_; } | |
| 441 bool allow_for_of() const { return allow_for_of_; } | |
| 442 bool allow_harmony_numeric_literals() { | |
| 443 return scanner().HarmonyNumericLiterals(); | |
| 444 } | |
| 445 | |
| 446 void set_allow_natives_syntax(bool allow) { allow_natives_syntax_ = allow; } | |
| 447 void set_allow_lazy(bool allow) { allow_lazy_ = allow; } | |
| 448 void set_allow_modules(bool allow) { scanner().SetHarmonyModules(allow); } | |
| 449 void set_allow_harmony_scoping(bool allow) { | |
| 450 scanner().SetHarmonyScoping(allow); | |
| 451 } | |
| 452 void set_allow_generators(bool allow) { allow_generators_ = allow; } | |
| 453 void set_allow_for_of(bool allow) { allow_for_of_ = allow; } | |
| 454 void set_allow_harmony_numeric_literals(bool allow) { | |
| 455 scanner().SetHarmonyNumericLiterals(allow); | |
| 456 } | |
| 457 | |
| 458 // Parses the source code represented by the compilation info and sets its | 436 // Parses the source code represented by the compilation info and sets its |
| 459 // function literal. Returns false (and deallocates any allocated AST | 437 // function literal. Returns false (and deallocates any allocated AST |
| 460 // nodes) if parsing failed. | 438 // nodes) if parsing failed. |
| 461 static bool Parse(CompilationInfo* info) { return Parser(info).Parse(); } | 439 static bool Parse(CompilationInfo* info) { return Parser(info).Parse(); } |
| 462 bool Parse(); | 440 bool Parse(); |
| 463 | 441 |
| 464 void ReportMessageAt(Scanner::Location loc, | |
| 465 const char* message, | |
| 466 Vector<const char*> args = Vector<const char*>::empty()); | |
| 467 void ReportMessageAt(Scanner::Location loc, | |
| 468 const char* message, | |
| 469 Vector<Handle<String> > args); | |
| 470 | |
| 471 private: | 442 private: |
| 472 static const int kMaxNumFunctionLocals = 131071; // 2^17-1 | 443 static const int kMaxNumFunctionLocals = 131071; // 2^17-1 |
| 473 | 444 |
| 474 enum Mode { | 445 enum Mode { |
| 475 PARSE_LAZILY, | 446 PARSE_LAZILY, |
| 476 PARSE_EAGERLY | 447 PARSE_EAGERLY |
| 477 }; | 448 }; |
| 478 | 449 |
| 479 enum VariableDeclarationContext { | 450 enum VariableDeclarationContext { |
| 480 kModuleElement, | 451 kModuleElement, |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 577 | 548 |
| 578 // Called by ParseProgram after setting up the scanner. | 549 // Called by ParseProgram after setting up the scanner. |
| 579 FunctionLiteral* DoParseProgram(CompilationInfo* info, | 550 FunctionLiteral* DoParseProgram(CompilationInfo* info, |
| 580 Handle<String> source); | 551 Handle<String> source); |
| 581 | 552 |
| 582 // Report syntax error | 553 // Report syntax error |
| 583 void ReportUnexpectedToken(Token::Value token); | 554 void ReportUnexpectedToken(Token::Value token); |
| 584 void ReportInvalidPreparseData(Handle<String> name, bool* ok); | 555 void ReportInvalidPreparseData(Handle<String> name, bool* ok); |
| 585 void ReportMessage(const char* message, Vector<const char*> args); | 556 void ReportMessage(const char* message, Vector<const char*> args); |
| 586 void ReportMessage(const char* message, Vector<Handle<String> > args); | 557 void ReportMessage(const char* message, Vector<Handle<String> > args); |
| 558 void ReportMessageAt(Scanner::Location location, const char* type) { |
| 559 ReportMessageAt(location, type, Vector<const char*>::empty()); |
| 560 } |
| 561 void ReportMessageAt(Scanner::Location loc, |
| 562 const char* message, |
| 563 Vector<const char*> args); |
| 564 void ReportMessageAt(Scanner::Location loc, |
| 565 const char* message, |
| 566 Vector<Handle<String> > args); |
| 587 | 567 |
| 588 void set_pre_parse_data(ScriptDataImpl *data) { | 568 void set_pre_parse_data(ScriptDataImpl *data) { |
| 589 pre_parse_data_ = data; | 569 pre_parse_data_ = data; |
| 590 symbol_cache_.Initialize(data ? data->symbol_count() : 0, zone()); | 570 symbol_cache_.Initialize(data ? data->symbol_count() : 0, zone()); |
| 591 } | 571 } |
| 592 | 572 |
| 593 bool inside_with() const { return top_scope_->inside_with(); } | 573 bool inside_with() const { return top_scope_->inside_with(); } |
| 594 Scanner& scanner() { return scanner_; } | 574 Scanner& scanner() { return scanner_; } |
| 595 Mode mode() const { return mode_; } | 575 Mode mode() const { return mode_; } |
| 596 ScriptDataImpl* pre_parse_data() const { return pre_parse_data_; } | 576 ScriptDataImpl* pre_parse_data() const { return pre_parse_data_; } |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 703 bool name_is_reserved, | 683 bool name_is_reserved, |
| 704 bool is_generator, | 684 bool is_generator, |
| 705 int function_token_position, | 685 int function_token_position, |
| 706 FunctionLiteral::FunctionType type, | 686 FunctionLiteral::FunctionType type, |
| 707 bool* ok); | 687 bool* ok); |
| 708 | 688 |
| 709 | 689 |
| 710 // Magical syntax support. | 690 // Magical syntax support. |
| 711 Expression* ParseV8Intrinsic(bool* ok); | 691 Expression* ParseV8Intrinsic(bool* ok); |
| 712 | 692 |
| 713 INLINE(Token::Value peek()) { | |
| 714 if (stack_overflow_) return Token::ILLEGAL; | |
| 715 return scanner().peek(); | |
| 716 } | |
| 717 | |
| 718 INLINE(Token::Value Next()) { | |
| 719 // BUG 1215673: Find a thread safe way to set a stack limit in | |
| 720 // pre-parse mode. Otherwise, we cannot safely pre-parse from other | |
| 721 // threads. | |
| 722 if (stack_overflow_) { | |
| 723 return Token::ILLEGAL; | |
| 724 } | |
| 725 if (StackLimitCheck(isolate()).HasOverflowed()) { | |
| 726 // Any further calls to Next or peek will return the illegal token. | |
| 727 // The current call must return the next token, which might already | |
| 728 // have been peek'ed. | |
| 729 stack_overflow_ = true; | |
| 730 } | |
| 731 return scanner().Next(); | |
| 732 } | |
| 733 | |
| 734 bool is_generator() const { return current_function_state_->is_generator(); } | 693 bool is_generator() const { return current_function_state_->is_generator(); } |
| 735 | 694 |
| 736 bool CheckInOrOf(bool accept_OF, ForEachStatement::VisitMode* visit_mode); | 695 bool CheckInOrOf(bool accept_OF, ForEachStatement::VisitMode* visit_mode); |
| 737 | 696 |
| 738 bool peek_any_identifier(); | |
| 739 | |
| 740 INLINE(void Consume(Token::Value token)); | |
| 741 void Expect(Token::Value token, bool* ok); | |
| 742 bool Check(Token::Value token); | |
| 743 void ExpectSemicolon(bool* ok); | |
| 744 bool CheckContextualKeyword(Vector<const char> keyword); | |
| 745 void ExpectContextualKeyword(Vector<const char> keyword, bool* ok); | |
| 746 | |
| 747 Handle<String> LiteralString(PretenureFlag tenured) { | 697 Handle<String> LiteralString(PretenureFlag tenured) { |
| 748 if (scanner().is_literal_ascii()) { | 698 if (scanner().is_literal_ascii()) { |
| 749 return isolate_->factory()->NewStringFromAscii( | 699 return isolate_->factory()->NewStringFromAscii( |
| 750 scanner().literal_ascii_string(), tenured); | 700 scanner().literal_ascii_string(), tenured); |
| 751 } else { | 701 } else { |
| 752 return isolate_->factory()->NewStringFromTwoByte( | 702 return isolate_->factory()->NewStringFromTwoByte( |
| 753 scanner().literal_utf16_string(), tenured); | 703 scanner().literal_utf16_string(), tenured); |
| 754 } | 704 } |
| 755 } | 705 } |
| 756 | 706 |
| 757 Handle<String> NextLiteralString(PretenureFlag tenured) { | 707 Handle<String> NextLiteralString(PretenureFlag tenured) { |
| 758 if (scanner().is_next_literal_ascii()) { | 708 if (scanner().is_next_literal_ascii()) { |
| 759 return isolate_->factory()->NewStringFromAscii( | 709 return isolate_->factory()->NewStringFromAscii( |
| 760 scanner().next_literal_ascii_string(), tenured); | 710 scanner().next_literal_ascii_string(), tenured); |
| 761 } else { | 711 } else { |
| 762 return isolate_->factory()->NewStringFromTwoByte( | 712 return isolate_->factory()->NewStringFromTwoByte( |
| 763 scanner().next_literal_utf16_string(), tenured); | 713 scanner().next_literal_utf16_string(), tenured); |
| 764 } | 714 } |
| 765 } | 715 } |
| 766 | 716 |
| 767 Handle<String> GetSymbol(); | 717 Handle<String> GetSymbol(); |
| 768 | 718 |
| 769 // Get odd-ball literals. | 719 // Get odd-ball literals. |
| 770 Literal* GetLiteralUndefined(); | 720 Literal* GetLiteralUndefined(int position); |
| 771 Literal* GetLiteralTheHole(); | 721 Literal* GetLiteralTheHole(int position); |
| 772 | 722 |
| 773 Handle<String> ParseIdentifier(bool* ok); | 723 Handle<String> ParseIdentifier(bool* ok); |
| 774 Handle<String> ParseIdentifierOrStrictReservedWord( | 724 Handle<String> ParseIdentifierOrStrictReservedWord( |
| 775 bool* is_strict_reserved, bool* ok); | 725 bool* is_strict_reserved, bool* ok); |
| 776 Handle<String> ParseIdentifierName(bool* ok); | 726 Handle<String> ParseIdentifierName(bool* ok); |
| 777 Handle<String> ParseIdentifierNameOrGetOrSet(bool* is_get, | 727 Handle<String> ParseIdentifierNameOrGetOrSet(bool* is_get, |
| 778 bool* is_set, | 728 bool* is_set, |
| 779 bool* ok); | 729 bool* ok); |
| 780 | 730 |
| 781 // Determine if the expression is a variable proxy and mark it as being used | 731 // Determine if the expression is a variable proxy and mark it as being used |
| 782 // in an assignment or with a increment/decrement operator. This is currently | 732 // in an assignment or with a increment/decrement operator. This is currently |
| 783 // used on for the statically checking assignments to harmony const bindings. | 733 // used on for the statically checking assignments to harmony const bindings. |
| 784 void MarkAsLValue(Expression* expression); | 734 void MarkAsLValue(Expression* expression); |
| 785 | 735 |
| 786 // Strict mode validation of LValue expressions | 736 // Strict mode validation of LValue expressions |
| 787 void CheckStrictModeLValue(Expression* expression, | 737 void CheckStrictModeLValue(Expression* expression, |
| 788 const char* error, | 738 const char* error, |
| 789 bool* ok); | 739 bool* ok); |
| 790 | 740 |
| 791 // Strict mode octal literal validation. | |
| 792 void CheckOctalLiteral(int beg_pos, int end_pos, bool* ok); | |
| 793 | |
| 794 // For harmony block scoping mode: Check if the scope has conflicting var/let | 741 // For harmony block scoping mode: Check if the scope has conflicting var/let |
| 795 // declarations from different scopes. It covers for example | 742 // declarations from different scopes. It covers for example |
| 796 // | 743 // |
| 797 // function f() { { { var x; } let x; } } | 744 // function f() { { { var x; } let x; } } |
| 798 // function g() { { var x; let x; } } | 745 // function g() { { var x; let x; } } |
| 799 // | 746 // |
| 800 // The var declarations are hoisted to the function scope, but originate from | 747 // The var declarations are hoisted to the function scope, but originate from |
| 801 // a scope where the name has also been let bound or the var declaration is | 748 // a scope where the name has also been let bound or the var declaration is |
| 802 // hoisted over such a scope. | 749 // hoisted over such a scope. |
| 803 void CheckConflictingVarDeclarations(Scope* scope, bool* ok); | 750 void CheckConflictingVarDeclarations(Scope* scope, bool* ok); |
| (...skipping 30 matching lines...) Expand all Loading... |
| 834 // type. Both arguments must be non-null (in the handle sense). | 781 // type. Both arguments must be non-null (in the handle sense). |
| 835 Expression* NewThrowTypeError(Handle<String> type, | 782 Expression* NewThrowTypeError(Handle<String> type, |
| 836 Handle<Object> first, | 783 Handle<Object> first, |
| 837 Handle<Object> second); | 784 Handle<Object> second); |
| 838 | 785 |
| 839 // Generic AST generator for throwing errors from compiled code. | 786 // Generic AST generator for throwing errors from compiled code. |
| 840 Expression* NewThrowError(Handle<String> constructor, | 787 Expression* NewThrowError(Handle<String> constructor, |
| 841 Handle<String> type, | 788 Handle<String> type, |
| 842 Vector< Handle<Object> > arguments); | 789 Vector< Handle<Object> > arguments); |
| 843 | 790 |
| 844 preparser::PreParser::PreParseResult LazyParseFunctionLiteral( | 791 PreParser::PreParseResult LazyParseFunctionLiteral( |
| 845 SingletonLogger* logger); | 792 SingletonLogger* logger); |
| 846 | 793 |
| 847 AstNodeFactory<AstConstructionVisitor>* factory() { | 794 AstNodeFactory<AstConstructionVisitor>* factory() { |
| 848 return current_function_state_->factory(); | 795 return current_function_state_->factory(); |
| 849 } | 796 } |
| 850 | 797 |
| 851 Isolate* isolate_; | 798 Isolate* isolate_; |
| 852 ZoneList<Handle<String> > symbol_cache_; | 799 ZoneList<Handle<String> > symbol_cache_; |
| 853 | 800 |
| 854 Handle<Script> script_; | 801 Handle<Script> script_; |
| 855 Scanner scanner_; | 802 Scanner scanner_; |
| 856 preparser::PreParser* reusable_preparser_; | 803 PreParser* reusable_preparser_; |
| 857 Scope* top_scope_; | 804 Scope* top_scope_; |
| 858 Scope* original_scope_; // for ES5 function declarations in sloppy eval | 805 Scope* original_scope_; // for ES5 function declarations in sloppy eval |
| 859 FunctionState* current_function_state_; | 806 FunctionState* current_function_state_; |
| 860 Target* target_stack_; // for break, continue statements | 807 Target* target_stack_; // for break, continue statements |
| 861 v8::Extension* extension_; | 808 v8::Extension* extension_; |
| 862 ScriptDataImpl* pre_parse_data_; | 809 ScriptDataImpl* pre_parse_data_; |
| 863 FuncNameInferrer* fni_; | 810 FuncNameInferrer* fni_; |
| 864 | 811 |
| 865 Mode mode_; | 812 Mode mode_; |
| 866 bool allow_natives_syntax_; | |
| 867 bool allow_lazy_; | |
| 868 bool allow_generators_; | |
| 869 bool allow_for_of_; | |
| 870 bool stack_overflow_; | |
| 871 // If true, the next (and immediately following) function literal is | 813 // If true, the next (and immediately following) function literal is |
| 872 // preceded by a parenthesis. | 814 // preceded by a parenthesis. |
| 873 // Heuristically that means that the function will be called immediately, | 815 // Heuristically that means that the function will be called immediately, |
| 874 // so never lazily compile it. | 816 // so never lazily compile it. |
| 875 bool parenthesized_function_; | 817 bool parenthesized_function_; |
| 876 | 818 |
| 877 Zone* zone_; | 819 Zone* zone_; |
| 878 CompilationInfo* info_; | 820 CompilationInfo* info_; |
| 879 friend class BlockState; | 821 friend class BlockState; |
| 880 friend class FunctionState; | 822 friend class FunctionState; |
| 881 friend class ObjectLiteralChecker<Parser>; | |
| 882 }; | 823 }; |
| 883 | 824 |
| 884 | 825 |
| 885 // Support for handling complex values (array and object literals) that | 826 // Support for handling complex values (array and object literals) that |
| 886 // can be fully handled at compile time. | 827 // can be fully handled at compile time. |
| 887 class CompileTimeValue: public AllStatic { | 828 class CompileTimeValue: public AllStatic { |
| 888 public: | 829 public: |
| 889 enum LiteralType { | 830 enum LiteralType { |
| 890 OBJECT_LITERAL_FAST_ELEMENTS, | 831 OBJECT_LITERAL_FAST_ELEMENTS, |
| 891 OBJECT_LITERAL_SLOW_ELEMENTS, | 832 OBJECT_LITERAL_SLOW_ELEMENTS, |
| (...skipping 14 matching lines...) Expand all Loading... |
| 906 private: | 847 private: |
| 907 static const int kLiteralTypeSlot = 0; | 848 static const int kLiteralTypeSlot = 0; |
| 908 static const int kElementsSlot = 1; | 849 static const int kElementsSlot = 1; |
| 909 | 850 |
| 910 DISALLOW_IMPLICIT_CONSTRUCTORS(CompileTimeValue); | 851 DISALLOW_IMPLICIT_CONSTRUCTORS(CompileTimeValue); |
| 911 }; | 852 }; |
| 912 | 853 |
| 913 } } // namespace v8::internal | 854 } } // namespace v8::internal |
| 914 | 855 |
| 915 #endif // V8_PARSER_H_ | 856 #endif // V8_PARSER_H_ |
| OLD | NEW |