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

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: Rebased after latest changes in runtime.{h,cc} Created 6 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « src/parser.h ('k') | src/preparser.h » ('j') | src/preparser.h » ('J')
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/parser.cc
diff --git a/src/parser.cc b/src/parser.cc
index c535704f02461b667d289c68caf7da3b113abfc6..f995bdda512245c8ae2178a05a4fbafdb53cd02a 100644
--- a/src/parser.cc
+++ b/src/parser.cc
@@ -757,8 +757,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);
}
@@ -859,6 +860,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);
}
@@ -994,6 +996,7 @@ FunctionLiteral* Parser::DoParseProgram(CompilationInfo* info,
FunctionLiteral::kGlobalOrEval,
FunctionLiteral::kNotParenthesized,
FunctionLiteral::kNotGenerator,
+ FunctionLiteral::kNotArrow,
0);
result->set_ast_properties(factory()->visitor()->ast_properties());
result->set_slot_processor(factory()->visitor()->slot_processor());
@@ -1079,14 +1082,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()) {
+ ASSERT(!is_generator);
+ result = reinterpret_cast<FunctionLiteral*>(
+ ParseArrowFunctionLiteral(shared_info->start_position(), NULL, &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));
}
@@ -3188,6 +3199,59 @@ Handle<FixedArray> CompileTimeValue::GetElements(Handle<FixedArray> value) {
}
+Vector<VariableProxy*> ParserTraits::ParameterListFromExpression(
marja 2014/04/24 08:08:06 It looks like this doesn't check that the expressi
aperez 2014/04/24 12:38:37 The example you mention would be a comma-expressio
marja 2014/04/29 09:38:44 Hmm, okay. It would be good to add test cases to m
+ 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 = Token::String(Token::THIS);
+ int error_pos = RelocInfo::kNoPosition;
+
+ Collector<VariableProxy*> collector;
+ while (expression->IsBinaryOperation()) {
+ BinaryOperation* binop = expression->AsBinaryOperation();
+
+ if (binop->op() != Token::COMMA) {
+ error_token = Token::String(expression->AsBinaryOperation()->op());
+ goto invalid_token;
marja 2014/04/24 08:08:06 Afaics, it would be possible to write this in a go
aperez 2014/04/24 12:38:37 Sure thing, I will do that. The function will not
+ }
+
+ expression = binop->right();
+ if (!expression->IsVariableProxy())
+ goto not_variable_proxy;
+ if (expression->AsVariableProxy()->is_this())
+ goto invalid_token;
+
+ collector.Add(expression->AsVariableProxy());
+ expression = binop->left();
+ }
+
+ if (!expression->IsVariableProxy())
+ goto not_variable_proxy;
+ if (expression->AsVariableProxy()->is_this())
+ goto invalid_token;
+
+ collector.Add(expression->AsVariableProxy());
+ return collector.ToVector();
+
+ not_variable_proxy:
+ error_token = "";
+ // fall-through
+ invalid_token:
+ error_pos = expression->position();
+ ParserTraits::ReportMessageAt(
+ Scanner::Location(error_pos, error_pos + strlen(error_token)),
+ "unexpected_token",
+ Vector<const char*>(&error_token, *error_token ? 1 : 0));
+ *ok = false;
+ return Vector<VariableProxy*>::empty();
+}
+
+
FunctionLiteral* Parser::ParseFunctionLiteral(
Handle<String> function_name,
Scanner::Location function_name_location,
@@ -3453,6 +3517,7 @@ FunctionLiteral* Parser::ParseFunctionLiteral(
FunctionLiteral::kIsFunction,
parenthesized,
generator,
+ FunctionLiteral::kNotArrow,
pos);
function_literal->set_function_token_position(function_token_pos);
function_literal->set_ast_properties(&ast_properties);
@@ -3620,6 +3685,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());
}
« no previous file with comments | « src/parser.h ('k') | src/preparser.h » ('j') | src/preparser.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698