2 #include "Cycript.tab.hh"
3 int cylex(YYSTYPE *lvalp, YYLTYPE *llocp, void *scanner);
4 #define scanner driver.scanner_
12 CYArgument *argument_;
16 CYDeclaration *declaration_;
17 CYDeclarations *declarations_;
19 CYExpression *expression_;
21 CYForInitialiser *for_;
22 CYForInInitialiser *forin_;
23 CYIdentifier *identifier_;
28 CYParameter *parameter_;
29 CYProperty *property_;
31 CYStatement *statement_;
45 @$.begin.filename = @$.end.filename = &driver.filename_;
53 %parse-param { CYDriver &driver }
54 %lex-param { void *scanner }
57 %token AmpersandAmpersand "&&"
58 %token AmpersandEqual "&="
60 %token CarrotEqual "^="
62 %token EqualEqual "=="
63 %token EqualEqualEqual "==="
64 %token Exclamation "!"
65 %token ExclamationEqual "!="
66 %token ExclamationEqualEqual "!=="
68 %token HyphenEqual "-="
69 %token HyphenHyphen "--"
70 %token HyphenRight "->"
74 %token LeftLeftEqual "<<="
76 %token PercentEqual "%="
85 %token RightEqual ">="
86 %token RightRight ">>"
87 %token RightRightEqual ">>="
88 %token RightRightRight ">>>"
89 %token RightRightRightEqual ">>>="
91 %token SlashEqual "/="
102 %token CloseParen ")"
105 %token CloseBrace "}"
107 %token OpenBracket "["
108 %token CloseBracket "]"
110 %token <word_> Break "break"
111 %token <word_> Case "case"
112 %token <word_> Catch "catch"
113 %token <word_> Continue "continue"
114 %token <word_> Default "default"
115 %token <word_> Delete "delete"
116 %token <word_> Do "do"
117 %token <word_> Else "else"
118 %token <false_> False "false"
119 %token <word_> Finally "finally"
120 %token <word_> For "for"
121 %token <word_> Function "function"
122 %token <word_> If "if"
123 %token <word_> In "in"
124 %token <word_> InstanceOf "instanceof"
125 %token <word_> New "new"
126 %token <null_> Null "null"
127 %token <word_> Return "return"
128 %token <word_> Switch "switch"
129 %token <this_> This "this"
130 %token <word_> Throw "throw"
131 %token <true_> True "true"
132 %token <word_> Try "try"
133 %token <word_> TypeOf "typeof"
134 %token <word_> Var "var"
135 %token <word_> Void "void"
136 %token <word_> While "while"
137 %token <word_> With "with"
139 %token <identifier_> Identifier
140 %token <number_> NumericLiteral
141 %token <string_> StringLiteral
143 %type <expression_> AdditiveExpression
144 %type <argument_> ArgumentList
145 %type <argument_> ArgumentList_
146 %type <argument_> ArgumentListOpt
147 %type <argument_> Arguments
148 %type <literal_> ArrayLiteral
149 %type <expression_> AssignmentExpression
150 %type <expression_> BitwiseANDExpression
151 %type <statement_> Block
152 %type <boolean_> BooleanLiteral
153 %type <expression_> BitwiseORExpression
154 %type <expression_> BitwiseXORExpression
155 %type <statement_> BreakStatement
156 %type <expression_> CallExpression
157 %type <clause_> CaseBlock
158 %type <clause_> CaseClause
159 %type <clause_> CaseClausesOpt
160 %type <catch_> CatchOpt
161 %type <expression_> ConditionalExpression
162 %type <statement_> ContinueStatement
163 %type <clause_> DefaultClause
164 %type <statement_> DoWhileStatement
165 %type <expression_> Element
166 %type <element_> ElementList
167 %type <element_> ElementList_
168 %type <statement_> ElseStatementOpt
169 %type <statement_> EmptyStatement
170 %type <expression_> EqualityExpression
171 %type <expression_> Expression
172 %type <expression_> Expression_
173 %type <expression_> ExpressionOpt
174 %type <statement_> ExpressionStatement
175 %type <statement_> FinallyOpt
176 %type <statement_> ForStatement
177 %type <for_> ForStatementInitialiser
178 %type <statement_> ForInStatement
179 %type <forin_> ForInStatementInitialiser
180 %type <parameter_> FormalParameterList
181 %type <parameter_> FormalParameterList_
182 %type <source_> FunctionBody
183 %type <source_> FunctionDeclaration
184 %type <expression_> FunctionExpression
185 %type <identifier_> IdentifierOpt
186 %type <statement_> IfStatement
187 %type <expression_> Initialiser
188 %type <expression_> InitialiserOpt
189 %type <statement_> IterationStatement
190 %type <statement_> LabelledStatement
191 %type <expression_> LeftHandSideExpression
192 %type <literal_> Literal
193 %type <expression_> LogicalANDExpression
194 %type <expression_> LogicalORExpression
195 %type <expression_> MemberExpression
196 %type <expression_> MultiplicativeExpression
197 %type <expression_> NewExpression
198 %type <null_> NullLiteral
199 %type <literal_> ObjectLiteral
200 %type <expression_> MessageExpression
201 %type <expression_> PostfixExpression
202 %type <expression_> PrimaryExpression
203 %type <source_> Program
204 %type <name_> PropertyName
205 %type <property_> PropertyNameAndValueList
206 %type <property_> PropertyNameAndValueList_
207 %type <property_> PropertyNameAndValueListOpt
208 %type <expression_> RelationalExpression
209 %type <statement_> ReturnStatement
210 %type <argument_> SelectorCall
211 %type <argument_> SelectorCall_
212 %type <argument_> SelectorList
213 %type <expression_> ShiftExpression
214 %type <source_> SourceElement
215 %type <source_> SourceElements
216 %type <statement_> Statement
217 %type <statement_> StatementListOpt
218 %type <statement_> SwitchStatement
219 %type <statement_> ThrowStatement
220 %type <statement_> TryStatement
221 %type <expression_> UnaryExpression
222 %type <declaration_> VariableDeclaration
223 %type <declarations_> VariableDeclarationList
224 %type <declarations_> VariableDeclarationList_
225 %type <statement_> VariableStatement
226 %type <argument_> VariadicCall
227 %type <statement_> WhileStatement
228 %type <statement_> WithStatement
230 %type <word_> WordOpt
242 : Identifier { $$ = $1; }
243 | "break" { $$ = $1; }
244 | "case" { $$ = $1; }
245 | "catch" { $$ = $1; }
246 | "continue" { $$ = $1; }
247 | "default" { $$ = $1; }
248 | "delete" { $$ = $1; }
250 | "else" { $$ = $1; }
251 | "false" { $$ = $1; }
252 | "finally" { $$ = $1; }
254 | "function" { $$ = $1; }
257 | "instanceof" { $$ = $1; }
259 | "null" { $$ = $1; }
260 | "return" { $$ = $1; }
261 | "switch" { $$ = $1; }
262 | "this" { $$ = $1; }
263 | "throw" { $$ = $1; }
264 | "true" { $$ = $1; }
266 | "typeof" { $$ = $1; }
268 | "void" { $$ = $1; }
269 | "while" { $$ = $1; }
270 | "with" { $$ = $1; }
274 : Identifier { $$ = $1; }
279 : NullLiteral { $$ = $1; }
280 | BooleanLiteral { $$ = $1; }
281 | NumericLiteral { $$ = $1; }
282 | StringLiteral { $$ = $1; }
286 : "null" { $$ = $1; }
290 : "true" { $$ = $1; }
291 | "false" { $$ = $1; }
294 /* Objective-C Extensions {{{ */
296 : "," AssignmentExpression VariadicCall { $$ = new CYArgument(NULL, $2, $3); }
301 : SelectorCall { $$ = $1; }
302 | VariadicCall { $$ = $1; }
306 : WordOpt ":" AssignmentExpression SelectorCall_ { $$ = new CYArgument($1 ?: new CYBlank(), $3, $4); }
310 : SelectorCall { $$ = $1; }
311 | Word { $$ = new CYArgument($1, NULL); }
315 : "[" AssignmentExpression SelectorList "]" { $$ = new CYMessage($2, $3); }
319 /* 11.1 Primary Expressions {{{ */
321 : "this" { $$ = $1; }
322 | Identifier { $$ = new CYVariable($1); }
323 | Literal { $$ = $1; }
324 | ArrayLiteral { $$ = $1; }
325 | ObjectLiteral { $$ = $1; }
326 | "(" Expression ")" { $$ = $2; }
327 | MessageExpression { $$ = $1; }
330 /* 11.1.4 Array Initialiser {{{ */
332 : "[" ElementList "]" { $$ = $2; }
336 : AssignmentExpression { $$ = $1; }
341 : "," ElementList { $$ = $2; }
346 : Element ElementList_ { $$ = new CYElement($1, $2); }
349 /* 11.1.5 Object Initialiser {{{ */
351 : "{" PropertyNameAndValueListOpt "}" { $$ = $2; }
354 PropertyNameAndValueList_
355 : "," PropertyNameAndValueList { $$ = $2; }
359 PropertyNameAndValueListOpt
360 : PropertyNameAndValueList { $$ = $1; }
364 PropertyNameAndValueList
365 : PropertyName ":" AssignmentExpression PropertyNameAndValueList_ { $$ = new CYProperty($1, $3, $4); }
369 : Identifier { $$ = $1; }
370 | StringLiteral { $$ = $1; }
371 | NumericLiteral { $$ = $1; }
376 : PrimaryExpression { $$ = $1; }
377 | FunctionExpression { $$ = $1; }
378 | MemberExpression "[" Expression "]" { $$ = new CYMember($1, $3); }
379 | MemberExpression "." Identifier { $$ = new CYMember($1, new CYString($3)); }
380 | "new" MemberExpression Arguments { $$ = new CYNew($2, $3); }
384 : MemberExpression { $$ = $1; }
385 | "new" NewExpression { $$ = new CYNew($2, NULL); }
389 : MemberExpression Arguments { $$ = new CYCall($1, $2); }
390 | CallExpression Arguments { $$ = new CYCall($1, $2); }
391 | CallExpression "[" Expression "]" { $$ = new CYMember($1, $3); }
392 | CallExpression "." Identifier { $$ = new CYMember($1, new CYString($3)); }
396 : "," ArgumentList { $$ = $2; }
401 : ArgumentList { $$ = $1; }
406 : AssignmentExpression ArgumentList_ { $$ = new CYArgument(NULL, $1, $2); }
410 : "(" ArgumentListOpt ")" { $$ = $2; }
413 LeftHandSideExpression
414 : NewExpression { $$ = $1; }
415 | CallExpression { $$ = $1; }
416 | "*" LeftHandSideExpression { $$ = new CYIndirect($2); }
420 : LeftHandSideExpression { $$ = $1; }
421 | LeftHandSideExpression "++" { $$ = new CYPostIncrement($1); }
422 | LeftHandSideExpression "--" { $$ = new CYPostDecrement($1); }
426 : PostfixExpression { $$ = $1; }
427 | "delete" UnaryExpression { $$ = new CYDelete($2); }
428 | "void" UnaryExpression { $$ = new CYVoid($2); }
429 | "typeof" UnaryExpression { $$ = new CYTypeOf($2); }
430 | "++" UnaryExpression { $$ = new CYPreIncrement($2); }
431 | "--" UnaryExpression { $$ = new CYPreDecrement($2); }
432 | "+" UnaryExpression { $$ = $2; }
433 | "-" UnaryExpression { $$ = new CYNegate($2); }
434 | "~" UnaryExpression { $$ = new CYBitwiseNot($2); }
435 | "!" UnaryExpression { $$ = new CYLogicalNot($2); }
436 | "&" UnaryExpression { $$ = new CYAddressOf($2); }
439 MultiplicativeExpression
440 : UnaryExpression { $$ = $1; }
441 | MultiplicativeExpression "*" UnaryExpression { $$ = new CYMultiply($1, $3); }
442 | MultiplicativeExpression "/" UnaryExpression { $$ = new CYDivide($1, $3); }
443 | MultiplicativeExpression "%" UnaryExpression { $$ = new CYModulus($1, $3); }
447 : MultiplicativeExpression { $$ = $1; }
448 | AdditiveExpression "+" MultiplicativeExpression { $$ = new CYAdd($1, $3); }
449 | AdditiveExpression "-" MultiplicativeExpression { $$ = new CYSubtract($1, $3); }
453 : AdditiveExpression { $$ = $1; }
454 | ShiftExpression "<<" AdditiveExpression { $$ = new CYShiftLeft($1, $3); }
455 | ShiftExpression ">>" AdditiveExpression { $$ = new CYShiftRightSigned($1, $3); }
456 | ShiftExpression ">>>" AdditiveExpression { $$ = new CYShiftRightUnsigned($1, $3); }
460 : ShiftExpression { $$ = $1; }
461 | RelationalExpression "<" ShiftExpression { $$ = new CYLess($1, $3); }
462 | RelationalExpression ">" ShiftExpression { $$ = new CYGreater($1, $3); }
463 | RelationalExpression "<=" ShiftExpression { $$ = new CYLessOrEqual($1, $3); }
464 | RelationalExpression ">=" ShiftExpression { $$ = new CYGreaterOrEqual($1, $3); }
465 | RelationalExpression "instanceof" ShiftExpression { $$ = new CYInstanceOf($1, $3); }
466 | RelationalExpression "in" ShiftExpression { $$ = new CYIn($1, $3); }
470 : RelationalExpression { $$ = $1; }
471 | EqualityExpression "==" RelationalExpression { $$ = new CYEqual($1, $3); }
472 | EqualityExpression "!=" RelationalExpression { $$ = new CYNotEqual($1, $3); }
473 | EqualityExpression "===" RelationalExpression { $$ = new CYIdentical($1, $3); }
474 | EqualityExpression "!==" RelationalExpression { $$ = new CYNotIdentical($1, $3); }
478 : EqualityExpression { $$ = $1; }
479 | BitwiseANDExpression "&" EqualityExpression { $$ = new CYBitwiseAnd($1, $3); }
483 : BitwiseANDExpression { $$ = $1; }
484 | BitwiseXORExpression "^" BitwiseANDExpression { $$ = new CYBitwiseXOr($1, $3); }
488 : BitwiseXORExpression { $$ = $1; }
489 | BitwiseORExpression "|" BitwiseXORExpression { $$ = new CYBitwiseOr($1, $3); }
493 : BitwiseORExpression { $$ = $1; }
494 | LogicalANDExpression "&&" BitwiseORExpression { $$ = new CYLogicalAnd($1, $3); }
498 : LogicalANDExpression { $$ = $1; }
499 | LogicalORExpression "||" LogicalANDExpression { $$ = new CYLogicalOr($1, $3); }
502 ConditionalExpression
503 : LogicalORExpression { $$ = $1; }
504 | LogicalORExpression "?" AssignmentExpression ":" AssignmentExpression { $$ = new CYCondition($1, $3, $5); }
508 : ConditionalExpression { $$ = $1; }
509 | LeftHandSideExpression "=" AssignmentExpression { $$ = new CYAssign($1, $3); }
510 | LeftHandSideExpression "*=" AssignmentExpression { $$ = new CYMultiplyAssign($1, $3); }
511 | LeftHandSideExpression "/=" AssignmentExpression { $$ = new CYDivideAssign($1, $3); }
512 | LeftHandSideExpression "%=" AssignmentExpression { $$ = new CYModulusAssign($1, $3); }
513 | LeftHandSideExpression "+=" AssignmentExpression { $$ = new CYAddAssign($1, $3); }
514 | LeftHandSideExpression "-=" AssignmentExpression { $$ = new CYSubtractAssign($1, $3); }
515 | LeftHandSideExpression "<<=" AssignmentExpression { $$ = new CYShiftLeftAssign($1, $3); }
516 | LeftHandSideExpression ">>=" AssignmentExpression { $$ = new CYShiftRightSignedAssign($1, $3); }
517 | LeftHandSideExpression ">>>=" AssignmentExpression { $$ = new CYShiftRightUnsignedAssign($1, $3); }
518 | LeftHandSideExpression "&=" AssignmentExpression { $$ = new CYBitwiseAndAssign($1, $3); }
519 | LeftHandSideExpression "^=" AssignmentExpression { $$ = new CYBitwiseXOrAssign($1, $3); }
520 | LeftHandSideExpression "|=" AssignmentExpression { $$ = new CYBitwiseOrAssign($1, $3); }
524 : "," Expression { $$ = $2; }
529 : Expression { $$ = $1; }
534 : AssignmentExpression Expression_ { if ($1 == NULL) $$ = $2; else { $1->SetNext($2); $$ = $1; } }
539 | VariableStatement { $$ = $1; }
540 | EmptyStatement { $$ = $1; }
541 | ExpressionStatement { $$ = $1; }
542 | IfStatement { $$ = $1; }
543 | IterationStatement { $$ = $1; }
544 | ContinueStatement { $$ = $1; }
545 | BreakStatement { $$ = $1; }
546 | ReturnStatement { $$ = $1; }
547 | WithStatement { $$ = $1; }
548 | LabelledStatement { $$ = $1; }
549 | SwitchStatement { $$ = $1; }
550 | ThrowStatement { $$ = $1; }
551 | TryStatement { $$ = $1; }
555 : "{" StatementListOpt "}" { $$ = $2 ?: new CYEmpty(); }
559 : Statement StatementListOpt { $1->SetNext($2); $$ = $1; }
564 : "var" VariableDeclarationList ";" { $$ = $2; }
567 VariableDeclarationList_
568 : "," VariableDeclarationList { $$ = $2; }
572 VariableDeclarationList
573 : VariableDeclaration VariableDeclarationList_ { $$ = new CYDeclarations($1, $2); }
577 : Identifier InitialiserOpt { $$ = new CYDeclaration($1, $2); }
581 : Initialiser { $$ = $1; }
586 : "=" AssignmentExpression { $$ = $2; }
590 : ";" { $$ = new CYEmpty(); }
594 : Expression ";" { $$ = new CYExpress($1); }
598 : "else" Statement { $$ = $2; }
603 : "if" "(" Expression ")" Statement ElseStatementOpt { $$ = new CYIf($3, $5, $6); }
607 : DoWhileStatement { $$ = $1; }
608 | WhileStatement { $$ = $1; }
609 | ForStatement { $$ = $1; }
610 | ForInStatement { $$ = $1; }
614 : "do" Statement "while" "(" Expression ")" ";" { $$ = new CYDoWhile($5, $2); }
618 : "while" "(" Expression ")" Statement { $$ = new CYWhile($3, $5); }
622 : "for" "(" ForStatementInitialiser ";" ExpressionOpt ";" ExpressionOpt ")" Statement { $$ = new CYFor($3, $5, $7, $9); }
625 ForStatementInitialiser
626 : ExpressionOpt { $$ = $1; }
627 | "var" VariableDeclarationList { $$ = $2; }
631 : "for" "(" ForInStatementInitialiser "in" Expression ")" Statement { $$ = new CYForIn($3, $5, $7); }
634 ForInStatementInitialiser
635 : LeftHandSideExpression { $$ = $1; }
636 | "var" VariableDeclaration { $$ = $2; }
640 : "continue" IdentifierOpt ";" { $$ = new CYContinue($2); }
644 : "break" IdentifierOpt ";" { $$ = new CYBreak($2); }
648 : "return" ExpressionOpt ";" { $$ = new CYReturn($2); }
652 : "with" "(" Expression ")" Statement { $$ = new CYWith($3, $5); }
656 : "switch" "(" Expression ")" CaseBlock { $$ = new CYSwitch($3, $5); }
660 : "{" CaseClausesOpt "}" { $$ = $2; }
664 : CaseClause CaseClausesOpt { $1->SetNext($2); $$ = $1; }
665 | DefaultClause CaseClausesOpt { $1->SetNext($2); $$ = $1; }
670 : "case" Expression ":" StatementListOpt { $$ = new CYClause($2, $4); }
674 : "default" ":" StatementListOpt { $$ = new CYClause(NULL, $3); }
678 : Identifier ":" Statement { $3->AddLabel($1); $$ = $3; }
682 : "throw" Expression ";" { $$ = new CYThrow($2); }
686 : "try" Block CatchOpt FinallyOpt { $$ = new CYTry($2, $3, $4); }
690 : "catch" "(" Identifier ")" Block { $$ = new CYCatch($3, $5); }
695 : "finally" Block { $$ = $2; }
700 : "function" Identifier "(" FormalParameterList ")" "{" FunctionBody "}" { $$ = new CYFunction($2, $4, $7); }
704 : "function" IdentifierOpt "(" FormalParameterList ")" "{" FunctionBody "}" { $$ = new CYLambda($2, $4, $7); }
708 : "," FormalParameterList { $$ = $2; }
713 : Identifier FormalParameterList_ { $$ = new CYParameter($1, $2); }
718 : SourceElements { $$ = $1; }
722 : SourceElements { driver.source_ = $1; $$ = $1; }
726 : SourceElement SourceElements { $1->SetNext($2); $$ = $1; }
731 : Statement { $$ = $1; }
732 | FunctionDeclaration { $$ = $1; }