| 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_PARSING_PREPARSER_H | 5 #ifndef V8_PARSING_PREPARSER_H |
| 6 #define V8_PARSING_PREPARSER_H | 6 #define V8_PARSING_PREPARSER_H |
| 7 | 7 |
| 8 #include "src/ast/scopes.h" | 8 #include "src/ast/scopes.h" |
| 9 #include "src/bailout-reason.h" | 9 #include "src/bailout-reason.h" |
| 10 #include "src/base/hashmap.h" | 10 #include "src/base/hashmap.h" |
| 11 #include "src/messages.h" | 11 #include "src/messages.h" |
| 12 #include "src/parsing/expression-classifier.h" | 12 #include "src/parsing/expression-classifier.h" |
| 13 #include "src/parsing/func-name-inferrer.h" | 13 #include "src/parsing/func-name-inferrer.h" |
| 14 #include "src/parsing/parser-base.h" | 14 #include "src/parsing/parser-base.h" |
| 15 #include "src/parsing/scanner.h" | 15 #include "src/parsing/scanner.h" |
| 16 #include "src/parsing/token.h" | 16 #include "src/parsing/token.h" |
| 17 | 17 |
| 18 namespace v8 { | 18 namespace v8 { |
| 19 namespace internal { | 19 namespace internal { |
| 20 | 20 |
| 21 // Whereas the Parser generates AST during the recursive descent, |
| 22 // the PreParser doesn't create a tree. Instead, it passes around minimal |
| 23 // data objects (PreParserExpression, PreParserIdentifier etc.) which contain |
| 24 // just enough data for the upper layer functions. PreParserFactory is |
| 25 // responsible for creating these dummy objects. It provides a similar kind of |
| 26 // interface as AstNodeFactory, so ParserBase doesn't need to care which one is |
| 27 // used. |
| 28 |
| 21 class PreParserIdentifier { | 29 class PreParserIdentifier { |
| 22 public: | 30 public: |
| 23 PreParserIdentifier() : type_(kUnknownIdentifier) {} | 31 PreParserIdentifier() : type_(kUnknownIdentifier) {} |
| 24 static PreParserIdentifier Default() { | 32 static PreParserIdentifier Default() { |
| 25 return PreParserIdentifier(kUnknownIdentifier); | 33 return PreParserIdentifier(kUnknownIdentifier); |
| 26 } | 34 } |
| 27 static PreParserIdentifier Eval() { | 35 static PreParserIdentifier Eval() { |
| 28 return PreParserIdentifier(kEvalIdentifier); | 36 return PreParserIdentifier(kEvalIdentifier); |
| 29 } | 37 } |
| 30 static PreParserIdentifier Arguments() { | 38 static PreParserIdentifier Arguments() { |
| (...skipping 547 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 578 int arity = 0; | 586 int arity = 0; |
| 579 | 587 |
| 580 int Arity() const { return arity; } | 588 int Arity() const { return arity; } |
| 581 PreParserIdentifier at(int i) { return PreParserIdentifier(); } // Dummy | 589 PreParserIdentifier at(int i) { return PreParserIdentifier(); } // Dummy |
| 582 }; | 590 }; |
| 583 | 591 |
| 584 | 592 |
| 585 class PreParser; | 593 class PreParser; |
| 586 | 594 |
| 587 template <> | 595 template <> |
| 588 class ParserBaseTraits<PreParser> { | 596 struct ParserTypes<PreParser> { |
| 589 public: | 597 typedef ParserBase<PreParser> Base; |
| 590 struct Type { | 598 typedef PreParser Impl; |
| 591 typedef ParserBase<PreParser> Base; | |
| 592 typedef PreParser Impl; | |
| 593 | 599 |
| 594 // PreParser doesn't need to store generator variables. | 600 // PreParser doesn't need to store generator variables. |
| 595 typedef void GeneratorVariable; | 601 typedef void GeneratorVariable; |
| 596 | 602 |
| 597 typedef int AstProperties; | 603 typedef int AstProperties; |
| 598 | 604 |
| 599 typedef v8::internal::ExpressionClassifier<ParserBaseTraits<PreParser>> | 605 typedef v8::internal::ExpressionClassifier<ParserTypes<PreParser>> |
| 600 ExpressionClassifier; | 606 ExpressionClassifier; |
| 601 | 607 |
| 602 // Return types for traversing functions. | 608 // Return types for traversing functions. |
| 603 typedef PreParserIdentifier Identifier; | 609 typedef PreParserIdentifier Identifier; |
| 604 typedef PreParserExpression Expression; | 610 typedef PreParserExpression Expression; |
| 605 typedef PreParserExpression YieldExpression; | 611 typedef PreParserExpression YieldExpression; |
| 606 typedef PreParserExpression FunctionLiteral; | 612 typedef PreParserExpression FunctionLiteral; |
| 607 typedef PreParserExpression ClassLiteral; | 613 typedef PreParserExpression ClassLiteral; |
| 608 typedef PreParserExpression Literal; | 614 typedef PreParserExpression Literal; |
| 609 typedef PreParserExpression ObjectLiteralProperty; | 615 typedef PreParserExpression ObjectLiteralProperty; |
| 610 typedef PreParserExpressionList ExpressionList; | 616 typedef PreParserExpressionList ExpressionList; |
| 611 typedef PreParserExpressionList PropertyList; | 617 typedef PreParserExpressionList PropertyList; |
| 612 typedef PreParserIdentifier FormalParameter; | 618 typedef PreParserIdentifier FormalParameter; |
| 613 typedef PreParserFormalParameters FormalParameters; | 619 typedef PreParserFormalParameters FormalParameters; |
| 614 typedef PreParserStatementList StatementList; | 620 typedef PreParserStatementList StatementList; |
| 615 | 621 |
| 616 // For constructing objects returned by the traversing functions. | 622 // For constructing objects returned by the traversing functions. |
| 617 typedef PreParserFactory Factory; | 623 typedef PreParserFactory Factory; |
| 618 }; | |
| 619 }; | 624 }; |
| 620 | 625 |
| 621 | 626 |
| 622 // Preparsing checks a JavaScript program and emits preparse-data that helps | 627 // Preparsing checks a JavaScript program and emits preparse-data that helps |
| 623 // a later parsing to be faster. | 628 // a later parsing to be faster. |
| 624 // See preparse-data-format.h for the data format. | 629 // See preparse-data-format.h for the data format. |
| 625 | 630 |
| 626 // The PreParser checks that the syntax follows the grammar for JavaScript, | 631 // The PreParser checks that the syntax follows the grammar for JavaScript, |
| 627 // and collects some information about the program along the way. | 632 // and collects some information about the program along the way. |
| 628 // The grammar check is only performed in order to understand the program | 633 // The grammar check is only performed in order to understand the program |
| 629 // sufficiently to deduce some information about it, that can be used | 634 // sufficiently to deduce some information about it, that can be used |
| 630 // to speed up later parsing. Finding errors is not the goal of pre-parsing, | 635 // to speed up later parsing. Finding errors is not the goal of pre-parsing, |
| 631 // rather it is to speed up properly written and correct programs. | 636 // rather it is to speed up properly written and correct programs. |
| 632 // That means that contextual checks (like a label being declared where | 637 // That means that contextual checks (like a label being declared where |
| 633 // it is used) are generally omitted. | 638 // it is used) are generally omitted. |
| 634 class PreParser : public ParserBase<PreParser> { | 639 class PreParser : public ParserBase<PreParser> { |
| 635 friend class ParserBase<PreParser>; | 640 friend class ParserBase<PreParser>; |
| 636 friend class v8::internal::ExpressionClassifier<ParserBaseTraits<PreParser>>; | 641 friend class v8::internal::ExpressionClassifier<ParserTypes<PreParser>>; |
| 637 | 642 |
| 638 public: | 643 public: |
| 639 typedef PreParserIdentifier Identifier; | 644 typedef PreParserIdentifier Identifier; |
| 640 typedef PreParserExpression Expression; | 645 typedef PreParserExpression Expression; |
| 641 typedef PreParserStatement Statement; | 646 typedef PreParserStatement Statement; |
| 642 | 647 |
| 643 enum PreParseResult { | 648 enum PreParseResult { |
| 644 kPreParseStackOverflow, | 649 kPreParseStackOverflow, |
| 645 kPreParseSuccess | 650 kPreParseSuccess |
| 646 }; | 651 }; |
| (...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 829 | 834 |
| 830 V8_INLINE PreParserExpression | 835 V8_INLINE PreParserExpression |
| 831 RewriteAwaitExpression(PreParserExpression value, int pos) { | 836 RewriteAwaitExpression(PreParserExpression value, int pos) { |
| 832 return value; | 837 return value; |
| 833 } | 838 } |
| 834 V8_INLINE PreParserExpression RewriteYieldStar(PreParserExpression generator, | 839 V8_INLINE PreParserExpression RewriteYieldStar(PreParserExpression generator, |
| 835 PreParserExpression expression, | 840 PreParserExpression expression, |
| 836 int pos) { | 841 int pos) { |
| 837 return PreParserExpression::Default(); | 842 return PreParserExpression::Default(); |
| 838 } | 843 } |
| 839 V8_INLINE void RewriteNonPattern(Type::ExpressionClassifier* classifier, | 844 V8_INLINE void RewriteNonPattern(ExpressionClassifier* classifier, bool* ok) { |
| 840 bool* ok) { | |
| 841 ValidateExpression(classifier, ok); | 845 ValidateExpression(classifier, ok); |
| 842 } | 846 } |
| 843 | 847 |
| 844 V8_INLINE void QueueDestructuringAssignmentForRewriting( | 848 V8_INLINE void QueueDestructuringAssignmentForRewriting( |
| 845 PreParserExpression assignment) {} | 849 PreParserExpression assignment) {} |
| 846 V8_INLINE void QueueNonPatternForRewriting(PreParserExpression expr, | 850 V8_INLINE void QueueNonPatternForRewriting(PreParserExpression expr, |
| 847 bool* ok) {} | 851 bool* ok) {} |
| 848 | 852 |
| 849 // Helper functions for recursive descent. | 853 // Helper functions for recursive descent. |
| 850 V8_INLINE bool IsEval(PreParserIdentifier identifier) const { | 854 V8_INLINE bool IsEval(PreParserIdentifier identifier) const { |
| (...skipping 234 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1085 bool is_async, bool* ok) {} | 1089 bool is_async, bool* ok) {} |
| 1086 | 1090 |
| 1087 V8_INLINE void AddFormalParameter(PreParserFormalParameters* parameters, | 1091 V8_INLINE void AddFormalParameter(PreParserFormalParameters* parameters, |
| 1088 PreParserExpression pattern, | 1092 PreParserExpression pattern, |
| 1089 PreParserExpression initializer, | 1093 PreParserExpression initializer, |
| 1090 int initializer_end_position, | 1094 int initializer_end_position, |
| 1091 bool is_rest) { | 1095 bool is_rest) { |
| 1092 ++parameters->arity; | 1096 ++parameters->arity; |
| 1093 } | 1097 } |
| 1094 | 1098 |
| 1095 V8_INLINE void DeclareFormalParameter( | 1099 V8_INLINE void DeclareFormalParameter(DeclarationScope* scope, |
| 1096 DeclarationScope* scope, PreParserIdentifier parameter, | 1100 PreParserIdentifier parameter, |
| 1097 Type::ExpressionClassifier* classifier) { | 1101 ExpressionClassifier* classifier) { |
| 1098 if (!classifier->is_simple_parameter_list()) { | 1102 if (!classifier->is_simple_parameter_list()) { |
| 1099 scope->SetHasNonSimpleParameters(); | 1103 scope->SetHasNonSimpleParameters(); |
| 1100 } | 1104 } |
| 1101 } | 1105 } |
| 1102 | 1106 |
| 1103 V8_INLINE void ParseArrowFunctionFormalParameterList( | 1107 V8_INLINE void ParseArrowFunctionFormalParameterList( |
| 1104 PreParserFormalParameters* parameters, PreParserExpression params, | 1108 PreParserFormalParameters* parameters, PreParserExpression params, |
| 1105 const Scanner::Location& params_loc, Scanner::Location* duplicate_loc, | 1109 const Scanner::Location& params_loc, Scanner::Location* duplicate_loc, |
| 1106 const Scope::Snapshot& scope_snapshot, bool* ok) { | 1110 const Scope::Snapshot& scope_snapshot, bool* ok) { |
| 1107 // TODO(wingo): Detect duplicated identifiers in paramlists. Detect | 1111 // TODO(wingo): Detect duplicated identifiers in paramlists. Detect |
| (...skipping 20 matching lines...) Expand all Loading... |
| 1128 V8_INLINE PreParserExpression | 1132 V8_INLINE PreParserExpression |
| 1129 ExpressionListToExpression(PreParserExpressionList args) { | 1133 ExpressionListToExpression(PreParserExpressionList args) { |
| 1130 return PreParserExpression::Default(); | 1134 return PreParserExpression::Default(); |
| 1131 } | 1135 } |
| 1132 | 1136 |
| 1133 V8_INLINE void SetFunctionNameFromPropertyName(PreParserExpression property, | 1137 V8_INLINE void SetFunctionNameFromPropertyName(PreParserExpression property, |
| 1134 PreParserIdentifier name) {} | 1138 PreParserIdentifier name) {} |
| 1135 V8_INLINE void SetFunctionNameFromIdentifierRef( | 1139 V8_INLINE void SetFunctionNameFromIdentifierRef( |
| 1136 PreParserExpression value, PreParserExpression identifier) {} | 1140 PreParserExpression value, PreParserExpression identifier) {} |
| 1137 | 1141 |
| 1138 V8_INLINE ZoneList<typename Type::ExpressionClassifier::Error>* | 1142 V8_INLINE ZoneList<typename ExpressionClassifier::Error>* |
| 1139 GetReportedErrorList() const { | 1143 GetReportedErrorList() const { |
| 1140 return function_state_->GetReportedErrorList(); | 1144 return function_state_->GetReportedErrorList(); |
| 1141 } | 1145 } |
| 1142 | 1146 |
| 1143 V8_INLINE ZoneList<PreParserExpression>* GetNonPatternList() const { | 1147 V8_INLINE ZoneList<PreParserExpression>* GetNonPatternList() const { |
| 1144 return function_state_->non_patterns_to_rewrite(); | 1148 return function_state_->non_patterns_to_rewrite(); |
| 1145 } | 1149 } |
| 1146 | 1150 |
| 1147 // Preparser's private field members. | 1151 // Preparser's private field members. |
| 1148 | 1152 |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1189 function_state_->NextMaterializedLiteralIndex(); | 1193 function_state_->NextMaterializedLiteralIndex(); |
| 1190 function_state_->NextMaterializedLiteralIndex(); | 1194 function_state_->NextMaterializedLiteralIndex(); |
| 1191 } | 1195 } |
| 1192 return EmptyExpression(); | 1196 return EmptyExpression(); |
| 1193 } | 1197 } |
| 1194 | 1198 |
| 1195 } // namespace internal | 1199 } // namespace internal |
| 1196 } // namespace v8 | 1200 } // namespace v8 |
| 1197 | 1201 |
| 1198 #endif // V8_PARSING_PREPARSER_H | 1202 #endif // V8_PARSING_PREPARSER_H |
| OLD | NEW |