]> git.saurik.com Git - cycript.git/commitdiff
Finally implemented *entire* ECMAScript 6 grammar.
authorJay Freeman (saurik) <saurik@saurik.com>
Wed, 2 Dec 2015 10:30:22 +0000 (02:30 -0800)
committerJay Freeman (saurik) <saurik@saurik.com>
Wed, 2 Dec 2015 10:30:22 +0000 (02:30 -0800)
Output.cpp
Parser.ypp.in
Replace.cpp
Syntax.hpp

index c8205e5c05ebd106b1f222d0abbe876741d0395c..1496509a7b4ee3ea38b8ea1935e18044b06e214b 100644 (file)
@@ -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 {
index 295fa8104c1f9eddbdbf016a81f566739209b786..26d73c84bcf9852d25ce7ddcbaa822bb64bb0cbd 100644 (file)
@@ -406,9 +406,10 @@ _finline int yylex(cy::parser::semantic_type *semantic, CYLocation *location, CY
 %type <functionParameter_> ArrowParameters
 %type <expression_> AssignmentExpression
 %type <expression_> AssignmentExpressionOpt
-%type <identifier_> Binding
 %type <identifier_> BindingIdentifier
 %type <identifier_> BindingIdentifierOpt
+%type <declarations_> BindingList_
+%type <declarations_> BindingList
 %type <expression_> BitwiseANDExpression
 %type <statement_> Block
 %type <statement_> BlockStatement
@@ -450,8 +451,10 @@ _finline int yylex(cy::parser::semantic_type *semantic, CYLocation *location, CY
 %type <expression_> ExpressionOpt
 %type <statement_> ExpressionStatement
 %type <finally_> Finally
-%type <for_> ForStatementInitializer
+%type <declaration_> ForBinding
+%type <declaration_> ForDeclaration
 %type <forin_> ForInStatementInitializer
+%type <for_> ForStatementInitializer
 %type <declaration_> FormalParameter
 %type <functionParameter_> FormalParameterList_
 %type <functionParameter_> FormalParameterList
@@ -478,6 +481,7 @@ _finline int yylex(cy::parser::semantic_type *semantic, CYLocation *location, CY
 %type <statement_> LabelledStatement
 %type <expression_> LeftHandSideExpression
 %type <statement_> LetStatement
+%type <declaration_> LexicalBinding
 %type <statement_> LexicalDeclaration
 %type <literal_> Literal
 %type <propertyName_> 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
index bc162d3abd461b56a2ed07f808b9c6ddf0d578e2..54e931ea7307166a197c33f424505b106aa8ae62 100644 (file)
@@ -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)
         ))
     )));
index 3d248d6c58e20905919ef95ff83d7f8b83b335a5..b20e5115409268475925c44130fcffacab7ab4eb 100644 (file)
@@ -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;