| 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_PREPARSER_H | 5 #ifndef V8_PREPARSER_H |
| 6 #define V8_PREPARSER_H | 6 #define V8_PREPARSER_H |
| 7 | 7 |
| 8 #include "src/v8.h" | 8 #include "src/v8.h" |
| 9 | 9 |
| 10 #include "src/func-name-inferrer.h" | 10 #include "src/func-name-inferrer.h" |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 107 void set_allow_arrow_functions(bool allow) { allow_arrow_functions_ = allow; } | 107 void set_allow_arrow_functions(bool allow) { allow_arrow_functions_ = allow; } |
| 108 void set_allow_modules(bool allow) { scanner()->SetHarmonyModules(allow); } | 108 void set_allow_modules(bool allow) { scanner()->SetHarmonyModules(allow); } |
| 109 void set_allow_harmony_scoping(bool allow) { | 109 void set_allow_harmony_scoping(bool allow) { |
| 110 scanner()->SetHarmonyScoping(allow); | 110 scanner()->SetHarmonyScoping(allow); |
| 111 } | 111 } |
| 112 void set_allow_harmony_numeric_literals(bool allow) { | 112 void set_allow_harmony_numeric_literals(bool allow) { |
| 113 scanner()->SetHarmonyNumericLiterals(allow); | 113 scanner()->SetHarmonyNumericLiterals(allow); |
| 114 } | 114 } |
| 115 | 115 |
| 116 protected: | 116 protected: |
| 117 friend class Traits::Type::Checkpoint; |
| 118 |
| 117 enum AllowEvalOrArgumentsAsIdentifier { | 119 enum AllowEvalOrArgumentsAsIdentifier { |
| 118 kAllowEvalOrArguments, | 120 kAllowEvalOrArguments, |
| 119 kDontAllowEvalOrArguments | 121 kDontAllowEvalOrArguments |
| 120 }; | 122 }; |
| 121 | 123 |
| 122 enum Mode { | 124 enum Mode { |
| 123 PARSE_LAZILY, | 125 PARSE_LAZILY, |
| 124 PARSE_EAGERLY | 126 PARSE_EAGERLY |
| 125 }; | 127 }; |
| 126 | 128 |
| 129 class ParserCheckpoint; |
| 130 |
| 127 // --------------------------------------------------------------------------- | 131 // --------------------------------------------------------------------------- |
| 128 // FunctionState and BlockState together implement the parser's scope stack. | 132 // FunctionState and BlockState together implement the parser's scope stack. |
| 129 // The parser's current scope is in scope_. BlockState and FunctionState | 133 // The parser's current scope is in scope_. BlockState and FunctionState |
| 130 // constructors push on the scope stack and the destructors pop. They are also | 134 // constructors push on the scope stack and the destructors pop. They are also |
| 131 // used to hold the parser's per-function and per-block state. | 135 // used to hold the parser's per-function and per-block state. |
| 132 class BlockState BASE_EMBEDDED { | 136 class BlockState BASE_EMBEDDED { |
| 133 public: | 137 public: |
| 134 BlockState(typename Traits::Type::Scope** scope_stack, | 138 BlockState(typename Traits::Type::Scope** scope_stack, |
| 135 typename Traits::Type::Scope* scope) | 139 typename Traits::Type::Scope* scope) |
| 136 : scope_stack_(scope_stack), | 140 : scope_stack_(scope_stack), |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 212 | 216 |
| 213 FunctionState** function_state_stack_; | 217 FunctionState** function_state_stack_; |
| 214 FunctionState* outer_function_state_; | 218 FunctionState* outer_function_state_; |
| 215 typename Traits::Type::Scope** scope_stack_; | 219 typename Traits::Type::Scope** scope_stack_; |
| 216 typename Traits::Type::Scope* outer_scope_; | 220 typename Traits::Type::Scope* outer_scope_; |
| 217 int saved_ast_node_id_; // Only used by ParserTraits. | 221 int saved_ast_node_id_; // Only used by ParserTraits. |
| 218 typename Traits::Type::Zone* extra_param_; | 222 typename Traits::Type::Zone* extra_param_; |
| 219 typename Traits::Type::Factory factory_; | 223 typename Traits::Type::Factory factory_; |
| 220 | 224 |
| 221 friend class ParserTraits; | 225 friend class ParserTraits; |
| 226 friend class ParserCheckpoint; |
| 227 }; |
| 228 |
| 229 // Annoyingly, arrow functions first parse as comma expressions, then when we |
| 230 // see the => we have to go back and reinterpret the arguments as being formal |
| 231 // parameters. To do so we need to reset some of the parser state back to |
| 232 // what it was before the arguments were first seen. |
| 233 class ParserCheckpoint : public Traits::Type::Checkpoint { |
| 234 public: |
| 235 template <typename Parser> |
| 236 explicit ParserCheckpoint(Parser* parser) |
| 237 : Traits::Type::Checkpoint(parser) { |
| 238 function_state_ = parser->function_state_; |
| 239 next_materialized_literal_index_ = |
| 240 function_state_->next_materialized_literal_index_; |
| 241 next_handler_index_ = function_state_->next_handler_index_; |
| 242 expected_property_count_ = function_state_->expected_property_count_; |
| 243 } |
| 244 |
| 245 void Restore() { |
| 246 Traits::Type::Checkpoint::Restore(); |
| 247 function_state_->next_materialized_literal_index_ = |
| 248 next_materialized_literal_index_; |
| 249 function_state_->next_handler_index_ = next_handler_index_; |
| 250 function_state_->expected_property_count_ = expected_property_count_; |
| 251 } |
| 252 |
| 253 private: |
| 254 FunctionState* function_state_; |
| 255 int next_materialized_literal_index_; |
| 256 int next_handler_index_; |
| 257 int expected_property_count_; |
| 222 }; | 258 }; |
| 223 | 259 |
| 224 class ParsingModeScope BASE_EMBEDDED { | 260 class ParsingModeScope BASE_EMBEDDED { |
| 225 public: | 261 public: |
| 226 ParsingModeScope(ParserBase* parser, Mode mode) | 262 ParsingModeScope(ParserBase* parser, Mode mode) |
| 227 : parser_(parser), | 263 : parser_(parser), |
| 228 old_mode_(parser->mode()) { | 264 old_mode_(parser->mode()) { |
| 229 parser_->mode_ = mode; | 265 parser_->mode_ = mode; |
| 230 } | 266 } |
| 231 ~ParsingModeScope() { | 267 ~ParsingModeScope() { |
| (...skipping 795 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1027 public: | 1063 public: |
| 1028 struct Type { | 1064 struct Type { |
| 1029 // TODO(marja): To be removed. The Traits object should contain all the data | 1065 // TODO(marja): To be removed. The Traits object should contain all the data |
| 1030 // it needs. | 1066 // it needs. |
| 1031 typedef PreParser* Parser; | 1067 typedef PreParser* Parser; |
| 1032 | 1068 |
| 1033 // Used by FunctionState and BlockState. | 1069 // Used by FunctionState and BlockState. |
| 1034 typedef PreParserScope Scope; | 1070 typedef PreParserScope Scope; |
| 1035 typedef PreParserScope ScopePtr; | 1071 typedef PreParserScope ScopePtr; |
| 1036 | 1072 |
| 1073 class Checkpoint BASE_EMBEDDED { |
| 1074 public: |
| 1075 template <typename Parser> |
| 1076 explicit Checkpoint(Parser* parser) {} |
| 1077 void Restore() {} |
| 1078 }; |
| 1079 |
| 1037 // PreParser doesn't need to store generator variables. | 1080 // PreParser doesn't need to store generator variables. |
| 1038 typedef void GeneratorVariable; | 1081 typedef void GeneratorVariable; |
| 1039 // No interaction with Zones. | 1082 // No interaction with Zones. |
| 1040 typedef void Zone; | 1083 typedef void Zone; |
| 1041 | 1084 |
| 1042 typedef int AstProperties; | 1085 typedef int AstProperties; |
| 1043 typedef Vector<PreParserIdentifier> ParameterIdentifierVector; | 1086 typedef Vector<PreParserIdentifier> ParameterIdentifierVector; |
| 1044 | 1087 |
| 1045 // Return types for traversing functions. | 1088 // Return types for traversing functions. |
| 1046 typedef PreParserIdentifier Identifier; | 1089 typedef PreParserIdentifier Identifier; |
| (...skipping 952 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1999 // YieldExpression | 2042 // YieldExpression |
| 2000 // LeftHandSideExpression AssignmentOperator AssignmentExpression | 2043 // LeftHandSideExpression AssignmentOperator AssignmentExpression |
| 2001 | 2044 |
| 2002 Scanner::Location lhs_location = scanner()->peek_location(); | 2045 Scanner::Location lhs_location = scanner()->peek_location(); |
| 2003 | 2046 |
| 2004 if (peek() == Token::YIELD && is_generator()) { | 2047 if (peek() == Token::YIELD && is_generator()) { |
| 2005 return this->ParseYieldExpression(ok); | 2048 return this->ParseYieldExpression(ok); |
| 2006 } | 2049 } |
| 2007 | 2050 |
| 2008 if (fni_ != NULL) fni_->Enter(); | 2051 if (fni_ != NULL) fni_->Enter(); |
| 2052 ParserCheckpoint checkpoint(this); |
| 2009 ExpressionT expression = | 2053 ExpressionT expression = |
| 2010 this->ParseConditionalExpression(accept_IN, CHECK_OK); | 2054 this->ParseConditionalExpression(accept_IN, CHECK_OK); |
| 2011 | 2055 |
| 2012 if (allow_arrow_functions() && peek() == Token::ARROW) { | 2056 if (allow_arrow_functions() && peek() == Token::ARROW) { |
| 2057 checkpoint.Restore(); |
| 2013 expression = this->ParseArrowFunctionLiteral(lhs_location.beg_pos, | 2058 expression = this->ParseArrowFunctionLiteral(lhs_location.beg_pos, |
| 2014 expression, CHECK_OK); | 2059 expression, CHECK_OK); |
| 2015 return expression; | 2060 return expression; |
| 2016 } | 2061 } |
| 2017 | 2062 |
| 2018 if (!Token::IsAssignmentOp(peek())) { | 2063 if (!Token::IsAssignmentOp(peek())) { |
| 2019 if (fni_ != NULL) fni_->Leave(); | 2064 if (fni_ != NULL) fni_->Leave(); |
| 2020 // Parsed conditional expression only (no assignment). | 2065 // Parsed conditional expression only (no assignment). |
| 2021 return expression; | 2066 return expression; |
| 2022 } | 2067 } |
| (...skipping 590 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2613 parser()->ReportMessage("accessor_get_set"); | 2658 parser()->ReportMessage("accessor_get_set"); |
| 2614 } | 2659 } |
| 2615 *ok = false; | 2660 *ok = false; |
| 2616 } | 2661 } |
| 2617 } | 2662 } |
| 2618 | 2663 |
| 2619 | 2664 |
| 2620 } } // v8::internal | 2665 } } // v8::internal |
| 2621 | 2666 |
| 2622 #endif // V8_PREPARSER_H | 2667 #endif // V8_PREPARSER_H |
| OLD | NEW |