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

Unified 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, 7 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 side-by-side diff with in-line comments
Download patch
Index: src/parser.cc
diff --git a/src/parser.cc b/src/parser.cc
index a08148c5974f3de8fb882935b774afd1e0d20abf..c91f31aebc3e92be7231002d4c50353f355630bb 100644
--- a/src/parser.cc
+++ b/src/parser.cc
@@ -682,8 +682,9 @@ Handle<String> ParserTraits::NextLiteralString(Scanner* scanner,
Expression* ParserTraits::ThisExpression(
Scope* scope,
- AstNodeFactory<AstConstructionVisitor>* factory) {
- return factory->NewVariableProxy(scope->receiver());
+ AstNodeFactory<AstConstructionVisitor>* factory,
+ int pos) {
+ return factory->NewVariableProxy(scope->receiver(), pos);
}
@@ -786,6 +787,7 @@ Parser::Parser(CompilationInfo* info)
set_allow_lazy(false); // Must be explicitly enabled.
set_allow_generators(FLAG_harmony_generators);
set_allow_for_of(FLAG_harmony_iteration);
+ set_allow_arrow_functions(FLAG_harmony_arrow_functions);
set_allow_harmony_numeric_literals(FLAG_harmony_numeric_literals);
}
@@ -922,7 +924,7 @@ FunctionLiteral* Parser::DoParseProgram(CompilationInfo* info,
FunctionLiteral::ANONYMOUS_EXPRESSION,
FunctionLiteral::kGlobalOrEval,
FunctionLiteral::kNotParenthesized,
- FunctionLiteral::kNotGenerator,
+ FunctionLiteral::kNormalFunction,
0);
result->set_ast_properties(factory()->visitor()->ast_properties());
result->set_dont_optimize_reason(
@@ -1009,14 +1011,22 @@ FunctionLiteral* Parser::ParseLazy(Utf16CharacterStream* source) {
? FunctionLiteral::ANONYMOUS_EXPRESSION
: FunctionLiteral::NAMED_EXPRESSION)
: FunctionLiteral::DECLARATION;
+ bool is_generator = shared_info->is_generator();
bool ok = true;
- result = ParseFunctionLiteral(name,
- Scanner::Location::invalid(),
- false, // Strict mode name already checked.
- shared_info->is_generator(),
- RelocInfo::kNoPosition,
- function_type,
- &ok);
+
+ if (shared_info->is_arrow()) {
marja 2014/05/26 12:46:06 Probably the same refactoring applies to SharedInf
+ ASSERT(!is_generator);
+ result =
+ reinterpret_cast<FunctionLiteral*>(ParseArrowFunctionLiteral(&ok));
+ } else {
+ result = ParseFunctionLiteral(name,
+ Scanner::Location::invalid(),
+ false, // Strict mode name already checked.
+ is_generator,
+ RelocInfo::kNoPosition,
+ function_type,
+ &ok);
+ }
// Make sure the results agree.
ASSERT(ok == (result != NULL));
}
@@ -3266,6 +3276,66 @@ Handle<FixedArray> CompileTimeValue::GetElements(Handle<FixedArray> value) {
}
+Vector<VariableProxy*> ParserTraits::ParameterListFromExpression(
+ Expression* expression, bool* ok) {
+
+ // Parsing the parameter list of an arrow function does not have parameters
+ // (as in: () => ...) returns NULL, so expression being NULL is not an error,
+ // but an valid empty parameter list.
+ if (expression == NULL)
+ return Vector<VariableProxy*>::empty();
+
+ const char* error_token = NULL;
+ Collector<VariableProxy*> collector;
+
+ while (expression->IsBinaryOperation()) {
+ BinaryOperation* binop = expression->AsBinaryOperation();
+
+ if (binop->op() != Token::COMMA) {
+ error_token = Token::String(expression->AsBinaryOperation()->op());
+ break;
+ }
+
+ expression = binop->right();
+ if (!expression->IsVariableProxy()) {
+ error_token = "";
+ break;
+ }
+ if (expression->AsVariableProxy()->is_this()) {
+ error_token = Token::String(Token::THIS);
+ break;
+ }
+
+ collector.Add(expression->AsVariableProxy());
+ expression = binop->left();
+ }
+
+ // No errors were found in the loop above, try to add the remaining item.
+ if (error_token == NULL) {
+ if (!expression->IsVariableProxy()) {
+ error_token = "";
+ } else if (expression->AsVariableProxy()->is_this()) {
+ error_token = Token::String(Token::THIS);
+ } else {
+ collector.Add(expression->AsVariableProxy());
+ return collector.ToVector();
+ }
+ }
+
+ // Report errors.
+ ASSERT_NE(error_token, NULL);
+ int error_pos = expression->position();
+ int error_token_len = strlen(error_token);
+ ParserTraits::ReportMessageAt(
+ Scanner::Location(error_pos,
+ error_pos + error_token_len ? error_token_len : 1),
+ "unexpected_token",
+ error_token);
+ *ok = false;
+ return Vector<VariableProxy*>::empty();
+}
+
+
FunctionLiteral* Parser::ParseFunctionLiteral(
Handle<String> function_name,
Scanner::Location function_name_location,
@@ -3513,9 +3583,9 @@ FunctionLiteral* Parser::ParseFunctionLiteral(
CheckConflictingVarDeclarations(scope, CHECK_OK);
}
- FunctionLiteral::IsGeneratorFlag generator = is_generator
- ? FunctionLiteral::kIsGenerator
- : FunctionLiteral::kNotGenerator;
+ FunctionLiteral::KindFlag kind = is_generator
+ ? FunctionLiteral::kGeneratorFunction
+ : FunctionLiteral::kNormalFunction;
FunctionLiteral* function_literal =
factory()->NewFunctionLiteral(function_name,
scope,
@@ -3528,7 +3598,7 @@ FunctionLiteral* Parser::ParseFunctionLiteral(
function_type,
FunctionLiteral::kIsFunction,
parenthesized,
- generator,
+ kind,
pos);
function_literal->set_function_token_position(function_token_pos);
function_literal->set_ast_properties(&ast_properties);
@@ -3690,6 +3760,7 @@ PreParser::PreParseResult Parser::ParseLazyFunctionBodyWithPreParser(
reusable_preparser_->set_allow_lazy(true);
reusable_preparser_->set_allow_generators(allow_generators());
reusable_preparser_->set_allow_for_of(allow_for_of());
+ reusable_preparser_->set_allow_arrow_functions(allow_arrow_functions());
reusable_preparser_->set_allow_harmony_numeric_literals(
allow_harmony_numeric_literals());
}

Powered by Google App Engine
This is Rietveld 408576698