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_PARSER_BASE_H | 5 #ifndef V8_PARSING_PARSER_BASE_H |
6 #define V8_PARSING_PARSER_BASE_H | 6 #define V8_PARSING_PARSER_BASE_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" |
(...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
192 stack_overflow_(false), | 192 stack_overflow_(false), |
193 allow_lazy_(false), | 193 allow_lazy_(false), |
194 allow_natives_(false), | 194 allow_natives_(false), |
195 allow_tailcalls_(false), | 195 allow_tailcalls_(false), |
196 allow_harmony_restrictive_declarations_(false), | 196 allow_harmony_restrictive_declarations_(false), |
197 allow_harmony_do_expressions_(false), | 197 allow_harmony_do_expressions_(false), |
198 allow_harmony_for_in_(false), | 198 allow_harmony_for_in_(false), |
199 allow_harmony_function_name_(false), | 199 allow_harmony_function_name_(false), |
200 allow_harmony_function_sent_(false), | 200 allow_harmony_function_sent_(false), |
201 allow_harmony_async_await_(false), | 201 allow_harmony_async_await_(false), |
202 allow_harmony_restrictive_generators_(false) {} | 202 allow_harmony_restrictive_generators_(false), |
203 allow_harmony_trailing_commas_in_parameters_(false) {} | |
203 | 204 |
204 #define ALLOW_ACCESSORS(name) \ | 205 #define ALLOW_ACCESSORS(name) \ |
205 bool allow_##name() const { return allow_##name##_; } \ | 206 bool allow_##name() const { return allow_##name##_; } \ |
206 void set_allow_##name(bool allow) { allow_##name##_ = allow; } | 207 void set_allow_##name(bool allow) { allow_##name##_ = allow; } |
207 | 208 |
208 #define SCANNER_ACCESSORS(name) \ | 209 #define SCANNER_ACCESSORS(name) \ |
209 bool allow_##name() const { return scanner_->allow_##name(); } \ | 210 bool allow_##name() const { return scanner_->allow_##name(); } \ |
210 void set_allow_##name(bool allow) { \ | 211 void set_allow_##name(bool allow) { \ |
211 return scanner_->set_allow_##name(allow); \ | 212 return scanner_->set_allow_##name(allow); \ |
212 } | 213 } |
213 | 214 |
214 ALLOW_ACCESSORS(lazy); | 215 ALLOW_ACCESSORS(lazy); |
215 ALLOW_ACCESSORS(natives); | 216 ALLOW_ACCESSORS(natives); |
216 ALLOW_ACCESSORS(tailcalls); | 217 ALLOW_ACCESSORS(tailcalls); |
217 ALLOW_ACCESSORS(harmony_restrictive_declarations); | 218 ALLOW_ACCESSORS(harmony_restrictive_declarations); |
218 ALLOW_ACCESSORS(harmony_do_expressions); | 219 ALLOW_ACCESSORS(harmony_do_expressions); |
219 ALLOW_ACCESSORS(harmony_for_in); | 220 ALLOW_ACCESSORS(harmony_for_in); |
220 ALLOW_ACCESSORS(harmony_function_name); | 221 ALLOW_ACCESSORS(harmony_function_name); |
221 ALLOW_ACCESSORS(harmony_function_sent); | 222 ALLOW_ACCESSORS(harmony_function_sent); |
222 ALLOW_ACCESSORS(harmony_async_await); | 223 ALLOW_ACCESSORS(harmony_async_await); |
223 ALLOW_ACCESSORS(harmony_restrictive_generators); | 224 ALLOW_ACCESSORS(harmony_restrictive_generators); |
225 ALLOW_ACCESSORS(harmony_trailing_commas_in_parameters); | |
224 SCANNER_ACCESSORS(harmony_exponentiation_operator); | 226 SCANNER_ACCESSORS(harmony_exponentiation_operator); |
225 | 227 |
226 #undef SCANNER_ACCESSORS | 228 #undef SCANNER_ACCESSORS |
227 #undef ALLOW_ACCESSORS | 229 #undef ALLOW_ACCESSORS |
228 | 230 |
229 uintptr_t stack_limit() const { return stack_limit_; } | 231 uintptr_t stack_limit() const { return stack_limit_; } |
230 | 232 |
231 protected: | 233 protected: |
232 enum AllowRestrictedIdentifiers { | 234 enum AllowRestrictedIdentifiers { |
233 kAllowRestrictedIdentifiers, | 235 kAllowRestrictedIdentifiers, |
(...skipping 951 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1185 bool allow_lazy_; | 1187 bool allow_lazy_; |
1186 bool allow_natives_; | 1188 bool allow_natives_; |
1187 bool allow_tailcalls_; | 1189 bool allow_tailcalls_; |
1188 bool allow_harmony_restrictive_declarations_; | 1190 bool allow_harmony_restrictive_declarations_; |
1189 bool allow_harmony_do_expressions_; | 1191 bool allow_harmony_do_expressions_; |
1190 bool allow_harmony_for_in_; | 1192 bool allow_harmony_for_in_; |
1191 bool allow_harmony_function_name_; | 1193 bool allow_harmony_function_name_; |
1192 bool allow_harmony_function_sent_; | 1194 bool allow_harmony_function_sent_; |
1193 bool allow_harmony_async_await_; | 1195 bool allow_harmony_async_await_; |
1194 bool allow_harmony_restrictive_generators_; | 1196 bool allow_harmony_restrictive_generators_; |
1197 bool allow_harmony_trailing_commas_in_parameters_; | |
1195 }; | 1198 }; |
1196 | 1199 |
1197 template <class Traits> | 1200 template <class Traits> |
1198 ParserBase<Traits>::FunctionState::FunctionState( | 1201 ParserBase<Traits>::FunctionState::FunctionState( |
1199 FunctionState** function_state_stack, Scope** scope_stack, Scope* scope, | 1202 FunctionState** function_state_stack, Scope** scope_stack, Scope* scope, |
1200 FunctionKind kind, typename Traits::Type::Factory* factory) | 1203 FunctionKind kind, typename Traits::Type::Factory* factory) |
1201 : next_materialized_literal_index_(0), | 1204 : next_materialized_literal_index_(0), |
1202 expected_property_count_(0), | 1205 expected_property_count_(0), |
1203 this_location_(Scanner::Location::invalid()), | 1206 this_location_(Scanner::Location::invalid()), |
1204 return_location_(Scanner::Location::invalid()), | 1207 return_location_(Scanner::Location::invalid()), |
(...skipping 488 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1693 while (peek() == Token::COMMA) { | 1696 while (peek() == Token::COMMA) { |
1694 CheckNoTailCallExpressions(classifier, CHECK_OK); | 1697 CheckNoTailCallExpressions(classifier, CHECK_OK); |
1695 if (seen_rest) { | 1698 if (seen_rest) { |
1696 // At this point the production can't possibly be valid, but we don't know | 1699 // At this point the production can't possibly be valid, but we don't know |
1697 // which error to signal. | 1700 // which error to signal. |
1698 classifier->RecordArrowFormalParametersError( | 1701 classifier->RecordArrowFormalParametersError( |
1699 scanner()->peek_location(), MessageTemplate::kParamAfterRest); | 1702 scanner()->peek_location(), MessageTemplate::kParamAfterRest); |
1700 } | 1703 } |
1701 Consume(Token::COMMA); | 1704 Consume(Token::COMMA); |
1702 bool is_rest = false; | 1705 bool is_rest = false; |
1703 if (peek() == Token::ELLIPSIS) { | 1706 if (allow_harmony_trailing_commas_in_parameters() && |
1707 peek() == Token::RPAREN && PeekAhead() == Token::ARROW) { | |
1708 // a trailing comma is allowed at the end of an arrow parameter list | |
1709 break; | |
1710 } else if (peek() == Token::ELLIPSIS) { | |
1704 // 'x, y, ...z' in CoverParenthesizedExpressionAndArrowParameterList only | 1711 // 'x, y, ...z' in CoverParenthesizedExpressionAndArrowParameterList only |
1705 // as the formal parameters of'(x, y, ...z) => foo', and is not itself a | 1712 // as the formal parameters of'(x, y, ...z) => foo', and is not itself a |
1706 // valid expression or binding pattern. | 1713 // valid expression or binding pattern. |
1707 ExpressionUnexpectedToken(classifier); | 1714 ExpressionUnexpectedToken(classifier); |
1708 BindingPatternUnexpectedToken(classifier); | 1715 BindingPatternUnexpectedToken(classifier); |
1709 Consume(Token::ELLIPSIS); | 1716 Consume(Token::ELLIPSIS); |
1710 seen_rest = is_rest = true; | 1717 seen_rest = is_rest = true; |
1711 } | 1718 } |
1712 int pos = position(), expr_pos = peek_position(); | 1719 int pos = position(), expr_pos = peek_position(); |
1713 ExpressionClassifier binding_classifier(this); | 1720 ExpressionClassifier binding_classifier(this); |
(...skipping 470 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2184 } | 2191 } |
2185 | 2192 |
2186 if (result->length() > Code::kMaxArguments) { | 2193 if (result->length() > Code::kMaxArguments) { |
2187 ReportMessage(MessageTemplate::kTooManyArguments); | 2194 ReportMessage(MessageTemplate::kTooManyArguments); |
2188 *ok = false; | 2195 *ok = false; |
2189 return this->NullExpressionList(); | 2196 return this->NullExpressionList(); |
2190 } | 2197 } |
2191 done = (peek() != Token::COMMA); | 2198 done = (peek() != Token::COMMA); |
2192 if (!done) { | 2199 if (!done) { |
2193 Next(); | 2200 Next(); |
2201 if (allow_harmony_trailing_commas_in_parameters() && | |
2202 peek() == Token::RPAREN) { | |
2203 // allow trailing comma | |
2204 done = true; | |
2205 } | |
2194 } | 2206 } |
2195 } | 2207 } |
2196 Scanner::Location location = scanner_->location(); | 2208 Scanner::Location location = scanner_->location(); |
2197 if (Token::RPAREN != Next()) { | 2209 if (Token::RPAREN != Next()) { |
2198 ReportMessageAt(location, MessageTemplate::kUnterminatedArgList); | 2210 ReportMessageAt(location, MessageTemplate::kUnterminatedArgList); |
2199 *ok = false; | 2211 *ok = false; |
2200 return this->NullExpressionList(); | 2212 return this->NullExpressionList(); |
2201 } | 2213 } |
2202 *first_spread_arg_loc = spread_arg; | 2214 *first_spread_arg_loc = spread_arg; |
2203 | 2215 |
(...skipping 984 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
3188 } | 3200 } |
3189 | 3201 |
3190 Traits::AddFormalParameter(parameters, pattern, initializer, | 3202 Traits::AddFormalParameter(parameters, pattern, initializer, |
3191 scanner()->location().end_pos, is_rest); | 3203 scanner()->location().end_pos, is_rest); |
3192 } | 3204 } |
3193 | 3205 |
3194 | 3206 |
3195 template <class Traits> | 3207 template <class Traits> |
3196 void ParserBase<Traits>::ParseFormalParameterList( | 3208 void ParserBase<Traits>::ParseFormalParameterList( |
3197 FormalParametersT* parameters, ExpressionClassifier* classifier, bool* ok) { | 3209 FormalParametersT* parameters, ExpressionClassifier* classifier, bool* ok) { |
3198 // FormalParameters[Yield,GeneratorParameter] : | 3210 // FormalParameters[Yield] : |
3199 // [empty] | 3211 // [empty] |
3200 // FormalParameterList[?Yield, ?GeneratorParameter] | 3212 // FunctionRestParameter[?Yield] |
3213 // FormalParameterList[?Yield] | |
3214 // FormalParameterList[?Yield] , | |
3215 // FormalParameterList[?Yield] , FunctionRestParameter[?Yield] | |
3201 // | 3216 // |
3202 // FormalParameterList[Yield,GeneratorParameter] : | 3217 // FormalParameterList[Yield] : |
3203 // FunctionRestParameter[?Yield] | 3218 // FormalParameter[?Yield] |
3204 // FormalsList[?Yield, ?GeneratorParameter] | 3219 // FormalParameterList[?Yield] , FormalParameter[?Yield] |
3205 // FormalsList[?Yield, ?GeneratorParameter] , FunctionRestParameter[?Yield] | |
3206 // | |
3207 // FormalsList[Yield,GeneratorParameter] : | |
3208 // FormalParameter[?Yield, ?GeneratorParameter] | |
3209 // FormalsList[?Yield, ?GeneratorParameter] , | |
3210 // FormalParameter[?Yield,?GeneratorParameter] | |
3211 | 3220 |
3212 DCHECK_EQ(0, parameters->Arity()); | 3221 DCHECK_EQ(0, parameters->Arity()); |
3213 | 3222 |
3214 if (peek() != Token::RPAREN) { | 3223 if (peek() != Token::RPAREN) { |
3215 do { | 3224 while (true) { |
3216 if (parameters->Arity() > Code::kMaxArguments) { | 3225 if (parameters->Arity() > Code::kMaxArguments) { |
3217 ReportMessage(MessageTemplate::kTooManyParameters); | 3226 ReportMessage(MessageTemplate::kTooManyParameters); |
3218 *ok = false; | 3227 *ok = false; |
3219 return; | 3228 return; |
3220 } | 3229 } |
3221 parameters->has_rest = Check(Token::ELLIPSIS); | 3230 parameters->has_rest = Check(Token::ELLIPSIS); |
3222 ParseFormalParameter(parameters, classifier, ok); | 3231 ParseFormalParameter(parameters, classifier, ok); |
3223 if (!*ok) return; | 3232 if (!*ok) return; |
3224 } while (!parameters->has_rest && Check(Token::COMMA)); | |
adamk
2016/06/28 00:39:47
Can't you keep this same do/while form and just ad
jwolfe
2016/06/28 21:04:16
I rewrote it for clarity. I find multiple breaks m
adamk
2016/06/28 21:10:51
I'm OK with the multiple breaks too, mostly wanted
| |
3225 | 3233 |
3226 if (parameters->has_rest) { | 3234 if (parameters->has_rest) { |
3227 parameters->is_simple = false; | 3235 parameters->is_simple = false; |
3228 classifier->RecordNonSimpleParameter(); | 3236 classifier->RecordNonSimpleParameter(); |
3229 if (peek() == Token::COMMA) { | 3237 if (peek() == Token::COMMA) { |
3230 ReportMessageAt(scanner()->peek_location(), | 3238 ReportMessageAt(scanner()->peek_location(), |
3231 MessageTemplate::kParamAfterRest); | 3239 MessageTemplate::kParamAfterRest); |
3232 *ok = false; | 3240 *ok = false; |
3233 return; | 3241 return; |
3242 } | |
3243 break; | |
3244 } | |
3245 if (!Check(Token::COMMA)) break; | |
3246 if (allow_harmony_trailing_commas_in_parameters() && | |
3247 peek() == Token::RPAREN) { | |
3248 // allow the trailing comma | |
3249 break; | |
3234 } | 3250 } |
3235 } | 3251 } |
3236 } | 3252 } |
3237 | 3253 |
3238 for (int i = 0; i < parameters->Arity(); ++i) { | 3254 for (int i = 0; i < parameters->Arity(); ++i) { |
3239 auto parameter = parameters->at(i); | 3255 auto parameter = parameters->at(i); |
3240 Traits::DeclareFormalParameter(parameters->scope, parameter, classifier); | 3256 Traits::DeclareFormalParameter(parameters->scope, parameter, classifier); |
3241 } | 3257 } |
3242 } | 3258 } |
3243 | 3259 |
(...skipping 383 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
3627 has_seen_constructor_ = true; | 3643 has_seen_constructor_ = true; |
3628 return; | 3644 return; |
3629 } | 3645 } |
3630 } | 3646 } |
3631 | 3647 |
3632 | 3648 |
3633 } // namespace internal | 3649 } // namespace internal |
3634 } // namespace v8 | 3650 } // namespace v8 |
3635 | 3651 |
3636 #endif // V8_PARSING_PARSER_BASE_H | 3652 #endif // V8_PARSING_PARSER_BASE_H |
OLD | NEW |