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

Side by Side Diff: src/parser.cc

Issue 12646003: Add parser support for generators. (Closed) Base URL: git://github.com/v8/v8.git@bleeding_edge
Patch Set: Fix bad initialization list in last preparser commit Created 7 years, 8 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
OLDNEW
1 // Copyright 2012 the V8 project authors. All rights reserved. 1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
(...skipping 468 matching lines...) Expand 10 before | Expand all | Expand 10 after
479 ~BlockState() { parser_->top_scope_ = outer_scope_; } 479 ~BlockState() { parser_->top_scope_ = outer_scope_; }
480 480
481 private: 481 private:
482 Parser* parser_; 482 Parser* parser_;
483 Scope* outer_scope_; 483 Scope* outer_scope_;
484 }; 484 };
485 485
486 486
487 Parser::FunctionState::FunctionState(Parser* parser, 487 Parser::FunctionState::FunctionState(Parser* parser,
488 Scope* scope, 488 Scope* scope,
489 bool is_generator,
489 Isolate* isolate) 490 Isolate* isolate)
490 : next_materialized_literal_index_(JSFunction::kLiteralsPrefixSize), 491 : next_materialized_literal_index_(JSFunction::kLiteralsPrefixSize),
491 next_handler_index_(0), 492 next_handler_index_(0),
492 expected_property_count_(0), 493 expected_property_count_(0),
494 is_generator_(is_generator),
493 only_simple_this_property_assignments_(false), 495 only_simple_this_property_assignments_(false),
494 this_property_assignments_(isolate->factory()->empty_fixed_array()), 496 this_property_assignments_(isolate->factory()->empty_fixed_array()),
495 parser_(parser), 497 parser_(parser),
496 outer_function_state_(parser->current_function_state_), 498 outer_function_state_(parser->current_function_state_),
497 outer_scope_(parser->top_scope_), 499 outer_scope_(parser->top_scope_),
498 saved_ast_node_id_(isolate->ast_node_id()), 500 saved_ast_node_id_(isolate->ast_node_id()),
499 factory_(isolate, parser->zone()) { 501 factory_(isolate, parser->zone()) {
500 parser->top_scope_ = scope; 502 parser->top_scope_ = scope;
501 parser->current_function_state_ = this; 503 parser->current_function_state_ = this;
502 isolate->set_ast_node_id(BailoutId::FirstUsable().ToInt()); 504 isolate->set_ast_node_id(BailoutId::FirstUsable().ToInt());
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after
635 scope->set_start_position(0); 637 scope->set_start_position(0);
636 scope->set_end_position(source->length()); 638 scope->set_end_position(source->length());
637 639
638 // Compute the parsing mode. 640 // Compute the parsing mode.
639 Mode mode = (FLAG_lazy && allow_lazy_) ? PARSE_LAZILY : PARSE_EAGERLY; 641 Mode mode = (FLAG_lazy && allow_lazy_) ? PARSE_LAZILY : PARSE_EAGERLY;
640 if (allow_natives_syntax_ || extension_ != NULL || scope->is_eval_scope()) { 642 if (allow_natives_syntax_ || extension_ != NULL || scope->is_eval_scope()) {
641 mode = PARSE_EAGERLY; 643 mode = PARSE_EAGERLY;
642 } 644 }
643 ParsingModeScope parsing_mode(this, mode); 645 ParsingModeScope parsing_mode(this, mode);
644 646
645 FunctionState function_state(this, scope, isolate()); // Enters 'scope'. 647 bool is_generator = false;
648 // Enters 'scope'.
649 FunctionState function_state(this, scope, is_generator, isolate());
650
646 top_scope_->SetLanguageMode(info->language_mode()); 651 top_scope_->SetLanguageMode(info->language_mode());
647 ZoneList<Statement*>* body = new(zone()) ZoneList<Statement*>(16, zone()); 652 ZoneList<Statement*>* body = new(zone()) ZoneList<Statement*>(16, zone());
648 bool ok = true; 653 bool ok = true;
649 int beg_loc = scanner().location().beg_pos; 654 int beg_loc = scanner().location().beg_pos;
650 ParseSourceElements(body, Token::EOS, info->is_eval(), true, &ok); 655 ParseSourceElements(body, Token::EOS, info->is_eval(), true, &ok);
651 if (ok && !top_scope_->is_classic_mode()) { 656 if (ok && !top_scope_->is_classic_mode()) {
652 CheckOctalLiteral(beg_loc, scanner().location().end_pos, &ok); 657 CheckOctalLiteral(beg_loc, scanner().location().end_pos, &ok);
653 } 658 }
654 659
655 if (ok && is_extended_mode()) { 660 if (ok && is_extended_mode()) {
(...skipping 17 matching lines...) Expand all
673 body, 678 body,
674 function_state.materialized_literal_count(), 679 function_state.materialized_literal_count(),
675 function_state.expected_property_count(), 680 function_state.expected_property_count(),
676 function_state.handler_count(), 681 function_state.handler_count(),
677 function_state.only_simple_this_property_assignments(), 682 function_state.only_simple_this_property_assignments(),
678 function_state.this_property_assignments(), 683 function_state.this_property_assignments(),
679 0, 684 0,
680 FunctionLiteral::kNoDuplicateParameters, 685 FunctionLiteral::kNoDuplicateParameters,
681 FunctionLiteral::ANONYMOUS_EXPRESSION, 686 FunctionLiteral::ANONYMOUS_EXPRESSION,
682 FunctionLiteral::kGlobalOrEval, 687 FunctionLiteral::kGlobalOrEval,
683 FunctionLiteral::kNotParenthesized); 688 FunctionLiteral::kNotParenthesized,
689 FunctionLiteral::kNotGenerator);
684 result->set_ast_properties(factory()->visitor()->ast_properties()); 690 result->set_ast_properties(factory()->visitor()->ast_properties());
685 } else if (stack_overflow_) { 691 } else if (stack_overflow_) {
686 isolate()->StackOverflow(); 692 isolate()->StackOverflow();
687 } 693 }
688 } 694 }
689 695
690 // Make sure the target stack is empty. 696 // Make sure the target stack is empty.
691 ASSERT(target_stack_ == NULL); 697 ASSERT(target_stack_ == NULL);
692 698
693 // If there was a syntax error we have to get rid of the AST 699 // If there was a syntax error we have to get rid of the AST
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
747 FunctionLiteral* result = NULL; 753 FunctionLiteral* result = NULL;
748 754
749 { 755 {
750 // Parse the function literal. 756 // Parse the function literal.
751 Scope* scope = NewScope(top_scope_, GLOBAL_SCOPE); 757 Scope* scope = NewScope(top_scope_, GLOBAL_SCOPE);
752 info()->SetGlobalScope(scope); 758 info()->SetGlobalScope(scope);
753 if (!info()->closure().is_null()) { 759 if (!info()->closure().is_null()) {
754 scope = Scope::DeserializeScopeChain(info()->closure()->context(), scope, 760 scope = Scope::DeserializeScopeChain(info()->closure()->context(), scope,
755 zone()); 761 zone());
756 } 762 }
757 FunctionState function_state(this, scope, isolate()); 763 bool is_generator = false; // Top scope is not a generator.
764 FunctionState function_state(this, scope, is_generator, isolate());
758 ASSERT(scope->language_mode() != STRICT_MODE || !info()->is_classic_mode()); 765 ASSERT(scope->language_mode() != STRICT_MODE || !info()->is_classic_mode());
759 ASSERT(scope->language_mode() != EXTENDED_MODE || 766 ASSERT(scope->language_mode() != EXTENDED_MODE ||
760 info()->is_extended_mode()); 767 info()->is_extended_mode());
761 ASSERT(info()->language_mode() == shared_info->language_mode()); 768 ASSERT(info()->language_mode() == shared_info->language_mode());
762 scope->SetLanguageMode(shared_info->language_mode()); 769 scope->SetLanguageMode(shared_info->language_mode());
763 FunctionLiteral::Type type = shared_info->is_expression() 770 FunctionLiteral::Type type = shared_info->is_expression()
764 ? (shared_info->is_anonymous() 771 ? (shared_info->is_anonymous()
765 ? FunctionLiteral::ANONYMOUS_EXPRESSION 772 ? FunctionLiteral::ANONYMOUS_EXPRESSION
766 : FunctionLiteral::NAMED_EXPRESSION) 773 : FunctionLiteral::NAMED_EXPRESSION)
767 : FunctionLiteral::DECLARATION; 774 : FunctionLiteral::DECLARATION;
768 bool ok = true; 775 bool ok = true;
769 result = ParseFunctionLiteral(name, 776 result = ParseFunctionLiteral(name,
770 false, // Strict mode name already checked. 777 false, // Strict mode name already checked.
778 shared_info->is_generator(),
771 RelocInfo::kNoPosition, 779 RelocInfo::kNoPosition,
772 type, 780 type,
773 &ok); 781 &ok);
774 // Make sure the results agree. 782 // Make sure the results agree.
775 ASSERT(ok == (result != NULL)); 783 ASSERT(ok == (result != NULL));
776 } 784 }
777 785
778 // Make sure the target stack is empty. 786 // Make sure the target stack is empty.
779 ASSERT(target_stack_ == NULL); 787 ASSERT(target_stack_ == NULL);
780 788
(...skipping 344 matching lines...) Expand 10 before | Expand all | Expand 10 after
1125 // Statement 1133 // Statement
1126 // FunctionDeclaration 1134 // FunctionDeclaration
1127 // 1135 //
1128 // In harmony mode we allow additionally the following productions 1136 // In harmony mode we allow additionally the following productions
1129 // ModuleElement: 1137 // ModuleElement:
1130 // LetDeclaration 1138 // LetDeclaration
1131 // ConstDeclaration 1139 // ConstDeclaration
1132 // ModuleDeclaration 1140 // ModuleDeclaration
1133 // ImportDeclaration 1141 // ImportDeclaration
1134 // ExportDeclaration 1142 // ExportDeclaration
1143 // GeneratorDeclaration
1135 1144
1136 switch (peek()) { 1145 switch (peek()) {
1137 case Token::FUNCTION: 1146 case Token::FUNCTION:
1138 return ParseFunctionDeclaration(NULL, ok); 1147 return ParseFunctionDeclaration(NULL, ok);
1139 case Token::LET: 1148 case Token::LET:
1140 case Token::CONST: 1149 case Token::CONST:
1141 return ParseVariableStatement(kModuleElement, NULL, ok); 1150 return ParseVariableStatement(kModuleElement, NULL, ok);
1142 case Token::IMPORT: 1151 case Token::IMPORT:
1143 return ParseImportDeclaration(ok); 1152 return ParseImportDeclaration(ok);
1144 case Token::EXPORT: 1153 case Token::EXPORT:
(...skipping 278 matching lines...) Expand 10 before | Expand all | Expand 10 after
1423 1432
1424 return block; 1433 return block;
1425 } 1434 }
1426 1435
1427 1436
1428 Statement* Parser::ParseExportDeclaration(bool* ok) { 1437 Statement* Parser::ParseExportDeclaration(bool* ok) {
1429 // ExportDeclaration: 1438 // ExportDeclaration:
1430 // 'export' Identifier (',' Identifier)* ';' 1439 // 'export' Identifier (',' Identifier)* ';'
1431 // 'export' VariableDeclaration 1440 // 'export' VariableDeclaration
1432 // 'export' FunctionDeclaration 1441 // 'export' FunctionDeclaration
1442 // 'export' GeneratorDeclaration
1433 // 'export' ModuleDeclaration 1443 // 'export' ModuleDeclaration
1434 // 1444 //
1435 // TODO(ES6): implement structuring ExportSpecifiers 1445 // TODO(ES6): implement structuring ExportSpecifiers
1436 1446
1437 Expect(Token::EXPORT, CHECK_OK); 1447 Expect(Token::EXPORT, CHECK_OK);
1438 1448
1439 Statement* result = NULL; 1449 Statement* result = NULL;
1440 ZoneStringList names(1, zone()); 1450 ZoneStringList names(1, zone());
1441 switch (peek()) { 1451 switch (peek()) {
1442 case Token::IDENTIFIER: { 1452 case Token::IDENTIFIER: {
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
1502 bool* ok) { 1512 bool* ok) {
1503 // (Ecma 262 5th Edition, clause 14): 1513 // (Ecma 262 5th Edition, clause 14):
1504 // SourceElement: 1514 // SourceElement:
1505 // Statement 1515 // Statement
1506 // FunctionDeclaration 1516 // FunctionDeclaration
1507 // 1517 //
1508 // In harmony mode we allow additionally the following productions 1518 // In harmony mode we allow additionally the following productions
1509 // BlockElement (aka SourceElement): 1519 // BlockElement (aka SourceElement):
1510 // LetDeclaration 1520 // LetDeclaration
1511 // ConstDeclaration 1521 // ConstDeclaration
1522 // GeneratorDeclaration
1512 1523
1513 switch (peek()) { 1524 switch (peek()) {
1514 case Token::FUNCTION: 1525 case Token::FUNCTION:
1515 return ParseFunctionDeclaration(NULL, ok); 1526 return ParseFunctionDeclaration(NULL, ok);
1516 case Token::LET: 1527 case Token::LET:
1517 case Token::CONST: 1528 case Token::CONST:
1518 return ParseVariableStatement(kModuleElement, NULL, ok); 1529 return ParseVariableStatement(kModuleElement, NULL, ok);
1519 default: 1530 default:
1520 return ParseStatement(labels, ok); 1531 return ParseStatement(labels, ok);
1521 } 1532 }
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after
1621 } 1632 }
1622 1633
1623 case Token::FUNCTION: { 1634 case Token::FUNCTION: {
1624 // FunctionDeclaration is only allowed in the context of SourceElements 1635 // FunctionDeclaration is only allowed in the context of SourceElements
1625 // (Ecma 262 5th Edition, clause 14): 1636 // (Ecma 262 5th Edition, clause 14):
1626 // SourceElement: 1637 // SourceElement:
1627 // Statement 1638 // Statement
1628 // FunctionDeclaration 1639 // FunctionDeclaration
1629 // Common language extension is to allow function declaration in place 1640 // Common language extension is to allow function declaration in place
1630 // of any statement. This language extension is disabled in strict mode. 1641 // of any statement. This language extension is disabled in strict mode.
1642 //
1643 // In Harmony mode, this case also handles the extension:
1644 // Statement:
1645 // GeneratorDeclaration
1631 if (!top_scope_->is_classic_mode()) { 1646 if (!top_scope_->is_classic_mode()) {
1632 ReportMessageAt(scanner().peek_location(), "strict_function", 1647 ReportMessageAt(scanner().peek_location(), "strict_function",
1633 Vector<const char*>::empty()); 1648 Vector<const char*>::empty());
1634 *ok = false; 1649 *ok = false;
1635 return NULL; 1650 return NULL;
1636 } 1651 }
1637 return ParseFunctionDeclaration(NULL, ok); 1652 return ParseFunctionDeclaration(NULL, ok);
1638 } 1653 }
1639 1654
1640 case Token::DEBUGGER: 1655 case Token::DEBUGGER:
(...skipping 242 matching lines...) Expand 10 before | Expand all | Expand 10 after
1883 factory()->NewSharedFunctionInfoLiteral(shared); 1898 factory()->NewSharedFunctionInfoLiteral(shared);
1884 return factory()->NewExpressionStatement( 1899 return factory()->NewExpressionStatement(
1885 factory()->NewAssignment( 1900 factory()->NewAssignment(
1886 Token::INIT_VAR, proxy, lit, RelocInfo::kNoPosition)); 1901 Token::INIT_VAR, proxy, lit, RelocInfo::kNoPosition));
1887 } 1902 }
1888 1903
1889 1904
1890 Statement* Parser::ParseFunctionDeclaration(ZoneStringList* names, bool* ok) { 1905 Statement* Parser::ParseFunctionDeclaration(ZoneStringList* names, bool* ok) {
1891 // FunctionDeclaration :: 1906 // FunctionDeclaration ::
1892 // 'function' Identifier '(' FormalParameterListopt ')' '{' FunctionBody '}' 1907 // 'function' Identifier '(' FormalParameterListopt ')' '{' FunctionBody '}'
1908 // GeneratorDeclaration ::
1909 // 'function' '*' Identifier '(' FormalParameterListopt ')'
1910 // '{' FunctionBody '}'
1893 Expect(Token::FUNCTION, CHECK_OK); 1911 Expect(Token::FUNCTION, CHECK_OK);
1894 int function_token_position = scanner().location().beg_pos; 1912 int function_token_position = scanner().location().beg_pos;
1913 bool is_generator = FLAG_harmony_generators && Check(Token::MUL);
1895 bool is_strict_reserved = false; 1914 bool is_strict_reserved = false;
1896 Handle<String> name = ParseIdentifierOrStrictReservedWord( 1915 Handle<String> name = ParseIdentifierOrStrictReservedWord(
1897 &is_strict_reserved, CHECK_OK); 1916 &is_strict_reserved, CHECK_OK);
1898 FunctionLiteral* fun = ParseFunctionLiteral(name, 1917 FunctionLiteral* fun = ParseFunctionLiteral(name,
1899 is_strict_reserved, 1918 is_strict_reserved,
1919 is_generator,
1900 function_token_position, 1920 function_token_position,
1901 FunctionLiteral::DECLARATION, 1921 FunctionLiteral::DECLARATION,
1902 CHECK_OK); 1922 CHECK_OK);
1903 // Even if we're not at the top-level of the global or a function 1923 // Even if we're not at the top-level of the global or a function
1904 // scope, we treat it as such and introduce the function with its 1924 // scope, we treat it as such and introduce the function with its
1905 // initial value upon entering the corresponding scope. 1925 // initial value upon entering the corresponding scope.
1906 // In extended mode, a function behaves as a lexical binding, except in the 1926 // In extended mode, a function behaves as a lexical binding, except in the
1907 // global scope. 1927 // global scope.
1908 VariableMode mode = 1928 VariableMode mode =
1909 is_extended_mode() && !top_scope_->is_global_scope() ? LET : VAR; 1929 is_extended_mode() && !top_scope_->is_global_scope() ? LET : VAR;
(...skipping 1087 matching lines...) Expand 10 before | Expand all | Expand 10 after
2997 factory()->NewBinaryOperation(Token::COMMA, result, right, position); 3017 factory()->NewBinaryOperation(Token::COMMA, result, right, position);
2998 } 3018 }
2999 return result; 3019 return result;
3000 } 3020 }
3001 3021
3002 3022
3003 // Precedence = 2 3023 // Precedence = 2
3004 Expression* Parser::ParseAssignmentExpression(bool accept_IN, bool* ok) { 3024 Expression* Parser::ParseAssignmentExpression(bool accept_IN, bool* ok) {
3005 // AssignmentExpression :: 3025 // AssignmentExpression ::
3006 // ConditionalExpression 3026 // ConditionalExpression
3027 // YieldExpression
3007 // LeftHandSideExpression AssignmentOperator AssignmentExpression 3028 // LeftHandSideExpression AssignmentOperator AssignmentExpression
3008 3029
3030 if (peek() == Token::YIELD && is_generator()) {
3031 return ParseYieldExpression(ok);
3032 }
3033
3009 if (fni_ != NULL) fni_->Enter(); 3034 if (fni_ != NULL) fni_->Enter();
3010 Expression* expression = ParseConditionalExpression(accept_IN, CHECK_OK); 3035 Expression* expression = ParseConditionalExpression(accept_IN, CHECK_OK);
3011 3036
3012 if (!Token::IsAssignmentOp(peek())) { 3037 if (!Token::IsAssignmentOp(peek())) {
3013 if (fni_ != NULL) fni_->Leave(); 3038 if (fni_ != NULL) fni_->Leave();
3014 // Parsed conditional expression only (no assignment). 3039 // Parsed conditional expression only (no assignment).
3015 return expression; 3040 return expression;
3016 } 3041 }
3017 3042
3018 // Signal a reference error if the expression is an invalid left-hand 3043 // Signal a reference error if the expression is an invalid left-hand
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
3067 } else { 3092 } else {
3068 fni_->RemoveLastFunction(); 3093 fni_->RemoveLastFunction();
3069 } 3094 }
3070 fni_->Leave(); 3095 fni_->Leave();
3071 } 3096 }
3072 3097
3073 return factory()->NewAssignment(op, expression, right, pos); 3098 return factory()->NewAssignment(op, expression, right, pos);
3074 } 3099 }
3075 3100
3076 3101
3102 Expression* Parser::ParseYieldExpression(bool* ok) {
3103 // YieldExpression ::
3104 // 'yield' '*'? AssignmentExpression
3105 int position = scanner().peek_location().beg_pos;
3106 Expect(Token::YIELD, CHECK_OK);
3107 bool is_yield_star = Check(Token::MUL);
3108 Expression* expression = ParseAssignmentExpression(false, CHECK_OK);
3109 return factory()->NewYield(expression, is_yield_star, position);
3110 }
3111
3112
3077 // Precedence = 3 3113 // Precedence = 3
3078 Expression* Parser::ParseConditionalExpression(bool accept_IN, bool* ok) { 3114 Expression* Parser::ParseConditionalExpression(bool accept_IN, bool* ok) {
3079 // ConditionalExpression :: 3115 // ConditionalExpression ::
3080 // LogicalOrExpression 3116 // LogicalOrExpression
3081 // LogicalOrExpression '?' AssignmentExpression ':' AssignmentExpression 3117 // LogicalOrExpression '?' AssignmentExpression ':' AssignmentExpression
3082 3118
3083 // We start using the binary expression parser for prec >= 4 only! 3119 // We start using the binary expression parser for prec >= 4 only!
3084 Expression* expression = ParseBinaryExpression(4, accept_IN, CHECK_OK); 3120 Expression* expression = ParseBinaryExpression(4, accept_IN, CHECK_OK);
3085 if (peek() != Token::CONDITIONAL) return expression; 3121 if (peek() != Token::CONDITIONAL) return expression;
3086 Consume(Token::CONDITIONAL); 3122 Consume(Token::CONDITIONAL);
(...skipping 356 matching lines...) Expand 10 before | Expand all | Expand 10 after
3443 bool* ok) { 3479 bool* ok) {
3444 // MemberExpression :: 3480 // MemberExpression ::
3445 // (PrimaryExpression | FunctionLiteral) 3481 // (PrimaryExpression | FunctionLiteral)
3446 // ('[' Expression ']' | '.' Identifier | Arguments)* 3482 // ('[' Expression ']' | '.' Identifier | Arguments)*
3447 3483
3448 // Parse the initial primary or function expression. 3484 // Parse the initial primary or function expression.
3449 Expression* result = NULL; 3485 Expression* result = NULL;
3450 if (peek() == Token::FUNCTION) { 3486 if (peek() == Token::FUNCTION) {
3451 Expect(Token::FUNCTION, CHECK_OK); 3487 Expect(Token::FUNCTION, CHECK_OK);
3452 int function_token_position = scanner().location().beg_pos; 3488 int function_token_position = scanner().location().beg_pos;
3489 bool is_generator = FLAG_harmony_generators && Check(Token::MUL);
3453 Handle<String> name; 3490 Handle<String> name;
3454 bool is_strict_reserved_name = false; 3491 bool is_strict_reserved_name = false;
3455 if (peek_any_identifier()) { 3492 if (peek_any_identifier()) {
3456 name = ParseIdentifierOrStrictReservedWord(&is_strict_reserved_name, 3493 name = ParseIdentifierOrStrictReservedWord(&is_strict_reserved_name,
3457 CHECK_OK); 3494 CHECK_OK);
3458 } 3495 }
3459 FunctionLiteral::Type type = name.is_null() 3496 FunctionLiteral::Type type = name.is_null()
3460 ? FunctionLiteral::ANONYMOUS_EXPRESSION 3497 ? FunctionLiteral::ANONYMOUS_EXPRESSION
3461 : FunctionLiteral::NAMED_EXPRESSION; 3498 : FunctionLiteral::NAMED_EXPRESSION;
3462 result = ParseFunctionLiteral(name, 3499 result = ParseFunctionLiteral(name,
3463 is_strict_reserved_name, 3500 is_strict_reserved_name,
3501 is_generator,
3464 function_token_position, 3502 function_token_position,
3465 type, 3503 type,
3466 CHECK_OK); 3504 CHECK_OK);
3467 } else { 3505 } else {
3468 result = ParsePrimaryExpression(CHECK_OK); 3506 result = ParsePrimaryExpression(CHECK_OK);
3469 } 3507 }
3470 3508
3471 while (true) { 3509 while (true) {
3472 switch (peek()) { 3510 switch (peek()) {
3473 case Token::LBRACK: { 3511 case Token::LBRACK: {
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after
3597 Consume(Token::TRUE_LITERAL); 3635 Consume(Token::TRUE_LITERAL);
3598 result = factory()->NewLiteral(isolate()->factory()->true_value()); 3636 result = factory()->NewLiteral(isolate()->factory()->true_value());
3599 break; 3637 break;
3600 3638
3601 case Token::FALSE_LITERAL: 3639 case Token::FALSE_LITERAL:
3602 Consume(Token::FALSE_LITERAL); 3640 Consume(Token::FALSE_LITERAL);
3603 result = factory()->NewLiteral(isolate()->factory()->false_value()); 3641 result = factory()->NewLiteral(isolate()->factory()->false_value());
3604 break; 3642 break;
3605 3643
3606 case Token::IDENTIFIER: 3644 case Token::IDENTIFIER:
3645 case Token::YIELD:
3607 case Token::FUTURE_STRICT_RESERVED_WORD: { 3646 case Token::FUTURE_STRICT_RESERVED_WORD: {
3608 Handle<String> name = ParseIdentifier(CHECK_OK); 3647 Handle<String> name = ParseIdentifier(CHECK_OK);
3609 if (fni_ != NULL) fni_->PushVariableName(name); 3648 if (fni_ != NULL) fni_->PushVariableName(name);
3610 // The name may refer to a module instance object, so its type is unknown. 3649 // The name may refer to a module instance object, so its type is unknown.
3611 #ifdef DEBUG 3650 #ifdef DEBUG
3612 if (FLAG_print_interface_details) 3651 if (FLAG_print_interface_details)
3613 PrintF("# Variable %s ", name->ToAsciiArray()); 3652 PrintF("# Variable %s ", name->ToAsciiArray());
3614 #endif 3653 #endif
3615 Interface* interface = Interface::NewUnknown(zone()); 3654 Interface* interface = Interface::NewUnknown(zone());
3616 result = top_scope_->NewUnresolved( 3655 result = top_scope_->NewUnresolved(
(...skipping 385 matching lines...) Expand 10 before | Expand all | Expand 10 after
4002 next == Token::STRING || is_keyword) { 4041 next == Token::STRING || is_keyword) {
4003 Handle<String> name; 4042 Handle<String> name;
4004 if (is_keyword) { 4043 if (is_keyword) {
4005 name = isolate_->factory()->InternalizeUtf8String(Token::String(next)); 4044 name = isolate_->factory()->InternalizeUtf8String(Token::String(next));
4006 } else { 4045 } else {
4007 name = GetSymbol(CHECK_OK); 4046 name = GetSymbol(CHECK_OK);
4008 } 4047 }
4009 FunctionLiteral* value = 4048 FunctionLiteral* value =
4010 ParseFunctionLiteral(name, 4049 ParseFunctionLiteral(name,
4011 false, // reserved words are allowed here 4050 false, // reserved words are allowed here
4051 false, // not a generator
4012 RelocInfo::kNoPosition, 4052 RelocInfo::kNoPosition,
4013 FunctionLiteral::ANONYMOUS_EXPRESSION, 4053 FunctionLiteral::ANONYMOUS_EXPRESSION,
4014 CHECK_OK); 4054 CHECK_OK);
4015 // Allow any number of parameters for compatibilty with JSC. 4055 // Allow any number of parameters for compatibilty with JSC.
4016 // Specification only allows zero parameters for get and one for set. 4056 // Specification only allows zero parameters for get and one for set.
4017 return factory()->NewObjectLiteralProperty(is_getter, value); 4057 return factory()->NewObjectLiteralProperty(is_getter, value);
4018 } else { 4058 } else {
4019 ReportUnexpectedToken(next); 4059 ReportUnexpectedToken(next);
4020 *ok = false; 4060 *ok = false;
4021 return NULL; 4061 return NULL;
(...skipping 281 matching lines...) Expand 10 before | Expand all | Expand 10 after
4303 int properties_; 4343 int properties_;
4304 LanguageMode mode_; 4344 LanguageMode mode_;
4305 // For error messages. 4345 // For error messages.
4306 const char* message_; 4346 const char* message_;
4307 const char* argument_opt_; 4347 const char* argument_opt_;
4308 }; 4348 };
4309 4349
4310 4350
4311 FunctionLiteral* Parser::ParseFunctionLiteral(Handle<String> function_name, 4351 FunctionLiteral* Parser::ParseFunctionLiteral(Handle<String> function_name,
4312 bool name_is_strict_reserved, 4352 bool name_is_strict_reserved,
4353 bool is_generator,
4313 int function_token_position, 4354 int function_token_position,
4314 FunctionLiteral::Type type, 4355 FunctionLiteral::Type type,
4315 bool* ok) { 4356 bool* ok) {
4316 // Function :: 4357 // Function ::
4317 // '(' FormalParameterList? ')' '{' FunctionBody '}' 4358 // '(' FormalParameterList? ')' '{' FunctionBody '}'
4318 4359
4319 // Anonymous functions were passed either the empty symbol or a null 4360 // Anonymous functions were passed either the empty symbol or a null
4320 // handle as the function name. Remember if we were passed a non-empty 4361 // handle as the function name. Remember if we were passed a non-empty
4321 // handle to decide whether to invoke function name inference. 4362 // handle to decide whether to invoke function name inference.
4322 bool should_infer_name = function_name.is_null(); 4363 bool should_infer_name = function_name.is_null();
(...skipping 14 matching lines...) Expand all
4337 int materialized_literal_count = -1; 4378 int materialized_literal_count = -1;
4338 int expected_property_count = -1; 4379 int expected_property_count = -1;
4339 int handler_count = 0; 4380 int handler_count = 0;
4340 bool only_simple_this_property_assignments; 4381 bool only_simple_this_property_assignments;
4341 Handle<FixedArray> this_property_assignments; 4382 Handle<FixedArray> this_property_assignments;
4342 FunctionLiteral::ParameterFlag duplicate_parameters = 4383 FunctionLiteral::ParameterFlag duplicate_parameters =
4343 FunctionLiteral::kNoDuplicateParameters; 4384 FunctionLiteral::kNoDuplicateParameters;
4344 FunctionLiteral::IsParenthesizedFlag parenthesized = parenthesized_function_ 4385 FunctionLiteral::IsParenthesizedFlag parenthesized = parenthesized_function_
4345 ? FunctionLiteral::kIsParenthesized 4386 ? FunctionLiteral::kIsParenthesized
4346 : FunctionLiteral::kNotParenthesized; 4387 : FunctionLiteral::kNotParenthesized;
4388 FunctionLiteral::IsGeneratorFlag generator = is_generator
4389 ? FunctionLiteral::kIsGenerator
4390 : FunctionLiteral::kNotGenerator;
4347 AstProperties ast_properties; 4391 AstProperties ast_properties;
4348 // Parse function body. 4392 // Parse function body.
4349 { FunctionState function_state(this, scope, isolate()); 4393 { FunctionState function_state(this, scope, is_generator, isolate());
4350 top_scope_->SetScopeName(function_name); 4394 top_scope_->SetScopeName(function_name);
4351 4395
4352 // FormalParameterList :: 4396 // FormalParameterList ::
4353 // '(' (Identifier)*[','] ')' 4397 // '(' (Identifier)*[','] ')'
4354 Expect(Token::LPAREN, CHECK_OK); 4398 Expect(Token::LPAREN, CHECK_OK);
4355 scope->set_start_position(scanner().location().beg_pos); 4399 scope->set_start_position(scanner().location().beg_pos);
4356 Scanner::Location name_loc = Scanner::Location::invalid(); 4400 Scanner::Location name_loc = Scanner::Location::invalid();
4357 Scanner::Location dupe_loc = Scanner::Location::invalid(); 4401 Scanner::Location dupe_loc = Scanner::Location::invalid();
4358 Scanner::Location reserved_loc = Scanner::Location::invalid(); 4402 Scanner::Location reserved_loc = Scanner::Location::invalid();
4359 4403
(...skipping 217 matching lines...) Expand 10 before | Expand all | Expand 10 after
4577 body, 4621 body,
4578 materialized_literal_count, 4622 materialized_literal_count,
4579 expected_property_count, 4623 expected_property_count,
4580 handler_count, 4624 handler_count,
4581 only_simple_this_property_assignments, 4625 only_simple_this_property_assignments,
4582 this_property_assignments, 4626 this_property_assignments,
4583 num_parameters, 4627 num_parameters,
4584 duplicate_parameters, 4628 duplicate_parameters,
4585 type, 4629 type,
4586 FunctionLiteral::kIsFunction, 4630 FunctionLiteral::kIsFunction,
4587 parenthesized); 4631 parenthesized,
4632 generator);
4588 function_literal->set_function_token_position(function_token_position); 4633 function_literal->set_function_token_position(function_token_position);
4589 function_literal->set_ast_properties(&ast_properties); 4634 function_literal->set_ast_properties(&ast_properties);
4590 4635
4591 if (fni_ != NULL && should_infer_name) fni_->AddFunction(function_literal); 4636 if (fni_ != NULL && should_infer_name) fni_->AddFunction(function_literal);
4592 return function_literal; 4637 return function_literal;
4593 } 4638 }
4594 4639
4595 4640
4596 preparser::PreParser::PreParseResult Parser::LazyParseFunctionLiteral( 4641 preparser::PreParser::PreParseResult Parser::LazyParseFunctionLiteral(
4597 SingletonLogger* logger) { 4642 SingletonLogger* logger) {
4598 HistogramTimerScope preparse_scope(isolate()->counters()->pre_parse()); 4643 HistogramTimerScope preparse_scope(isolate()->counters()->pre_parse());
4599 ASSERT_EQ(Token::LBRACE, scanner().current_token()); 4644 ASSERT_EQ(Token::LBRACE, scanner().current_token());
4600 4645
4601 if (reusable_preparser_ == NULL) { 4646 if (reusable_preparser_ == NULL) {
4602 intptr_t stack_limit = isolate()->stack_guard()->real_climit(); 4647 intptr_t stack_limit = isolate()->stack_guard()->real_climit();
4603 bool do_allow_lazy = true; 4648 bool do_allow_lazy = true;
4604 reusable_preparser_ = new preparser::PreParser(&scanner_, 4649 reusable_preparser_ = new preparser::PreParser(&scanner_,
4605 NULL, 4650 NULL,
4606 stack_limit, 4651 stack_limit,
4607 do_allow_lazy, 4652 do_allow_lazy,
4608 allow_natives_syntax_, 4653 allow_natives_syntax_,
4609 allow_modules_); 4654 allow_modules_,
4655 FLAG_harmony_generators);
4610 } 4656 }
4611 preparser::PreParser::PreParseResult result = 4657 preparser::PreParser::PreParseResult result =
4612 reusable_preparser_->PreParseLazyFunction(top_scope_->language_mode(), 4658 reusable_preparser_->PreParseLazyFunction(top_scope_->language_mode(),
4659 is_generator(),
4613 logger); 4660 logger);
4614 return result; 4661 return result;
4615 } 4662 }
4616 4663
4617 4664
4618 Expression* Parser::ParseV8Intrinsic(bool* ok) { 4665 Expression* Parser::ParseV8Intrinsic(bool* ok) {
4619 // CallRuntime :: 4666 // CallRuntime ::
4620 // '%' Identifier Arguments 4667 // '%' Identifier Arguments
4621 4668
4622 Expect(Token::MOD, CHECK_OK); 4669 Expect(Token::MOD, CHECK_OK);
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
4665 4712
4666 // We have a valid intrinsics call or a call to a builtin. 4713 // We have a valid intrinsics call or a call to a builtin.
4667 return factory()->NewCallRuntime(name, function, args); 4714 return factory()->NewCallRuntime(name, function, args);
4668 } 4715 }
4669 4716
4670 4717
4671 bool Parser::peek_any_identifier() { 4718 bool Parser::peek_any_identifier() {
4672 Token::Value next = peek(); 4719 Token::Value next = peek();
4673 return next == Token::IDENTIFIER || 4720 return next == Token::IDENTIFIER ||
4674 next == Token::FUTURE_RESERVED_WORD || 4721 next == Token::FUTURE_RESERVED_WORD ||
4675 next == Token::FUTURE_STRICT_RESERVED_WORD; 4722 next == Token::FUTURE_STRICT_RESERVED_WORD ||
4723 next == Token::YIELD;
4676 } 4724 }
4677 4725
4678 4726
4679 void Parser::Consume(Token::Value token) { 4727 void Parser::Consume(Token::Value token) {
4680 Token::Value next = Next(); 4728 Token::Value next = Next();
4681 USE(next); 4729 USE(next);
4682 USE(token); 4730 USE(token);
4683 ASSERT(next == token); 4731 ASSERT(next == token);
4684 } 4732 }
4685 4733
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
4737 4785
4738 4786
4739 Literal* Parser::GetLiteralTheHole() { 4787 Literal* Parser::GetLiteralTheHole() {
4740 return factory()->NewLiteral(isolate()->factory()->the_hole_value()); 4788 return factory()->NewLiteral(isolate()->factory()->the_hole_value());
4741 } 4789 }
4742 4790
4743 4791
4744 // Parses an identifier that is valid for the current scope, in particular it 4792 // Parses an identifier that is valid for the current scope, in particular it
4745 // fails on strict mode future reserved keywords in a strict scope. 4793 // fails on strict mode future reserved keywords in a strict scope.
4746 Handle<String> Parser::ParseIdentifier(bool* ok) { 4794 Handle<String> Parser::ParseIdentifier(bool* ok) {
4747 if (!top_scope_->is_classic_mode()) { 4795 Token::Value next = Next();
4748 Expect(Token::IDENTIFIER, ok); 4796 if (next == Token::IDENTIFIER ||
4749 } else if (!Check(Token::IDENTIFIER)) { 4797 (top_scope_->is_classic_mode() &&
4750 Expect(Token::FUTURE_STRICT_RESERVED_WORD, ok); 4798 (next == Token::FUTURE_STRICT_RESERVED_WORD ||
4799 (next == Token::YIELD && !is_generator())))) {
4800 return GetSymbol(ok);
4801 } else {
4802 ReportUnexpectedToken(next);
4803 *ok = false;
4804 return Handle<String>();
4751 } 4805 }
4752 if (!*ok) return Handle<String>();
4753 return GetSymbol(ok);
4754 } 4806 }
4755 4807
4756 4808
4757 // Parses and identifier or a strict mode future reserved word, and indicate 4809 // Parses and identifier or a strict mode future reserved word, and indicate
4758 // whether it is strict mode future reserved. 4810 // whether it is strict mode future reserved.
4759 Handle<String> Parser::ParseIdentifierOrStrictReservedWord( 4811 Handle<String> Parser::ParseIdentifierOrStrictReservedWord(
4760 bool* is_strict_reserved, bool* ok) { 4812 bool* is_strict_reserved, bool* ok) {
4761 *is_strict_reserved = false; 4813 Token::Value next = Next();
4762 if (!Check(Token::IDENTIFIER)) { 4814 if (next == Token::IDENTIFIER) {
4763 Expect(Token::FUTURE_STRICT_RESERVED_WORD, ok); 4815 *is_strict_reserved = false;
4816 } else if (next == Token::FUTURE_STRICT_RESERVED_WORD ||
4817 (next == Token::YIELD && !is_generator())) {
4764 *is_strict_reserved = true; 4818 *is_strict_reserved = true;
4819 } else {
4820 ReportUnexpectedToken(next);
4821 *ok = false;
4822 return Handle<String>();
4765 } 4823 }
4766 if (!*ok) return Handle<String>();
4767 return GetSymbol(ok); 4824 return GetSymbol(ok);
4768 } 4825 }
4769 4826
4770 4827
4771 Handle<String> Parser::ParseIdentifierName(bool* ok) { 4828 Handle<String> Parser::ParseIdentifierName(bool* ok) {
4772 Token::Value next = Next(); 4829 Token::Value next = Next();
4773 if (next != Token::IDENTIFIER && 4830 if (next != Token::IDENTIFIER &&
4774 next != Token::FUTURE_RESERVED_WORD && 4831 next != Token::FUTURE_RESERVED_WORD &&
4775 next != Token::FUTURE_STRICT_RESERVED_WORD && 4832 next != Token::FUTURE_STRICT_RESERVED_WORD &&
4776 !Token::IsKeyword(next)) { 4833 !Token::IsKeyword(next)) {
(...skipping 1091 matching lines...) Expand 10 before | Expand all | Expand 10 after
5868 } 5925 }
5869 5926
5870 5927
5871 ScriptDataImpl* ParserApi::PreParse(Utf16CharacterStream* source, 5928 ScriptDataImpl* ParserApi::PreParse(Utf16CharacterStream* source,
5872 v8::Extension* extension, 5929 v8::Extension* extension,
5873 int flags) { 5930 int flags) {
5874 Handle<Script> no_script; 5931 Handle<Script> no_script;
5875 if (FLAG_lazy && (extension == NULL)) { 5932 if (FLAG_lazy && (extension == NULL)) {
5876 flags |= kAllowLazy; 5933 flags |= kAllowLazy;
5877 } 5934 }
5935 if (FLAG_harmony_generators) {
5936 flags |= kAllowGenerators;
5937 }
5878 CompleteParserRecorder recorder; 5938 CompleteParserRecorder recorder;
5879 return DoPreParse(source, flags, &recorder); 5939 return DoPreParse(source, flags, &recorder);
5880 } 5940 }
5881 5941
5882 5942
5883 bool RegExpParser::ParseRegExp(FlatStringReader* input, 5943 bool RegExpParser::ParseRegExp(FlatStringReader* input,
5884 bool multiline, 5944 bool multiline,
5885 RegExpCompileData* result, 5945 RegExpCompileData* result,
5886 Zone* zone) { 5946 Zone* zone) {
5887 ASSERT(result != NULL); 5947 ASSERT(result != NULL);
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
5942 ASSERT(info->isolate()->has_pending_exception()); 6002 ASSERT(info->isolate()->has_pending_exception());
5943 } else { 6003 } else {
5944 result = parser.ParseProgram(); 6004 result = parser.ParseProgram();
5945 } 6005 }
5946 } 6006 }
5947 info->SetFunction(result); 6007 info->SetFunction(result);
5948 return (result != NULL); 6008 return (result != NULL);
5949 } 6009 }
5950 6010
5951 } } // namespace v8::internal 6011 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « src/parser.h ('k') | src/preparser.h » ('j') | test/mjsunit/harmony/generators-parsing.js » ('J')

Powered by Google App Engine
This is Rietveld 408576698