2 #include "Cycript.tab.hh"
3 int cylex(YYSTYPE *lvalp, YYLTYPE *llocp, void *scanner);
4 #define scanner driver.scanner_
14 CYArgument *argument_;
18 CYDeclaration *declaration_;
19 CYDeclarations *declarations_;
21 CYExpression *expression_;
23 CYForInitialiser *for_;
24 CYForInInitialiser *forin_;
25 CYIdentifier *identifier_;
30 CYParameter *parameter_;
31 CYProperty *property_;
33 CYStatement *statement_;
50 @$.begin.filename = @$.end.filename = &driver.filename_;
58 %parse-param { CYDriver &driver }
59 %lex-param { void *scanner }
62 %token AmpersandAmpersand "&&"
63 %token AmpersandEqual "&="
65 %token CarrotEqual "^="
67 %token EqualEqual "=="
68 %token EqualEqualEqual "==="
69 %token Exclamation "!"
70 %token ExclamationEqual "!="
71 %token ExclamationEqualEqual "!=="
73 %token HyphenEqual "-="
74 %token HyphenHyphen "--"
75 %token HyphenHyphen_ "\n--"
76 %token HyphenRight "->"
80 %token LeftLeftEqual "<<="
82 %token PercentEqual "%="
90 %token PlusPlus_ "\n++"
92 %token RightEqual ">="
93 %token RightRight ">>"
94 %token RightRightEqual ">>="
95 %token RightRightRight ">>>"
96 %token RightRightRightEqual ">>>="
98 %token SlashEqual "/="
100 %token StarEqual "*="
110 %token CloseParen ")"
113 %token CloseBrace "}"
115 %token OpenBracket "["
116 %token CloseBracket "]"
118 %token <word_> Break "break"
119 %token <word_> Case "case"
120 %token <word_> Catch "catch"
121 %token <word_> Continue "continue"
122 %token <word_> Default "default"
123 %token <word_> Delete "delete"
124 %token <word_> Do "do"
125 %token <word_> Else "else"
126 %token <false_> False "false"
127 %token <word_> Finally "finally"
128 %token <word_> For "for"
129 %token <word_> Function "function"
130 %token <word_> If "if"
131 %token <word_> In "in"
132 %token <word_> InstanceOf "instanceof"
133 %token <word_> New "new"
134 %token <null_> Null "null"
135 %token <word_> Return "return"
136 %token <word_> Switch "switch"
137 %token <this_> This "this"
138 %token <word_> Throw "throw"
139 %token <true_> True "true"
140 %token <word_> Try "try"
141 %token <word_> TypeOf "typeof"
142 %token <word_> Var "var"
143 %token <word_> Void "void"
144 %token <word_> While "while"
145 %token <word_> With "with"
147 %token <identifier_> Identifier
148 %token <number_> NumericLiteral
149 %token <string_> StringLiteral
151 %type <expression_> AdditiveExpression
152 %type <argument_> ArgumentList
153 %type <argument_> ArgumentList_
154 %type <argument_> ArgumentListOpt
155 %type <argument_> Arguments
156 %type <literal_> ArrayLiteral
157 %type <expression_> AssignmentExpression
158 %type <expression_> BitwiseANDExpression
159 %type <statement_> Block
160 %type <boolean_> BooleanLiteral
161 %type <expression_> BitwiseORExpression
162 %type <expression_> BitwiseXORExpression
163 %type <statement_> BreakStatement
164 %type <expression_> CallExpression
165 %type <clause_> CaseBlock
166 %type <clause_> CaseClause
167 %type <clause_> CaseClausesOpt
168 %type <catch_> CatchOpt
169 %type <expression_> ConditionalExpression
170 %type <statement_> ContinueStatement
171 %type <clause_> DefaultClause
172 %type <statement_> DoWhileStatement
173 %type <expression_> Element
174 %type <element_> ElementList
175 %type <element_> ElementList_
176 %type <statement_> ElseStatementOpt
177 %type <statement_> EmptyStatement
178 %type <expression_> EqualityExpression
179 %type <expression_> Expression
180 %type <expression_> Expression_
181 %type <expression_> ExpressionOpt
182 %type <statement_> ExpressionStatement
183 %type <statement_> FinallyOpt
184 %type <statement_> ForStatement
185 %type <for_> ForStatementInitialiser
186 %type <statement_> ForInStatement
187 %type <forin_> ForInStatementInitialiser
188 %type <parameter_> FormalParameterList
189 %type <parameter_> FormalParameterList_
190 %type <source_> FunctionBody
191 %type <source_> FunctionDeclaration
192 %type <expression_> FunctionExpression
193 %type <identifier_> IdentifierOpt
194 %type <statement_> IfStatement
195 %type <expression_> Initialiser
196 %type <expression_> InitialiserOpt
197 %type <statement_> IterationStatement
198 %type <statement_> LabelledStatement
199 %type <expression_> LeftHandSideExpression
200 %type <literal_> Literal
201 %type <expression_> LogicalANDExpression
202 %type <expression_> LogicalORExpression
203 %type <expression_> MemberExpression
204 %type <expression_> MultiplicativeExpression
205 %type <expression_> NewExpression
206 %type <null_> NullLiteral
207 %type <literal_> ObjectLiteral
208 %type <expression_> MessageExpression
209 %type <expression_> PostfixExpression
210 %type <expression_> PrimaryExpression
211 %type <source_> Program
212 %type <name_> PropertyName
213 %type <property_> PropertyNameAndValueList
214 %type <property_> PropertyNameAndValueList_
215 %type <property_> PropertyNameAndValueListOpt
216 %type <expression_> RelationalExpression
217 %type <statement_> ReturnStatement
218 %type <argument_> SelectorCall
219 %type <argument_> SelectorCall_
220 %type <argument_> SelectorList
221 %type <expression_> ShiftExpression
222 %type <source_> SourceElement
223 %type <source_> SourceElements
224 %type <statement_> Statement
225 %type <statement_> StatementListOpt
226 %type <statement_> SwitchStatement
227 %type <statement_> ThrowStatement
228 %type <statement_> TryStatement
229 %type <expression_> UnaryExpression
230 %type <declaration_> VariableDeclaration
231 %type <declarations_> VariableDeclarationList
232 %type <declarations_> VariableDeclarationList_
233 %type <statement_> VariableStatement
234 %type <argument_> VariadicCall
235 %type <statement_> WhileStatement
236 %type <statement_> WithStatement
238 %type <word_> WordOpt
270 : Identifier { $$ = $1; }
271 | "break" NewLineOpt { $$ = $1; }
272 | "case" { $$ = $1; }
273 | "catch" { $$ = $1; }
274 | "continue" NewLineOpt { $$ = $1; }
275 | "default" { $$ = $1; }
276 | "delete" { $$ = $1; }
278 | "else" { $$ = $1; }
279 | "false" { $$ = $1; }
280 | "finally" { $$ = $1; }
282 | "function" { $$ = $1; }
285 | "instanceof" { $$ = $1; }
287 | "null" { $$ = $1; }
288 | "return" NewLineOpt { $$ = $1; }
289 | "switch" { $$ = $1; }
290 | "this" { $$ = $1; }
291 | "throw" NewLineOpt { $$ = $1; }
292 | "true" { $$ = $1; }
294 | "typeof" { $$ = $1; }
296 | "void" { $$ = $1; }
297 | "while" { $$ = $1; }
298 | "with" { $$ = $1; }
302 : Identifier { $$ = $1; }
307 : NullLiteral { $$ = $1; }
308 | BooleanLiteral { $$ = $1; }
309 | NumericLiteral { $$ = $1; }
310 | StringLiteral { $$ = $1; }
314 : "null" { $$ = $1; }
318 : "true" { $$ = $1; }
319 | "false" { $$ = $1; }
322 /* Objective-C Extensions {{{ */
324 : "," AssignmentExpression VariadicCall { $$ = new(driver.pool_) CYArgument(NULL, $2, $3); }
329 : SelectorCall { $$ = $1; }
330 | VariadicCall { $$ = $1; }
334 : WordOpt ":" AssignmentExpression SelectorCall_ { $$ = new(driver.pool_) CYArgument($1 ?: new(driver.pool_) CYBlank(), $3, $4); }
338 : SelectorCall { $$ = $1; }
339 | Word { $$ = new(driver.pool_) CYArgument($1, NULL); }
343 : "[" AssignmentExpression SelectorList "]" { $$ = new(driver.pool_) CYMessage($2, $3); }
347 /* 11.1 Primary Expressions {{{ */
349 : "this" { $$ = $1; }
350 | Identifier { $$ = new(driver.pool_) CYVariable($1); }
351 | Literal { $$ = $1; }
352 | ArrayLiteral { $$ = $1; }
353 | ObjectLiteral { $$ = $1; }
354 | "(" Expression ")" { $$ = $2; }
355 | MessageExpression { $$ = $1; }
358 /* 11.1.4 Array Initialiser {{{ */
360 : "[" ElementList "]" { $$ = $2; }
364 : AssignmentExpression { $$ = $1; }
369 : "," ElementList { $$ = $2; }
374 : Element ElementList_ { $$ = new(driver.pool_) CYElement($1, $2); }
377 /* 11.1.5 Object Initialiser {{{ */
379 : "{" PropertyNameAndValueListOpt "}" { $$ = $2; }
382 PropertyNameAndValueList_
383 : "," PropertyNameAndValueList { $$ = $2; }
387 PropertyNameAndValueListOpt
388 : PropertyNameAndValueList { $$ = $1; }
392 PropertyNameAndValueList
393 : PropertyName ":" AssignmentExpression PropertyNameAndValueList_ { $$ = new(driver.pool_) CYProperty($1, $3, $4); }
397 : Identifier { $$ = $1; }
398 | StringLiteral { $$ = $1; }
399 | NumericLiteral { $$ = $1; }
404 : PrimaryExpression { $$ = $1; }
405 | FunctionExpression { $$ = $1; }
406 | MemberExpression "[" Expression "]" { $$ = new(driver.pool_) CYMember($1, $3); }
407 | MemberExpression "." Identifier { $$ = new(driver.pool_) CYMember($1, new(driver.pool_) CYString($3)); }
408 | "new" MemberExpression Arguments { $$ = new(driver.pool_) CYNew($2, $3); }
412 : MemberExpression { $$ = $1; }
413 | "new" NewExpression { $$ = new(driver.pool_) CYNew($2, NULL); }
417 : MemberExpression Arguments { $$ = new(driver.pool_) CYCall($1, $2); }
418 | CallExpression Arguments { $$ = new(driver.pool_) CYCall($1, $2); }
419 | CallExpression "[" Expression "]" { $$ = new(driver.pool_) CYMember($1, $3); }
420 | CallExpression "." Identifier { $$ = new(driver.pool_) CYMember($1, new(driver.pool_) CYString($3)); }
424 : "," ArgumentList { $$ = $2; }
429 : ArgumentList { $$ = $1; }
434 : AssignmentExpression ArgumentList_ { $$ = new(driver.pool_) CYArgument(NULL, $1, $2); }
438 : "(" ArgumentListOpt ")" { $$ = $2; }
441 LeftHandSideExpression
442 : NewExpression { $$ = $1; }
443 | CallExpression { $$ = $1; }
444 | "*" LeftHandSideExpression { $$ = new(driver.pool_) CYIndirect($2); }
448 : LeftHandSideExpression { $$ = $1; }
449 | LeftHandSideExpression "++" { $$ = new(driver.pool_) CYPostIncrement($1); }
450 | LeftHandSideExpression "--" { $$ = new(driver.pool_) CYPostDecrement($1); }
454 : PostfixExpression { $$ = $1; }
455 | "delete" UnaryExpression { $$ = new(driver.pool_) CYDelete($2); }
456 | "void" UnaryExpression { $$ = new(driver.pool_) CYVoid($2); }
457 | "typeof" UnaryExpression { $$ = new(driver.pool_) CYTypeOf($2); }
458 | "++" UnaryExpression { $$ = new(driver.pool_) CYPreIncrement($2); }
459 | "\n++" UnaryExpression { $$ = new(driver.pool_) CYPreIncrement($2); }
460 | "--" UnaryExpression { $$ = new(driver.pool_) CYPreDecrement($2); }
461 | "\n--" UnaryExpression { $$ = new(driver.pool_) CYPreDecrement($2); }
462 | "+" UnaryExpression { $$ = $2; }
463 | "-" UnaryExpression { $$ = new(driver.pool_) CYNegate($2); }
464 | "~" UnaryExpression { $$ = new(driver.pool_) CYBitwiseNot($2); }
465 | "!" UnaryExpression { $$ = new(driver.pool_) CYLogicalNot($2); }
466 | "&" UnaryExpression { $$ = new(driver.pool_) CYAddressOf($2); }
469 MultiplicativeExpression
470 : UnaryExpression { $$ = $1; }
471 | MultiplicativeExpression "*" UnaryExpression { $$ = new(driver.pool_) CYMultiply($1, $3); }
472 | MultiplicativeExpression "/" UnaryExpression { $$ = new(driver.pool_) CYDivide($1, $3); }
473 | MultiplicativeExpression "%" UnaryExpression { $$ = new(driver.pool_) CYModulus($1, $3); }
477 : MultiplicativeExpression { $$ = $1; }
478 | AdditiveExpression "+" MultiplicativeExpression { $$ = new(driver.pool_) CYAdd($1, $3); }
479 | AdditiveExpression "-" MultiplicativeExpression { $$ = new(driver.pool_) CYSubtract($1, $3); }
483 : AdditiveExpression { $$ = $1; }
484 | ShiftExpression "<<" AdditiveExpression { $$ = new(driver.pool_) CYShiftLeft($1, $3); }
485 | ShiftExpression ">>" AdditiveExpression { $$ = new(driver.pool_) CYShiftRightSigned($1, $3); }
486 | ShiftExpression ">>>" AdditiveExpression { $$ = new(driver.pool_) CYShiftRightUnsigned($1, $3); }
490 : ShiftExpression { $$ = $1; }
491 | RelationalExpression "<" ShiftExpression { $$ = new(driver.pool_) CYLess($1, $3); }
492 | RelationalExpression ">" ShiftExpression { $$ = new(driver.pool_) CYGreater($1, $3); }
493 | RelationalExpression "<=" ShiftExpression { $$ = new(driver.pool_) CYLessOrEqual($1, $3); }
494 | RelationalExpression ">=" ShiftExpression { $$ = new(driver.pool_) CYGreaterOrEqual($1, $3); }
495 | RelationalExpression "instanceof" ShiftExpression { $$ = new(driver.pool_) CYInstanceOf($1, $3); }
496 | RelationalExpression "in" ShiftExpression { $$ = new(driver.pool_) CYIn($1, $3); }
500 : RelationalExpression { $$ = $1; }
501 | EqualityExpression "==" RelationalExpression { $$ = new(driver.pool_) CYEqual($1, $3); }
502 | EqualityExpression "!=" RelationalExpression { $$ = new(driver.pool_) CYNotEqual($1, $3); }
503 | EqualityExpression "===" RelationalExpression { $$ = new(driver.pool_) CYIdentical($1, $3); }
504 | EqualityExpression "!==" RelationalExpression { $$ = new(driver.pool_) CYNotIdentical($1, $3); }
508 : EqualityExpression { $$ = $1; }
509 | BitwiseANDExpression "&" EqualityExpression { $$ = new(driver.pool_) CYBitwiseAnd($1, $3); }
513 : BitwiseANDExpression { $$ = $1; }
514 | BitwiseXORExpression "^" BitwiseANDExpression { $$ = new(driver.pool_) CYBitwiseXOr($1, $3); }
518 : BitwiseXORExpression { $$ = $1; }
519 | BitwiseORExpression "|" BitwiseXORExpression { $$ = new(driver.pool_) CYBitwiseOr($1, $3); }
523 : BitwiseORExpression { $$ = $1; }
524 | LogicalANDExpression "&&" BitwiseORExpression { $$ = new(driver.pool_) CYLogicalAnd($1, $3); }
528 : LogicalANDExpression { $$ = $1; }
529 | LogicalORExpression "||" LogicalANDExpression { $$ = new(driver.pool_) CYLogicalOr($1, $3); }
532 ConditionalExpression
533 : LogicalORExpression { $$ = $1; }
534 | LogicalORExpression "?" AssignmentExpression ":" AssignmentExpression { $$ = new(driver.pool_) CYCondition($1, $3, $5); }
538 : ConditionalExpression { $$ = $1; }
539 | LeftHandSideExpression "=" AssignmentExpression { $$ = new(driver.pool_) CYAssign($1, $3); }
540 | LeftHandSideExpression "*=" AssignmentExpression { $$ = new(driver.pool_) CYMultiplyAssign($1, $3); }
541 | LeftHandSideExpression "/=" AssignmentExpression { $$ = new(driver.pool_) CYDivideAssign($1, $3); }
542 | LeftHandSideExpression "%=" AssignmentExpression { $$ = new(driver.pool_) CYModulusAssign($1, $3); }
543 | LeftHandSideExpression "+=" AssignmentExpression { $$ = new(driver.pool_) CYAddAssign($1, $3); }
544 | LeftHandSideExpression "-=" AssignmentExpression { $$ = new(driver.pool_) CYSubtractAssign($1, $3); }
545 | LeftHandSideExpression "<<=" AssignmentExpression { $$ = new(driver.pool_) CYShiftLeftAssign($1, $3); }
546 | LeftHandSideExpression ">>=" AssignmentExpression { $$ = new(driver.pool_) CYShiftRightSignedAssign($1, $3); }
547 | LeftHandSideExpression ">>>=" AssignmentExpression { $$ = new(driver.pool_) CYShiftRightUnsignedAssign($1, $3); }
548 | LeftHandSideExpression "&=" AssignmentExpression { $$ = new(driver.pool_) CYBitwiseAndAssign($1, $3); }
549 | LeftHandSideExpression "^=" AssignmentExpression { $$ = new(driver.pool_) CYBitwiseXOrAssign($1, $3); }
550 | LeftHandSideExpression "|=" AssignmentExpression { $$ = new(driver.pool_) CYBitwiseOrAssign($1, $3); }
554 : "," Expression { $$ = $2; }
559 : Expression { $$ = $1; }
564 : AssignmentExpression Expression_ { if ($1) { $1->SetNext($2); $$ = $1; } else $$ = $2; }
569 | VariableStatement { $$ = $1; }
570 | EmptyStatement { $$ = $1; }
571 | ExpressionStatement { $$ = $1; }
572 | IfStatement { $$ = $1; }
573 | IterationStatement { $$ = $1; }
574 | ContinueStatement { $$ = $1; }
575 | BreakStatement { $$ = $1; }
576 | ReturnStatement { $$ = $1; }
577 | WithStatement { $$ = $1; }
578 | LabelledStatement { $$ = $1; }
579 | SwitchStatement { $$ = $1; }
580 | ThrowStatement { $$ = $1; }
581 | TryStatement { $$ = $1; }
585 : "{" StatementListOpt "}" { $$ = $2 ?: new(driver.pool_) CYEmpty(); }
589 : Statement StatementListOpt { $1->SetNext($2); $$ = $1; }
594 : "var" VariableDeclarationList Terminator { $$ = $2; }
597 VariableDeclarationList_
598 : "," VariableDeclarationList { $$ = $2; }
602 VariableDeclarationList
603 : VariableDeclaration VariableDeclarationList_ { $$ = new(driver.pool_) CYDeclarations($1, $2); }
607 : Identifier InitialiserOpt { $$ = new(driver.pool_) CYDeclaration($1, $2); }
611 : Initialiser { $$ = $1; }
616 : "=" AssignmentExpression { $$ = $2; }
620 : ";" { $$ = new(driver.pool_) CYEmpty(); }
624 : Expression Terminator { $$ = new(driver.pool_) CYExpress($1); }
628 : "else" Statement { $$ = $2; }
629 | %prec "if" { $$ = NULL; }
633 : "if" "(" Expression ")" Statement ElseStatementOpt { $$ = new(driver.pool_) CYIf($3, $5, $6); }
637 : DoWhileStatement { $$ = $1; }
638 | WhileStatement { $$ = $1; }
639 | ForStatement { $$ = $1; }
640 | ForInStatement { $$ = $1; }
644 : "do" Statement "while" "(" Expression ")" TerminatorOpt { $$ = new(driver.pool_) CYDoWhile($5, $2); }
648 : "while" "(" Expression ")" Statement { $$ = new(driver.pool_) CYWhile($3, $5); }
652 : "for" "(" ForStatementInitialiser ";" ExpressionOpt ";" ExpressionOpt ")" Statement { $$ = new(driver.pool_) CYFor($3, $5, $7, $9); }
655 ForStatementInitialiser
656 : ExpressionOpt { $$ = $1; }
657 | "var" VariableDeclarationList { $$ = $2; }
661 : "for" "(" ForInStatementInitialiser "in" Expression ")" Statement { $$ = new(driver.pool_) CYForIn($3, $5, $7); }
664 ForInStatementInitialiser
665 : LeftHandSideExpression { $$ = $1; }
666 | "var" VariableDeclaration { $$ = $2; }
670 : "continue" IdentifierOpt Terminator { $$ = new(driver.pool_) CYContinue($2); }
674 : "break" IdentifierOpt Terminator { $$ = new(driver.pool_) CYBreak($2); }
678 : "return" ExpressionOpt Terminator { $$ = new(driver.pool_) CYReturn($2); }
682 : "with" "(" Expression ")" Statement { $$ = new(driver.pool_) CYWith($3, $5); }
686 : "switch" "(" Expression ")" CaseBlock { $$ = new(driver.pool_) CYSwitch($3, $5); }
690 : "{" CaseClausesOpt "}" { $$ = $2; }
694 : CaseClause CaseClausesOpt { $1->SetNext($2); $$ = $1; }
695 | DefaultClause CaseClausesOpt { $1->SetNext($2); $$ = $1; }
700 : "case" Expression ":" StatementListOpt { $$ = new(driver.pool_) CYClause($2, $4); }
704 : "default" ":" StatementListOpt { $$ = new(driver.pool_) CYClause(NULL, $3); }
708 : Identifier ":" Statement { $3->AddLabel($1); $$ = $3; }
712 : "throw" Expression Terminator { $$ = new(driver.pool_) CYThrow($2); }
716 : "try" Block CatchOpt FinallyOpt { $$ = new(driver.pool_) CYTry($2, $3, $4); }
720 : "catch" "(" Identifier ")" Block { $$ = new(driver.pool_) CYCatch($3, $5); }
725 : "finally" Block { $$ = $2; }
730 : "function" Identifier "(" FormalParameterList ")" "{" FunctionBody "}" { $$ = new(driver.pool_) CYFunction($2, $4, $7); }
734 : "function" IdentifierOpt "(" FormalParameterList ")" "{" FunctionBody "}" { $$ = new(driver.pool_) CYLambda($2, $4, $7); }
738 : "," FormalParameterList { $$ = $2; }
743 : Identifier FormalParameterList_ { $$ = new(driver.pool_) CYParameter($1, $2); }
748 : SourceElements { $$ = $1; }
752 : SourceElements { driver.source_.push_back($1); $$ = $1; }
756 : SourceElement SourceElements { $1->SetNext($2); $$ = $1; }
761 : SourceElement { driver.source_.push_back($2); if (driver.filename_.empty() && false) YYACCEPT; $2->Show(std::cout); }
765 : Statement { $$ = $1; }
766 | FunctionDeclaration { $$ = $1; }