From: Jay Freeman (saurik) Date: Wed, 2 Dec 2015 01:29:51 +0000 (-0800) Subject: Stub all of ECMAScript 6, but leave unimplemented. X-Git-Tag: v0.9.590~253 X-Git-Url: https://git.saurik.com/cycript.git/commitdiff_plain/9d2b125d81372b42e8ae310c8de8650d13010d1e?ds=sidebyside Stub all of ECMAScript 6, but leave unimplemented. --- diff --git a/Cycript.l.in b/Cycript.l.in index 800ef74..03f82ce 100644 --- a/Cycript.l.in +++ b/Cycript.l.in @@ -451,8 +451,10 @@ XMLName {XMLNameStart}{XMLNamePart}* "finally" L /*KKK*/ F(tk::_finally_, hi::Control); "float" L /*FII*/ F(tk::_float_, hi::Type); "for" L /*KKK*/ F(tk::_for_, hi::Control); +"from" L /*III*/ F(tk::_from_, hi::Meta); "function" L /*KKK*/ F(tk::_function_, hi::Meta); "goto" L /*FII*/ F(tk::_goto_, hi::Control); +"get" L /*III*/ F(tk::_get_, hi::Meta); "if" L /*KKK*/ F(tk::_if_, hi::Control); "implements" L /*FSS*/ F(tk::_implements_, hi::Meta); "import" L /*FFK*/ F(tk::_import_, hi::Meta); @@ -469,7 +471,8 @@ XMLName {XMLNameStart}{XMLNamePart}* "private" L /*FSS*/ F(tk::_private_, hi::Meta); "protected" L /*FSS*/ F(tk::_protected_, hi::Meta); "public" L /*FSS*/ F(tk::_public_, hi::Meta); -"return" L /*KKK*/ F(tk::_return_, hi::Control); +"return" L /*KKK*/ F(yyextra->return_.top() ? tk::_return__ : tk::_return_, hi::Control); +"set" L /*III*/ F(tk::_set_, hi::Meta); "short" L /*FII*/ F(tk::_short_, hi::Type); "static" L /*FS?*/ F(tk::_static_, hi::Meta); "super" L /*FFK*/ F(tk::_super_, hi::Constant); @@ -487,7 +490,7 @@ XMLName {XMLNameStart}{XMLNamePart}* "volatile" L /*FII*/ F(tk::_volatile_, hi::Meta); "while" L /*KKK*/ F(tk::_while_, hi::Control); "with" L /*KKK*/ F(tk::_with_, hi::Control); -"yield" L /*IS?*/ F(tk::_yield_, hi::Control); +"yield" L /*IS?*/ F(yyextra->yield_.top() ? tk::_yield__ : tk::_yield_, hi::Control); "auto" L F(tk::_auto_, hi::Meta); "each" L F(tk::_each_, hi::Control); diff --git a/Cycript.yy.in b/Cycript.yy.in index 1ff6736..d8aee24 100644 --- a/Cycript.yy.in +++ b/Cycript.yy.in @@ -20,7 +20,6 @@ /* }}} */ %code top { -#define cyscanner driver.scanner_ #define YYSTACKEXPANDABLE 1 } @@ -54,6 +53,7 @@ %union { CYElement *element_; } %union { CYExpression *expression_; } %union { CYFalse *false_; } +%union { CYVariable *variable_; } %union { CYFinally *finally_; } %union { CYForInitializer *for_; } %union { CYForInInitializer *forin_; } @@ -112,16 +112,25 @@ int cylex(YYSTYPE *, CYLocation *, void *); %code { #undef yylex -_finline int yylex(cy::parser::semantic_type *semantic, CYLocation *location, void *scanner) { +_finline int yylex(cy::parser::semantic_type *semantic, CYLocation *location, CYDriver &driver) { + if (driver.mark_ == CYMarkIgnore); + else if (driver.mark_ == CYMarkScript) { + driver.mark_ = CYMarkIgnore; + return cy::parser::token::MarkScript; + } else if (driver.mark_ == CYMarkModule) { + driver.mark_ = CYMarkIgnore; + return cy::parser::token::MarkModule; + } + YYSTYPE data; - int token(cylex(&data, location, scanner)); + int token(cylex(&data, location, driver.scanner_)); *semantic = data.semantic_; return token; } #define CYMAP(to, from) do { \ if (yyla.empty()) \ - yyla.type = yytranslate_(yylex(&yyla.value, &yyla.location, cyscanner)); \ + yyla.type = yytranslate_(yylex(&yyla.value, &yyla.location, driver)); \ if (yyla.type == yytranslate_(token::from)) \ yyla.type = yytranslate_(token::to); \ } while (false) @@ -131,6 +140,9 @@ _finline int yylex(cy::parser::semantic_type *semantic, CYLocation *location, vo YYABORT; \ } while (false) +#define CYNOT(location) \ + CYERR(location, "unimplemented feature") + } %name-prefix "cy" @@ -151,8 +163,7 @@ _finline int yylex(cy::parser::semantic_type *semantic, CYLocation *location, vo %error-verbose -%parse-param { CYDriver &driver } -%lex-param { void *cyscanner } +%param { CYDriver &driver } /* Token Declarations {{{ */ @begin E4X @@ -308,6 +319,7 @@ _finline int yylex(cy::parser::semantic_type *semantic, CYLocation *location, vo %token _instanceof_ "instanceof" %token _new_ "new" %token _return_ "return" +%token _return__ "!return" %token _super_ "super" %token _switch_ "switch" %token _this_ "this" @@ -327,6 +339,8 @@ _finline int yylex(cy::parser::semantic_type *semantic, CYLocation *location, vo %token _double_ "double" %token _final_ "final" %token _float_ "float" +%token _from_ "from" +%token _get_ "get" %token _goto_ "goto" %token _implements_ "implements" %token _int_ "int" @@ -338,6 +352,7 @@ _finline int yylex(cy::parser::semantic_type *semantic, CYLocation *location, vo %token _private_ "private" %token _protected_ "protected" %token _public_ "public" +%token _set_ "set" %token _short_ "short" %token _static_ "static" %token _synchronized_ "synchronized" @@ -345,6 +360,7 @@ _finline int yylex(cy::parser::semantic_type *semantic, CYLocation *location, vo %token _transient_ "transient" %token _volatile_ "volatile" %token _yield_ "yield" +%token _yield__ "!yield" %token _undefined_ "undefined" @@ -367,6 +383,7 @@ _finline int yylex(cy::parser::semantic_type *semantic, CYLocation *location, vo @end %token AutoComplete +%token YieldStar %token Identifier_ %token NumericLiteral @@ -391,6 +408,7 @@ _finline int yylex(cy::parser::semantic_type *semantic, CYLocation *location, vo %type AssignmentExpressionOpt %type Binding %type BindingIdentifier +%type BindingIdentifierOpt %type BitwiseANDExpression %type Block %type BlockStatement @@ -407,10 +425,13 @@ _finline int yylex(cy::parser::semantic_type *semantic, CYLocation *location, vo %type CaseClausesOpt %type Catch %type CatchParameter +%type ClassDeclaration +%type ClassExpression %type Comprehension %type ComprehensionFor %type ComprehensionIf %type ComprehensionTail +%type ComputedPropertyName %type ConditionalExpression %type ContinueStatement %type ConciseBody @@ -434,16 +455,20 @@ _finline int yylex(cy::parser::semantic_type *semantic, CYLocation *location, vo %type FormalParameter %type FormalParameterList_ %type FormalParameterList -%type FormalParameterListOpt +%type FormalParameters %type FunctionBody %type FunctionDeclaration %type FunctionExpression +%type FunctionStatementList +%type GeneratorBody +%type GeneratorDeclaration +%type GeneratorExpression +%type GeneratorMethod %type HoistableDeclaration %type Identifier -%type IdentifierOpt %type IdentifierType %type IdentifierName -%type IdentifierReference +%type IdentifierReference %type IfStatement %type Initializer %type InitializerOpt @@ -455,24 +480,25 @@ _finline int yylex(cy::parser::semantic_type *semantic, CYLocation *location, vo %type LetStatement %type LexicalDeclaration %type Literal +%type LiteralPropertyName %type LogicalANDExpression %type LogicalORExpression %type MemberAccess -%type MemberExpression_ %type MemberExpression -%type Module +%type MethodDefinition +%type ModulePath %type MultiplicativeExpression %type NewExpression %type NullLiteral %type ObjectLiteral %type PostfixExpression %type PrimaryExpression -%type PropertyName_ %type PropertyName %type PropertyDefinition %type PropertyDefinitionList_ %type PropertyDefinitionList %type PropertyDefinitionListOpt +%type PropertySetParameterList %type RelationalExpression %type ReturnStatement %type RubyProcExpression @@ -491,6 +517,7 @@ _finline int yylex(cy::parser::semantic_type *semantic, CYLocation *location, vo %type StatementList %type StatementListOpt %type StatementListItem +%type StrictFormalParameters %type SwitchStatement %type TemplateLiteral %type TemplateSpans @@ -507,6 +534,7 @@ _finline int yylex(cy::parser::semantic_type *semantic, CYLocation *location, vo @begin ObjectiveC %type WordOpt @end +%type YieldExpression @begin C %type IntegerType @@ -582,22 +610,28 @@ _finline int yylex(cy::parser::semantic_type *semantic, CYLocation *location, vo %nonassoc "else" /* }}} */ -%start Script +%start Program +%token MarkModule +%token MarkScript %% -/* Lexer State {{{ */ -LexPushInOn - : { driver.in_.push(true); } +Program + : MarkScript Script + | MarkModule Module ; -LexPushInOff - : { driver.in_.push(false); } - ; +/* Lexer State {{{ */ +LexPushInOn: { driver.in_.push(true); }; +LexPushInOff: { driver.in_.push(false); }; +LexPopIn: { driver.in_.pop(); }; -LexPopIn - : { driver.in_.pop(); } - ; +LexPushReturnOn: { driver.return_.push(true); }; +LexPopReturn: { driver.return_.pop(); }; + +LexPushYieldOn: { driver.yield_.push(true); }; +LexPushYieldOff: { driver.yield_.push(false); }; +LexPopYield: { driver.yield_.pop(); }; LexSetRegExp : { driver.SetCondition(CYDriver::RegExpCondition); } @@ -607,6 +641,10 @@ LexNewLine : { if (!yyla.empty() && yyla.type_get() != yyeof_) CYERR(@$, "unexpected lookahead"); driver.next_ = true; } ; +LexNoStar + : { CYMAP(YieldStar, Star); } + ; + LexNoBrace : { CYMAP(OpenBrace__, OpenBrace); CYMAP(OpenBrace__, OpenBrace_); } ; @@ -642,6 +680,11 @@ IdentifierName | "instanceof" { $$ = CYNew CYWord("instanceof"); } ; +NewLineOpt + : "\n" + | + ; + Word : Identifier { $$ = $1; } | "auto" { $$ = CYNew CYWord("auto"); } @@ -669,6 +712,7 @@ Word | "new" LexSetRegExp { $$ = CYNew CYWord("new"); } | "null" { $$ = CYNew CYWord("null"); } | "return" { $$ = CYNew CYWord("return"); } + | "!return" { $$ = CYNew CYWord("return"); } | "super" { $$ = CYNew CYWord("super"); } | "switch" { $$ = CYNew CYWord("switch"); } | "this" { $$ = CYNew CYWord("this"); } @@ -680,6 +724,9 @@ Word | "void" LexSetRegExp { $$ = CYNew CYWord("void"); } | "while" { $$ = CYNew CYWord("while"); } | "with" { $$ = CYNew CYWord("with"); } + | "yield" { $$ = CYNew CYIdentifier("yield"); } + + | Yield LexSetRegExp NewLineOpt { $$ = CYNew CYIdentifier("yield"); } // XXX: should be Identifier | "let" { $$ = CYNew CYIdentifier("let"); } @@ -728,17 +775,22 @@ TerminatorOpt /* 12.1 Identifiers {{{ */ IdentifierReference : Identifier { $$ = CYNew CYVariable($1); } - // XXX: | "yield" + | "yield" { $$ = CYNew CYVariable(CYNew CYIdentifier("yield")); } ; BindingIdentifier : Identifier { $$ = $1; } - // XXX: | "yield" + | "yield" { $$ = CYNew CYIdentifier("yield"); } + ; + +BindingIdentifierOpt + : BindingIdentifier { $$ = $1; } + | { $$ = NULL; } ; LabelIdentifier : Identifier { $$ = $1; } - // XXX: | yield + | "yield" { $$ = CYNew CYIdentifier("yield"); } ; IdentifierType @@ -764,7 +816,6 @@ IdentifierType | "synchronized" { $$ = CYNew CYIdentifier("synchronized"); } | "throws" { $$ = CYNew CYIdentifier("throws"); } | "transient" { $$ = CYNew CYIdentifier("transient"); } - | "yield" { $$ = CYNew CYIdentifier("yield"); } @begin ObjectiveC | "bool" { $$ = CYNew CYIdentifier("bool"); } | "BOOL" { $$ = CYNew CYIdentifier("BOOL"); } @@ -776,8 +827,11 @@ IdentifierType Identifier : IdentifierType | "char" { $$ = CYNew CYIdentifier("char"); } + | "from" { $$ = CYNew CYIdentifier("from"); } + | "get" { $$ = CYNew CYIdentifier("get"); } | "int" { $$ = CYNew CYIdentifier("int"); } | "long" { $$ = CYNew CYIdentifier("long"); } + | "set" { $$ = CYNew CYIdentifier("set"); } | "short" { $$ = CYNew CYIdentifier("short"); } | "undefined" { $$ = CYNew CYIdentifier("undefined"); } | "volatile" { $$ = CYNew CYIdentifier("volatile"); } @@ -794,11 +848,6 @@ Identifier | "YES" { $$ = CYNew CYIdentifier("YES"); } @end ; - -IdentifierOpt - : Identifier { $$ = $1; } - | { $$ = NULL; } - ; /* }}} */ /* 12.2 Primary Expression {{{ */ PrimaryExpression @@ -807,6 +856,9 @@ PrimaryExpression | Literal { $$ = $1; } | ArrayLiteral { $$ = $1; } | ObjectLiteral { $$ = $1; } + | FunctionExpression { $$ = $1; } + | ClassExpression { $$ = $1; } + | GeneratorExpression { $$ = $1; } | RegularExpressionLiteral { $$ = $1; } | TemplateLiteral { $$ = $1; } | CoverParenthesizedExpressionAndArrowParameterList { if ($1 == NULL) CYERR(@1, "invalid parenthetical"); $$ = $1; } @@ -816,6 +868,8 @@ PrimaryExpression CoverParenthesizedExpressionAndArrowParameterList : "(" LexPushInOff Expression ")" LexPopIn { $$ = CYNew CYParenthetical($3); } | "(" LexPushInOff LexSetRegExp ")" LexPopIn { $$ = NULL; } + | "(" LexPushInOff LexSetRegExp "..." BindingIdentifier ")" LexPopIn { CYNOT(@$); } + | "(" LexPushInOff Expression "," LexSetRegExp "..." BindingIdentifier ")" LexPopIn { CYNOT(@$); } ; /* }}} */ /* 12.2.4 Literals {{{ */ @@ -848,8 +902,7 @@ ObjectLiteral ; PropertyDefinitionList_ - : "," PropertyDefinitionList { $$ = $2; } - | "," LexSetRegExp { $$ = NULL; } + : "," PropertyDefinitionListOpt { $$ = $2; } | { $$ = NULL; } ; @@ -858,27 +911,35 @@ PropertyDefinitionList ; PropertyDefinitionListOpt - : PropertyDefinitionList { $$ = $1; } + : LexSetRegExp PropertyDefinitionList { $$ = $2; } | LexSetRegExp { $$ = NULL; } ; PropertyDefinition - // XXX: this should be IdentifierName - : LexSetRegExp Identifier { $$ = CYNew CYProperty($2, CYNew CYVariable($2)); } + : IdentifierReference { $$ = CYNew CYProperty($1->name_, $1); } + | CoverInitializedName { CYNOT(@$); } | PropertyName ":" AssignmentExpression { $$ = CYNew CYProperty($1, $3); } - //| MethodDefinition + | MethodDefinition { $$ = $1; } + ; + +PropertyName + : LiteralPropertyName { $$ = $1; } + | ComputedPropertyName { CYNOT(@$); /* $$ = $1; */ } ; -PropertyName_ +LiteralPropertyName : IdentifierName { $$ = $1; } | StringLiteral { $$ = $1; } | NumericLiteral { $$ = $1; } ; -PropertyName - : LexSetRegExp PropertyName_ { $$ = $2; } +ComputedPropertyName + : "[" AssignmentExpression "]" { $$ = $2; } ; +CoverInitializedName + : IdentifierReference Initializer + ; Initializer : "=" AssignmentExpression { $$ = $2; } @@ -906,35 +967,50 @@ MemberAccess : "[" LexPushInOff Expression "]" LexPopIn { $$ = CYNew CYDirectMember(NULL, $3); } | "." IdentifierName { $$ = CYNew CYDirectMember(NULL, CYNew CYString($2)); } | "." AutoComplete { driver.mode_ = CYDriver::AutoDirect; YYACCEPT; } - ; - -MemberExpression_ - : MemberExpression { $$ = $1; } - //| "super" { $$ = $1; } + | TemplateLiteral { CYNOT(@$); } ; MemberExpression : LexSetRegExp PrimaryExpression { $$ = $2; } - | LexSetRegExp FunctionExpression { $$ = $2; } - | MemberExpression_ { driver.context_ = $1; } MemberAccess { $3->SetLeft($1); $$ = $3; } + | MemberExpression { driver.context_ = $1; } MemberAccess { $3->SetLeft($1); $$ = $3; } + | SuperProperty { CYNOT(@$); } + | MetaProperty { CYNOT(@$); } | LexSetRegExp "new" MemberExpression Arguments { $$ = CYNew cy::Syntax::New($3, $4); } ; +SuperProperty + : LexSetRegExp "super" "[" Expression "]" + | LexSetRegExp "super" "." IdentifierName + ; + +MetaProperty + : NewTarget + ; + +NewTarget + : LexSetRegExp "new" LexSetRegExp "." "target" + ; + NewExpression : MemberExpression { $$ = $1; } | LexSetRegExp "new" NewExpression { $$ = CYNew cy::Syntax::New($3, NULL); } ; CallExpression_ - : MemberExpression_ + : MemberExpression | CallExpression ; CallExpression : CallExpression_ Arguments { $$ = CYNew CYCall($1, $2); } + | SuperCall { CYNOT(@$); } | CallExpression { driver.context_ = $1; } MemberAccess { $3->SetLeft($1); $$ = $3; } ; +SuperCall + : LexSetRegExp "super" Arguments + ; + Arguments : "(" LexPushInOff ArgumentListOpt ")" LexPopIn { $$ = $3; } ; @@ -946,7 +1022,7 @@ ArgumentList_ ArgumentList : AssignmentExpression ArgumentList_ { $$ = CYNew CYArgument(NULL, $1, $2); } - | LexSetRegExp Word ":" AssignmentExpression ArgumentList_ { $$ = CYNew CYArgument($2, $4, $5); } + | LexSetRegExp "..." AssignmentExpression { CYNOT(@$); } ; ArgumentListOpt @@ -1065,7 +1141,7 @@ ConditionalExpression /* 12.14 Assignment Operators {{{ */ AssignmentExpression : ConditionalExpression { $$ = $1; } - // XXX: | YieldExpression { $$ = $1; } + | LexSetRegExp YieldExpression { $$ = $2; } | ArrowFunction { $$ = $1; } | LeftHandSideExpression "=" AssignmentExpression { $$ = CYNew CYAssign($1, $3); } | LeftHandSideExpression "*=" AssignmentExpression { $$ = CYNew CYMultiplyAssign($1, $3); } @@ -1089,8 +1165,7 @@ AssignmentExpressionOpt /* 12.15 Comma Operator ( , ) {{{ */ Expression : AssignmentExpression { $$ = $1; } - /* XXX: I have this backwards... */ - | AssignmentExpression "," Expression { $$ = CYNew CYCompound($1, $3); } + | Expression "," AssignmentExpression { $$ = CYNew CYCompound($1, $3); } ; ExpressionOpt @@ -1127,7 +1202,7 @@ Statement Declaration__ : HoistableDeclaration { $$ = $1; } - // XXX: | ClassDeclaration { $$ = $1; } + | ClassDeclaration { $$ = $1; } | LexicalDeclaration { $$ = $1; } ; @@ -1140,8 +1215,8 @@ Declaration ; HoistableDeclaration - : FunctionDeclaration - // XXX: | GeneratorDeclaration + : FunctionDeclaration { $$ = $1; } + | GeneratorDeclaration { $$ = $1; } ; BreakableStatement @@ -1207,16 +1282,51 @@ VariableDeclaration // XXX: | BindingPattern Initializer { $$ = CYNew CYDeclaration($1, $2); } ; /* }}} */ -/* 13.3.3+ Destructuring Binding Patterns {{{ */ -// XXX: * +/* 13.3.3 Destructuring Binding Patterns {{{ */ +BindingPattern + : ObjectBindingPattern + | ArrayBindingPattern + ; + +ObjectBindingPattern + : BRACE BindingPropertyListOpt "}" + ; + +ArrayBindingPattern + : "[" { CYNOT(@$); } + ; + +BindingPropertyList_ + : "," BindingPropertyListOpt + | + ; + +BindingPropertyList + : BindingProperty BindingPropertyList_ + ; + +BindingPropertyListOpt + : BindingPropertyList + | + ; + +BindingProperty + : SingleNameBinding + | PropertyName ":" BindingElement + ; BindingElement : SingleNameBinding { $$ = $1; } + | BindingPattern InitializerOpt { CYNOT(@$); } ; SingleNameBinding : BindingIdentifier InitializerOpt { $$ = CYNew CYDeclaration($1, $2); } ; + +BindingRestElement + : "..." BindingIdentifier + ; /* }}} */ /* 13.4 Empty Statement {{{ */ EmptyStatement @@ -1279,7 +1389,7 @@ BreakStatement /* }}} */ /* 13.10 The return Statement {{{ */ Return - : "return" LexNewLine + : "!return" LexNewLine ; ReturnStatement @@ -1353,7 +1463,7 @@ Finally CatchParameter : BindingIdentifier { $$ = $1; } - // XXX: BindingPattern + | BindingPattern { CYNOT(@$); } ; /* }}} */ /* 13.16 The debugger Statement {{{ */ @@ -1362,13 +1472,22 @@ DebuggerStatement ; /* }}} */ -/* 14.1+ Function Definitions {{{ */ +/* 14.1 Function Definitions {{{ */ FunctionDeclaration - : ";function" Identifier "(" FormalParameterListOpt ")" BRACE FunctionBody "}" { $$ = CYNew CYFunctionStatement($2, $4, $7); } + : ";function" BindingIdentifier "(" FormalParameters ")" BRACE FunctionBody "}" { $$ = CYNew CYFunctionStatement($2, $4, $7); } ; FunctionExpression - : "function" IdentifierOpt "(" LexPushInOff FormalParameterListOpt ")" LexPopIn BRACE LexPushInOff FunctionBody "}" LexPopIn { $$ = CYNew CYFunctionExpression($2, $5, $10); } + : "function" BindingIdentifierOpt "(" LexPushInOff FormalParameters ")" LexPopIn BRACE LexPushInOff FunctionBody "}" LexPopIn { $$ = CYNew CYFunctionExpression($2, $5, $10); } + ; + +StrictFormalParameters + : FormalParameters { $$ = $1; } + ; + +FormalParameters + : { $$ = NULL; } + | FormalParameterList ; FormalParameterList_ @@ -1377,25 +1496,24 @@ FormalParameterList_ ; FormalParameterList - // XXX: : FunctionRestParameter { $$ = $1; } - : FormalParameter FormalParameterList_ { $$ = CYNew CYFunctionParameter($1, $2); } + : FunctionRestParameter { CYNOT(@$); } + | FormalParameter FormalParameterList_ { $$ = CYNew CYFunctionParameter($1, $2); } ; -FormalParameterListOpt - : FormalParameterList - | { $$ = NULL; } +FunctionRestParameter + : BindingRestElement ; -/* XXX: FunctionRestParameter - : "..." BindingIdentifier { $$ = CYNew CYFunctionRestParameter($2); } - ;*/ - FormalParameter : BindingElement { $$ = $1; } ; FunctionBody - : StatementListOpt { $$ = $1; } + : LexPushYieldOff FunctionStatementList LexPopYield { $$ = $2; } + ; + +FunctionStatementList + : LexPushReturnOn StatementListOpt LexPopReturn { $$ = $2; } ; /* }}} */ /* 14.2 Arrow Function Definitions {{{ */ @@ -1413,11 +1531,90 @@ ConciseBody | LexSetRegExp ";{" LexPushInOff FunctionBody "}" LexPopIn { $$ = $4; } ; /* }}} */ -/* 14.3+ Method Definitions {{{ */ +/* 14.3 Method Definitions {{{ */ +MethodDefinition + : PropertyName "(" StrictFormalParameters ")" BRACE FunctionBody "}" { CYNOT(@$); /* $$ = CYNew CYFunctionMethod($1, $3, $6); */ } + | GeneratorMethod { $$ = $1; } + | "get" PropertyName "(" ")" BRACE FunctionBody "}" { CYNOT(@$); /* $$ = CYNew CYMethodGet($2, $6); */ } + | "set" PropertyName "(" PropertySetParameterList ")" BRACE FunctionBody "}" { CYNOT(@$); /* $$ = CYNew CYMethodSet($2, $4); */ } + ; + +PropertySetParameterList + : FormalParameter { $$ = $1; } + ; /* }}} */ -/* 14.4+ Generator Function Definitions {{{ */ +/* 14.4 Generator Function Definitions {{{ */ +GeneratorMethod + : "*" PropertyName "(" StrictFormalParameters ")" BRACE GeneratorBody "}" { CYNOT(@$); /* $$ = CYNew CYGeneratorMethod($2, $4, $7); */ } + ; + +GeneratorDeclaration + : ";function" "*" BindingIdentifier "(" FormalParameters ")" BRACE GeneratorBody "}" { CYNOT(@$); /* $$ = CYNew CYGeneratorStatement($3, $5, $8); */ } + ; + +GeneratorExpression + : "function" "*" BindingIdentifierOpt "(" FormalParameters ")" BRACE GeneratorBody "}" { CYNOT(@$); /* $$ = CYNew CYGeneratorExpression($3, $5, $8); */ } + ; + +GeneratorBody + : LexPushYieldOn FunctionStatementList LexPopYield { $$ = $2; } + ; + +Yield + : "!yield" LexNewLine LexNoStar + ; + +YieldExpression + : Yield LexSetRegExp "\n" { CYNOT(@$); /* $$ = CYNew CYYieldValue(NULL); */ } + | Yield AssignmentExpression { CYNOT(@$); /* $$ = CYNew CYYieldValue($2); */ } + | Yield LexSetRegExp YieldStar AssignmentExpression { CYNOT(@$); /* $$ = CYNew CYYieldGenerator($4); */ } + ; /* }}} */ -/* 14.5+ Class Definitions {{{ */ +/* 14.5 Class Definitions {{{ */ +ClassDeclaration + : ";class" BindingIdentifier ClassTail { CYNOT(@$); } + ; + +ClassExpression + : "class" BindingIdentifierOpt ClassTail { CYNOT(@$); } + ; + +ClassTail + : ClassHeritageOpt BRACE ClassBodyOpt "}" + ; + +ClassHeritage + : "extends" LeftHandSideExpression + ; + +ClassHeritageOpt + : ClassHeritage + | + ; + +ClassBody + : ClassElementList + ; + +ClassBodyOpt + : ClassBody + | + ; + +ClassElementList + : ClassElementListOpt ClassElement + ; + +ClassElementListOpt + : ClassElementList + | + ; + +ClassElement + : MethodDefinition + | "static" MethodDefinition + | ";" + ; /* }}} */ /* 15.1 Scripts {{{ */ @@ -1434,11 +1631,130 @@ ScriptBodyOpt | LexSetStatement LexSetRegExp { $$ = NULL; } ; /* }}} */ -/* 15.2+ Modules {{{ */ +/* 15.2 Modules {{{ */ +Module + : ModuleBodyOpt + ; + +ModuleBody + : ModuleItemList + ; + +ModuleBodyOpt + : ModuleBody + | + ; + +ModuleItemList + : ModuleItemListOpt ModuleItem + ; + +ModuleItemListOpt + : ModuleItemList + | + ; + +ModuleItem + : LexSetStatement LexSetRegExp ImportDeclaration + | LexSetStatement LexSetRegExp ExportDeclaration + | StatementListItem + ; /* }}} */ -/* 15.2.2+ Imports {{{ */ +/* 15.2.2 Imports {{{ */ +ImportDeclaration + : "import" ImportClause FromClause Terminator + | "import" ModuleSpecifier Terminator + ; + +ImportClause + : ImportedDefaultBinding + | NameSpaceImport + | NamedImports + | ImportedDefaultBinding "," NameSpaceImport + | ImportedDefaultBinding "," NamedImports + ; + +ImportedDefaultBinding + : ImportedBinding + ; + +NameSpaceImport + : "*" "as" ImportedBinding + ; + +NamedImports + : BRACE ImportsListOpt "}" + ; + +FromClause + : "from" ModuleSpecifier + ; + +ImportsList_ + : "," ImportsListOpt + | + ; + +ImportsList + : ImportSpecifier ImportsList_ + ; + +ImportsListOpt + : ImportsList + | + ; + +ImportSpecifier + : ImportedBinding + | IdentifierName "as" ImportedBinding + ; + +ModuleSpecifier + : StringLiteral + ; + +ImportedBinding + : BindingIdentifier + ; /* }}} */ -/* 15.2.3+ Exports {{{ */ +/* 15.2.3 Exports {{{ */ +ExportDeclaration_ + : "*" FromClause Terminator + | ExportClause FromClause Terminator + | ExportClause Terminator + | VariableStatement + | "default" LexSetStatement LexSetRegExp HoistableDeclaration + | "default" LexSetStatement LexSetRegExp ClassDeclaration + | "default" LexSetStatement AssignmentExpression Terminator + ; + +ExportDeclaration + : "export" LexSetStatement LexSetRegExp ExportDeclaration_ + | "export" Declaration + ; + +ExportClause + : ";{" ExportsListOpt "}" + ; + +ExportsList_ + : "," ExportsListOpt + | + ; + +ExportsList + : ExportSpecifier ExportsList_ + ; + +ExportsListOpt + : ExportsList + | + ; + +ExportSpecifier + : IdentifierName + | IdentifierName "as" IdentifierName + ; /* }}} */ @begin C @@ -1649,13 +1965,13 @@ PrimaryExpression @end /* Cycript: @import Directive {{{ */ -Module - : Module "." Word { $$ = CYNew CYModule($3, $1); } +ModulePath + : ModulePath "." Word { $$ = CYNew CYModule($3, $1); } | Word { $$ = CYNew CYModule($1); } ; Declaration__ - : "@import" Module { $$ = CYNew CYImport($2); } + : "@import" ModulePath { $$ = CYNew CYImport($2); } ; /* }}} */ @@ -1978,6 +2294,11 @@ ConditionalExpression : LogicalORExpression "?" LexPushInOff LexSetRegExp ":" LexPopIn AssignmentExpression { $$ = CYNew CYCondition($1, $1, $7); } ; /* }}} */ +/* JavaScript FTW: Named Arguments {{{ */ +ArgumentList + : LexSetRegExp Word ":" AssignmentExpression ArgumentList_ { $$ = CYNew CYArgument($2, $4, $5); } + ; +/* }}} */ /* JavaScript FTW: Ruby Blocks {{{ */ RubyProcParameterList_ : "," RubyProcParameterList { $$ = $2; } diff --git a/Driver.cpp b/Driver.cpp index 1fd0d6f..eab0bb9 100644 --- a/Driver.cpp +++ b/Driver.cpp @@ -38,7 +38,10 @@ CYDriver::CYDriver(CYPool &pool, std::istream &data, const std::string &filename mode_(AutoNone) { in_.push(false); + return_.push(false); template_.push(false); + yield_.push(false); + ScannerInit(); } @@ -46,7 +49,8 @@ CYDriver::~CYDriver() { ScannerDestroy(); } -bool CYDriver::Parse() { +bool CYDriver::Parse(CYMark mark) { + mark_ = mark; CYLocal local(&pool_); cy::parser parser(*this); #ifdef YYDEBUG diff --git a/Driver.hpp b/Driver.hpp index 2a0955a..0df39d7 100644 --- a/Driver.hpp +++ b/Driver.hpp @@ -31,6 +31,12 @@ #include "Location.hpp" #include "Parser.hpp" +enum CYMark { + CYMarkIgnore, + CYMarkScript, + CYMarkModule, +}; + class _visible CYDriver { public: CYPool &pool_; @@ -40,13 +46,16 @@ class _visible CYDriver { bool tail_; std::stack in_; + std::stack return_; std::stack template_; + std::stack yield_; bool newline_; bool last_; bool next_; std::istream &data_; + CYMark mark_; int debug_; bool strict_; @@ -106,7 +115,7 @@ class _visible CYDriver { CYDriver(CYPool &pool, std::istream &data, const std::string &filename = ""); ~CYDriver(); - bool Parse(); + bool Parse(CYMark mark = CYMarkScript); void Replace(CYOptions &options); void SetCondition(Condition condition); diff --git a/Parser.hpp b/Parser.hpp index 7602a63..1ea1451 100644 --- a/Parser.hpp +++ b/Parser.hpp @@ -1647,6 +1647,38 @@ struct CYReturn : virtual void Output(CYOutput &out, CYFlags flags) const; }; +struct CYYieldGenerator : + CYExpression +{ + CYExpression *value_; + + CYYieldGenerator(CYExpression *value) : + value_(value) + { + } + + CYPrecedence(0) + + virtual CYExpression *Replace(CYContext &context); + virtual void Output(CYOutput &out, CYFlags flags) const; +}; + +struct CYYieldValue : + CYExpression +{ + CYExpression *value_; + + CYYieldValue(CYExpression *value) : + value_(value) + { + } + + CYPrecedence(0) + + virtual CYExpression *Replace(CYContext &context); + virtual void Output(CYOutput &out, CYFlags flags) const; +}; + struct CYEmpty : CYStatement {