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 #include "src/parsing/parser.h" | 5 #include "src/parsing/parser.h" |
6 | 6 |
7 #include "src/api.h" | 7 #include "src/api.h" |
8 #include "src/ast/ast.h" | 8 #include "src/ast/ast.h" |
9 #include "src/ast/ast-expression-visitor.h" | 9 #include "src/ast/ast-expression-visitor.h" |
10 #include "src/ast/ast-literal-reindexer.h" | 10 #include "src/ast/ast-literal-reindexer.h" |
(...skipping 1547 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1558 | 1558 |
1559 case Token::CLASS: | 1559 case Token::CLASS: |
1560 // TODO(ES6): Support parsing anonymous class declarations here. | 1560 // TODO(ES6): Support parsing anonymous class declarations here. |
1561 result = ParseClassDeclaration(&names, CHECK_OK); | 1561 result = ParseClassDeclaration(&names, CHECK_OK); |
1562 break; | 1562 break; |
1563 | 1563 |
1564 default: { | 1564 default: { |
1565 int pos = peek_position(); | 1565 int pos = peek_position(); |
1566 ExpressionClassifier classifier; | 1566 ExpressionClassifier classifier; |
1567 Expression* expr = ParseAssignmentExpression(true, &classifier, CHECK_OK); | 1567 Expression* expr = ParseAssignmentExpression(true, &classifier, CHECK_OK); |
1568 ValidateExpression(&classifier, CHECK_OK); | 1568 expr = ParserTraits::RewriteNonPattern(expr, &classifier, CHECK_OK); |
1569 | 1569 |
1570 ExpectSemicolon(CHECK_OK); | 1570 ExpectSemicolon(CHECK_OK); |
1571 result = factory()->NewExpressionStatement(expr, pos); | 1571 result = factory()->NewExpressionStatement(expr, pos); |
1572 break; | 1572 break; |
1573 } | 1573 } |
1574 } | 1574 } |
1575 | 1575 |
1576 const AstRawString* default_string = ast_value_factory()->default_string(); | 1576 const AstRawString* default_string = ast_value_factory()->default_string(); |
1577 | 1577 |
1578 DCHECK_LE(names.length(), 1); | 1578 DCHECK_LE(names.length(), 1); |
(...skipping 806 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2385 } | 2385 } |
2386 | 2386 |
2387 Expression* value = NULL; | 2387 Expression* value = NULL; |
2388 // Harmony consts have non-optional initializers. | 2388 // Harmony consts have non-optional initializers. |
2389 int initializer_position = RelocInfo::kNoPosition; | 2389 int initializer_position = RelocInfo::kNoPosition; |
2390 if (Check(Token::ASSIGN)) { | 2390 if (Check(Token::ASSIGN)) { |
2391 ExpressionClassifier classifier; | 2391 ExpressionClassifier classifier; |
2392 value = ParseAssignmentExpression(var_context != kForStatement, | 2392 value = ParseAssignmentExpression(var_context != kForStatement, |
2393 &classifier, ok); | 2393 &classifier, ok); |
2394 if (!*ok) return; | 2394 if (!*ok) return; |
2395 ValidateExpression(&classifier, ok); | 2395 value = ParserTraits::RewriteNonPattern(value, &classifier, ok); |
2396 if (!*ok) return; | 2396 if (!*ok) return; |
2397 variable_loc.end_pos = scanner()->location().end_pos; | 2397 variable_loc.end_pos = scanner()->location().end_pos; |
2398 | 2398 |
2399 if (!parsing_result->first_initializer_loc.IsValid()) { | 2399 if (!parsing_result->first_initializer_loc.IsValid()) { |
2400 parsing_result->first_initializer_loc = variable_loc; | 2400 parsing_result->first_initializer_loc = variable_loc; |
2401 } | 2401 } |
2402 | 2402 |
2403 // Don't infer if it is "a = function(){...}();"-like expression. | 2403 // Don't infer if it is "a = function(){...}();"-like expression. |
2404 if (single_name) { | 2404 if (single_name) { |
2405 if (fni_ != NULL && value->AsCall() == NULL && | 2405 if (fni_ != NULL && value->AsCall() == NULL && |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2496 if (is_strong(language_mode()) && | 2496 if (is_strong(language_mode()) && |
2497 IsClassConstructor(function_state_->kind())) { | 2497 IsClassConstructor(function_state_->kind())) { |
2498 bool is_this = peek() == Token::THIS; | 2498 bool is_this = peek() == Token::THIS; |
2499 Expression* expr; | 2499 Expression* expr; |
2500 ExpressionClassifier classifier; | 2500 ExpressionClassifier classifier; |
2501 if (is_this) { | 2501 if (is_this) { |
2502 expr = ParseStrongInitializationExpression(&classifier, CHECK_OK); | 2502 expr = ParseStrongInitializationExpression(&classifier, CHECK_OK); |
2503 } else { | 2503 } else { |
2504 expr = ParseStrongSuperCallExpression(&classifier, CHECK_OK); | 2504 expr = ParseStrongSuperCallExpression(&classifier, CHECK_OK); |
2505 } | 2505 } |
2506 ValidateExpression(&classifier, CHECK_OK); | 2506 expr = ParserTraits::RewriteNonPattern(expr, &classifier, CHECK_OK); |
2507 switch (peek()) { | 2507 switch (peek()) { |
2508 case Token::SEMICOLON: | 2508 case Token::SEMICOLON: |
2509 Consume(Token::SEMICOLON); | 2509 Consume(Token::SEMICOLON); |
2510 break; | 2510 break; |
2511 case Token::RBRACE: | 2511 case Token::RBRACE: |
2512 case Token::EOS: | 2512 case Token::EOS: |
2513 break; | 2513 break; |
2514 default: | 2514 default: |
2515 if (!scanner()->HasAnyLineTerminatorBeforeNext()) { | 2515 if (!scanner()->HasAnyLineTerminatorBeforeNext()) { |
2516 ReportMessageAt(function_state_->this_location(), | 2516 ReportMessageAt(function_state_->this_location(), |
(...skipping 1200 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3717 | 3717 |
3718 bool is_for_each = CheckInOrOf(&mode, ok); | 3718 bool is_for_each = CheckInOrOf(&mode, ok); |
3719 if (!*ok) return nullptr; | 3719 if (!*ok) return nullptr; |
3720 bool is_destructuring = | 3720 bool is_destructuring = |
3721 is_for_each && allow_harmony_destructuring_assignment() && | 3721 is_for_each && allow_harmony_destructuring_assignment() && |
3722 (expression->IsArrayLiteral() || expression->IsObjectLiteral()); | 3722 (expression->IsArrayLiteral() || expression->IsObjectLiteral()); |
3723 | 3723 |
3724 if (is_destructuring) { | 3724 if (is_destructuring) { |
3725 ValidateAssignmentPattern(&classifier, CHECK_OK); | 3725 ValidateAssignmentPattern(&classifier, CHECK_OK); |
3726 } else { | 3726 } else { |
3727 ValidateExpression(&classifier, CHECK_OK); | 3727 expression = |
| 3728 ParserTraits::RewriteNonPattern(expression, &classifier, CHECK_OK); |
3728 } | 3729 } |
3729 | 3730 |
3730 if (is_for_each) { | 3731 if (is_for_each) { |
3731 if (!is_destructuring) { | 3732 if (!is_destructuring) { |
3732 expression = this->CheckAndRewriteReferenceExpression( | 3733 expression = this->CheckAndRewriteReferenceExpression( |
3733 expression, lhs_beg_pos, lhs_end_pos, | 3734 expression, lhs_beg_pos, lhs_end_pos, |
3734 MessageTemplate::kInvalidLhsInFor, kSyntaxError, CHECK_OK); | 3735 MessageTemplate::kInvalidLhsInFor, kSyntaxError, CHECK_OK); |
3735 } | 3736 } |
3736 | 3737 |
3737 ForEachStatement* loop = | 3738 ForEachStatement* loop = |
(...skipping 1003 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4741 proxy, CONST, block_scope, pos, is_class_declaration, | 4742 proxy, CONST, block_scope, pos, is_class_declaration, |
4742 scope_->class_declaration_group_start()); | 4743 scope_->class_declaration_group_start()); |
4743 Declare(declaration, DeclarationDescriptor::NORMAL, true, CHECK_OK); | 4744 Declare(declaration, DeclarationDescriptor::NORMAL, true, CHECK_OK); |
4744 } | 4745 } |
4745 | 4746 |
4746 Expression* extends = NULL; | 4747 Expression* extends = NULL; |
4747 if (Check(Token::EXTENDS)) { | 4748 if (Check(Token::EXTENDS)) { |
4748 block_scope->set_start_position(scanner()->location().end_pos); | 4749 block_scope->set_start_position(scanner()->location().end_pos); |
4749 ExpressionClassifier classifier; | 4750 ExpressionClassifier classifier; |
4750 extends = ParseLeftHandSideExpression(&classifier, CHECK_OK); | 4751 extends = ParseLeftHandSideExpression(&classifier, CHECK_OK); |
4751 ValidateExpression(&classifier, CHECK_OK); | 4752 extends = ParserTraits::RewriteNonPattern(extends, &classifier, CHECK_OK); |
4752 } else { | 4753 } else { |
4753 block_scope->set_start_position(scanner()->location().end_pos); | 4754 block_scope->set_start_position(scanner()->location().end_pos); |
4754 } | 4755 } |
4755 | 4756 |
4756 | 4757 |
4757 ClassLiteralChecker checker(this); | 4758 ClassLiteralChecker checker(this); |
4758 ZoneList<ObjectLiteral::Property*>* properties = NewPropertyList(4, zone()); | 4759 ZoneList<ObjectLiteral::Property*>* properties = NewPropertyList(4, zone()); |
4759 FunctionLiteral* constructor = NULL; | 4760 FunctionLiteral* constructor = NULL; |
4760 bool has_seen_constructor = false; | 4761 bool has_seen_constructor = false; |
4761 | 4762 |
4762 Expect(Token::LBRACE, CHECK_OK); | 4763 Expect(Token::LBRACE, CHECK_OK); |
4763 | 4764 |
4764 const bool has_extends = extends != nullptr; | 4765 const bool has_extends = extends != nullptr; |
4765 while (peek() != Token::RBRACE) { | 4766 while (peek() != Token::RBRACE) { |
4766 if (Check(Token::SEMICOLON)) continue; | 4767 if (Check(Token::SEMICOLON)) continue; |
4767 FuncNameInferrer::State fni_state(fni_); | 4768 FuncNameInferrer::State fni_state(fni_); |
4768 const bool in_class = true; | 4769 const bool in_class = true; |
4769 const bool is_static = false; | 4770 const bool is_static = false; |
4770 bool is_computed_name = false; // Classes do not care about computed | 4771 bool is_computed_name = false; // Classes do not care about computed |
4771 // property names here. | 4772 // property names here. |
4772 ExpressionClassifier classifier; | 4773 ExpressionClassifier classifier; |
4773 const AstRawString* name = nullptr; | 4774 const AstRawString* name = nullptr; |
4774 ObjectLiteral::Property* property = ParsePropertyDefinition( | 4775 ObjectLiteral::Property* property = ParsePropertyDefinition( |
4775 &checker, in_class, has_extends, is_static, &is_computed_name, | 4776 &checker, in_class, has_extends, is_static, &is_computed_name, |
4776 &has_seen_constructor, &classifier, &name, CHECK_OK); | 4777 &has_seen_constructor, &classifier, &name, CHECK_OK); |
4777 ValidateExpression(&classifier, CHECK_OK); | 4778 property = ParserTraits::RewriteNonPatternObjectLiteralProperty( |
| 4779 property, &classifier, CHECK_OK); |
4778 | 4780 |
4779 if (has_seen_constructor && constructor == NULL) { | 4781 if (has_seen_constructor && constructor == NULL) { |
4780 constructor = GetPropertyValue(property)->AsFunctionLiteral(); | 4782 constructor = GetPropertyValue(property)->AsFunctionLiteral(); |
4781 DCHECK_NOT_NULL(constructor); | 4783 DCHECK_NOT_NULL(constructor); |
4782 } else { | 4784 } else { |
4783 properties->Add(property, zone()); | 4785 properties->Add(property, zone()); |
4784 } | 4786 } |
4785 | 4787 |
4786 if (fni_ != NULL) fni_->Infer(); | 4788 if (fni_ != NULL) fni_->Infer(); |
4787 | 4789 |
(...skipping 30 matching lines...) Expand all Loading... |
4818 | 4820 |
4819 int pos = peek_position(); | 4821 int pos = peek_position(); |
4820 Expect(Token::MOD, CHECK_OK); | 4822 Expect(Token::MOD, CHECK_OK); |
4821 // Allow "eval" or "arguments" for backward compatibility. | 4823 // Allow "eval" or "arguments" for backward compatibility. |
4822 const AstRawString* name = ParseIdentifier(kAllowRestrictedIdentifiers, | 4824 const AstRawString* name = ParseIdentifier(kAllowRestrictedIdentifiers, |
4823 CHECK_OK); | 4825 CHECK_OK); |
4824 Scanner::Location spread_pos; | 4826 Scanner::Location spread_pos; |
4825 ExpressionClassifier classifier; | 4827 ExpressionClassifier classifier; |
4826 ZoneList<Expression*>* args = | 4828 ZoneList<Expression*>* args = |
4827 ParseArguments(&spread_pos, &classifier, CHECK_OK); | 4829 ParseArguments(&spread_pos, &classifier, CHECK_OK); |
4828 ValidateExpression(&classifier, CHECK_OK); | 4830 args = RewriteNonPatternArguments(args, &classifier, CHECK_OK); |
4829 | 4831 |
4830 DCHECK(!spread_pos.IsValid()); | 4832 DCHECK(!spread_pos.IsValid()); |
4831 | 4833 |
4832 if (extension_ != NULL) { | 4834 if (extension_ != NULL) { |
4833 // The extension structures are only accessible while parsing the | 4835 // The extension structures are only accessible while parsing the |
4834 // very first time not when reparsing because of lazy compilation. | 4836 // very first time not when reparsing because of lazy compilation. |
4835 scope_->DeclarationScope()->ForceEagerCompilation(); | 4837 scope_->DeclarationScope()->ForceEagerCompilation(); |
4836 } | 4838 } |
4837 | 4839 |
4838 const Runtime::Function* function = Runtime::FunctionForName(name->string()); | 4840 const Runtime::Function* function = Runtime::FunctionForName(name->string()); |
(...skipping 542 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
5381 SetLanguageMode(scope_, | 5383 SetLanguageMode(scope_, |
5382 static_cast<LanguageMode>(scope_->language_mode() | mode)); | 5384 static_cast<LanguageMode>(scope_->language_mode() | mode)); |
5383 } | 5385 } |
5384 | 5386 |
5385 | 5387 |
5386 void ParserTraits::RewriteDestructuringAssignments() { | 5388 void ParserTraits::RewriteDestructuringAssignments() { |
5387 parser_->RewriteDestructuringAssignments(); | 5389 parser_->RewriteDestructuringAssignments(); |
5388 } | 5390 } |
5389 | 5391 |
5390 | 5392 |
| 5393 Expression* ParserTraits::RewriteNonPattern( |
| 5394 Expression* expr, const ExpressionClassifier* classifier, bool* ok) { |
| 5395 return parser_->RewriteNonPattern(expr, classifier, ok); |
| 5396 } |
| 5397 |
| 5398 |
| 5399 ZoneList<Expression*>* ParserTraits::RewriteNonPatternArguments( |
| 5400 ZoneList<Expression*>* args, const ExpressionClassifier* classifier, |
| 5401 bool* ok) { |
| 5402 return parser_->RewriteNonPatternArguments(args, classifier, ok); |
| 5403 } |
| 5404 |
| 5405 |
| 5406 ObjectLiteralProperty* ParserTraits::RewriteNonPatternObjectLiteralProperty( |
| 5407 ObjectLiteralProperty* property, const ExpressionClassifier* classifier, |
| 5408 bool* ok) { |
| 5409 return parser_->RewriteNonPatternObjectLiteralProperty(property, classifier, |
| 5410 ok); |
| 5411 } |
| 5412 |
| 5413 |
| 5414 Expression* Parser::RewriteNonPattern(Expression* expr, |
| 5415 const ExpressionClassifier* classifier, |
| 5416 bool* ok) { |
| 5417 // For the time being, this does no rewriting at all. |
| 5418 ValidateExpression(classifier, ok); |
| 5419 return expr; |
| 5420 } |
| 5421 |
| 5422 |
| 5423 ZoneList<Expression*>* Parser::RewriteNonPatternArguments( |
| 5424 ZoneList<Expression*>* args, const ExpressionClassifier* classifier, |
| 5425 bool* ok) { |
| 5426 // For the time being, this does no rewriting at all. |
| 5427 ValidateExpression(classifier, ok); |
| 5428 return args; |
| 5429 } |
| 5430 |
| 5431 |
| 5432 ObjectLiteralProperty* Parser::RewriteNonPatternObjectLiteralProperty( |
| 5433 ObjectLiteralProperty* property, const ExpressionClassifier* classifier, |
| 5434 bool* ok) { |
| 5435 if (property != nullptr) { |
| 5436 Expression* key = RewriteNonPattern(property->key(), classifier, ok); |
| 5437 property->set_key(key); |
| 5438 Expression* value = RewriteNonPattern(property->value(), classifier, ok); |
| 5439 property->set_value(value); |
| 5440 } |
| 5441 return property; |
| 5442 } |
| 5443 |
| 5444 |
5391 void Parser::RewriteDestructuringAssignments() { | 5445 void Parser::RewriteDestructuringAssignments() { |
5392 FunctionState* func = function_state_; | 5446 FunctionState* func = function_state_; |
5393 if (!allow_harmony_destructuring_assignment()) return; | 5447 if (!allow_harmony_destructuring_assignment()) return; |
5394 const List<DestructuringAssignment>& assignments = | 5448 const List<DestructuringAssignment>& assignments = |
5395 func->destructuring_assignments_to_rewrite(); | 5449 func->destructuring_assignments_to_rewrite(); |
5396 for (int i = assignments.length() - 1; i >= 0; --i) { | 5450 for (int i = assignments.length() - 1; i >= 0; --i) { |
5397 // Rewrite list in reverse, so that nested assignment patterns are rewritten | 5451 // Rewrite list in reverse, so that nested assignment patterns are rewritten |
5398 // correctly. | 5452 // correctly. |
5399 DestructuringAssignment pair = assignments.at(i); | 5453 DestructuringAssignment pair = assignments.at(i); |
5400 RewritableAssignmentExpression* to_rewrite = | 5454 RewritableAssignmentExpression* to_rewrite = |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
5471 auto class_literal = value->AsClassLiteral(); | 5525 auto class_literal = value->AsClassLiteral(); |
5472 if (class_literal->raw_name() == nullptr) { | 5526 if (class_literal->raw_name() == nullptr) { |
5473 class_literal->set_raw_name(name); | 5527 class_literal->set_raw_name(name); |
5474 } | 5528 } |
5475 } | 5529 } |
5476 } | 5530 } |
5477 | 5531 |
5478 | 5532 |
5479 } // namespace internal | 5533 } // namespace internal |
5480 } // namespace v8 | 5534 } // namespace v8 |
OLD | NEW |