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/bailout-reason.h" | 10 #include "src/bailout-reason.h" |
(...skipping 316 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
327 } | 327 } |
328 ~ParsingModeScope() { | 328 ~ParsingModeScope() { |
329 parser_->mode_ = old_mode_; | 329 parser_->mode_ = old_mode_; |
330 } | 330 } |
331 | 331 |
332 private: | 332 private: |
333 ParserBase* parser_; | 333 ParserBase* parser_; |
334 Mode old_mode_; | 334 Mode old_mode_; |
335 }; | 335 }; |
336 | 336 |
337 Scope* NewScope(Scope* parent, ScopeType scope_type, | 337 Scope* NewScope(Scope* parent, ScopeType scope_type) { |
338 FunctionKind kind = kNormalFunction) { | 338 // Must always pass the function kind for FUNCTION_SCOPE and ARROW_SCOPE. |
| 339 DCHECK(scope_type != FUNCTION_SCOPE && scope_type != ARROW_SCOPE); |
| 340 return NewScope(parent, scope_type, kNormalFunction); |
| 341 } |
| 342 |
| 343 Scope* NewScope(Scope* parent, ScopeType scope_type, FunctionKind kind) { |
339 DCHECK(ast_value_factory()); | 344 DCHECK(ast_value_factory()); |
340 DCHECK(scope_type != MODULE_SCOPE || allow_harmony_modules()); | 345 DCHECK(scope_type != MODULE_SCOPE || allow_harmony_modules()); |
341 DCHECK((scope_type == FUNCTION_SCOPE && IsValidFunctionKind(kind)) || | 346 DCHECK(scope_type != ARROW_SCOPE || IsArrowFunction(kind)); |
342 kind == kNormalFunction); | |
343 Scope* result = new (zone()) | 347 Scope* result = new (zone()) |
344 Scope(zone(), parent, scope_type, ast_value_factory(), kind); | 348 Scope(zone(), parent, scope_type, ast_value_factory(), kind); |
345 result->Initialize(); | 349 result->Initialize(); |
346 return result; | 350 return result; |
347 } | 351 } |
348 | 352 |
349 Scanner* scanner() const { return scanner_; } | 353 Scanner* scanner() const { return scanner_; } |
350 AstValueFactory* ast_value_factory() const { return ast_value_factory_; } | 354 AstValueFactory* ast_value_factory() const { return ast_value_factory_; } |
351 int position() { return scanner_->location().beg_pos; } | 355 int position() { return scanner_->location().beg_pos; } |
352 int peek_position() { return scanner_->peek_location().beg_pos; } | 356 int peek_position() { return scanner_->peek_location().beg_pos; } |
(...skipping 1345 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1698 return PreParserIdentifier::Default(); | 1702 return PreParserIdentifier::Default(); |
1699 } | 1703 } |
1700 | 1704 |
1701 static PreParserExpression ThisExpression(Scope* scope, | 1705 static PreParserExpression ThisExpression(Scope* scope, |
1702 PreParserFactory* factory, | 1706 PreParserFactory* factory, |
1703 int pos) { | 1707 int pos) { |
1704 return PreParserExpression::This(); | 1708 return PreParserExpression::This(); |
1705 } | 1709 } |
1706 | 1710 |
1707 static PreParserExpression SuperReference(Scope* scope, | 1711 static PreParserExpression SuperReference(Scope* scope, |
1708 PreParserFactory* factory) { | 1712 PreParserFactory* factory, |
| 1713 int pos) { |
1709 return PreParserExpression::Default(); | 1714 return PreParserExpression::Default(); |
1710 } | 1715 } |
1711 | 1716 |
1712 static PreParserExpression DefaultConstructor(bool call_super, Scope* scope, | 1717 static PreParserExpression DefaultConstructor(bool call_super, Scope* scope, |
1713 int pos, int end_pos) { | 1718 int pos, int end_pos) { |
1714 return PreParserExpression::Default(); | 1719 return PreParserExpression::Default(); |
1715 } | 1720 } |
1716 | 1721 |
1717 static PreParserExpression ExpressionFromLiteral( | 1722 static PreParserExpression ExpressionFromLiteral( |
1718 Token::Value token, int pos, Scanner* scanner, | 1723 Token::Value token, int pos, Scanner* scanner, |
(...skipping 661 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2380 if (!classifier->is_valid_binding_pattern()) { | 2385 if (!classifier->is_valid_binding_pattern()) { |
2381 ArrowFormalParametersUnexpectedToken(classifier); | 2386 ArrowFormalParametersUnexpectedToken(classifier); |
2382 } | 2387 } |
2383 BindingPatternUnexpectedToken(classifier); | 2388 BindingPatternUnexpectedToken(classifier); |
2384 Consume(Token::LPAREN); | 2389 Consume(Token::LPAREN); |
2385 if (allow_harmony_arrow_functions() && Check(Token::RPAREN)) { | 2390 if (allow_harmony_arrow_functions() && Check(Token::RPAREN)) { |
2386 // As a primary expression, the only thing that can follow "()" is "=>". | 2391 // As a primary expression, the only thing that can follow "()" is "=>". |
2387 classifier->RecordBindingPatternError(scanner()->location(), | 2392 classifier->RecordBindingPatternError(scanner()->location(), |
2388 MessageTemplate::kUnexpectedToken, | 2393 MessageTemplate::kUnexpectedToken, |
2389 Token::String(Token::RPAREN)); | 2394 Token::String(Token::RPAREN)); |
2390 Scope* scope = this->NewScope(scope_, ARROW_SCOPE); | 2395 Scope* scope = |
| 2396 this->NewScope(scope_, ARROW_SCOPE, FunctionKind::kArrowFunction); |
2391 scope->set_start_position(beg_pos); | 2397 scope->set_start_position(beg_pos); |
2392 ExpressionClassifier args_classifier; | 2398 ExpressionClassifier args_classifier; |
2393 bool has_rest = false; | 2399 bool has_rest = false; |
2394 result = this->ParseArrowFunctionLiteral(scope, has_rest, | 2400 result = this->ParseArrowFunctionLiteral(scope, has_rest, |
2395 args_classifier, CHECK_OK); | 2401 args_classifier, CHECK_OK); |
2396 } else { | 2402 } else { |
2397 // Heuristically try to detect immediately called functions before | 2403 // Heuristically try to detect immediately called functions before |
2398 // seeing the call parentheses. | 2404 // seeing the call parentheses. |
2399 parenthesized_function_ = (peek() == Token::FUNCTION); | 2405 parenthesized_function_ = (peek() == Token::FUNCTION); |
2400 result = this->ParseExpression(true, classifier, CHECK_OK); | 2406 result = this->ParseExpression(true, classifier, CHECK_OK); |
(...skipping 505 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2906 accept_IN, &arrow_formals_classifier, CHECK_OK); | 2912 accept_IN, &arrow_formals_classifier, CHECK_OK); |
2907 classifier->Accumulate(arrow_formals_classifier); | 2913 classifier->Accumulate(arrow_formals_classifier); |
2908 | 2914 |
2909 if (allow_harmony_arrow_functions() && peek() == Token::ARROW) { | 2915 if (allow_harmony_arrow_functions() && peek() == Token::ARROW) { |
2910 checkpoint.Restore(); | 2916 checkpoint.Restore(); |
2911 BindingPatternUnexpectedToken(classifier); | 2917 BindingPatternUnexpectedToken(classifier); |
2912 ValidateArrowFormalParameters(&arrow_formals_classifier, expression, | 2918 ValidateArrowFormalParameters(&arrow_formals_classifier, expression, |
2913 CHECK_OK); | 2919 CHECK_OK); |
2914 Scanner::Location loc(lhs_location.beg_pos, scanner()->location().end_pos); | 2920 Scanner::Location loc(lhs_location.beg_pos, scanner()->location().end_pos); |
2915 bool has_rest = false; | 2921 bool has_rest = false; |
2916 Scope* scope = this->NewScope(scope_, ARROW_SCOPE); | 2922 Scope* scope = |
| 2923 this->NewScope(scope_, ARROW_SCOPE, FunctionKind::kArrowFunction); |
2917 scope->set_start_position(lhs_location.beg_pos); | 2924 scope->set_start_position(lhs_location.beg_pos); |
2918 Scanner::Location duplicate_loc = Scanner::Location::invalid(); | 2925 Scanner::Location duplicate_loc = Scanner::Location::invalid(); |
2919 this->ParseArrowFunctionFormalParameters(scope, expression, loc, &has_rest, | 2926 this->ParseArrowFunctionFormalParameters(scope, expression, loc, &has_rest, |
2920 &duplicate_loc, CHECK_OK); | 2927 &duplicate_loc, CHECK_OK); |
2921 if (duplicate_loc.IsValid()) { | 2928 if (duplicate_loc.IsValid()) { |
2922 arrow_formals_classifier.RecordDuplicateFormalParameterError( | 2929 arrow_formals_classifier.RecordDuplicateFormalParameterError( |
2923 duplicate_loc); | 2930 duplicate_loc); |
2924 } | 2931 } |
2925 expression = this->ParseArrowFunctionLiteral( | 2932 expression = this->ParseArrowFunctionLiteral( |
2926 scope, has_rest, arrow_formals_classifier, CHECK_OK); | 2933 scope, has_rest, arrow_formals_classifier, CHECK_OK); |
(...skipping 566 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3493 typename ParserBase<Traits>::ExpressionT | 3500 typename ParserBase<Traits>::ExpressionT |
3494 ParserBase<Traits>::ParseStrongSuperCallExpression( | 3501 ParserBase<Traits>::ParseStrongSuperCallExpression( |
3495 ExpressionClassifier* classifier, bool* ok) { | 3502 ExpressionClassifier* classifier, bool* ok) { |
3496 // SuperCallExpression :: (strong mode) | 3503 // SuperCallExpression :: (strong mode) |
3497 // 'super' '(' ExpressionList ')' | 3504 // 'super' '(' ExpressionList ')' |
3498 BindingPatternUnexpectedToken(classifier); | 3505 BindingPatternUnexpectedToken(classifier); |
3499 | 3506 |
3500 Consume(Token::SUPER); | 3507 Consume(Token::SUPER); |
3501 int pos = position(); | 3508 int pos = position(); |
3502 Scanner::Location super_loc = scanner()->location(); | 3509 Scanner::Location super_loc = scanner()->location(); |
3503 ExpressionT expr = this->SuperReference(scope_, factory()); | 3510 ExpressionT expr = this->SuperReference(scope_, factory(), pos); |
3504 | 3511 |
3505 if (peek() != Token::LPAREN) { | 3512 if (peek() != Token::LPAREN) { |
3506 ReportMessage(MessageTemplate::kStrongConstructorSuper); | 3513 ReportMessage(MessageTemplate::kStrongConstructorSuper); |
3507 *ok = false; | 3514 *ok = false; |
3508 return this->EmptyExpression(); | 3515 return this->EmptyExpression(); |
3509 } | 3516 } |
3510 | 3517 |
3511 Scanner::Location spread_pos; | 3518 Scanner::Location spread_pos; |
3512 typename Traits::Type::ExpressionList args = | 3519 typename Traits::Type::ExpressionList args = |
3513 ParseArguments(&spread_pos, classifier, CHECK_OK); | 3520 ParseArguments(&spread_pos, classifier, CHECK_OK); |
(...skipping 28 matching lines...) Expand all Loading... |
3542 return factory()->NewCall(expr, args, pos); | 3549 return factory()->NewCall(expr, args, pos); |
3543 } | 3550 } |
3544 } | 3551 } |
3545 | 3552 |
3546 | 3553 |
3547 template <class Traits> | 3554 template <class Traits> |
3548 typename ParserBase<Traits>::ExpressionT | 3555 typename ParserBase<Traits>::ExpressionT |
3549 ParserBase<Traits>::ParseSuperExpression(bool is_new, | 3556 ParserBase<Traits>::ParseSuperExpression(bool is_new, |
3550 ExpressionClassifier* classifier, | 3557 ExpressionClassifier* classifier, |
3551 bool* ok) { | 3558 bool* ok) { |
| 3559 int pos = position(); |
3552 Expect(Token::SUPER, CHECK_OK); | 3560 Expect(Token::SUPER, CHECK_OK); |
3553 | 3561 |
3554 // TODO(wingo): Does this actually work with lazily compiled arrows? | 3562 Scope* scope = scope_->DeclarationScope(); |
3555 FunctionState* function_state = function_state_; | 3563 |
3556 while (IsArrowFunction(function_state->kind())) { | 3564 while (scope->is_eval_scope() || scope->is_arrow_scope()) { |
3557 function_state = function_state->outer(); | 3565 scope = scope->outer_scope(); |
| 3566 DCHECK_NOT_NULL(scope); |
| 3567 scope = scope->DeclarationScope(); |
3558 } | 3568 } |
3559 // TODO(arv): Handle eval scopes similarly. | |
3560 | 3569 |
3561 FunctionKind kind = function_state->kind(); | 3570 FunctionKind kind = scope->function_kind(); |
3562 if (IsConciseMethod(kind) || IsAccessorFunction(kind) || | 3571 if (IsConciseMethod(kind) || IsAccessorFunction(kind) || |
3563 i::IsConstructor(kind)) { | 3572 i::IsConstructor(kind)) { |
3564 if (peek() == Token::PERIOD || peek() == Token::LBRACK) { | 3573 if (peek() == Token::PERIOD || peek() == Token::LBRACK) { |
3565 scope_->RecordSuperPropertyUsage(); | 3574 scope->RecordSuperPropertyUsage(); |
3566 return this->SuperReference(scope_, factory()); | 3575 return this->SuperReference(scope_, factory(), pos); |
3567 } | 3576 } |
3568 // new super() is never allowed. | 3577 // new super() is never allowed. |
3569 // super() is only allowed in derived constructor | 3578 // super() is only allowed in derived constructor |
3570 if (!is_new && peek() == Token::LPAREN && IsSubclassConstructor(kind)) { | 3579 if (!is_new && peek() == Token::LPAREN && IsSubclassConstructor(kind)) { |
3571 if (is_strong(language_mode())) { | 3580 if (is_strong(language_mode())) { |
3572 // Super calls in strong mode are parsed separately. | 3581 // Super calls in strong mode are parsed separately. |
3573 ReportMessageAt(scanner()->location(), | 3582 ReportMessageAt(scanner()->location(), |
3574 MessageTemplate::kStrongConstructorSuper); | 3583 MessageTemplate::kStrongConstructorSuper); |
3575 *ok = false; | 3584 *ok = false; |
3576 return this->EmptyExpression(); | 3585 return this->EmptyExpression(); |
3577 } | 3586 } |
3578 function_state->set_super_location(scanner()->location()); | 3587 // TODO(rossberg): This might not be the correct FunctionState for the |
3579 return this->SuperReference(scope_, factory()); | 3588 // method here. |
| 3589 function_state_->set_super_location(scanner()->location()); |
| 3590 return this->SuperReference(scope_, factory(), pos); |
3580 } | 3591 } |
3581 } | 3592 } |
3582 | 3593 |
3583 ReportMessageAt(scanner()->location(), MessageTemplate::kUnexpectedSuper); | 3594 ReportMessageAt(scanner()->location(), MessageTemplate::kUnexpectedSuper); |
3584 *ok = false; | 3595 *ok = false; |
3585 return this->EmptyExpression(); | 3596 return this->EmptyExpression(); |
3586 } | 3597 } |
3587 | 3598 |
3588 | 3599 |
3589 template <class Traits> | 3600 template <class Traits> |
(...skipping 430 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4020 *ok = false; | 4031 *ok = false; |
4021 return; | 4032 return; |
4022 } | 4033 } |
4023 has_seen_constructor_ = true; | 4034 has_seen_constructor_ = true; |
4024 return; | 4035 return; |
4025 } | 4036 } |
4026 } | 4037 } |
4027 } } // v8::internal | 4038 } } // v8::internal |
4028 | 4039 |
4029 #endif // V8_PREPARSER_H | 4040 #endif // V8_PREPARSER_H |
OLD | NEW |