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