Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(61)

Side by Side Diff: src/preparser.h

Issue 437393004: Rewind additional parser state when reinterpreting arrow arguments (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: ParserCheckpoint constructor takes argument Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « src/parser.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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
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 782 matching lines...) Expand 10 before | Expand all | Expand 10 after
1014 // dummy method right in this class. 1050 // dummy method right in this class.
1015 PreParserFactory* visitor() { return this; } 1051 PreParserFactory* visitor() { return this; }
1016 BailoutReason dont_optimize_reason() { return kNoReason; } 1052 BailoutReason dont_optimize_reason() { return kNoReason; }
1017 int* ast_properties() { 1053 int* ast_properties() {
1018 static int dummy = 42; 1054 static int dummy = 42;
1019 return &dummy; 1055 return &dummy;
1020 } 1056 }
1021 }; 1057 };
1022 1058
1023 1059
1060 class PreParserCheckpoint BASE_EMBEDDED {
1061 public:
1062 template <typename Parser>
1063 explicit PreParserCheckpoint(Parser* parser) {}
1064 void Restore() {}
1065 };
1066
1067
1024 class PreParser; 1068 class PreParser;
1025 1069
1026 class PreParserTraits { 1070 class PreParserTraits {
1027 public: 1071 public:
1028 struct Type { 1072 struct Type {
1029 // TODO(marja): To be removed. The Traits object should contain all the data 1073 // TODO(marja): To be removed. The Traits object should contain all the data
1030 // it needs. 1074 // it needs.
1031 typedef PreParser* Parser; 1075 typedef PreParser* Parser;
1032 1076
1033 // Used by FunctionState and BlockState. 1077 // Used by FunctionState and BlockState.
1034 typedef PreParserScope Scope; 1078 typedef PreParserScope Scope;
1035 typedef PreParserScope ScopePtr; 1079 typedef PreParserScope ScopePtr;
1080 typedef PreParserCheckpoint Checkpoint;
1036 1081
1037 // PreParser doesn't need to store generator variables. 1082 // PreParser doesn't need to store generator variables.
1038 typedef void GeneratorVariable; 1083 typedef void GeneratorVariable;
1039 // No interaction with Zones. 1084 // No interaction with Zones.
1040 typedef void Zone; 1085 typedef void Zone;
1041 1086
1042 typedef int AstProperties; 1087 typedef int AstProperties;
1043 typedef Vector<PreParserIdentifier> ParameterIdentifierVector; 1088 typedef Vector<PreParserIdentifier> ParameterIdentifierVector;
1044 1089
1045 // Return types for traversing functions. 1090 // Return types for traversing functions.
(...skipping 953 matching lines...) Expand 10 before | Expand all | Expand 10 after
1999 // YieldExpression 2044 // YieldExpression
2000 // LeftHandSideExpression AssignmentOperator AssignmentExpression 2045 // LeftHandSideExpression AssignmentOperator AssignmentExpression
2001 2046
2002 Scanner::Location lhs_location = scanner()->peek_location(); 2047 Scanner::Location lhs_location = scanner()->peek_location();
2003 2048
2004 if (peek() == Token::YIELD && is_generator()) { 2049 if (peek() == Token::YIELD && is_generator()) {
2005 return this->ParseYieldExpression(ok); 2050 return this->ParseYieldExpression(ok);
2006 } 2051 }
2007 2052
2008 if (fni_ != NULL) fni_->Enter(); 2053 if (fni_ != NULL) fni_->Enter();
2054 ParserCheckpoint checkpoint(this);
2009 ExpressionT expression = 2055 ExpressionT expression =
2010 this->ParseConditionalExpression(accept_IN, CHECK_OK); 2056 this->ParseConditionalExpression(accept_IN, CHECK_OK);
2011 2057
2012 if (allow_arrow_functions() && peek() == Token::ARROW) { 2058 if (allow_arrow_functions() && peek() == Token::ARROW) {
2059 checkpoint.Restore();
2013 expression = this->ParseArrowFunctionLiteral(lhs_location.beg_pos, 2060 expression = this->ParseArrowFunctionLiteral(lhs_location.beg_pos,
2014 expression, CHECK_OK); 2061 expression, CHECK_OK);
2015 return expression; 2062 return expression;
2016 } 2063 }
2017 2064
2018 if (!Token::IsAssignmentOp(peek())) { 2065 if (!Token::IsAssignmentOp(peek())) {
2019 if (fni_ != NULL) fni_->Leave(); 2066 if (fni_ != NULL) fni_->Leave();
2020 // Parsed conditional expression only (no assignment). 2067 // Parsed conditional expression only (no assignment).
2021 return expression; 2068 return expression;
2022 } 2069 }
(...skipping 590 matching lines...) Expand 10 before | Expand all | Expand 10 after
2613 parser()->ReportMessage("accessor_get_set"); 2660 parser()->ReportMessage("accessor_get_set");
2614 } 2661 }
2615 *ok = false; 2662 *ok = false;
2616 } 2663 }
2617 } 2664 }
2618 2665
2619 2666
2620 } } // v8::internal 2667 } } // v8::internal
2621 2668
2622 #endif // V8_PREPARSER_H 2669 #endif // V8_PREPARSER_H
OLDNEW
« no previous file with comments | « src/parser.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698