]> git.saurik.com Git - cycript.git/blob - Cycript.y
The glue code required for Bison/Flex parsers is insane.
[cycript.git] / Cycript.y
1 %code top {
2 #include "Cycript.tab.hh"
3 int cylex(YYSTYPE *lvalp, YYLTYPE *llocp, void *scanner);
4 #define scanner driver->scanner_
5 }
6
7 %code requires {
8 #include "Parser.hpp"
9 }
10
11 %union {
12 CYExpression *expression_;
13 CYTokenIdentifier *identifier_;
14 CYTokenNumber *number_;
15 CYTokenString *string_;
16 }
17
18 %name-prefix "cy"
19
20 %language "C++"
21 %locations
22 %glr-parser
23
24 %defines
25
26 %debug
27 %error-verbose
28
29 %parse-param { CYParser *driver }
30 %lex-param { void *scanner }
31
32 %token Ampersand "&"
33 %token AmpersandAmpersand "&&"
34 %token AmpersandEqual "&="
35 %token Carrot "^"
36 %token CarrotEqual "^="
37 %token Equal "="
38 %token EqualEqual "=="
39 %token EqualEqualEqual "==="
40 %token Exclamation "!"
41 %token ExclamationEqual "!="
42 %token ExclamationEqualEqual "!=="
43 %token Hyphen "-"
44 %token HyphenEqual "-="
45 %token HyphenHyphen "--"
46 %token HyphenRight "->"
47 %token Left "<"
48 %token LeftEqual "<="
49 %token LeftLeft "<<"
50 %token LeftLeftEqual "<<="
51 %token Percent "%"
52 %token PercentEqual "%="
53 %token Period "."
54 %token Pipe "|"
55 %token PipeEqual "|="
56 %token PipePipe "||"
57 %token Plus "+"
58 %token PlusEqual "+="
59 %token PlusPlus "++"
60 %token Right ">"
61 %token RightEqual ">="
62 %token RightRight ">>"
63 %token RightRightEqual ">>="
64 %token RightRightRight ">>>"
65 %token RightRightRightEqual ">>>="
66 %token Slash "/"
67 %token SlashEqual "/="
68 %token Star "*"
69 %token StarEqual "*="
70 %token Tilde "~"
71
72 %token Colon ":"
73 %token Comma ","
74 %token Question "?"
75 %token SemiColon ";"
76
77 %token OpenParen "("
78 %token CloseParen ")"
79
80 %token OpenBrace "{"
81 %token CloseBrace "}"
82
83 %token OpenBracket "["
84 %token CloseBracket "]"
85
86 %token Break "break"
87 %token Case "case"
88 %token Catch "catch"
89 %token Continue "continue"
90 %token Default "default"
91 %token Delete "delete"
92 %token Do "do"
93 %token Else "else"
94 %token False "false"
95 %token Finally "finally"
96 %token For "for"
97 %token Function "function"
98 %token If "if"
99 %token In "in"
100 %token InstanceOf "instanceof"
101 %token New "new"
102 %token Null "null"
103 %token Return "return"
104 %token Switch "switch"
105 %token This "this"
106 %token Throw "throw"
107 %token True "true"
108 %token Try "try"
109 %token TypeOf "typeof"
110 %token Var "var"
111 %token Void "void"
112 %token While "while"
113 %token With "with"
114
115 %token <identifier_> Identifier
116 %token <number_> NumericLiteral
117 %token <string_> StringLiteral
118
119 %%
120
121 %start Program;
122
123 IdentifierOpt
124 : Identifier
125 |
126 ;
127
128 Literal
129 : NullLiteral
130 | BooleanLiteral
131 | NumericLiteral
132 | StringLiteral
133 ;
134
135 NullLiteral
136 : "null"
137 ;
138
139 BooleanLiteral
140 : "true"
141 | "false"
142 ;
143
144 /* Objective-C Extensions {{{ */
145 VariadicCall
146 : "," AssignmentExpression VariadicCall
147 |
148 ;
149
150 SelectorCall_
151 : SelectorCall
152 | VariadicCall
153 ;
154
155 SelectorCall
156 : IdentifierOpt ":" AssignmentExpression SelectorCall_
157 ;
158
159 SelectorList
160 : SelectorCall
161 | Identifier
162 ;
163
164 ObjectiveCall
165 : "[" AssignmentExpression SelectorList "]"
166 ;
167 /* }}} */
168
169 /* 11.1 Primary Expressions {{{ */
170 PrimaryExpression
171 : "this"
172 | Identifier
173 | Literal
174 | ArrayLiteral
175 | ObjectLiteral
176 | "(" Expression ")"
177 | ObjectiveCall
178 ;
179 /* }}} */
180 /* 11.1.4 Array Initialiser {{{ */
181 ArrayLiteral
182 : "[" ElementList "]"
183 ;
184
185 Element
186 : AssignmentExpression
187 |
188 ;
189
190 ElementList_
191 : "," ElementList
192 |
193 ;
194
195 ElementList
196 : Element ElementList_
197 ;
198 /* }}} */
199 /* 11.1.5 Object Initialiser {{{ */
200 ObjectLiteral
201 : "{" PropertyNameAndValueListOpt "}"
202 ;
203
204 PropertyNameAndValueList_
205 : "," PropertyNameAndValueList
206 |
207 ;
208
209 PropertyNameAndValueListOpt
210 : PropertyNameAndValueList
211 |
212 ;
213
214 PropertyNameAndValueList
215 : PropertyName ":" AssignmentExpression PropertyNameAndValueList_
216 ;
217
218 PropertyName
219 : Identifier
220 | StringLiteral
221 | NumericLiteral
222 ;
223 /* }}} */
224
225 MemberExpression
226 : PrimaryExpression
227 | FunctionExpression
228 | MemberExpression "[" Expression "]"
229 | MemberExpression "." Identifier
230 | "new" MemberExpression Arguments
231 ;
232
233 NewExpression
234 : MemberExpression
235 | "new" NewExpression
236 ;
237
238 CallExpression
239 : MemberExpression Arguments
240 | CallExpression Arguments
241 | CallExpression "[" Expression "]"
242 | CallExpression "." Identifier
243 ;
244
245 ArgumentList_
246 : "," ArgumentList
247 |
248 ;
249
250 ArgumentListOpt
251 : ArgumentList
252 |
253 ;
254
255 ArgumentList
256 : AssignmentExpression ArgumentList_
257 ;
258
259 Arguments
260 : "(" ArgumentListOpt ")"
261 ;
262
263 LeftHandSideExpression
264 : NewExpression
265 | CallExpression
266 ;
267
268 PostfixExpression
269 : LeftHandSideExpression
270 | LeftHandSideExpression "++"
271 | LeftHandSideExpression "--"
272 ;
273
274 UnaryExpression
275 : PostfixExpression
276 | "delete" UnaryExpression
277 | "void" UnaryExpression
278 | "typeof" UnaryExpression
279 | "++" UnaryExpression
280 | "--" UnaryExpression
281 | "+" UnaryExpression
282 | "-" UnaryExpression
283 | "~" UnaryExpression
284 | "!" UnaryExpression
285 | "*" UnaryExpression
286 | "&" UnaryExpression
287 ;
288
289 MultiplicativeExpression
290 : UnaryExpression
291 | MultiplicativeExpression "*" UnaryExpression
292 | MultiplicativeExpression "/" UnaryExpression
293 | MultiplicativeExpression "%" UnaryExpression
294 ;
295
296 AdditiveExpression
297 : MultiplicativeExpression
298 | AdditiveExpression "+" MultiplicativeExpression
299 | AdditiveExpression "-" MultiplicativeExpression
300 ;
301
302 ShiftExpression
303 : AdditiveExpression
304 | ShiftExpression "<<" AdditiveExpression
305 | ShiftExpression ">>" AdditiveExpression
306 | ShiftExpression ">>>" AdditiveExpression
307 ;
308
309 RelationalExpression
310 : ShiftExpression
311 | RelationalExpression "<" ShiftExpression
312 | RelationalExpression ">" ShiftExpression
313 | RelationalExpression "<=" ShiftExpression
314 | RelationalExpression ">=" ShiftExpression
315 | RelationalExpression "instanceof" ShiftExpression
316 | RelationalExpression "in" ShiftExpression
317 ;
318
319 EqualityExpression
320 : RelationalExpression
321 | EqualityExpression "==" RelationalExpression
322 | EqualityExpression "!=" RelationalExpression
323 | EqualityExpression "===" RelationalExpression
324 | EqualityExpression "!==" RelationalExpression
325 ;
326
327 BitwiseANDExpression
328 : EqualityExpression
329 | BitwiseANDExpression "&" EqualityExpression
330 ;
331
332 BitwiseXORExpression
333 : BitwiseANDExpression
334 | BitwiseXORExpression "^" BitwiseANDExpression
335 ;
336
337 BitwiseORExpression
338 : BitwiseXORExpression
339 | BitwiseORExpression "|" BitwiseXORExpression
340 ;
341
342 LogicalANDExpression
343 : BitwiseORExpression
344 | LogicalANDExpression "&&" BitwiseORExpression
345 ;
346
347 LogicalORExpression
348 : LogicalANDExpression
349 | LogicalORExpression "||" LogicalANDExpression
350 ;
351
352 ConditionalExpression
353 : LogicalORExpression
354 | LogicalORExpression "?" AssignmentExpression ":" AssignmentExpression
355 ;
356
357 AssignmentExpression
358 : ConditionalExpression
359 | LeftHandSideExpression AssignmentOperator AssignmentExpression
360 ;
361
362 AssignmentOperator
363 : "="
364 | "*="
365 | "/="
366 | "%="
367 | "+="
368 | "-="
369 | "<<="
370 | ">>="
371 | ">>>="
372 | "&="
373 | "^="
374 | "|="
375 ;
376
377 Expression_
378 : "," Expression
379 |
380 ;
381
382 ExpressionOpt
383 : Expression
384 |
385 ;
386
387 Expression
388 : AssignmentExpression Expression_
389 ;
390
391 Statement
392 : Block
393 | VariableStatement
394 | EmptyStatement
395 | ExpressionStatement
396 | IfStatement
397 | IterationStatement
398 | ContinueStatement
399 | BreakStatement
400 | ReturnStatement
401 | WithStatement
402 | LabelledStatement
403 | SwitchStatement
404 | ThrowStatement
405 | TryStatement
406 ;
407
408 Block
409 : "{" StatementListOpt "}"
410 ;
411
412 StatementListOpt
413 : Statement StatementListOpt
414 |
415 ;
416
417 VariableStatement
418 : "var" VariableDeclarationList ";"
419 ;
420
421 VariableDeclarationList_
422 : "," VariableDeclarationList
423 |
424 ;
425
426 VariableDeclarationList
427 : VariableDeclaration VariableDeclarationList_
428 ;
429
430 VariableDeclaration
431 : Identifier InitialiserOpt
432 ;
433
434 InitialiserOpt
435 : Initialiser
436 |
437 ;
438
439 Initialiser
440 : "=" AssignmentExpression
441 ;
442
443 EmptyStatement
444 : ";"
445 ;
446
447 ExpressionStatement
448 : Expression ";"
449 ;
450
451 ElseStatementOpt
452 : "else" Statement
453 |
454 ;
455
456 IfStatement
457 : "if" "(" Expression ")" Statement ElseStatementOpt
458 ;
459
460 IterationStatement
461 : DoWhileStatement
462 | WhileStatement
463 | ForStatement
464 | ForInStatement
465 ;
466
467 DoWhileStatement
468 : "do" Statement "while" "(" Expression ")" ";"
469 ;
470
471 WhileStatement
472 : "while" "(" Expression ")" Statement
473 ;
474
475 ForStatement
476 : "for" "(" ForStatementInitialiser ";" ExpressionOpt ";" ExpressionOpt ")" Statement
477 ;
478
479 ForStatementInitialiser
480 : ExpressionOpt
481 | "var" VariableDeclarationList
482 ;
483
484 ForInStatement
485 : "for" "(" ForInStatementInitialiser "in" Expression ")" Statement
486 ;
487
488 ForInStatementInitialiser
489 : LeftHandSideExpression
490 | "var" VariableDeclaration
491 ;
492
493 ContinueStatement
494 : "continue" IdentifierOpt ";"
495 ;
496
497 BreakStatement
498 : "break" IdentifierOpt ";"
499 ;
500
501 ReturnStatement
502 : "return" ExpressionOpt ";"
503 ;
504
505 WithStatement
506 : "with" "(" Expression ")" Statement
507 ;
508
509 SwitchStatement
510 : "switch" "(" Expression ")" CaseBlock
511 ;
512
513 CaseBlock
514 : "{" CaseClausesOpt "}"
515 ;
516
517 CaseClausesOpt
518 : CaseClause CaseClausesOpt
519 | DefaultClause CaseClausesOpt
520 |
521 ;
522
523 CaseClause
524 : "case" Expression ":" StatementListOpt
525 ;
526
527 DefaultClause
528 : "default" ":" StatementListOpt
529 ;
530
531 LabelledStatement
532 : Identifier ":" Statement
533 ;
534
535 ThrowStatement
536 : "throw" Expression ";"
537 ;
538
539 TryStatement
540 : "try" Block CatchOpt FinallyOpt
541 ;
542
543 CatchOpt
544 : "catch" "(" Identifier ")" Block
545 |
546 ;
547
548 FinallyOpt
549 : "finally" Block
550 |
551 ;
552
553 FunctionDeclaration
554 : "function" Identifier "(" FormalParameterList ")" "{" FunctionBody "}"
555 ;
556
557 FunctionExpression
558 : "function" IdentifierOpt "(" FormalParameterList ")" "{" FunctionBody "}"
559 ;
560
561 FormalParameterList_
562 : "," FormalParameterList
563 |
564 ;
565
566 FormalParameterList
567 : Identifier FormalParameterList_
568 |
569 ;
570
571 FunctionBody
572 : SourceElements
573 ;
574
575 Program
576 : SourceElements
577 ;
578
579 SourceElements
580 : SourceElement SourceElements
581 |
582 ;
583
584 SourceElement
585 : Statement
586 | FunctionDeclaration
587 ;
588
589 %%