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

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: Extra parens in parameter lists now recognized, no longer segfaults on "()" 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
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 "v8.h" 5 #include "v8.h"
6 6
7 #include "api.h" 7 #include "api.h"
8 #include "ast.h" 8 #include "ast.h"
9 #include "bootstrapper.h" 9 #include "bootstrapper.h"
10 #include "char-predicates-inl.h" 10 #include "char-predicates-inl.h"
(...skipping 664 matching lines...) Expand 10 before | Expand all | Expand 10 after
675 675
676 676
677 Handle<String> ParserTraits::NextLiteralString(Scanner* scanner, 677 Handle<String> ParserTraits::NextLiteralString(Scanner* scanner,
678 PretenureFlag tenured) { 678 PretenureFlag tenured) {
679 return scanner->AllocateNextLiteralString(parser_->isolate(), tenured); 679 return scanner->AllocateNextLiteralString(parser_->isolate(), tenured);
680 } 680 }
681 681
682 682
683 Expression* ParserTraits::ThisExpression( 683 Expression* ParserTraits::ThisExpression(
684 Scope* scope, 684 Scope* scope,
685 AstNodeFactory<AstConstructionVisitor>* factory) { 685 AstNodeFactory<AstConstructionVisitor>* factory,
686 return factory->NewVariableProxy(scope->receiver()); 686 int pos) {
687 return factory->NewVariableProxy(scope->receiver(), pos);
687 } 688 }
688 689
689 690
690 Literal* ParserTraits::ExpressionFromLiteral( 691 Literal* ParserTraits::ExpressionFromLiteral(
691 Token::Value token, int pos, 692 Token::Value token, int pos,
692 Scanner* scanner, 693 Scanner* scanner,
693 AstNodeFactory<AstConstructionVisitor>* factory) { 694 AstNodeFactory<AstConstructionVisitor>* factory) {
694 Factory* isolate_factory = parser_->isolate()->factory(); 695 Factory* isolate_factory = parser_->isolate()->factory();
695 switch (token) { 696 switch (token) {
696 case Token::NULL_LITERAL: 697 case Token::NULL_LITERAL:
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
779 pending_error_message_(NULL), 780 pending_error_message_(NULL),
780 pending_error_char_arg_(NULL) { 781 pending_error_char_arg_(NULL) {
781 ASSERT(!script_.is_null()); 782 ASSERT(!script_.is_null());
782 isolate_->set_ast_node_id(0); 783 isolate_->set_ast_node_id(0);
783 set_allow_harmony_scoping(!info->is_native() && FLAG_harmony_scoping); 784 set_allow_harmony_scoping(!info->is_native() && FLAG_harmony_scoping);
784 set_allow_modules(!info->is_native() && FLAG_harmony_modules); 785 set_allow_modules(!info->is_native() && FLAG_harmony_modules);
785 set_allow_natives_syntax(FLAG_allow_natives_syntax || info->is_native()); 786 set_allow_natives_syntax(FLAG_allow_natives_syntax || info->is_native());
786 set_allow_lazy(false); // Must be explicitly enabled. 787 set_allow_lazy(false); // Must be explicitly enabled.
787 set_allow_generators(FLAG_harmony_generators); 788 set_allow_generators(FLAG_harmony_generators);
788 set_allow_for_of(FLAG_harmony_iteration); 789 set_allow_for_of(FLAG_harmony_iteration);
790 set_allow_arrow_functions(FLAG_harmony_arrow_functions);
789 set_allow_harmony_numeric_literals(FLAG_harmony_numeric_literals); 791 set_allow_harmony_numeric_literals(FLAG_harmony_numeric_literals);
790 } 792 }
791 793
792 794
793 FunctionLiteral* Parser::ParseProgram() { 795 FunctionLiteral* Parser::ParseProgram() {
794 // TODO(bmeurer): We temporarily need to pass allow_nesting = true here, 796 // TODO(bmeurer): We temporarily need to pass allow_nesting = true here,
795 // see comment for HistogramTimerScope class. 797 // see comment for HistogramTimerScope class.
796 HistogramTimerScope timer_scope(isolate()->counters()->parse(), true); 798 HistogramTimerScope timer_scope(isolate()->counters()->parse(), true);
797 Handle<String> source(String::cast(script_->source())); 799 Handle<String> source(String::cast(script_->source()));
798 isolate()->counters()->total_parse_size()->Increment(source->length()); 800 isolate()->counters()->total_parse_size()->Increment(source->length());
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after
915 scope_, 917 scope_,
916 body, 918 body,
917 function_state.materialized_literal_count(), 919 function_state.materialized_literal_count(),
918 function_state.expected_property_count(), 920 function_state.expected_property_count(),
919 function_state.handler_count(), 921 function_state.handler_count(),
920 0, 922 0,
921 FunctionLiteral::kNoDuplicateParameters, 923 FunctionLiteral::kNoDuplicateParameters,
922 FunctionLiteral::ANONYMOUS_EXPRESSION, 924 FunctionLiteral::ANONYMOUS_EXPRESSION,
923 FunctionLiteral::kGlobalOrEval, 925 FunctionLiteral::kGlobalOrEval,
924 FunctionLiteral::kNotParenthesized, 926 FunctionLiteral::kNotParenthesized,
925 FunctionLiteral::kNotGenerator, 927 FunctionLiteral::kNormalFunction,
926 0); 928 0);
927 result->set_ast_properties(factory()->visitor()->ast_properties()); 929 result->set_ast_properties(factory()->visitor()->ast_properties());
928 result->set_dont_optimize_reason( 930 result->set_dont_optimize_reason(
929 factory()->visitor()->dont_optimize_reason()); 931 factory()->visitor()->dont_optimize_reason());
930 } else if (stack_overflow()) { 932 } else if (stack_overflow()) {
931 isolate()->StackOverflow(); 933 isolate()->StackOverflow();
932 } else { 934 } else {
933 ThrowPendingError(); 935 ThrowPendingError();
934 } 936 }
935 } 937 }
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
1002 original_scope_ = scope; 1004 original_scope_ = scope;
1003 FunctionState function_state(&function_state_, &scope_, scope, zone()); 1005 FunctionState function_state(&function_state_, &scope_, scope, zone());
1004 ASSERT(scope->strict_mode() == SLOPPY || info()->strict_mode() == STRICT); 1006 ASSERT(scope->strict_mode() == SLOPPY || info()->strict_mode() == STRICT);
1005 ASSERT(info()->strict_mode() == shared_info->strict_mode()); 1007 ASSERT(info()->strict_mode() == shared_info->strict_mode());
1006 scope->SetStrictMode(shared_info->strict_mode()); 1008 scope->SetStrictMode(shared_info->strict_mode());
1007 FunctionLiteral::FunctionType function_type = shared_info->is_expression() 1009 FunctionLiteral::FunctionType function_type = shared_info->is_expression()
1008 ? (shared_info->is_anonymous() 1010 ? (shared_info->is_anonymous()
1009 ? FunctionLiteral::ANONYMOUS_EXPRESSION 1011 ? FunctionLiteral::ANONYMOUS_EXPRESSION
1010 : FunctionLiteral::NAMED_EXPRESSION) 1012 : FunctionLiteral::NAMED_EXPRESSION)
1011 : FunctionLiteral::DECLARATION; 1013 : FunctionLiteral::DECLARATION;
1014 bool is_generator = shared_info->is_generator();
1012 bool ok = true; 1015 bool ok = true;
1013 result = ParseFunctionLiteral(name, 1016
1014 Scanner::Location::invalid(), 1017 if (shared_info->is_arrow()) {
marja 2014/05/26 12:46:06 Probably the same refactoring applies to SharedInf
1015 false, // Strict mode name already checked. 1018 ASSERT(!is_generator);
1016 shared_info->is_generator(), 1019 result =
1017 RelocInfo::kNoPosition, 1020 reinterpret_cast<FunctionLiteral*>(ParseArrowFunctionLiteral(&ok));
1018 function_type, 1021 } else {
1019 &ok); 1022 result = ParseFunctionLiteral(name,
1023 Scanner::Location::invalid(),
1024 false, // Strict mode name already checked.
1025 is_generator,
1026 RelocInfo::kNoPosition,
1027 function_type,
1028 &ok);
1029 }
1020 // Make sure the results agree. 1030 // Make sure the results agree.
1021 ASSERT(ok == (result != NULL)); 1031 ASSERT(ok == (result != NULL));
1022 } 1032 }
1023 1033
1024 // Make sure the target stack is empty. 1034 // Make sure the target stack is empty.
1025 ASSERT(target_stack_ == NULL); 1035 ASSERT(target_stack_ == NULL);
1026 1036
1027 if (result == NULL) { 1037 if (result == NULL) {
1028 if (stack_overflow()) { 1038 if (stack_overflow()) {
1029 isolate()->StackOverflow(); 1039 isolate()->StackOverflow();
(...skipping 2229 matching lines...) Expand 10 before | Expand all | Expand 10 after
3259 Smi* literal_type = Smi::cast(value->get(kLiteralTypeSlot)); 3269 Smi* literal_type = Smi::cast(value->get(kLiteralTypeSlot));
3260 return static_cast<LiteralType>(literal_type->value()); 3270 return static_cast<LiteralType>(literal_type->value());
3261 } 3271 }
3262 3272
3263 3273
3264 Handle<FixedArray> CompileTimeValue::GetElements(Handle<FixedArray> value) { 3274 Handle<FixedArray> CompileTimeValue::GetElements(Handle<FixedArray> value) {
3265 return Handle<FixedArray>(FixedArray::cast(value->get(kElementsSlot))); 3275 return Handle<FixedArray>(FixedArray::cast(value->get(kElementsSlot)));
3266 } 3276 }
3267 3277
3268 3278
3279 Vector<VariableProxy*> ParserTraits::ParameterListFromExpression(
3280 Expression* expression, bool* ok) {
3281
3282 // Parsing the parameter list of an arrow function does not have parameters
3283 // (as in: () => ...) returns NULL, so expression being NULL is not an error,
3284 // but an valid empty parameter list.
3285 if (expression == NULL)
3286 return Vector<VariableProxy*>::empty();
3287
3288 const char* error_token = NULL;
3289 Collector<VariableProxy*> collector;
3290
3291 while (expression->IsBinaryOperation()) {
3292 BinaryOperation* binop = expression->AsBinaryOperation();
3293
3294 if (binop->op() != Token::COMMA) {
3295 error_token = Token::String(expression->AsBinaryOperation()->op());
3296 break;
3297 }
3298
3299 expression = binop->right();
3300 if (!expression->IsVariableProxy()) {
3301 error_token = "";
3302 break;
3303 }
3304 if (expression->AsVariableProxy()->is_this()) {
3305 error_token = Token::String(Token::THIS);
3306 break;
3307 }
3308
3309 collector.Add(expression->AsVariableProxy());
3310 expression = binop->left();
3311 }
3312
3313 // No errors were found in the loop above, try to add the remaining item.
3314 if (error_token == NULL) {
3315 if (!expression->IsVariableProxy()) {
3316 error_token = "";
3317 } else if (expression->AsVariableProxy()->is_this()) {
3318 error_token = Token::String(Token::THIS);
3319 } else {
3320 collector.Add(expression->AsVariableProxy());
3321 return collector.ToVector();
3322 }
3323 }
3324
3325 // Report errors.
3326 ASSERT_NE(error_token, NULL);
3327 int error_pos = expression->position();
3328 int error_token_len = strlen(error_token);
3329 ParserTraits::ReportMessageAt(
3330 Scanner::Location(error_pos,
3331 error_pos + error_token_len ? error_token_len : 1),
3332 "unexpected_token",
3333 error_token);
3334 *ok = false;
3335 return Vector<VariableProxy*>::empty();
3336 }
3337
3338
3269 FunctionLiteral* Parser::ParseFunctionLiteral( 3339 FunctionLiteral* Parser::ParseFunctionLiteral(
3270 Handle<String> function_name, 3340 Handle<String> function_name,
3271 Scanner::Location function_name_location, 3341 Scanner::Location function_name_location,
3272 bool name_is_strict_reserved, 3342 bool name_is_strict_reserved,
3273 bool is_generator, 3343 bool is_generator,
3274 int function_token_pos, 3344 int function_token_pos,
3275 FunctionLiteral::FunctionType function_type, 3345 FunctionLiteral::FunctionType function_type,
3276 bool* ok) { 3346 bool* ok) {
3277 // Function :: 3347 // Function ::
3278 // '(' FormalParameterList? ')' '{' FunctionBody '}' 3348 // '(' FormalParameterList? ')' '{' FunctionBody '}'
(...skipping 227 matching lines...) Expand 10 before | Expand all | Expand 10 after
3506 CHECK_OK); 3576 CHECK_OK);
3507 } 3577 }
3508 ast_properties = *factory()->visitor()->ast_properties(); 3578 ast_properties = *factory()->visitor()->ast_properties();
3509 dont_optimize_reason = factory()->visitor()->dont_optimize_reason(); 3579 dont_optimize_reason = factory()->visitor()->dont_optimize_reason();
3510 } 3580 }
3511 3581
3512 if (allow_harmony_scoping() && strict_mode() == STRICT) { 3582 if (allow_harmony_scoping() && strict_mode() == STRICT) {
3513 CheckConflictingVarDeclarations(scope, CHECK_OK); 3583 CheckConflictingVarDeclarations(scope, CHECK_OK);
3514 } 3584 }
3515 3585
3516 FunctionLiteral::IsGeneratorFlag generator = is_generator 3586 FunctionLiteral::KindFlag kind = is_generator
3517 ? FunctionLiteral::kIsGenerator 3587 ? FunctionLiteral::kGeneratorFunction
3518 : FunctionLiteral::kNotGenerator; 3588 : FunctionLiteral::kNormalFunction;
3519 FunctionLiteral* function_literal = 3589 FunctionLiteral* function_literal =
3520 factory()->NewFunctionLiteral(function_name, 3590 factory()->NewFunctionLiteral(function_name,
3521 scope, 3591 scope,
3522 body, 3592 body,
3523 materialized_literal_count, 3593 materialized_literal_count,
3524 expected_property_count, 3594 expected_property_count,
3525 handler_count, 3595 handler_count,
3526 num_parameters, 3596 num_parameters,
3527 duplicate_parameters, 3597 duplicate_parameters,
3528 function_type, 3598 function_type,
3529 FunctionLiteral::kIsFunction, 3599 FunctionLiteral::kIsFunction,
3530 parenthesized, 3600 parenthesized,
3531 generator, 3601 kind,
3532 pos); 3602 pos);
3533 function_literal->set_function_token_position(function_token_pos); 3603 function_literal->set_function_token_position(function_token_pos);
3534 function_literal->set_ast_properties(&ast_properties); 3604 function_literal->set_ast_properties(&ast_properties);
3535 function_literal->set_dont_optimize_reason(dont_optimize_reason); 3605 function_literal->set_dont_optimize_reason(dont_optimize_reason);
3536 3606
3537 if (fni_ != NULL && should_infer_name) fni_->AddFunction(function_literal); 3607 if (fni_ != NULL && should_infer_name) fni_->AddFunction(function_literal);
3538 return function_literal; 3608 return function_literal;
3539 } 3609 }
3540 3610
3541 3611
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after
3683 3753
3684 if (reusable_preparser_ == NULL) { 3754 if (reusable_preparser_ == NULL) {
3685 intptr_t stack_limit = isolate()->stack_guard()->real_climit(); 3755 intptr_t stack_limit = isolate()->stack_guard()->real_climit();
3686 reusable_preparser_ = new PreParser(&scanner_, NULL, stack_limit); 3756 reusable_preparser_ = new PreParser(&scanner_, NULL, stack_limit);
3687 reusable_preparser_->set_allow_harmony_scoping(allow_harmony_scoping()); 3757 reusable_preparser_->set_allow_harmony_scoping(allow_harmony_scoping());
3688 reusable_preparser_->set_allow_modules(allow_modules()); 3758 reusable_preparser_->set_allow_modules(allow_modules());
3689 reusable_preparser_->set_allow_natives_syntax(allow_natives_syntax()); 3759 reusable_preparser_->set_allow_natives_syntax(allow_natives_syntax());
3690 reusable_preparser_->set_allow_lazy(true); 3760 reusable_preparser_->set_allow_lazy(true);
3691 reusable_preparser_->set_allow_generators(allow_generators()); 3761 reusable_preparser_->set_allow_generators(allow_generators());
3692 reusable_preparser_->set_allow_for_of(allow_for_of()); 3762 reusable_preparser_->set_allow_for_of(allow_for_of());
3763 reusable_preparser_->set_allow_arrow_functions(allow_arrow_functions());
3693 reusable_preparser_->set_allow_harmony_numeric_literals( 3764 reusable_preparser_->set_allow_harmony_numeric_literals(
3694 allow_harmony_numeric_literals()); 3765 allow_harmony_numeric_literals());
3695 } 3766 }
3696 PreParser::PreParseResult result = 3767 PreParser::PreParseResult result =
3697 reusable_preparser_->PreParseLazyFunction(strict_mode(), 3768 reusable_preparser_->PreParseLazyFunction(strict_mode(),
3698 is_generator(), 3769 is_generator(),
3699 logger); 3770 logger);
3700 return result; 3771 return result;
3701 } 3772 }
3702 3773
(...skipping 1078 matching lines...) Expand 10 before | Expand all | Expand 10 after
4781 ASSERT(info()->isolate()->has_pending_exception()); 4852 ASSERT(info()->isolate()->has_pending_exception());
4782 } else { 4853 } else {
4783 result = ParseProgram(); 4854 result = ParseProgram();
4784 } 4855 }
4785 } 4856 }
4786 info()->SetFunction(result); 4857 info()->SetFunction(result);
4787 return (result != NULL); 4858 return (result != NULL);
4788 } 4859 }
4789 4860
4790 } } // namespace v8::internal 4861 } } // namespace v8::internal
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698