From a7d8b4130acfb550e437d8cf1f6d3d81cb0cf6ee Mon Sep 17 00:00:00 2001 From: "Jay Freeman (saurik)" Date: Sun, 29 Nov 2015 13:38:02 -0800 Subject: [PATCH] Reorganize grammar to match the ECMAScript 6 spec. --- Complete.cpp | 10 +- Console.cpp | 8 +- Cycript.l.in | 3 +- Cycript.yy.in | 470 +++++++++++++++++++++++++++----------------------- Driver.cpp | 4 +- Driver.hpp | 3 +- Library.cpp | 4 +- Output.cpp | 2 +- Parser.hpp | 32 ++-- Replace.cpp | 2 +- 10 files changed, 285 insertions(+), 253 deletions(-) diff --git a/Complete.cpp b/Complete.cpp index 75b80c0..13590bb 100644 --- a/Complete.cpp +++ b/Complete.cpp @@ -38,11 +38,11 @@ static CYExpression *ParseExpression(CYPool &pool, CYUTF8String code) { CYOptions options; CYContext context(options); - CYStatement *statement(driver.program_->code_); + CYStatement *statement(driver.script_->code_); _assert(statement != NULL); _assert(statement->next_ == NULL); - CYExpress *express(dynamic_cast(driver.program_->code_)); + CYExpress *express(dynamic_cast(driver.script_->code_)); _assert(express != NULL); CYParenthetical *parenthetical(dynamic_cast(express->expression_)); @@ -99,7 +99,7 @@ _visible char **CYComplete(const char *word, const std::string &line, CYUTF8Stri std::string begin(prefix.str()); - driver.program_ = $ CYProgram($ CYExpress($C3(ParseExpression(pool, + driver.script_ = $ CYScript($ CYExpress($C3(ParseExpression(pool, " function(object, prefix, word) {\n" " var names = [];\n" " var before = prefix.length;\n" @@ -112,11 +112,11 @@ _visible char **CYComplete(const char *word, const std::string &line, CYUTF8Stri " }\n" ), expression, $S(begin.c_str()), $S(word)))); - driver.program_->Replace(context); + driver.script_->Replace(context); std::stringbuf str; CYOutput out(str, options); - out << *driver.program_; + out << *driver.script_; std::string code(str.str()); CYUTF8String json(run(pool, code)); diff --git a/Console.cpp b/Console.cpp index d2ca3b2..a191fc0 100644 --- a/Console.cpp +++ b/Console.cpp @@ -425,13 +425,13 @@ static void Console(CYOptions &options) { goto read; } - if (driver.program_ == NULL) + if (driver.script_ == NULL) goto restart; std::stringbuf str; CYOutput out(str, options); Setup(out, driver, options, lower); - out << *driver.program_; + out << *driver.script_; code = str.str(); } @@ -808,11 +808,11 @@ int Main(int argc, char * const argv[], char const * const envp[]) { if (failed || !driver.errors_.empty()) { for (CYDriver::Errors::const_iterator i(driver.errors_.begin()); i != driver.errors_.end(); ++i) std::cerr << i->location_.begin << ": " << i->message_ << std::endl; - } else if (driver.program_ != NULL) { + } else if (driver.script_ != NULL) { std::stringbuf str; CYOutput out(str, options); Setup(out, driver, options, true); - out << *driver.program_; + out << *driver.script_; std::string code(str.str()); if (compile) std::cout << code; diff --git a/Cycript.l.in b/Cycript.l.in index 2b1c454..42c45e2 100644 --- a/Cycript.l.in +++ b/Cycript.l.in @@ -50,6 +50,7 @@ typedef cy::parser::token tk; @begin ObjectiveC yyextra->no_.AtImplementation = false; \ @end + yyextra->no_.Class = false; \ yyextra->no_.Function = false; \ yyextra->no_.NewLine = false; \ yyextra->no_.OpenBrace = false; \ @@ -403,7 +404,7 @@ XMLName {XMLNameStart}{XMLNamePart}* "case" L C /*KKK*/ F(tk::_case_, hi::Control); "catch" L C /*KKK*/ F(tk::_catch_, hi::Control); "char" L C /*FII*/ F(tk::_char_, hi::Type); -"class" L C /*FFK*/ F(tk::_class_, hi::Meta); +"class" L C /*FFK*/ F(yyextra->no_.Class ? tk::_class__ : tk::_class_, hi::Meta); "const" L C /*FFK*/ F(tk::_const_, hi::Meta); "continue" L C /*KKK*/ F(tk::_continue_, hi::Control); "debugger" L C /*FKK*/ F(tk::_debugger_, hi::Meta); diff --git a/Cycript.yy.in b/Cycript.yy.in index 6948982..2261ab6 100644 --- a/Cycript.yy.in +++ b/Cycript.yy.in @@ -56,8 +56,8 @@ %union { CYExpression *expression_; } %union { CYFalse *false_; } %union { CYFinally *finally_; } -%union { CYForInitialiser *for_; } -%union { CYForInInitialiser *forin_; } +%union { CYForInitializer *for_; } +%union { CYForInInitializer *forin_; } %union { CYFunctionParameter *functionParameter_; } %union { CYIdentifier *identifier_; } %union { CYInfix *infix_; } @@ -67,7 +67,6 @@ %union { CYNull *null_; } %union { CYNumber *number_; } %union { CYParenthetical *parenthetical_; } -%union { CYProgram *program_; } %union { CYProperty *property_; } %union { CYPropertyName *propertyName_; } %union { CYRubyProc *rubyProc_; } @@ -276,6 +275,7 @@ _finline int yylex(cy::parser::semantic_type *semantic, CYLocation *location, vo %token _case_ "case" %token _catch_ "catch" %token _class_ "class" +%token _class__ "!class" %token _const_ "const" %token _continue_ "continue" %token _debugger_ "debugger" @@ -368,7 +368,6 @@ _finline int yylex(cy::parser::semantic_type *semantic, CYLocation *location, vo %type ArgumentListOpt %type Arguments %type ArrayComprehension -%type ArrayInitialiser %type ArrayLiteral %type ArrowFunction %type ArrowParameters @@ -376,8 +375,8 @@ _finline int yylex(cy::parser::semantic_type *semantic, CYLocation *location, vo %type Binding %type BindingIdentifier %type BitwiseANDExpression -%type Block_ %type Block +%type BlockStatement %type BooleanLiteral %type BindingElement %type BitwiseORExpression @@ -389,7 +388,8 @@ _finline int yylex(cy::parser::semantic_type *semantic, CYLocation *location, vo %type CaseBlock %type CaseClause %type CaseClausesOpt -%type CatchOpt +%type Catch +%type CatchParameter %type Comprehension %type ComprehensionFor %type ComprehensionIf @@ -397,6 +397,7 @@ _finline int yylex(cy::parser::semantic_type *semantic, CYLocation *location, vo %type ConditionalExpression %type ContinueStatement %type ConciseBody +%type CoverParenthesizedExpressionAndArrowParameterList %type DebuggerStatement %type Declaration__ %type Declaration_ @@ -412,9 +413,9 @@ _finline int yylex(cy::parser::semantic_type *semantic, CYLocation *location, vo %type Expression %type ExpressionOpt %type ExpressionStatement -%type FinallyOpt -%type ForStatementInitialiser -%type ForInStatementInitialiser +%type Finally +%type ForStatementInitializer +%type ForInStatementInitializer %type FormalParameter %type FormalParameterList_ %type FormalParameterList @@ -422,20 +423,23 @@ _finline int yylex(cy::parser::semantic_type *semantic, CYLocation *location, vo %type FunctionBody %type FunctionDeclaration %type FunctionExpression +%type HoistableDeclaration %type Identifier %type IdentifierOpt %type IdentifierType %type IdentifierName +%type IdentifierReference %type IfStatement -%type Initialiser -%type InitialiserOpt +%type Initializer +%type InitializerOpt %type IterationStatement +%type LabelIdentifier +%type LabelledItem %type LabelledStatement %type LeftHandSideExpression %type LetStatement %type LexicalDeclaration %type Literal -%type ValueLiteral %type LogicalANDExpression %type LogicalORExpression %type MemberAccess @@ -446,12 +450,8 @@ _finline int yylex(cy::parser::semantic_type *semantic, CYLocation *location, vo %type NewExpression %type NullLiteral %type ObjectLiteral -%type Parenthetical %type PostfixExpression %type PrimaryExpression -%type Program -%type ProgramBody -%type ProgramBodyOpt %type PropertyName_ %type PropertyName %type PropertyDefinition @@ -465,6 +465,9 @@ _finline int yylex(cy::parser::semantic_type *semantic, CYLocation *location, vo %type RubyProcParameterList %type RubyProcParameters %type RubyProcParametersOpt +%type Script +%type ScriptBody +%type ScriptBodyOpt %type ShiftExpression %type SingleNameBinding %type Statement__ @@ -487,7 +490,6 @@ _finline int yylex(cy::parser::semantic_type *semantic, CYLocation *location, vo @begin ObjectiveC %type WordOpt @end -%type Variable @begin C %type IntegerType @@ -565,7 +567,7 @@ _finline int yylex(cy::parser::semantic_type *semantic, CYLocation *location, vo %nonassoc "else" /* }}} */ -%start Program +%start Script %% @@ -594,6 +596,10 @@ LexNoBrace : { if (yyla.empty()) driver.no_.OpenBrace = true; else if (yyla.type == yytranslate_(token::OpenBrace) || yyla.type == yytranslate_(token::OpenBrace_)) yyla.type = yytranslate_(token::OpenBrace__); } ; +LexNoClass + : { if (yyla.empty()) driver.no_.Class = true; else if (yyla.type == yytranslate_(token::_class_)) yyla.type = yytranslate_(token::_class__); } + ; + LexNoFunction : { if (yyla.empty()) driver.no_.Function = true; else if (yyla.type == yytranslate_(token::_function_)) yyla.type = yytranslate_(token::_function__); } ; @@ -605,7 +611,7 @@ LexNoAtImplementation : ; LexSetStatement - : LexNoBrace LexNoFunction LexNoAtImplementation + : LexNoBrace LexNoClass LexNoFunction LexNoAtImplementation ; /* }}} */ /* Virtual Tokens {{{ */ @@ -619,7 +625,7 @@ Var_ ; /* }}} */ -/* 7.6 Identifier Names and Identifiers {{{ */ +/* 11.6 Names and Keywords {{{ */ IdentifierName : Word { $$ = $1; } ; @@ -632,6 +638,7 @@ Word | "case" { $$ = CYNew CYWord("case"); } | "catch" { $$ = CYNew CYWord("catch"); } | "class" { $$ = CYNew CYWord("class"); } + | "!class" { $$ = CYNew CYWord("class"); } | "const" { $$ = CYNew CYWord("const"); } | "continue" { $$ = CYNew CYWord("continue"); } | "debugger" { $$ = CYNew CYWord("debugger"); } @@ -677,6 +684,55 @@ WordOpt | { $$ = NULL; } ; @end +/* }}} */ +/* 11.8.1 Null Literals {{{ */ +NullLiteral + : "null" { $$ = CYNew CYNull(); } + ; +/* }}} */ +/* 11.8.2 Boolean Literals {{{ */ +BooleanLiteral + : "true" { $$ = CYNew CYTrue(); } + | "false" { $$ = CYNew CYFalse(); } + ; +/* }}} */ + +/* 11.9 Automatic Semicolon Insertion {{{ */ +StrictSemi + : { driver.Warning(@$, "warning, automatic semi-colon insertion required"); } + ; + +TerminatorSoft + : ";" + | "\n" StrictSemi + ; + +Terminator + : ";" + | error { if (yyla.type_get() != yyeof_ && yyla.type != yytranslate_(token::CloseBrace) && !driver.newline_) { error(@1, "required semi-colon"); } else { yyerrok; driver.errors_.pop_back(); } } StrictSemi + ; + +TerminatorOpt + : ";" + | error { yyerrok; driver.errors_.pop_back(); } StrictSemi + ; +/* }}} */ + +/* 12.1 Identifiers {{{ */ +IdentifierReference + : Identifier { $$ = CYNew CYVariable($1); } + // XXX: | "yield" + ; + +BindingIdentifier + : Identifier { $$ = $1; } + // XXX: | "yield" + ; + +LabelIdentifier + : Identifier { $$ = $1; } + // XXX: | yield + ; IdentifierType : Identifier_ { $$ = $1; } @@ -744,79 +800,32 @@ IdentifierOpt | { $$ = NULL; } ; /* }}} */ - -/* 7.8 Literals {{{ */ -Literal - : NullLiteral { $$ = $1; } - | ValueLiteral { $$ = $1; } - ; - -ValueLiteral - : BooleanLiteral { $$ = $1; } - | NumericLiteral { $$ = $1; } - | StringLiteral { $$ = $1; } - | RegularExpressionLiteral { $$ = $1; } - ; -/* }}} */ -/* 7.8.1 Null Literals {{{ */ -NullLiteral - : "null" { $$ = CYNew CYNull(); } - ; -/* }}} */ -/* 7.8.2 Boolean Literals {{{ */ -BooleanLiteral - : "true" { $$ = CYNew CYTrue(); } - | "false" { $$ = CYNew CYFalse(); } - ; -/* }}} */ - -/* 7.9 Automatic Semicolon Insertion {{{ */ -StrictSemi - : { driver.Warning(@$, "warning, automatic semi-colon insertion required"); } - ; - -TerminatorSoft - : ";" - | "\n" StrictSemi - ; - -Terminator - : ";" - | error { if (yyla.type_get() != yyeof_ && yyla.type != yytranslate_(token::CloseBrace) && !driver.newline_) { error(@1, "required semi-colon"); } else { yyerrok; driver.errors_.pop_back(); } } StrictSemi - ; - -TerminatorOpt - : ";" - | error { yyerrok; driver.errors_.pop_back(); } StrictSemi - ; -/* }}} */ - -/* 11.1 Primary Expressions {{{ */ -Parenthetical - : "(" LexPushInOff Expression ")" LexPopIn { $$ = CYNew CYParenthetical($3); } - ; - -Variable - : Identifier { $$ = CYNew CYVariable($1); } - ; - +/* 12.2 Primary Expression {{{ */ PrimaryExpression : "this" { $$ = CYNew CYThis(); } - | Variable { $$ = $1; } + | IdentifierReference { $$ = $1; } | Literal { $$ = $1; } - | ArrayInitialiser { $$ = $1; } + | ArrayLiteral { $$ = $1; } | ObjectLiteral { $$ = $1; } - | Parenthetical { $$ = $1; } + | RegularExpressionLiteral { $$ = $1; } + | CoverParenthesizedExpressionAndArrowParameterList { if ($1 == NULL) error(@1, "invalid parenthetical"); $$ = $1; } | AutoComplete { driver.mode_ = CYDriver::AutoPrimary; YYACCEPT; } ; + +CoverParenthesizedExpressionAndArrowParameterList + : "(" LexPushInOff Expression ")" LexPopIn { $$ = CYNew CYParenthetical($3); } + | "(" LexPushInOff LexSetRegExp ")" LexPopIn { $$ = NULL; } + ; /* }}} */ -/* 11.1.4 Array Initializer {{{ */ -ArrayInitialiser - : ArrayLiteral { $$ = $1; } - | ArrayComprehension { $$ = $1; } +/* 12.2.4 Literals {{{ */ +Literal + : NullLiteral { $$ = $1; } + | BooleanLiteral { $$ = $1; } + | NumericLiteral { $$ = $1; } + | StringLiteral { $$ = $1; } ; /* }}} */ -/* 11.1.4.1 Array Literal {{{ */ +/* 12.2.5 Array Initializer {{{ */ ArrayLiteral : "[" LexPushInOff ElementListOpt "]" LexPopIn { $$ = CYNew CYArray($3); } ; @@ -840,30 +849,7 @@ ElementListOpt | LexSetRegExp { $$ = NULL; } ; /* }}} */ -/* 11.1.4.2 Array Comprehension {{{ */ -ArrayComprehension - : "[" LexPushInOff Comprehension "]" LexPopIn { $$ = $3; } - ; - -Comprehension - : LexSetRegExp ComprehensionFor ComprehensionTail AssignmentExpression { $$ = CYNew CYArrayComprehension($4, $2->Modify($3)); } - ; - -ComprehensionTail - : { $$ = NULL; } - | ComprehensionFor ComprehensionTail { $$ = $1->Modify($2); } - | ComprehensionIf ComprehensionTail { $$ = $1->Modify($2); } - ; - -ComprehensionFor - : "for" "(" Binding "of" Expression ")" { $$ = CYNew CYForOfComprehension($3, $5); } - ; - -ComprehensionIf - : "if" "(" AssignmentExpression ")" { $$ = CYNew CYIfComprehension($3); } - ; -/* }}} */ -/* 11.1.5 Object Initialiser {{{ */ +/* 12.2.6 Object Initializer {{{ */ ObjectLiteral : BRACE LexPushInOff PropertyDefinitionListOpt "}" LexPopIn { $$ = CYNew CYObject($3); } ; @@ -899,9 +885,22 @@ PropertyName_ PropertyName : LexSetRegExp PropertyName_ { $$ = $2; } ; + + +Initializer + : "=" AssignmentExpression { $$ = $2; } + ; + +InitializerOpt + : Initializer { $$ = $1; } + | { $$ = NULL; } + ; +/* }}} */ +/* 12.2.9 Template Literals {{{ */ /* }}} */ -/* 11.2 Left-Hand-Side Expressions {{{ */ + +/* 12.3+ Left-Hand-Side Expressions {{{ */ MemberAccess : "[" LexPushInOff Expression "]" LexPopIn { $$ = CYNew CYDirectMember(NULL, $3); } | "." IdentifierName { $$ = CYNew CYDirectMember(NULL, CYNew CYString($2)); } @@ -959,14 +958,14 @@ LeftHandSideExpression | CallExpression { $$ = $1; } ; /* }}} */ -/* 11.3 Postfix Expressions {{{ */ +/* 12.4 Postfix Expressions {{{ */ PostfixExpression : %prec "" LeftHandSideExpression { $$ = $1; } | LeftHandSideExpression "++" { $$ = CYNew CYPostIncrement($1); } | LeftHandSideExpression "--" { $$ = CYNew CYPostDecrement($1); } ; /* }}} */ -/* 11.4 Unary Operators {{{ */ +/* 12.5 Unary Operators {{{ */ UnaryExpression_ : "delete" UnaryExpression { $$ = CYNew CYDelete($2); } | "void" UnaryExpression { $$ = CYNew CYVoid($2); } @@ -986,7 +985,7 @@ UnaryExpression | LexSetRegExp UnaryExpression_ { $$ = $2; } ; /* }}} */ -/* 11.5 Multiplicative Operators {{{ */ +/* 12.6 Multiplicative Operators {{{ */ MultiplicativeExpression : UnaryExpression { $$ = $1; } | MultiplicativeExpression "*" UnaryExpression { $$ = CYNew CYMultiply($1, $3); } @@ -994,14 +993,14 @@ MultiplicativeExpression | MultiplicativeExpression "%" UnaryExpression { $$ = CYNew CYModulus($1, $3); } ; /* }}} */ -/* 11.6 Additive Operators {{{ */ +/* 12.7 Additive Operators {{{ */ AdditiveExpression : MultiplicativeExpression { $$ = $1; } | AdditiveExpression "+" MultiplicativeExpression { $$ = CYNew CYAdd($1, $3); } | AdditiveExpression "-" MultiplicativeExpression { $$ = CYNew CYSubtract($1, $3); } ; /* }}} */ -/* 11.7 Bitwise Shift Operators {{{ */ +/* 12.8 Bitwise Shift Operators {{{ */ ShiftExpression : AdditiveExpression { $$ = $1; } | ShiftExpression "<<" AdditiveExpression { $$ = CYNew CYShiftLeft($1, $3); } @@ -1009,7 +1008,7 @@ ShiftExpression | ShiftExpression ">>>" AdditiveExpression { $$ = CYNew CYShiftRightUnsigned($1, $3); } ; /* }}} */ -/* 11.8 Relational Operators {{{ */ +/* 12.9 Relational Operators {{{ */ RelationalExpression : ShiftExpression { $$ = $1; } | RelationalExpression "<" ShiftExpression { $$ = CYNew CYLess($1, $3); } @@ -1020,7 +1019,7 @@ RelationalExpression | RelationalExpression "in" ShiftExpression { $$ = CYNew CYIn($1, $3); } ; /* }}} */ -/* 11.9 Equality Operators {{{ */ +/* 12.10 Equality Operators {{{ */ EqualityExpression : RelationalExpression { $$ = $1; } | EqualityExpression "==" RelationalExpression { $$ = CYNew CYEqual($1, $3); } @@ -1029,7 +1028,7 @@ EqualityExpression | EqualityExpression "!==" RelationalExpression { $$ = CYNew CYNotIdentical($1, $3); } ; /* }}} */ -/* 11.10 Binary Bitwise Operators {{{ */ +/* 12.11 Binary Bitwise Operators {{{ */ BitwiseANDExpression : EqualityExpression { $$ = $1; } | BitwiseANDExpression "&" EqualityExpression { $$ = CYNew CYBitwiseAnd($1, $3); } @@ -1045,7 +1044,7 @@ BitwiseORExpression | BitwiseORExpression "|" BitwiseXORExpression { $$ = CYNew CYBitwiseOr($1, $3); } ; /* }}} */ -/* 11.11 Binary Logical Operators {{{ */ +/* 12.12 Binary Logical Operators {{{ */ LogicalANDExpression : BitwiseORExpression { $$ = $1; } | LogicalANDExpression "&&" BitwiseORExpression { $$ = CYNew CYLogicalAnd($1, $3); } @@ -1056,16 +1055,16 @@ LogicalORExpression | LogicalORExpression "||" LogicalANDExpression { $$ = CYNew CYLogicalOr($1, $3); } ; /* }}} */ -/* 11.12 Conditional Operator ( ? : ) {{{ */ +/* 12.13 Conditional Operator ( ? : ) {{{ */ ConditionalExpression : LogicalORExpression { $$ = $1; } | LogicalORExpression "?" LexPushInOff AssignmentExpression ":" LexPopIn AssignmentExpression { $$ = CYNew CYCondition($1, $4, $7); } - | LogicalORExpression "?" LexPushInOff LexSetRegExp ":" LexPopIn AssignmentExpression { $$ = CYNew CYCondition($1, $1, $7); } ; /* }}} */ -/* 11.13 Assignment Operators {{{ */ +/* 12.14 Assignment Operators {{{ */ AssignmentExpression : ConditionalExpression { $$ = $1; } + // XXX: | YieldExpression { $$ = $1; } | ArrowFunction { $$ = $1; } | LeftHandSideExpression "=" AssignmentExpression { $$ = CYNew CYAssign($1, $3); } | LeftHandSideExpression "*=" AssignmentExpression { $$ = CYNew CYMultiplyAssign($1, $3); } @@ -1081,9 +1080,10 @@ AssignmentExpression | LeftHandSideExpression "|=" AssignmentExpression { $$ = CYNew CYBitwiseOrAssign($1, $3); } ; /* }}} */ -/* 11.14 Comma Operator {{{ */ +/* 12.15 Comma Operator ( , ) {{{ */ Expression : AssignmentExpression { $$ = $1; } + /* XXX: I have this backwards... */ | AssignmentExpression "," Expression { $$ = CYNew CYCompound($1, $3); } ; @@ -1093,9 +1093,9 @@ ExpressionOpt ; /* }}} */ -/* 12 Statements {{{ */ +/* 13 Statements and Declarations {{{ */ Statement__ - : Block { $$ = $1; } + : BlockStatement { $$ = $1; } | VariableStatement { $$ = $1; } | EmptyStatement { $$ = $1; } | IfStatement { $$ = $1; } @@ -1120,7 +1120,8 @@ Statement ; Declaration__ - : FunctionDeclaration { $$ = $1; } + : HoistableDeclaration { $$ = $1; } + // XXX: | ClassDeclaration { $$ = $1; } | LexicalDeclaration { $$ = $1; } ; @@ -1132,18 +1133,23 @@ Declaration : LexSetStatement Declaration_ { $$ = $2; } ; +HoistableDeclaration + : FunctionDeclaration + // XXX: | GeneratorDeclaration + ; + BreakableStatement : IterationStatement { $$ = $1; } | SwitchStatement { $$ = $1; } ; /* }}} */ -/* 12.1 Block {{{ */ -Block_ - : BRACE StatementListOpt "}" { $$ = $2; } +/* 13.2 Block {{{ */ +BlockStatement + : ";{" StatementListOpt "}" { $$ = CYNew CYBlock($2); } ; Block - : ";{" StatementListOpt "}" { $$ = CYNew CYBlock($2); } + : BRACE StatementListOpt "}" { $$ = $2; } ; StatementList @@ -1160,19 +1166,7 @@ StatementListItem | Declaration { $$ = $1; } ; /* }}} */ - -/* 12.2 Declarations {{{ */ -BindingIdentifier - : Identifier { $$ = $1; } - ; - -Binding - : BindingIdentifier - ; - -// XXX: BindingPattern -/* }}} */ -/* 12.2.1 Let and Const Declarations {{{ */ +/* 13.3+ Let and Const Declarations {{{ */ LexicalDeclaration : LetOrConst VariableDeclarationList Terminator { $$ = CYNew CYVar($2); } ; @@ -1181,8 +1175,14 @@ LetOrConst : "let" | "const" ; + +Binding + : BindingIdentifier + ; + +// XXX: lots of binding stuff /* }}} */ -/* 12.2.2 Variable Statement {{{ */ +/* 13.3.2+ Variable Statement {{{ */ VariableStatement : Var_ VariableDeclarationList Terminator { $$ = CYNew CYVar($2); } ; @@ -1197,20 +1197,11 @@ VariableDeclarationList ; VariableDeclaration - : BindingIdentifier InitialiserOpt { $$ = CYNew CYDeclaration($1, $2); } - // XXX: | BindingPattern Initialiser { $$ = CYNew CYDeclaration($1, $2); } - ; - -Initialiser - : "=" AssignmentExpression { $$ = $2; } - ; - -InitialiserOpt - : Initialiser { $$ = $1; } - | { $$ = NULL; } + : BindingIdentifier InitializerOpt { $$ = CYNew CYDeclaration($1, $2); } + // XXX: | BindingPattern Initializer { $$ = CYNew CYDeclaration($1, $2); } ; /* }}} */ -/* 12.2.4 Destructuring Binding Patterns {{{ */ +/* 13.3.3+ Destructuring Binding Patterns {{{ */ // XXX: * BindingElement @@ -1218,21 +1209,20 @@ BindingElement ; SingleNameBinding - : BindingIdentifier InitialiserOpt { $$ = CYNew CYDeclaration($1, $2); } + : BindingIdentifier InitializerOpt { $$ = CYNew CYDeclaration($1, $2); } ; /* }}} */ - -/* 12.3 Empty Statement {{{ */ +/* 13.4 Empty Statement {{{ */ EmptyStatement : ";" { $$ = CYNew CYEmpty(); } ; /* }}} */ -/* 12.4 Expression Statement {{{ */ +/* 13.5 Expression Statement {{{ */ ExpressionStatement : Expression Terminator { $$ = CYNew CYExpress($1); } ; /* }}} */ -/* 12.5 The if Statement {{{ */ +/* 13.6 The if Statement {{{ */ ElseStatementOpt : "else" Statement { $$ = $2; } | %prec "if" { $$ = NULL; } @@ -1242,40 +1232,26 @@ IfStatement : "if" "(" Expression ")" Statement ElseStatementOpt { $$ = CYNew CYIf($3, $5, $6); } ; /* }}} */ - -/* 12.6.1 The do-while Statement {{{ */ +/* 13.7+ Iteration Statements {{{ */ IterationStatement : "do" Statement "while" "(" Expression ")" TerminatorOpt { $$ = CYNew CYDoWhile($5, $2); } - ; -/* }}} */ -/* 12.6.2 The while Statement {{{ */ -IterationStatement - : "while" "(" Expression ")" Statement { $$ = CYNew CYWhile($3, $5); } - ; -/* }}} */ -/* 12.6.3 The for Statement {{{ */ -IterationStatement - : "for" "(" LexPushInOn ForStatementInitialiser ";" LexPopIn ExpressionOpt ";" ExpressionOpt ")" Statement { $$ = CYNew CYFor($4, $7, $9, $11); } + | "while" "(" Expression ")" Statement { $$ = CYNew CYWhile($3, $5); } + | "for" "(" LexPushInOn ForStatementInitializer ";" LexPopIn ExpressionOpt ";" ExpressionOpt ")" Statement { $$ = CYNew CYFor($4, $7, $9, $11); } + | "for" "(" LexPushInOn ForInStatementInitializer "!in" LexPopIn Expression ")" Statement { $$ = CYNew CYForIn($4, $7, $9); } + | "for" "(" LexPushInOn ForInStatementInitializer "of" LexPopIn Expression ")" Statement { $$ = CYNew CYForOf($4, $7, $9); } ; -ForStatementInitialiser +ForStatementInitializer : ExpressionOpt { $$ = $1; } | LexSetRegExp Var_ VariableDeclarationList { $$ = CYNew CYForDeclarations($3); } ; -/* }}} */ -/* 12.6.4 The for-in and for-of Statements {{{ */ -IterationStatement - : "for" "(" LexPushInOn ForInStatementInitialiser "!in" LexPopIn Expression ")" Statement { $$ = CYNew CYForIn($4, $7, $9); } - | "for" "(" LexPushInOn ForInStatementInitialiser "of" LexPopIn Expression ")" Statement { $$ = CYNew CYForOf($4, $7, $9); } - ; -ForInStatementInitialiser +ForInStatementInitializer : LeftHandSideExpression { $$ = $1; } | LexSetRegExp Var_ VariableDeclaration { $$ = $3; } ; /* }}} */ - -/* 12.7 The continue Statement {{{ */ +/* 13.8 The continue Statement {{{ */ Continue : "continue" LexNoNewLine ; @@ -1285,7 +1261,7 @@ ContinueStatement | Continue Identifier Terminator { $$ = CYNew CYContinue($2); } ; /* }}} */ -/* 12.8 The break Statement {{{ */ +/* 13.9 The break Statement {{{ */ Break : "break" LexNoNewLine ; @@ -1295,7 +1271,7 @@ BreakStatement | Break Identifier Terminator { $$ = CYNew CYBreak($2); } ; /* }}} */ -/* 12.9 The return Statement {{{ */ +/* 13.10 The return Statement {{{ */ Return : "return" LexNoNewLine ; @@ -1305,13 +1281,12 @@ ReturnStatement | Return Expression Terminator { $$ = CYNew CYReturn($2); } ; /* }}} */ -/* 12.10 The with Statement {{{ */ +/* 13.11 The with Statement {{{ */ WithStatement : "with" "(" Expression ")" Statement { $$ = CYNew CYWith($3, $5); } ; /* }}} */ - -/* 12.11 The switch Statement {{{ */ +/* 13.12 The switch Statement {{{ */ SwitchStatement : "switch" "(" Expression ")" CaseBlock { $$ = CYNew CYSwitch($3, $5); } ; @@ -1330,47 +1305,58 @@ CaseClausesOpt | { $$ = NULL; } ; +// XXX: the standard makes certain you can only have one of these DefaultClause : "default" ":" StatementListOpt { $$ = CYNew CYClause(NULL, $3); } ; /* }}} */ -/* 12.12 Labelled Statements {{{ */ +/* 13.13 Labelled Statements {{{ */ LabelledStatement - : Identifier ":" Statement { $$ = CYNew CYLabel($1, $3); } + : LabelIdentifier ":" LabelledItem { $$ = CYNew CYLabel($1, $3); } + ; + +LabelledItem + : Statement { $$ = $1; } + | LexSetStatement LexSetRegExp FunctionDeclaration { $$ = $3; } ; /* }}} */ -/* 12.13 The throw Statement {{{ */ -ThrowWord +/* 13.14 The throw Statement {{{ */ +Throw : "throw" LexNoNewLine ; ThrowStatement - : ThrowWord LexSetRegExp TerminatorSoft { error(@1, "throw without exception"); } - | ThrowWord Expression Terminator { $$ = CYNew cy::Syntax::Throw($2); } + : Throw LexSetRegExp TerminatorSoft { error(@1, "throw without exception"); } + | Throw Expression Terminator { $$ = CYNew cy::Syntax::Throw($2); } ; /* }}} */ -/* 12.14 The try Statement {{{ */ +/* 13.15 The try Statement {{{ */ TryStatement - : "try" Block_ CatchOpt FinallyOpt { $$ = CYNew cy::Syntax::Try($2, $3, $4); } + : "try" Block Catch { $$ = CYNew cy::Syntax::Try($2, $3, NULL); } + | "try" Block Finally { $$ = CYNew cy::Syntax::Try($2, NULL, $3); } + | "try" Block Catch Finally { $$ = CYNew cy::Syntax::Try($2, $3, $4); } ; -CatchOpt - : "catch" "(" Identifier ")" Block_ { $$ = CYNew cy::Syntax::Catch($3, $5); } - | { $$ = NULL; } +Catch + : "catch" "(" CatchParameter ")" Block { $$ = CYNew cy::Syntax::Catch($3, $5); } ; -FinallyOpt - : "finally" Block_ { $$ = CYNew CYFinally($2); } - | { $$ = NULL; } +Finally + : "finally" Block { $$ = CYNew CYFinally($2); } + ; + +CatchParameter + : BindingIdentifier { $$ = $1; } + // XXX: BindingPattern ; /* }}} */ -/* 12.14 The debugger Statement {{{ */ +/* 13.16 The debugger Statement {{{ */ DebuggerStatement : "debugger" Terminator { $$ = CYNew CYDebugger(); } ; /* }}} */ -/* 13.1 Function Definitions {{{ */ +/* 14.1+ Function Definitions {{{ */ FunctionDeclaration : ";function" Identifier "(" FormalParameterListOpt ")" BRACE FunctionBody "}" { $$ = CYNew CYFunctionStatement($2, $4, $7); } ; @@ -1406,15 +1392,14 @@ FunctionBody : StatementListOpt { $$ = $1; } ; /* }}} */ -/* 13.2 Arrow Function Definitions {{{ */ +/* 14.2 Arrow Function Definitions {{{ */ ArrowFunction : LexSetRegExp ArrowParameters "=>" LexNoBrace ConciseBody { $$ = CYNew CYFatArrow($2, $5); } ; ArrowParameters : BindingIdentifier { $$ = CYNew CYFunctionParameter(CYNew CYDeclaration($1)); } - | "(" LexPushInOff LexSetRegExp ")" LexPopIn { $$ = NULL; } - | Parenthetical { $$ = $1->expression_->Parameter(); if ($$ == NULL) error(@1, "invalid parameter list"); } + | CoverParenthesizedExpressionAndArrowParameterList { $$ = $1->expression_->Parameter(); if ($$ == NULL) error(@1, "invalid parameter list"); } ; ConciseBody @@ -1422,20 +1407,33 @@ ConciseBody | LexSetRegExp ";{" LexPushInOff FunctionBody "}" LexPopIn { $$ = $4; } ; /* }}} */ -/* 14 Program {{{ */ -Program - : ProgramBodyOpt { driver.program_ = CYNew CYProgram($1); } +/* 14.3+ Method Definitions {{{ */ +/* }}} */ +/* 14.4+ Generator Function Definitions {{{ */ +/* }}} */ +/* 14.5+ Class Definitions {{{ */ +/* }}} */ + +/* 15.1 Scripts {{{ */ +Script + : ScriptBodyOpt { driver.script_ = CYNew CYScript($1); } ; -ProgramBody +ScriptBody : StatementList { $$ = $1; } ; -ProgramBodyOpt - : ProgramBody { $$ = $1; } +ScriptBodyOpt + : ScriptBody { $$ = $1; } | LexSetStatement LexSetRegExp { $$ = NULL; } ; /* }}} */ +/* 15.2+ Modules {{{ */ +/* }}} */ +/* 15.2.2+ Imports {{{ */ +/* }}} */ +/* 15.2.3+ Exports {{{ */ +/* }}} */ @begin C /* Cycript (C): Type Encoding {{{ */ @@ -1678,7 +1676,7 @@ BoxableExpression | StringLiteral { $$ = $1; } | ArrayLiteral { $$ = $1; } | ObjectLiteral { $$ = $1; } - | Parenthetical { $$ = $1; } + | CoverParenthesizedExpressionAndArrowParameterList { $$ = $1; } | "YES" { $$ = CYNew CYTrue(); } | "NO" { $$ = CYNew CYFalse(); } ; @@ -1802,7 +1800,7 @@ Identifier | "xml" { $$ = CYNew CYIdentifier("xml"); } ; /* }}} */ -/* 8.3 XML Initialiser Input Elements {{{ */ +/* 8.3 XML Initializer Input Elements {{{ */ XMLMarkup : XMLComment { $$ = $1; } | XMLCDATA { $$ = $1; } @@ -1853,7 +1851,7 @@ WildcardIdentifier : "*" { $$ = CYNew CYWildcard(); } ; /* }}} */ -/* 11.1.4 XML Initialiser {{{ */ +/* 11.1.4 XML Initializer {{{ */ XMLInitialiser : XMLMarkup { $$ = $1; } | XMLElement { $$ = $1; } @@ -1908,7 +1906,7 @@ XMLElementContentOpt | ; /* }}} */ -/* 11.1.5 XMLList Initialiser {{{ */ +/* 11.1.5 XMLList Initializer {{{ */ XMLListInitialiser : "<>" LexPushInOff LexPushXMLContent XMLElementContent LexPop "" LexPopIn { $$ = CYNew CYXMLList($4); } ; @@ -1937,7 +1935,7 @@ Statement__ /* }}} */ @end -/* JavaScript 1.7: Array Comprehensions {{{ */ +/* JavaScript FTL: Array Comprehensions {{{ */ Comprehension : AssignmentExpression ComprehensionFor ComprehensionTail { $$ = CYNew CYArrayComprehension($1, $2->Modify($3)); } ; @@ -1947,12 +1945,12 @@ ComprehensionFor | "for" "each" "(" Binding "in" Expression ")" { $$ = CYNew CYForOfComprehension($4, $6); } ; /* }}} */ -/* JavaScript 1.7: for each {{{ */ +/* JavaScript FTL: for each {{{ */ IterationStatement - : "for" "each" "(" LexPushInOn ForInStatementInitialiser "!in" LexPopIn Expression ")" Statement { $$ = CYNew CYForOf($5, $8, $10); } + : "for" "each" "(" LexPushInOn ForInStatementInitializer "!in" LexPopIn Expression ")" Statement { $$ = CYNew CYForOf($5, $8, $10); } ; /* }}} */ -/* JavaScript 1.7: let Statements {{{ */ +/* JavaScript FTL: let Statements {{{ */ LetStatement : "let" "(" VariableDeclarationList ")" Statement { $$ = CYNew CYLetStatement($3, $5); } ; @@ -1962,6 +1960,38 @@ Statement__ ; /* }}} */ +/* JavaScript FTW: Array Comprehensions {{{ */ +PrimaryExpression + : ArrayComprehension + ; + +ArrayComprehension + : "[" LexPushInOff Comprehension "]" LexPopIn { $$ = $3; } + ; + +Comprehension + : LexSetRegExp ComprehensionFor ComprehensionTail AssignmentExpression { $$ = CYNew CYArrayComprehension($4, $2->Modify($3)); } + ; + +ComprehensionTail + : { $$ = NULL; } + | ComprehensionFor ComprehensionTail { $$ = $1->Modify($2); } + | ComprehensionIf ComprehensionTail { $$ = $1->Modify($2); } + ; + +ComprehensionFor + : "for" "(" Binding "of" Expression ")" { $$ = CYNew CYForOfComprehension($3, $5); } + ; + +ComprehensionIf + : "if" "(" AssignmentExpression ")" { $$ = CYNew CYIfComprehension($3); } + ; +/* }}} */ +/* JavaScript FTW: Coalesce Operator {{{ */ +ConditionalExpression + : LogicalORExpression "?" LexPushInOff LexSetRegExp ":" LexPopIn AssignmentExpression { $$ = CYNew CYCondition($1, $1, $7); } + ; +/* }}} */ /* JavaScript FTW: Ruby Blocks {{{ */ RubyProcParameterList_ : "," RubyProcParameterList { $$ = $2; } diff --git a/Driver.cpp b/Driver.cpp index 274cbb9..b944022 100644 --- a/Driver.cpp +++ b/Driver.cpp @@ -31,7 +31,7 @@ CYDriver::CYDriver(CYPool &pool, std::istream &data, const std::string &filename strict_(false), commented_(false), filename_(filename), - program_(NULL), + script_(NULL), auto_(false), context_(NULL), mode_(AutoNone) @@ -57,7 +57,7 @@ bool CYDriver::Parse() { void CYDriver::Replace(CYOptions &options) { CYLocal local(&pool_); CYContext context(options); - program_->Replace(context); + script_->Replace(context); } void CYDriver::Warning(const cy::parser::location_type &location, const char *message) { diff --git a/Driver.hpp b/Driver.hpp index 50d513c..a4458a2 100644 --- a/Driver.hpp +++ b/Driver.hpp @@ -43,6 +43,7 @@ class _visible CYDriver { struct { bool AtImplementation; + bool Class; bool Function; bool OpenBrace; bool NewLine; @@ -70,7 +71,7 @@ class _visible CYDriver { typedef std::vector Errors; - CYProgram *program_; + CYScript *script_; Errors errors_; bool auto_; diff --git a/Library.cpp b/Library.cpp index 258ef61..9ed238d 100644 --- a/Library.cpp +++ b/Library.cpp @@ -227,11 +227,11 @@ CYUTF8String CYPoolCode(CYPool &pool, std::istream &stream) { CYOptions options; CYContext context(options); - driver.program_->Replace(context); + driver.script_->Replace(context); std::stringbuf str; CYOutput out(str, options); - out << *driver.program_; + out << *driver.script_; return $pool.strdup(str.str().c_str()); } diff --git a/Output.cpp b/Output.cpp index c06f757..95d2f19 100644 --- a/Output.cpp +++ b/Output.cpp @@ -653,7 +653,7 @@ void CYPrefix::Output(CYOutput &out, CYFlags flags) const { rhs_->Output(out, Precedence(), CYRight(flags)); } -void CYProgram::Output(CYOutput &out) const { +void CYScript::Output(CYOutput &out) const { out << code_; } diff --git a/Parser.hpp b/Parser.hpp index a7ac66a..2d78a47 100644 --- a/Parser.hpp +++ b/Parser.hpp @@ -356,12 +356,12 @@ struct CYScope { void Close(CYContext &context, CYStatement *&statements); }; -struct CYProgram : +struct CYScript : CYThing { CYStatement *code_; - CYProgram(CYStatement *code) : + CYScript(CYStatement *code) : code_(code) { } @@ -478,12 +478,12 @@ struct CYBlock : virtual CYStatement *Return(); }; -struct CYForInitialiser { +struct CYForInitializer { virtual CYExpression *Replace(CYContext &context) = 0; virtual void Output(CYOutput &out, CYFlags flags) const = 0; }; -struct CYForInInitialiser { +struct CYForInInitializer { virtual void ForIn(CYOutput &out, CYFlags flags) const = 0; virtual CYStatement *ForEachIn(CYContext &out, CYExpression *value) = 0; @@ -499,8 +499,8 @@ struct CYNumber; struct CYString; struct CYExpression : - CYForInitialiser, - CYForInInitialiser, + CYForInitializer, + CYForInInitializer, CYClassName, CYThing { @@ -604,9 +604,9 @@ struct CYFunctionParameter : CYNext, CYThing { - CYForInInitialiser *initialiser_; + CYForInInitializer *initialiser_; - CYFunctionParameter(CYForInInitialiser *initialiser, CYFunctionParameter *next = NULL) : + CYFunctionParameter(CYForInInitializer *initialiser, CYFunctionParameter *next = NULL) : CYNext(next), initialiser_(initialiser) { @@ -1096,7 +1096,7 @@ struct CYProperty : }; struct CYDeclaration : - CYForInInitialiser + CYForInInitializer { CYIdentifier *identifier_; CYExpression *initialiser_; @@ -1142,7 +1142,7 @@ struct CYDeclarations : }; struct CYForDeclarations : - CYForInitialiser + CYForInitializer { CYDeclarations *declarations_; @@ -1192,12 +1192,12 @@ struct CYLetStatement : struct CYFor : CYStatement { - CYForInitialiser *initialiser_; + CYForInitializer *initialiser_; CYExpression *test_; CYExpression *increment_; CYStatement *code_; - CYFor(CYForInitialiser *initialiser, CYExpression *test, CYExpression *increment, CYStatement *code) : + CYFor(CYForInitializer *initialiser, CYExpression *test, CYExpression *increment, CYStatement *code) : initialiser_(initialiser), test_(test), increment_(increment), @@ -1214,11 +1214,11 @@ struct CYFor : struct CYForIn : CYStatement { - CYForInInitialiser *initialiser_; + CYForInInitializer *initialiser_; CYExpression *set_; CYStatement *code_; - CYForIn(CYForInInitialiser *initialiser, CYExpression *set, CYStatement *code) : + CYForIn(CYForInInitializer *initialiser, CYExpression *set, CYStatement *code) : initialiser_(initialiser), set_(set), code_(code) @@ -1234,11 +1234,11 @@ struct CYForIn : struct CYForOf : CYStatement { - CYForInInitialiser *initialiser_; + CYForInInitializer *initialiser_; CYExpression *set_; CYStatement *code_; - CYForOf(CYForInInitialiser *initialiser, CYExpression *set, CYStatement *code) : + CYForOf(CYForInInitializer *initialiser, CYExpression *set, CYStatement *code) : initialiser_(initialiser), set_(set), code_(code) diff --git a/Replace.cpp b/Replace.cpp index 7b99022..44a1143 100644 --- a/Replace.cpp +++ b/Replace.cpp @@ -657,7 +657,7 @@ namespace { typedef std::set IdentifierUsages; } -void CYProgram::Replace(CYContext &context) { +void CYScript::Replace(CYContext &context) { CYScope scope(true, context); context.nextlocal_ = $ CYNonLocal(); -- 2.45.2