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

Unified Diff: src/preparser.h

Issue 160073006: Implement handling of arrow functions in the parser (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Implement handling of arrow functions in the parser Created 6 years, 9 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/preparser.h
diff --git a/src/preparser.h b/src/preparser.h
index ee8e46b96e6e00bb41ca3d926918206a40606e5e..6b5b74353d667e6a1bb96b1101e434301b9cbe32 100644
--- a/src/preparser.h
+++ b/src/preparser.h
@@ -78,6 +78,13 @@ namespace internal {
// // ...
// };
+enum FunctionParsingMode {
+ kNormalFunction,
+ kArrowFunction,
+ kGeneratorFunction
+};
+
+
template <typename Traits>
class ParserBase : public Traits {
public:
@@ -105,6 +112,7 @@ class ParserBase : public Traits {
allow_natives_syntax_(false),
allow_generators_(false),
allow_for_of_(false),
+ allow_arrow_functions_(false),
zone_(zone) { }
// Getters that indicate whether certain syntactical constructs are
@@ -113,6 +121,7 @@ class ParserBase : public Traits {
bool allow_natives_syntax() const { return allow_natives_syntax_; }
bool allow_generators() const { return allow_generators_; }
bool allow_for_of() const { return allow_for_of_; }
+ bool allow_arrow_functions() const { return allow_arrow_functions_; }
bool allow_modules() const { return scanner()->HarmonyModules(); }
bool allow_harmony_scoping() const { return scanner()->HarmonyScoping(); }
bool allow_harmony_numeric_literals() const {
@@ -125,6 +134,7 @@ class ParserBase : public Traits {
void set_allow_natives_syntax(bool allow) { allow_natives_syntax_ = allow; }
void set_allow_generators(bool allow) { allow_generators_ = allow; }
void set_allow_for_of(bool allow) { allow_for_of_ = allow; }
+ void set_allow_arrow_functions(bool allow) { allow_arrow_functions_ = allow; }
void set_allow_modules(bool allow) { scanner()->SetHarmonyModules(allow); }
void set_allow_harmony_scoping(bool allow) {
scanner()->SetHarmonyScoping(allow);
@@ -501,6 +511,7 @@ class ParserBase : public Traits {
bool allow_natives_syntax_;
bool allow_generators_;
bool allow_for_of_;
+ bool allow_arrow_functions_;
typename Traits::Type::Zone* zone_; // Only used by Parser.
};
@@ -990,7 +1001,8 @@ class PreParserTraits {
PreParserIdentifier name,
Scanner::Location function_name_location,
bool name_is_strict_reserved,
- bool is_generator,
+ FunctionParsingMode parsing_mode,
+ PreParserExpression params_ast,
int function_token_position,
FunctionLiteral::FunctionType type,
bool* ok);
@@ -1164,7 +1176,8 @@ class PreParser : public ParserBase<PreParserTraits> {
Identifier name,
Scanner::Location function_name_location,
bool name_is_strict_reserved,
- bool is_generator,
+ FunctionParsingMode parsing_mode,
+ Expression params_ast,
int function_token_pos,
FunctionLiteral::FunctionType function_type,
bool* ok);
@@ -1448,6 +1461,11 @@ typename ParserBase<Traits>::ExpressionT ParserBase<Traits>::ParseExpression(
// AssignmentExpression
// Expression ',' AssignmentExpression
+ if (allow_arrow_functions() && peek() == Token::RPAREN) {
marja 2014/03/24 09:04:06 This doesn't sound right. I guess the idea is that
aperez 2014/04/09 08:47:16 It was causing an unexpected token error most of t
+ // Empty argument list for arrow functions: () => ...
+ return this->EmptyExpression();
+ }
+
ExpressionT result = this->ParseAssignmentExpression(accept_IN, CHECK_OK);
while (peek() == Token::COMMA) {
Expect(Token::COMMA, CHECK_OK);
@@ -1550,7 +1568,8 @@ typename ParserBase<Traits>::ExpressionT ParserBase<Traits>::ParseObjectLiteral(
this->ParseFunctionLiteral(
name, scanner()->location(),
false, // reserved words are allowed here
- false, // not a generator
+ kNormalFunction,
+ this->EmptyExpression(),
RelocInfo::kNoPosition, FunctionLiteral::ANONYMOUS_EXPRESSION,
CHECK_OK);
// Allow any number of parameters for compatibilty with JSC.
@@ -1688,6 +1707,7 @@ typename ParserBase<Traits>::ExpressionT
ParserBase<Traits>::ParseAssignmentExpression(bool accept_IN, bool* ok) {
// AssignmentExpression ::
// ConditionalExpression
+ // ArrowFunction
// YieldExpression
// LeftHandSideExpression AssignmentOperator AssignmentExpression
@@ -1701,6 +1721,17 @@ ParserBase<Traits>::ParseAssignmentExpression(bool accept_IN, bool* ok) {
ExpressionT expression =
this->ParseConditionalExpression(accept_IN, CHECK_OK);
+ if (allow_arrow_functions() && peek() == Token::ARROW) {
+ return this->ParseFunctionLiteral(Traits::EmptyIdentifier(),
+ Scanner::Location::invalid(),
+ false,
+ kArrowFunction,
+ expression,
+ lhs_location.beg_pos,
+ FunctionLiteral::ANONYMOUS_EXPRESSION,
+ CHECK_OK);
+ }
+
if (!Token::IsAssignmentOp(peek())) {
if (fni_ != NULL) fni_->Leave();
// Parsed conditional expression only (no assignment).
@@ -2062,7 +2093,10 @@ ParserBase<Traits>::ParseMemberExpression(bool* ok) {
if (peek() == Token::FUNCTION) {
Consume(Token::FUNCTION);
int function_token_position = position();
- bool is_generator = allow_generators() && Check(Token::MUL);
+ FunctionParsingMode parsing_mode =
+ (allow_generators() && Check(Token::MUL))
+ ? kGeneratorFunction
+ : kNormalFunction;
IdentifierT name;
bool is_strict_reserved_name = false;
Scanner::Location function_name_location = Scanner::Location::invalid();
@@ -2077,7 +2111,8 @@ ParserBase<Traits>::ParseMemberExpression(bool* ok) {
result = this->ParseFunctionLiteral(name,
function_name_location,
is_strict_reserved_name,
- is_generator,
+ parsing_mode,
+ this->EmptyExpression(),
function_token_position,
function_type,
CHECK_OK);

Powered by Google App Engine
This is Rietveld 408576698