From: Jay Freeman (saurik) Date: Wed, 6 Jun 2012 23:05:33 +0000 (-0700) Subject: Introduce ECMA 6 fat-arrow lambda function syntax. X-Git-Tag: v0.9.456~15 X-Git-Url: https://git.saurik.com/cycript.git/commitdiff_plain/4b2fd91c0bf3c787b75e5baa947db1f9437eb931 Introduce ECMA 6 fat-arrow lambda function syntax. --- diff --git a/Cycript.l.in b/Cycript.l.in index b53159c..f5e762c 100644 --- a/Cycript.l.in +++ b/Cycript.l.in @@ -196,6 +196,7 @@ XMLName {XMLNameStart}{XMLNamePart}* "=" L C F(tk::Equal); "==" L C F(tk::EqualEqual); "===" L C F(tk::EqualEqualEqual); +"=>" L C F(tk::EqualRight); "!" L C F(tk::Exclamation); "!=" L C F(tk::ExclamationEqual); "!==" L C F(tk::ExclamationEqualEqual); diff --git a/Cycript.yy.in b/Cycript.yy.in index dfabafb..40a0436 100644 --- a/Cycript.yy.in +++ b/Cycript.yy.in @@ -161,6 +161,7 @@ int cylex(YYSTYPE *, cy::location *, void *); %token Equal "=" %token EqualEqual "==" %token EqualEqualEqual "===" +%token EqualRight "=>" %token Exclamation "!" %token ExclamationEqual "!=" %token ExclamationEqualEqual "!==" @@ -327,6 +328,8 @@ int cylex(YYSTYPE *, cy::location *, void *); %type ArgumentListOpt %type Arguments %type ArrayLiteral +%type ArrowFunction +%type ArrowParameters %type AssignmentExpression %type BindingIdentifier %type BitwiseANDExpression @@ -348,6 +351,7 @@ int cylex(YYSTYPE *, cy::location *, void *); %type ComprehensionListOpt %type ConditionalExpression %type ContinueStatement +%type ConciseBody %type DebuggerStatement %type Declaration__ %type Declaration_ @@ -400,7 +404,8 @@ int cylex(YYSTYPE *, cy::location *, void *); %type NewExpression %type NullLiteral %type ObjectLiteral -%type Parenthetical +%type Parenthetical +%type ParentheticalOpt %type PostfixExpression %type PrimaryExpression %type Program @@ -518,17 +523,16 @@ LexSetRegExp : { driver.SetCondition(CYDriver::RegExpCondition); } ; -LexSetStatement - : { switch (yychar) { - case token::Function: - yychar = token::Function_; - break; +LexNoBrace + : { if (yychar == token::OpenBrace || yychar == token::OpenBrace_) yychar = token::OpenBrace__; } + ; - case token::OpenBrace: - case token::OpenBrace_: - yychar = token::OpenBrace__; - break; - } } +LexNoFunction + : { if (yychar == token::Function) yychar = token::Function_; } + ; + +LexSetStatement + : LexNoBrace LexNoFunction ; /* }}} */ @@ -685,6 +689,11 @@ Parenthetical : "(" LexPushInOff Expression LexPopIn ")" { $$ = $3; } ; +ParentheticalOpt + : Parenthetical { $$ = $1; } + | { $$ = NULL; } + ; + PrimaryExpression : "this" { $$ = $1; } | Identifier { $$ = CYNew CYVariable($1); } @@ -912,6 +921,7 @@ ConditionalExpression /* 11.13 Assignment Operators {{{ */ AssignmentExpression : ConditionalExpression { $$ = $1; } + | ArrowFunction { $$ = $1; } | LeftHandSideExpression "=" AssignmentExpression { $$ = CYNew CYAssign($1, $3); } | LeftHandSideExpression "*=" AssignmentExpression { $$ = CYNew CYMultiplyAssign($1, $3); } | LeftHandSideExpression "/=" AssignmentExpression { $$ = CYNew CYDivideAssign($1, $3); } @@ -1200,7 +1210,7 @@ DebuggerStatement ; /* }}} */ -/* 13 Function Definition {{{ */ +/* 13.1 Function Definitions {{{ */ FunctionDeclaration : ";function" Identifier "(" FormalParameterListOpt ")" BRACE FunctionBody "}" { $$ = CYNew CYFunctionStatement($2, $4, $7); } ; @@ -1236,6 +1246,21 @@ FunctionBody : StatementListOpt { $$ = $1; } ; /* }}} */ +/* 13.2 Arrow Function Definitions {{{ */ +ArrowFunction + : LexSetRegExp ArrowParameters "=>" LexNoBrace ConciseBody { $$ = CYNew CYFunctionExpression(NULL, $2, $5); } + ; + +ArrowParameters + : BindingIdentifier { $$ = CYNew CYFunctionParameter(CYNew CYDeclaration($1)); } + //| ParentheticalOpt { $$ = $1; } + ; + +ConciseBody + : AssignmentExpression { $$ = CYNew CYReturn($1); } + | ";{" LexPushInOff FunctionBody LexPopIn "}" { $$ = $3; } + ; +/* }}} */ /* 14 Program {{{ */ Program : ProgramBodyOpt { driver.program_ = CYNew CYProgram($1); }