| 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 386 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 397 bool multiline_; | 397 bool multiline_; |
| 398 bool simple_; | 398 bool simple_; |
| 399 bool contains_anchor_; | 399 bool contains_anchor_; |
| 400 bool is_scanned_for_captures_; | 400 bool is_scanned_for_captures_; |
| 401 bool failed_; | 401 bool failed_; |
| 402 }; | 402 }; |
| 403 | 403 |
| 404 // ---------------------------------------------------------------------------- | 404 // ---------------------------------------------------------------------------- |
| 405 // JAVASCRIPT PARSING | 405 // JAVASCRIPT PARSING |
| 406 | 406 |
| 407 class Parser; | 407 // Forward declaration. |
| 408 class SingletonLogger; | 408 class SingletonLogger; |
| 409 | 409 |
| 410 class ParserTraits { | 410 class Parser : public ParserBase { |
| 411 public: | |
| 412 typedef Parser* ParserType; | |
| 413 // Return types for traversing functions. | |
| 414 typedef Handle<String> IdentifierType; | |
| 415 | |
| 416 explicit ParserTraits(Parser* parser) : parser_(parser) {} | |
| 417 | |
| 418 // Helper functions for recursive descent. | |
| 419 bool is_classic_mode() const; | |
| 420 bool is_generator() const; | |
| 421 bool IsEvalOrArguments(Handle<String> identifier) const; | |
| 422 | |
| 423 // Reporting errors. | |
| 424 void ReportMessageAt(Scanner::Location source_location, | |
| 425 const char* message, | |
| 426 Vector<const char*> args); | |
| 427 void ReportMessage(const char* message, Vector<Handle<String> > args); | |
| 428 void ReportMessageAt(Scanner::Location source_location, | |
| 429 const char* message, | |
| 430 Vector<Handle<String> > args); | |
| 431 | |
| 432 // Identifiers: | |
| 433 static IdentifierType EmptyIdentifier() { | |
| 434 return Handle<String>(); | |
| 435 } | |
| 436 | |
| 437 IdentifierType GetSymbol(); | |
| 438 | |
| 439 private: | |
| 440 Parser* parser_; | |
| 441 }; | |
| 442 | |
| 443 | |
| 444 class Parser : public ParserBase<ParserTraits> { | |
| 445 public: | 411 public: |
| 446 explicit Parser(CompilationInfo* info); | 412 explicit Parser(CompilationInfo* info); |
| 447 ~Parser() { | 413 ~Parser() { |
| 448 delete reusable_preparser_; | 414 delete reusable_preparser_; |
| 449 reusable_preparser_ = NULL; | 415 reusable_preparser_ = NULL; |
| 450 } | 416 } |
| 451 | 417 |
| 452 // Parses the source code represented by the compilation info and sets its | 418 // Parses the source code represented by the compilation info and sets its |
| 453 // function literal. Returns false (and deallocates any allocated AST | 419 // function literal. Returns false (and deallocates any allocated AST |
| 454 // nodes) if parsing failed. | 420 // nodes) if parsing failed. |
| 455 static bool Parse(CompilationInfo* info, | 421 static bool Parse(CompilationInfo* info, |
| 456 bool allow_lazy = false) { | 422 bool allow_lazy = false) { |
| 457 Parser parser(info); | 423 Parser parser(info); |
| 458 parser.set_allow_lazy(allow_lazy); | 424 parser.set_allow_lazy(allow_lazy); |
| 459 return parser.Parse(); | 425 return parser.Parse(); |
| 460 } | 426 } |
| 461 bool Parse(); | 427 bool Parse(); |
| 462 | 428 |
| 463 private: | 429 private: |
| 464 friend class ParserTraits; | |
| 465 | |
| 466 static const int kMaxNumFunctionLocals = 131071; // 2^17-1 | 430 static const int kMaxNumFunctionLocals = 131071; // 2^17-1 |
| 467 | 431 |
| 468 enum Mode { | 432 enum Mode { |
| 469 PARSE_LAZILY, | 433 PARSE_LAZILY, |
| 470 PARSE_EAGERLY | 434 PARSE_EAGERLY |
| 471 }; | 435 }; |
| 472 | 436 |
| 473 enum VariableDeclarationContext { | 437 enum VariableDeclarationContext { |
| 474 kModuleElement, | 438 kModuleElement, |
| 475 kBlockElement, | 439 kBlockElement, |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 550 } | 514 } |
| 551 ~ParsingModeScope() { | 515 ~ParsingModeScope() { |
| 552 parser_->mode_ = old_mode_; | 516 parser_->mode_ = old_mode_; |
| 553 } | 517 } |
| 554 | 518 |
| 555 private: | 519 private: |
| 556 Parser* parser_; | 520 Parser* parser_; |
| 557 Mode old_mode_; | 521 Mode old_mode_; |
| 558 }; | 522 }; |
| 559 | 523 |
| 524 virtual bool is_classic_mode() { |
| 525 return top_scope_->is_classic_mode(); |
| 526 } |
| 527 |
| 560 // Returns NULL if parsing failed. | 528 // Returns NULL if parsing failed. |
| 561 FunctionLiteral* ParseProgram(); | 529 FunctionLiteral* ParseProgram(); |
| 562 | 530 |
| 563 FunctionLiteral* ParseLazy(); | 531 FunctionLiteral* ParseLazy(); |
| 564 FunctionLiteral* ParseLazy(Utf16CharacterStream* source); | 532 FunctionLiteral* ParseLazy(Utf16CharacterStream* source); |
| 565 | 533 |
| 566 Isolate* isolate() { return isolate_; } | 534 Isolate* isolate() { return isolate_; } |
| 567 Zone* zone() const { return zone_; } | 535 Zone* zone() const { return zone_; } |
| 568 CompilationInfo* info() const { return info_; } | 536 CompilationInfo* info() const { return info_; } |
| 569 | 537 |
| 570 // Called by ParseProgram after setting up the scanner. | 538 // Called by ParseProgram after setting up the scanner. |
| 571 FunctionLiteral* DoParseProgram(CompilationInfo* info, | 539 FunctionLiteral* DoParseProgram(CompilationInfo* info, |
| 572 Handle<String> source); | 540 Handle<String> source); |
| 573 | 541 |
| 574 // Report syntax error | 542 // Report syntax error |
| 575 void ReportInvalidPreparseData(Handle<String> name, bool* ok); | 543 void ReportInvalidPreparseData(Handle<String> name, bool* ok); |
| 544 void ReportMessage(const char* message, Vector<const char*> args); |
| 545 void ReportMessage(const char* message, Vector<Handle<String> > args); |
| 546 void ReportMessageAt(Scanner::Location location, const char* type) { |
| 547 ReportMessageAt(location, type, Vector<const char*>::empty()); |
| 548 } |
| 549 void ReportMessageAt(Scanner::Location loc, |
| 550 const char* message, |
| 551 Vector<const char*> args); |
| 552 void ReportMessageAt(Scanner::Location loc, |
| 553 const char* message, |
| 554 Vector<Handle<String> > args); |
| 576 | 555 |
| 577 void set_pre_parse_data(ScriptDataImpl *data) { | 556 void set_pre_parse_data(ScriptDataImpl *data) { |
| 578 pre_parse_data_ = data; | 557 pre_parse_data_ = data; |
| 579 symbol_cache_.Initialize(data ? data->symbol_count() : 0, zone()); | 558 symbol_cache_.Initialize(data ? data->symbol_count() : 0, zone()); |
| 580 } | 559 } |
| 581 | 560 |
| 582 bool inside_with() const { return top_scope_->inside_with(); } | 561 bool inside_with() const { return top_scope_->inside_with(); } |
| 583 Scanner& scanner() { return scanner_; } | 562 Scanner& scanner() { return scanner_; } |
| 584 Mode mode() const { return mode_; } | 563 Mode mode() const { return mode_; } |
| 585 ScriptDataImpl* pre_parse_data() const { return pre_parse_data_; } | 564 ScriptDataImpl* pre_parse_data() const { return pre_parse_data_; } |
| 586 bool is_extended_mode() { | 565 bool is_extended_mode() { |
| 587 ASSERT(top_scope_ != NULL); | 566 ASSERT(top_scope_ != NULL); |
| 588 return top_scope_->is_extended_mode(); | 567 return top_scope_->is_extended_mode(); |
| 589 } | 568 } |
| 590 Scope* DeclarationScope(VariableMode mode) { | 569 Scope* DeclarationScope(VariableMode mode) { |
| 591 return IsLexicalVariableMode(mode) | 570 return IsLexicalVariableMode(mode) |
| 592 ? top_scope_ : top_scope_->DeclarationScope(); | 571 ? top_scope_ : top_scope_->DeclarationScope(); |
| 593 } | 572 } |
| 594 | 573 |
| 574 // Check if the given string is 'eval' or 'arguments'. |
| 575 bool IsEvalOrArguments(Handle<String> string); |
| 576 |
| 595 // All ParseXXX functions take as the last argument an *ok parameter | 577 // All ParseXXX functions take as the last argument an *ok parameter |
| 596 // which is set to false if parsing failed; it is unchanged otherwise. | 578 // which is set to false if parsing failed; it is unchanged otherwise. |
| 597 // By making the 'exception handling' explicit, we are forced to check | 579 // By making the 'exception handling' explicit, we are forced to check |
| 598 // for failure at the call sites. | 580 // for failure at the call sites. |
| 599 void* ParseSourceElements(ZoneList<Statement*>* processor, int end_token, | 581 void* ParseSourceElements(ZoneList<Statement*>* processor, int end_token, |
| 600 bool is_eval, bool is_global, bool* ok); | 582 bool is_eval, bool is_global, bool* ok); |
| 601 Statement* ParseModuleElement(ZoneStringList* labels, bool* ok); | 583 Statement* ParseModuleElement(ZoneStringList* labels, bool* ok); |
| 602 Statement* ParseModuleDeclaration(ZoneStringList* names, bool* ok); | 584 Statement* ParseModuleDeclaration(ZoneStringList* names, bool* ok); |
| 603 Module* ParseModule(bool* ok); | 585 Module* ParseModule(bool* ok); |
| 604 Module* ParseModuleLiteral(bool* ok); | 586 Module* ParseModuleLiteral(bool* ok); |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 671 Scanner::Location function_name_location, | 653 Scanner::Location function_name_location, |
| 672 bool name_is_strict_reserved, | 654 bool name_is_strict_reserved, |
| 673 bool is_generator, | 655 bool is_generator, |
| 674 int function_token_position, | 656 int function_token_position, |
| 675 FunctionLiteral::FunctionType type, | 657 FunctionLiteral::FunctionType type, |
| 676 bool* ok); | 658 bool* ok); |
| 677 | 659 |
| 678 // Magical syntax support. | 660 // Magical syntax support. |
| 679 Expression* ParseV8Intrinsic(bool* ok); | 661 Expression* ParseV8Intrinsic(bool* ok); |
| 680 | 662 |
| 663 bool is_generator() const { return current_function_state_->is_generator(); } |
| 664 |
| 681 bool CheckInOrOf(bool accept_OF, ForEachStatement::VisitMode* visit_mode); | 665 bool CheckInOrOf(bool accept_OF, ForEachStatement::VisitMode* visit_mode); |
| 682 | 666 |
| 683 Handle<String> LiteralString(PretenureFlag tenured) { | 667 Handle<String> LiteralString(PretenureFlag tenured) { |
| 684 if (scanner().is_literal_ascii()) { | 668 if (scanner().is_literal_ascii()) { |
| 685 return isolate_->factory()->NewStringFromAscii( | 669 return isolate_->factory()->NewStringFromAscii( |
| 686 scanner().literal_ascii_string(), tenured); | 670 scanner().literal_ascii_string(), tenured); |
| 687 } else { | 671 } else { |
| 688 return isolate_->factory()->NewStringFromTwoByte( | 672 return isolate_->factory()->NewStringFromTwoByte( |
| 689 scanner().literal_utf16_string(), tenured); | 673 scanner().literal_utf16_string(), tenured); |
| 690 } | 674 } |
| 691 } | 675 } |
| 692 | 676 |
| 693 Handle<String> NextLiteralString(PretenureFlag tenured) { | 677 Handle<String> NextLiteralString(PretenureFlag tenured) { |
| 694 if (scanner().is_next_literal_ascii()) { | 678 if (scanner().is_next_literal_ascii()) { |
| 695 return isolate_->factory()->NewStringFromAscii( | 679 return isolate_->factory()->NewStringFromAscii( |
| 696 scanner().next_literal_ascii_string(), tenured); | 680 scanner().next_literal_ascii_string(), tenured); |
| 697 } else { | 681 } else { |
| 698 return isolate_->factory()->NewStringFromTwoByte( | 682 return isolate_->factory()->NewStringFromTwoByte( |
| 699 scanner().next_literal_utf16_string(), tenured); | 683 scanner().next_literal_utf16_string(), tenured); |
| 700 } | 684 } |
| 701 } | 685 } |
| 702 | 686 |
| 687 Handle<String> GetSymbol(); |
| 688 |
| 703 // Get odd-ball literals. | 689 // Get odd-ball literals. |
| 704 Literal* GetLiteralUndefined(int position); | 690 Literal* GetLiteralUndefined(int position); |
| 705 Literal* GetLiteralTheHole(int position); | 691 Literal* GetLiteralTheHole(int position); |
| 706 | 692 |
| 693 Handle<String> ParseIdentifier(AllowEvalOrArgumentsAsIdentifier, bool* ok); |
| 694 Handle<String> ParseIdentifierOrStrictReservedWord( |
| 695 bool* is_strict_reserved, bool* ok); |
| 696 Handle<String> ParseIdentifierName(bool* ok); |
| 697 Handle<String> ParseIdentifierNameOrGetOrSet(bool* is_get, |
| 698 bool* is_set, |
| 699 bool* ok); |
| 700 |
| 707 // Determine if the expression is a variable proxy and mark it as being used | 701 // Determine if the expression is a variable proxy and mark it as being used |
| 708 // in an assignment or with a increment/decrement operator. This is currently | 702 // in an assignment or with a increment/decrement operator. This is currently |
| 709 // used on for the statically checking assignments to harmony const bindings. | 703 // used on for the statically checking assignments to harmony const bindings. |
| 710 void MarkAsLValue(Expression* expression); | 704 void MarkAsLValue(Expression* expression); |
| 711 | 705 |
| 712 // Strict mode validation of LValue expressions | 706 // Strict mode validation of LValue expressions |
| 713 void CheckStrictModeLValue(Expression* expression, | 707 void CheckStrictModeLValue(Expression* expression, |
| 714 bool* ok); | 708 bool* ok); |
| 715 | 709 |
| 716 // For harmony block scoping mode: Check if the scope has conflicting var/let | 710 // For harmony block scoping mode: Check if the scope has conflicting var/let |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 822 private: | 816 private: |
| 823 static const int kLiteralTypeSlot = 0; | 817 static const int kLiteralTypeSlot = 0; |
| 824 static const int kElementsSlot = 1; | 818 static const int kElementsSlot = 1; |
| 825 | 819 |
| 826 DISALLOW_IMPLICIT_CONSTRUCTORS(CompileTimeValue); | 820 DISALLOW_IMPLICIT_CONSTRUCTORS(CompileTimeValue); |
| 827 }; | 821 }; |
| 828 | 822 |
| 829 } } // namespace v8::internal | 823 } } // namespace v8::internal |
| 830 | 824 |
| 831 #endif // V8_PARSER_H_ | 825 #endif // V8_PARSER_H_ |
| OLD | NEW |