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

Side by Side Diff: src/parser.cc

Issue 160073006: Implement handling of arrow functions in the parser (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Version with parsing code only, tests into test-parsing.cc Created 6 years, 6 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') | src/preparser.h » ('j') | src/scanner.h » ('J')
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 #include "src/v8.h" 5 #include "src/v8.h"
6 6
7 #include "src/api.h" 7 #include "src/api.h"
8 #include "src/ast.h" 8 #include "src/ast.h"
9 #include "src/bootstrapper.h" 9 #include "src/bootstrapper.h"
10 #include "src/char-predicates-inl.h" 10 #include "src/char-predicates-inl.h"
(...skipping 660 matching lines...) Expand 10 before | Expand all | Expand 10 after
671 } 671 }
672 672
673 673
674 const AstString* ParserTraits::GetNextSymbol(Scanner* scanner) { 674 const AstString* ParserTraits::GetNextSymbol(Scanner* scanner) {
675 return parser_->scanner()->NextSymbol(parser_->ast_value_factory_); 675 return parser_->scanner()->NextSymbol(parser_->ast_value_factory_);
676 } 676 }
677 677
678 678
679 Expression* ParserTraits::ThisExpression( 679 Expression* ParserTraits::ThisExpression(
680 Scope* scope, 680 Scope* scope,
681 AstNodeFactory<AstConstructionVisitor>* factory) { 681 AstNodeFactory<AstConstructionVisitor>* factory,
682 return factory->NewVariableProxy(scope->receiver()); 682 int pos) {
683 return factory->NewVariableProxy(scope->receiver(), pos);
683 } 684 }
684 685
685 686
686 Literal* ParserTraits::ExpressionFromLiteral( 687 Literal* ParserTraits::ExpressionFromLiteral(
687 Token::Value token, int pos, 688 Token::Value token, int pos,
688 Scanner* scanner, 689 Scanner* scanner,
689 AstNodeFactory<AstConstructionVisitor>* factory) { 690 AstNodeFactory<AstConstructionVisitor>* factory) {
690 switch (token) { 691 switch (token) {
691 case Token::NULL_LITERAL: 692 case Token::NULL_LITERAL:
692 return factory->NewNullLiteral(pos); 693 return factory->NewNullLiteral(pos);
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
775 pending_error_arg_(NULL), 776 pending_error_arg_(NULL),
776 pending_error_char_arg_(NULL) { 777 pending_error_char_arg_(NULL) {
777 ASSERT(!script_.is_null()); 778 ASSERT(!script_.is_null());
778 isolate_->set_ast_node_id(0); 779 isolate_->set_ast_node_id(0);
779 set_allow_harmony_scoping(!info->is_native() && FLAG_harmony_scoping); 780 set_allow_harmony_scoping(!info->is_native() && FLAG_harmony_scoping);
780 set_allow_modules(!info->is_native() && FLAG_harmony_modules); 781 set_allow_modules(!info->is_native() && FLAG_harmony_modules);
781 set_allow_natives_syntax(FLAG_allow_natives_syntax || info->is_native()); 782 set_allow_natives_syntax(FLAG_allow_natives_syntax || info->is_native());
782 set_allow_lazy(false); // Must be explicitly enabled. 783 set_allow_lazy(false); // Must be explicitly enabled.
783 set_allow_generators(FLAG_harmony_generators); 784 set_allow_generators(FLAG_harmony_generators);
784 set_allow_for_of(FLAG_harmony_iteration); 785 set_allow_for_of(FLAG_harmony_iteration);
786 set_allow_arrow_functions(FLAG_harmony_arrow_functions);
785 set_allow_harmony_numeric_literals(FLAG_harmony_numeric_literals); 787 set_allow_harmony_numeric_literals(FLAG_harmony_numeric_literals);
786 } 788 }
787 789
788 790
789 FunctionLiteral* Parser::ParseProgram() { 791 FunctionLiteral* Parser::ParseProgram() {
790 // TODO(bmeurer): We temporarily need to pass allow_nesting = true here, 792 // TODO(bmeurer): We temporarily need to pass allow_nesting = true here,
791 // see comment for HistogramTimerScope class. 793 // see comment for HistogramTimerScope class.
792 HistogramTimerScope timer_scope(isolate()->counters()->parse(), true); 794 HistogramTimerScope timer_scope(isolate()->counters()->parse(), true);
793 Handle<String> source(String::cast(script_->source())); 795 Handle<String> source(String::cast(script_->source()));
794 isolate()->counters()->total_parse_size()->Increment(source->length()); 796 isolate()->counters()->total_parse_size()->Increment(source->length());
(...skipping 2494 matching lines...) Expand 10 before | Expand all | Expand 10 after
3289 Smi* literal_type = Smi::cast(value->get(kLiteralTypeSlot)); 3291 Smi* literal_type = Smi::cast(value->get(kLiteralTypeSlot));
3290 return static_cast<LiteralType>(literal_type->value()); 3292 return static_cast<LiteralType>(literal_type->value());
3291 } 3293 }
3292 3294
3293 3295
3294 Handle<FixedArray> CompileTimeValue::GetElements(Handle<FixedArray> value) { 3296 Handle<FixedArray> CompileTimeValue::GetElements(Handle<FixedArray> value) {
3295 return Handle<FixedArray>(FixedArray::cast(value->get(kElementsSlot))); 3297 return Handle<FixedArray>(FixedArray::cast(value->get(kElementsSlot)));
3296 } 3298 }
3297 3299
3298 3300
3301 Vector<VariableProxy*> ParserTraits::ParameterListFromExpression(
3302 Expression* expression, bool* ok) {
3303
3304 // Parsing the parameter list of an arrow function does not have parameters
3305 // (as in: () => ...) returns NULL, so expression being NULL is not an error,
3306 // but an valid empty parameter list.
3307 if (expression == NULL)
3308 return Vector<VariableProxy*>::empty();
3309
3310 const char* error_token = NULL;
3311 Collector<VariableProxy*> collector;
3312
3313 while (expression->IsBinaryOperation()) {
3314 BinaryOperation* binop = expression->AsBinaryOperation();
3315
3316 if (binop->op() != Token::COMMA) {
3317 error_token = Token::String(expression->AsBinaryOperation()->op());
3318 break;
3319 }
3320
3321 expression = binop->right();
3322 if (!expression->IsVariableProxy()) {
3323 error_token = "";
marja 2014/06/17 11:47:37 In this case, don't we get an incorrect error end
3324 break;
3325 }
3326 if (expression->AsVariableProxy()->is_this()) {
3327 error_token = Token::String(Token::THIS);
3328 break;
3329 }
3330
3331 collector.Add(expression->AsVariableProxy());
3332 expression = binop->left();
3333 }
3334
3335 // No errors were found in the loop above, try to add the remaining item.
3336 if (error_token == NULL) {
3337 if (!expression->IsVariableProxy()) {
3338 error_token = "";
3339 } else if (expression->AsVariableProxy()->is_this()) {
3340 error_token = Token::String(Token::THIS);
3341 } else {
3342 collector.Add(expression->AsVariableProxy());
3343 return collector.ToVector();
3344 }
3345 }
3346
3347 // Report errors.
3348 ASSERT_NE(error_token, NULL);
3349 int error_pos = expression->position();
3350 int error_token_len = strlen(error_token);
3351 ParserTraits::ReportMessageAt(
3352 Scanner::Location(error_pos,
3353 error_pos + error_token_len ? error_token_len : 1),
3354 "unexpected_token",
3355 error_token);
3356 *ok = false;
3357 return Vector<VariableProxy*>::empty();
3358 }
3359
3360
3299 FunctionLiteral* Parser::ParseFunctionLiteral( 3361 FunctionLiteral* Parser::ParseFunctionLiteral(
3300 const AstString* function_name, 3362 const AstString* function_name,
3301 Scanner::Location function_name_location, 3363 Scanner::Location function_name_location,
3302 bool name_is_strict_reserved, 3364 bool name_is_strict_reserved,
3303 bool is_generator, 3365 bool is_generator,
3304 int function_token_pos, 3366 int function_token_pos,
3305 FunctionLiteral::FunctionType function_type, 3367 FunctionLiteral::FunctionType function_type,
3306 bool* ok) { 3368 bool* ok) {
3307 // Function :: 3369 // Function ::
3308 // '(' FormalParameterList? ')' '{' FunctionBody '}' 3370 // '(' FormalParameterList? ')' '{' FunctionBody '}'
(...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after
3499 SkipLazyFunctionBody(function_name, &materialized_literal_count, 3561 SkipLazyFunctionBody(function_name, &materialized_literal_count,
3500 &expected_property_count, CHECK_OK); 3562 &expected_property_count, CHECK_OK);
3501 } else { 3563 } else {
3502 body = ParseEagerFunctionBody(function_name, pos, fvar, fvar_init_op, 3564 body = ParseEagerFunctionBody(function_name, pos, fvar, fvar_init_op,
3503 is_generator, CHECK_OK); 3565 is_generator, CHECK_OK);
3504 materialized_literal_count = function_state.materialized_literal_count(); 3566 materialized_literal_count = function_state.materialized_literal_count();
3505 expected_property_count = function_state.expected_property_count(); 3567 expected_property_count = function_state.expected_property_count();
3506 handler_count = function_state.handler_count(); 3568 handler_count = function_state.handler_count();
3507 } 3569 }
3508 3570
3509 // Validate strict mode. We can do this only after parsing the function, 3571 // Validate strict mode.
3510 // since the function can declare itself strict.
3511 if (strict_mode() == STRICT) { 3572 if (strict_mode() == STRICT) {
3512 if (IsEvalOrArguments(function_name)) { 3573 CheckStrictFunctionNameAndParameters(function_name,
3513 ReportMessageAt(function_name_location, "strict_eval_arguments"); 3574 name_is_strict_reserved,
3514 *ok = false; 3575 function_name_location,
3515 return NULL; 3576 eval_args_error_log,
3516 } 3577 dupe_error_loc,
3517 if (name_is_strict_reserved) { 3578 reserved_loc,
3518 ReportMessageAt(function_name_location, "unexpected_strict_reserved"); 3579 CHECK_OK);
3519 *ok = false;
3520 return NULL;
3521 }
3522 if (eval_args_error_log.IsValid()) {
3523 ReportMessageAt(eval_args_error_log, "strict_eval_arguments");
3524 *ok = false;
3525 return NULL;
3526 }
3527 if (dupe_error_loc.IsValid()) {
3528 ReportMessageAt(dupe_error_loc, "strict_param_dupe");
3529 *ok = false;
3530 return NULL;
3531 }
3532 if (reserved_loc.IsValid()) {
3533 ReportMessageAt(reserved_loc, "unexpected_strict_reserved");
3534 *ok = false;
3535 return NULL;
3536 }
3537 CheckOctalLiteral(scope->start_position(), 3580 CheckOctalLiteral(scope->start_position(),
3538 scope->end_position(), 3581 scope->end_position(),
3539 CHECK_OK); 3582 CHECK_OK);
3540 } 3583 }
3541 ast_properties = *factory()->visitor()->ast_properties(); 3584 ast_properties = *factory()->visitor()->ast_properties();
3542 dont_optimize_reason = factory()->visitor()->dont_optimize_reason(); 3585 dont_optimize_reason = factory()->visitor()->dont_optimize_reason();
3543 } 3586 }
3544 3587
3545 if (allow_harmony_scoping() && strict_mode() == STRICT) { 3588 if (allow_harmony_scoping() && strict_mode() == STRICT) {
3546 CheckConflictingVarDeclarations(scope, CHECK_OK); 3589 CheckConflictingVarDeclarations(scope, CHECK_OK);
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after
3717 3760
3718 if (reusable_preparser_ == NULL) { 3761 if (reusable_preparser_ == NULL) {
3719 intptr_t stack_limit = isolate()->stack_guard()->real_climit(); 3762 intptr_t stack_limit = isolate()->stack_guard()->real_climit();
3720 reusable_preparser_ = new PreParser(&scanner_, NULL, stack_limit); 3763 reusable_preparser_ = new PreParser(&scanner_, NULL, stack_limit);
3721 reusable_preparser_->set_allow_harmony_scoping(allow_harmony_scoping()); 3764 reusable_preparser_->set_allow_harmony_scoping(allow_harmony_scoping());
3722 reusable_preparser_->set_allow_modules(allow_modules()); 3765 reusable_preparser_->set_allow_modules(allow_modules());
3723 reusable_preparser_->set_allow_natives_syntax(allow_natives_syntax()); 3766 reusable_preparser_->set_allow_natives_syntax(allow_natives_syntax());
3724 reusable_preparser_->set_allow_lazy(true); 3767 reusable_preparser_->set_allow_lazy(true);
3725 reusable_preparser_->set_allow_generators(allow_generators()); 3768 reusable_preparser_->set_allow_generators(allow_generators());
3726 reusable_preparser_->set_allow_for_of(allow_for_of()); 3769 reusable_preparser_->set_allow_for_of(allow_for_of());
3770 reusable_preparser_->set_allow_arrow_functions(allow_arrow_functions());
3727 reusable_preparser_->set_allow_harmony_numeric_literals( 3771 reusable_preparser_->set_allow_harmony_numeric_literals(
3728 allow_harmony_numeric_literals()); 3772 allow_harmony_numeric_literals());
3729 } 3773 }
3730 PreParser::PreParseResult result = 3774 PreParser::PreParseResult result =
3731 reusable_preparser_->PreParseLazyFunction(strict_mode(), 3775 reusable_preparser_->PreParseLazyFunction(strict_mode(),
3732 is_generator(), 3776 is_generator(),
3733 logger); 3777 logger);
3734 return result; 3778 return result;
3735 } 3779 }
3736 3780
(...skipping 1096 matching lines...) Expand 10 before | Expand all | Expand 10 after
4833 ast_value_factory_->Internalize(isolate()); 4877 ast_value_factory_->Internalize(isolate());
4834 // info takes ownership of ast_value_factory_. 4878 // info takes ownership of ast_value_factory_.
4835 if (info()->ast_value_factory() == NULL) { 4879 if (info()->ast_value_factory() == NULL) {
4836 info()->SetAstValueFactory(ast_value_factory_); 4880 info()->SetAstValueFactory(ast_value_factory_);
4837 } 4881 }
4838 ast_value_factory_ = NULL; 4882 ast_value_factory_ = NULL;
4839 return (result != NULL); 4883 return (result != NULL);
4840 } 4884 }
4841 4885
4842 } } // namespace v8::internal 4886 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « src/parser.h ('k') | src/preparser.h » ('j') | src/scanner.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698