From: Jay Freeman (saurik) Date: Wed, 2 Dec 2015 10:30:22 +0000 (-0800) Subject: Finally implemented *entire* ECMAScript 6 grammar. X-Git-Tag: v0.9.590~246 X-Git-Url: https://git.saurik.com/cycript.git/commitdiff_plain/4e3c905634abeaeea78747ce2be0adce06cc777a Finally implemented *entire* ECMAScript 6 grammar. --- diff --git a/Output.cpp b/Output.cpp index c8205e5..1496509 100644 --- a/Output.cpp +++ b/Output.cpp @@ -377,7 +377,9 @@ void CYForOf::Output(CYOutput &out, CYFlags flags) const { } void CYForOfComprehension::Output(CYOutput &out) const { - out << "for" << ' ' << "each" << ' ' << '(' << *name_ << ' ' << "in" << ' ' << *set_ << ')' << next_; + out << "for" << ' ' << "each" << ' ' << '('; + declaration_->Output(out, CYNoIn); + out << ' ' << "in" << ' ' << *set_ << ')' << next_; } void CYForIn::Output(CYOutput &out, CYFlags flags) const { @@ -389,7 +391,9 @@ void CYForIn::Output(CYOutput &out, CYFlags flags) const { } void CYForInComprehension::Output(CYOutput &out) const { - out << "for" << ' ' << '(' << *name_ << ' ' << "in" << ' ' << *set_ << ')'; + out << "for" << ' ' << '('; + declaration_->Output(out, CYNoIn); + out << ' ' << "in" << ' ' << *set_ << ')'; } void CYFunction::Output(CYOutput &out, CYFlags flags) const { diff --git a/Parser.ypp.in b/Parser.ypp.in index 295fa81..26d73c8 100644 --- a/Parser.ypp.in +++ b/Parser.ypp.in @@ -406,9 +406,10 @@ _finline int yylex(cy::parser::semantic_type *semantic, CYLocation *location, CY %type ArrowParameters %type AssignmentExpression %type AssignmentExpressionOpt -%type Binding %type BindingIdentifier %type BindingIdentifierOpt +%type BindingList_ +%type BindingList %type BitwiseANDExpression %type Block %type BlockStatement @@ -450,8 +451,10 @@ _finline int yylex(cy::parser::semantic_type *semantic, CYLocation *location, CY %type ExpressionOpt %type ExpressionStatement %type Finally -%type ForStatementInitializer +%type ForBinding +%type ForDeclaration %type ForInStatementInitializer +%type ForStatementInitializer %type FormalParameter %type FormalParameterList_ %type FormalParameterList @@ -478,6 +481,7 @@ _finline int yylex(cy::parser::semantic_type *semantic, CYLocation *location, CY %type LabelledStatement %type LeftHandSideExpression %type LetStatement +%type LexicalBinding %type LexicalDeclaration %type Literal %type LiteralPropertyName @@ -1247,9 +1251,9 @@ StatementListItem | Declaration { $$ = $1; } ; /* }}} */ -/* 13.3+ Let and Const Declarations {{{ */ +/* 13.3 Let and Const Declarations {{{ */ LexicalDeclaration - : LetOrConst VariableDeclarationList Terminator { $$ = CYNew CYVar($2); } + : LetOrConst BindingList Terminator { $$ = CYNew CYVar($2); } ; LetOrConst @@ -1257,13 +1261,21 @@ LetOrConst | "const" ; -Binding - : BindingIdentifier +BindingList_ + : "," BindingList { $$ = $2; } + | { $$ = NULL; } + ; + +BindingList + : LexicalBinding BindingList_ { $$ = CYNew CYDeclarations($1, $2); } ; -// XXX: lots of binding stuff +LexicalBinding + : BindingIdentifier InitializerOpt { $$ = CYNew CYDeclaration($1, $2); } + | BindingPattern Initializer { CYNOT(@1); } + ; /* }}} */ -/* 13.3.2+ Variable Statement {{{ */ +/* 13.3.2 Variable Statement {{{ */ VariableStatement : Var_ VariableDeclarationList Terminator { $$ = CYNew CYVar($2); } ; @@ -1279,7 +1291,7 @@ VariableDeclarationList VariableDeclaration : BindingIdentifier InitializerOpt { $$ = CYNew CYDeclaration($1, $2); } - // XXX: | BindingPattern Initializer { $$ = CYNew CYDeclaration($1, $2); } + | BindingPattern Initializer { CYNOT(@1); } ; /* }}} */ /* 13.3.3 Destructuring Binding Patterns {{{ */ @@ -1348,7 +1360,7 @@ IfStatement : "if" "(" Expression ")" Statement ElseStatementOpt { $$ = CYNew CYIf($3, $5, $6); } ; /* }}} */ -/* 13.7+ Iteration Statements {{{ */ +/* 13.7 Iteration Statements {{{ */ IterationStatement : "do" Statement "while" "(" Expression ")" TerminatorOpt { $$ = CYNew CYDoWhile($5, $2); } | "while" "(" Expression ")" Statement { $$ = CYNew CYWhile($3, $5); } @@ -1360,11 +1372,22 @@ IterationStatement ForStatementInitializer : ExpressionOpt { $$ = $1; } | LexSetRegExp Var_ VariableDeclarationList { $$ = CYNew CYForDeclarations($3); } + | LexSetRegExp LexicalDeclaration { CYNOT(@$); } ; ForInStatementInitializer : LeftHandSideExpression { $$ = $1; } - | LexSetRegExp Var_ VariableDeclaration { $$ = $3; } + | LexSetRegExp Var_ ForBinding { $$ = $3; } + | LexSetRegExp ForDeclaration { $$ = $2; } + ; + +ForDeclaration + : LetOrConst ForBinding { $$ = $2; } + ; + +ForBinding + : BindingIdentifier { $$ = CYNew CYDeclaration($1, NULL); } + | BindingPattern { CYNOT(@1); } ; /* }}} */ /* 13.8 The continue Statement {{{ */ @@ -2243,8 +2266,7 @@ Comprehension ; ComprehensionFor - : "for" "(" Binding "in" Expression ")" { $$ = CYNew CYForInComprehension($3, $5); } - | "for" "each" "(" Binding "in" Expression ")" { $$ = CYNew CYForOfComprehension($4, $6); } + : "for" "each" "(" LexPushInOn LexicalBinding "!in" LexPopIn Expression ")" { $$ = CYNew CYForOfComprehension($5, $8); } ; /* }}} */ /* JavaScript FTL: for each {{{ */ @@ -2282,7 +2304,8 @@ ComprehensionTail ; ComprehensionFor - : "for" "(" Binding "of" Expression ")" { $$ = CYNew CYForOfComprehension($3, $5); } + : "for" "(" LexPushInOn LexicalBinding "!in" LexPopIn Expression ")" { $$ = CYNew CYForInComprehension($4, $7); } + | "for" "(" LexPushInOn LexicalBinding "of" LexPopIn Expression ")" { $$ = CYNew CYForOfComprehension($4, $7); } ; ComprehensionIf diff --git a/Replace.cpp b/Replace.cpp index bc162d3..54e931e 100644 --- a/Replace.cpp +++ b/Replace.cpp @@ -396,11 +396,11 @@ CYStatement *CYForIn::Replace(CYContext &context) { } CYFunctionParameter *CYForInComprehension::Parameter(CYContext &context) const { - return $ CYFunctionParameter($ CYDeclaration(name_)); + return $ CYFunctionParameter(declaration_); } CYStatement *CYForInComprehension::Replace(CYContext &context, CYStatement *statement) const { - return $ CYForIn($V(name_), set_, CYComprehension::Replace(context, statement)); + return $ CYForIn(declaration_->Variable(context), set_, CYComprehension::Replace(context, statement)); } CYStatement *CYForOf::Replace(CYContext &context) { @@ -421,7 +421,7 @@ CYStatement *CYForOf::Replace(CYContext &context) { } CYFunctionParameter *CYForOfComprehension::Parameter(CYContext &context) const { - return $ CYFunctionParameter($ CYDeclaration(name_)); + return $ CYFunctionParameter(declaration_); } CYStatement *CYForOfComprehension::Replace(CYContext &context, CYStatement *statement) const { @@ -429,8 +429,8 @@ CYStatement *CYForOfComprehension::Replace(CYContext &context, CYStatement *stat return $E($C0($F(NULL, $P1($L("$cys")), $$->* $E($ CYAssign($V(cys), set_))->* - $ CYForIn($V(name_), $V(cys), $ CYBlock($$->* - $E($ CYAssign($V(name_), $M($V(cys), $V(name_))))->* + $ CYForIn(declaration_->Variable(context), $V(cys), $ CYBlock($$->* + $E($ CYAssign(declaration_->Variable(context), $M($V(cys), declaration_->Variable(context))))->* CYComprehension::Replace(context, statement) )) ))); diff --git a/Syntax.hpp b/Syntax.hpp index 3d248d6..b20e511 100644 --- a/Syntax.hpp +++ b/Syntax.hpp @@ -614,8 +614,6 @@ struct CYComprehension : return this; } - virtual const char *Name() const = 0; - virtual CYFunctionParameter *Parameter(CYContext &context) const = 0; CYFunctionParameter *Parameters(CYContext &context) const; virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const; @@ -625,20 +623,16 @@ struct CYComprehension : struct CYForInComprehension : CYComprehension { - CYIdentifier *name_; + CYDeclaration *declaration_; CYExpression *set_; - CYForInComprehension(CYIdentifier *name, CYExpression *set, CYComprehension *next = NULL) : + CYForInComprehension(CYDeclaration *declaration, CYExpression *set, CYComprehension *next = NULL) : CYComprehension(next), - name_(name), + declaration_(declaration), set_(set) { } - virtual const char *Name() const { - return name_->Word(); - } - virtual CYFunctionParameter *Parameter(CYContext &context) const; virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const; virtual void Output(CYOutput &out) const; @@ -647,20 +641,16 @@ struct CYForInComprehension : struct CYForOfComprehension : CYComprehension { - CYIdentifier *name_; + CYDeclaration *declaration_; CYExpression *set_; - CYForOfComprehension(CYIdentifier *name, CYExpression *set, CYComprehension *next = NULL) : + CYForOfComprehension(CYDeclaration *declaration, CYExpression *set, CYComprehension *next = NULL) : CYComprehension(next), - name_(name), + declaration_(declaration), set_(set) { } - virtual const char *Name() const { - return name_->Word(); - } - virtual CYFunctionParameter *Parameter(CYContext &context) const; virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const; virtual void Output(CYOutput &out) const; @@ -677,10 +667,6 @@ struct CYIfComprehension : { } - virtual const char *Name() const { - return NULL; - } - virtual CYFunctionParameter *Parameter(CYContext &context) const; virtual CYStatement *Replace(CYContext &context, CYStatement *statement) const; virtual void Output(CYOutput &out) const;