-
Notifications
You must be signed in to change notification settings - Fork 47
/
Grammar.y
741 lines (607 loc) · 34.7 KB
/
Grammar.y
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
{
module Language.JavaScript.Parser.Grammar (
parseProgram
, parseLiteral
, parsePrimaryExpression
, parseStatement
) where
import Control.Monad.Error.Class (throwError)
import Data.Char
import Language.JavaScript.Parser.Lexer
import Language.JavaScript.Parser.ParserMonad
--import Language.JavaScript.Parser.SrcLocation
import qualified Language.JavaScript.Parser.AST as AST
}
-- The name of the generated function to be exported from the module
%name parseProgram Program
%name parseLiteral Literal
%name parsePrimaryExpression PrimaryExpression
%name parseStatement Statement
%tokentype { Token }
%error { parseError }
%monad { P } { thenP } { returnP }
%lexer { lexCont } { EOFToken {} }
%token
';' { SemiColonToken {} }
',' { CommaToken {} }
'?' { HookToken {} }
':' { ColonToken {} }
'||' { OrToken {} }
'&&' { AndToken {} }
'|' { BitwiseOrToken {} }
'^' { BitwiseXorToken {} }
'&' { BitwiseAndToken {} }
'===' { StrictEqToken {} }
'==' { EqToken {} }
'=' { SimpleAssignToken {} }
'!==' { StrictNeToken {} }
'!=' { NeToken {} }
'<<' { LshToken {} }
'<=' { LeToken {} }
'<' { LtToken {} }
'>>>' { UrshToken {} }
'>>' { RshToken {} }
'>=' { GeToken {} }
'>' { GtToken {} }
'++' { IncrementToken {} }
'--' { DecrementToken {} }
'+' { PlusToken {} }
'-' { MinusToken {} }
'*' { MulToken {} }
'/' { DivToken {} }
'%' { ModToken {} }
'!' { NotToken {} }
'~' { BitwiseNotToken {} }
'.' { DotToken {} }
'[' { LeftBracketToken {} }
']' { RightBracketToken {} }
'{' { LeftCurlyToken {} }
'}' { RightCurlyToken {} }
'(' { LeftParenToken {} }
')' { RightParenToken {} }
'@*/' { CondcommentEndToken {} }
'break' { BreakToken {} }
'case' { CaseToken {} }
'catch' { CatchToken {} }
'const' { ConstToken {} }
'continue' { ContinueToken {} }
'debugger' { DebuggerToken {} }
'default' { DefaultToken {} }
'delete' { DeleteToken {} }
'do' { DoToken {} }
'else' { ElseToken {} }
'enum' { EnumToken {} }
'false' { FalseToken {} }
'finally' { FinallyToken {} }
'for' { ForToken {} }
'function' { FunctionToken {} }
'if' { IfToken {} }
'in' { InToken {} }
'instanceof' { InstanceofToken {} }
'new' { NewToken {} }
'null' { NullToken {} }
'return' { ReturnToken {} }
'switch' { SwitchToken {} }
'this' { ThisToken {} }
'throw' { ThrowToken {} }
'true' { TrueToken {} }
'try' { TryToken {} }
'typeof' { TypeofToken {} }
'var' { VarToken {} }
'void' { VoidToken {} }
'while' { WhileToken {} }
'with' { WithToken {} }
'ident' { IdentifierToken {} }
'decimal' { DecimalToken {} }
'hexinteger' { HexIntegerToken {} }
'octal' { OctalToken {} }
'string' { StringToken {} }
'regex' { RegExToken {} }
'assign' { AssignToken {} }
%%
-- --------------------------------------------------------------------
-- Start of GOLD Grammar for Javascript, used as a base
-- --------------------------------------------------------------------
-- "Name" = 'JavaScript Grammar'
-- "Author" = 'M.Schnoor-Matriciani'
-- "Version" = '0.9'
-- "About" = 'JavaScript Grammar, Subset of ECMA Edition 3'
-- "Start Symbol" = <Program>
-- "Case Sensitive" = 'True'
-- ! ------------------------------------------------- Sets
-- {ID Head} = {Letter} + [_] + [$]
-- {ID Tail} = {Alphanumeric} + [_] + [$]
-- {String Chars1} = {Printable} + {HT} - ["\]
-- {String Chars2} = {Printable} + {HT} - [\'']
-- {Hex Digit} = {Digit} + [ABCDEF] + [abcdef]
-- {RegExp Chars} = {Letter}+{Digit}+['^']+['$']+['*']+['+']+['?']+['{']+['}']+['|']+['-']+['.']+[',']+['#']+['[']+[']']+['_']+['<']+['>']
-- {Non Terminator} = {String Chars1} - {CR} - {LF}
-- {Non Zero Digits}={Digit}-[0]
-- ! ------------------------------------------------- Terminals
-- Identifier = {ID Head}{ID Tail}*
-- StringLiteral = '"' ( {String Chars1} | '\' {Printable} )* '"' | '' ( {String Chars2} | '\' {Printable} )* ''
-- HexIntegerLiteral = '0x' {Hex Digit}+
-- RegExp = '/' ({RegExp Chars} | '\' {Non Terminator})+ '/' ( 'g' | 'i' | 'm' )*
-- DecimalLiteral= {Non Zero Digits}+ '.' {Digit}* ('e' | 'E' ) {Non Zero Digits}+ {Digit}* | {Non Zero Digits}+ '.' {Digit}* | '0' '.' {Digit}+ ('e' | 'E' ) {Non Zero Digits}+ {Digit}* | {Non Zero Digits}+ {Digit}* | '0' | '0' '.' {Digit}+
-- Comment Start = '/*'
-- Comment End = '*/'
-- Comment Line = '//'
-- ! ------------------------------------------------- Rules
-- ---------------------------------------------------------------------
-- Sort out automatically inserted semi-colons
AutoSemi : ';' { AST.JSLiteral ";"}
| { AST.JSLiteral ""}
-- ---------------------------------------------------------------------
-- <Literal> ::= <Null Literal>
-- | <Boolean Literal>
-- | <Numeric Literal>
-- | StringLiteral
Literal : NullLiteral {$1}
| BooleanLiteral {$1}
| NumericLiteral {$1}
| StringLiteral {$1}
NullLiteral : 'null' { AST.JSLiteral "null" }
BooleanLiteral : 'true' { AST.JSLiteral "true" }
| 'false' { AST.JSLiteral "false" }
-- <Numeric Literal> ::= DecimalLiteral
-- | HexIntegerLiteral
NumericLiteral : 'decimal' { AST.JSDecimal (token_literal $1)}
| 'hexinteger' { AST.JSHexInteger (token_literal $1)}
| 'octal' { AST.JSOctal (token_literal $1)}
StringLiteral : 'string' {AST.JSStringLiteral (token_delimiter $1) (token_literal $1)}
-- <Regular Expression Literal> ::= RegExp
RegularExpressionLiteral : 'regex' {AST.JSRegEx (token_literal $1)}
-- <Primary Expression> ::= 'this'
-- | Identifier
-- | <Literal>
-- | <Array Literal>
-- | <Object Literal>
-- | '(' <Expression> ')'
-- | <Regular Expression Literal>
PrimaryExpression :: { AST.JSNode }
PrimaryExpression : 'this' { AST.JSLiteral "this" }
| Identifier { $1 {- PrimaryExpression1 -}}
| Literal { $1 {- PrimaryExpression2 -}}
| ArrayLiteral { $1 {- PrimaryExpression3 -}}
| ObjectLiteral { $1 {- PrimaryExpression4 -}}
| '(' Expression ')' { AST.JSExpressionParen $2 }
| RegularExpressionLiteral { $1 {- PrimaryExpression5 -}} -- Not in ECMA ed 5?
Identifier : 'ident' { AST.JSIdentifier (token_literal $1) }
-- <Array Literal> ::= '[' ']'
-- | '[' <Elision> ']'
-- | '[' <Element List> ']'
-- | '[' <Element List> ',' <Elision> ']'
ArrayLiteral : '[' ']' { AST.JSArrayLiteral [] }
| '[' Elision ']' { AST.JSArrayLiteral $2 }
| '[' ElementList ']' { AST.JSArrayLiteral $2 }
| '[' ElementList ',' Elision ']' { AST.JSArrayLiteral ($2++$4) }
| '[' ElementList ',' ']' { AST.JSArrayLiteral ($2++[AST.JSLiteral ","]) }
-- <Elision> ::= ','
-- | <Elision> ','
Elision : ',' { [(AST.JSElision [])] }
| Elision ',' { $1 ++ [(AST.JSElision [])] }
-- <Element List> ::= <Elision> <Assignment Expression>
-- | <Element List> ',' <Elision> <Assignment Expression>
-- | <Element List> ',' <Assignment Expression>
-- | <Assignment Expression>
ElementList : Elision AssignmentExpression { ($1++$2) {- ElementList -}}
| ElementList ',' Elision AssignmentExpression { ($1++[(AST.JSElision [])]++$3++$4) {- ElementList -}}
| ElementList ',' AssignmentExpression { ($1++[(AST.JSElision [])]++$3) {- ElementList -}}
| AssignmentExpression { $1 {- ElementList -}}
-- <Object Literal> ::= '{' <Property Name and Value List> '}'
ObjectLiteral :: { AST.JSNode }
ObjectLiteral : '{' PropertyNameandValueList '}' { AST.JSObjectLiteral $2 }
-- <Property Name and Value List> ::= <Property Name> ':' <Assignment Expression>
-- | <Property Name and Value List> ',' <Property Name> ':' <Assignment Expression>
-- Seems we can have function declarations in the value part too
-- TODO: And can end with a comma
PropertyNameandValueList :: { [ AST.JSNode ] }
PropertyNameandValueList : PropertyName ':' AssignmentExpression { [(AST.JSPropertyNameandValue $1 $3)] }
| PropertyName ':' FunctionDeclaration { [(AST.JSPropertyNameandValue $1 [$3])] }
| PropertyNameandValueList ',' PropertyName ':' AssignmentExpression
{ ($1 ++ [(AST.JSPropertyNameandValue $3 $5)]) }
| PropertyNameandValueList ',' PropertyName ':' FunctionDeclaration
{ ($1 ++ [(AST.JSPropertyNameandValue $3 [$5])]) }
| PropertyNameandValueList ','
{ ($1 ++ [(AST.JSLiteral ",")]) }
| { [] }
-- <Property Name> ::= Identifier
-- | StringLiteral
-- | <Numeric Literal>
PropertyName : Identifier { $1 {- PropertyName1 -}}
| StringLiteral { $1 {- PropertyName2 -}}
| NumericLiteral { $1 {- PropertyName3 -}}
-- <Member Expression > ::= <Primary Expression>
-- | <Function Expression>
-- | <Member Expression> '[' <Expression> ']'
-- | <Member Expression> '.' Identifier
-- | 'new' <Member Expression> <Arguments>
MemberExpression :: { [AST.JSNode] }
MemberExpression : PrimaryExpression { [$1] {- MemberExpression -}}
| FunctionExpression { [$1] {- MemberExpression -}}
| MemberExpression '[' Expression ']' { [AST.JSMemberSquare $1 $3] }
| MemberExpression '.' Identifier { [AST.JSMemberDot $1 $3] }
| 'new' MemberExpression Arguments { (((AST.JSLiteral "new "):$2)++[$3])}
-- <New Expression> ::= <Member Expression>
-- | new <New Expression>
NewExpression : MemberExpression {$1 {- NewExpression -}}
| 'new' NewExpression { (AST.JSLiteral "new "):$2 }
-- <Call Expression> ::= <Member Expression> <Arguments>
-- | <Call Expression> <Arguments>
-- | <Call Expression> '[' <Expression> ']'
-- | <Call Expression> '.' Identifier
CallExpression :: { [AST.JSNode] }
CallExpression : MemberExpression Arguments { $1++[$2] {- CallExpression -} }
| CallExpression Arguments { ($1++[(AST.JSCallExpression "()" [$2])]) }
| CallExpression '[' Expression ']' { ($1++[(AST.JSCallExpression "[]" [$3])]) }
| CallExpression '.' Identifier { ($1++[(AST.JSCallExpression "." [$3])]) }
-- <Arguments> ::= '(' ')'
-- | '(' <Argument List> ')'
Arguments : '(' ')' { (AST.JSArguments []) }
| '(' ArgumentList ')' { (AST.JSArguments $2) }
-- <Argument List> ::= <Assignment Expression>
-- | <Argument List> ',' <Assignment Expression>
ArgumentList :: { [[AST.JSNode]] }
ArgumentList : AssignmentExpression { [$1] {- ArgumentList -}}
| ArgumentList ',' AssignmentExpression { $1++[$3] {- ArgumentList2 -} }
-- <Left Hand Side Expression> ::= <New Expression>
-- | <Call Expression>
LeftHandSideExpression : NewExpression { $1 {- LeftHandSideExpression1 -}}
| CallExpression { $1 {- LeftHandSideExpression12 -}}
-- <Postfix Expression> ::= <Left Hand Side Expression>
-- | <Postfix Expression> '++'
-- | <Postfix Expression> '--'
PostfixExpression : LeftHandSideExpression { $1 {- PostfixExpression -} }
| PostfixExpression '++' {[(AST.JSExpressionPostfix "++" $1)]}
| PostfixExpression '--' {[(AST.JSExpressionPostfix "--" $1)]}
-- <Unary Expression> ::= <Postfix Expression>
-- | 'delete' <Unary Expression>
-- | 'void' <Unary Expression>
-- | 'typeof' <Unary Expression>
-- | '++' <Unary Expression>
-- | '--' <Unary Expression>
-- | '+' <Unary Expression>
-- | '-' <Unary Expression>
-- | '~' <Unary Expression>
-- | '!' <Unary Expression>
UnaryExpression :: { [AST.JSNode] }
UnaryExpression : PostfixExpression { $1 {- UnaryExpression -} }
| 'delete' UnaryExpression { ((AST.JSUnary "delete "):$2)}
| 'void' UnaryExpression { ((AST.JSUnary "void "):$2)}
| 'typeof' UnaryExpression { ((AST.JSUnary "typeof "):$2)}
| '++' UnaryExpression { ((AST.JSUnary "++"):$2) }
| '--' UnaryExpression { ((AST.JSUnary "--"):$2)}
| '+' UnaryExpression { ((AST.JSUnary "+"):$2)}
| '-' UnaryExpression { ((AST.JSUnary "-"):$2)}
| '~' UnaryExpression { ((AST.JSUnary "~"):$2)}
| '!' UnaryExpression { ((AST.JSUnary "!"):$2)}
-- <Multiplicative Expression> ::= <Unary Expression>
-- | <Unary Expression> '*' <Multiplicative Expression>
-- | <Unary Expression> '/' <Multiplicative Expression>
-- | <Unary Expression> '%' <Multiplicative Expression>
MultiplicativeExpression :: { [AST.JSNode] }
MultiplicativeExpression : UnaryExpression { $1 {- MultiplicativeExpression -}}
| UnaryExpression '*' MultiplicativeExpression { [(AST.JSExpressionBinary "*" $1 $3)]}
| UnaryExpression '/' MultiplicativeExpression { [(AST.JSExpressionBinary "/" $1 $3)]}
| UnaryExpression '%' MultiplicativeExpression { [(AST.JSExpressionBinary "%" $1 $3)]}
-- <Additive Expression> ::= <Additive Expression>'+'<Multiplicative Expression>
-- | <Additive Expression>'-'<Multiplicative Expression>
-- | <Multiplicative Expression>
AdditiveExpression :: { [AST.JSNode] }
AdditiveExpression : AdditiveExpression '+' MultiplicativeExpression { [(AST.JSExpressionBinary "+" $1 $3)]}
| AdditiveExpression '-' MultiplicativeExpression { [(AST.JSExpressionBinary "-" $1 $3)]}
| MultiplicativeExpression { $1 {- (goRegExp $1)-} {- AdditiveExpression -} }
-- <Shift Expression> ::= <Shift Expression> '<<' <Additive Expression>
-- | <Shift Expression> '>>' <Additive Expression>
-- | <Shift Expression> '>>>' <Additive Expression>
-- | <Additive Expression>
ShiftExpression :: { [AST.JSNode] }
ShiftExpression : ShiftExpression '<<' AdditiveExpression { [(AST.JSExpressionBinary "<<" $1 $3)]}
| ShiftExpression '>>' AdditiveExpression { [(AST.JSExpressionBinary ">>" $1 $3)]}
| ShiftExpression '>>>' AdditiveExpression { [(AST.JSExpressionBinary ">>>" $1 $3)]}
| AdditiveExpression { $1 {- ShiftExpression -}}
-- <Relational Expression>::= <Shift Expression>
-- | <Relational Expression> '<' <Shift Expression>
-- | <Relational Expression> '>' <Shift Expression>
-- | <Relational Expression> '<=' <Shift Expression>
-- | <Relational Expression> '>=' <Shift Expression>
-- | <Relational Expression> 'instanceof' <Shift Expression>
RelationalExpression :: { [AST.JSNode] }
RelationalExpression : ShiftExpression { $1 {- RelationalExpression -}}
| RelationalExpression '<' ShiftExpression { [(AST.JSExpressionBinary "<" $1 $3)]}
| RelationalExpression '>' ShiftExpression { [(AST.JSExpressionBinary ">" $1 $3)]}
| RelationalExpression '<=' ShiftExpression { [(AST.JSExpressionBinary "<=" $1 $3)]}
| RelationalExpression '>=' ShiftExpression { [(AST.JSExpressionBinary ">=" $1 $3)]}
| RelationalExpression 'instanceof' ShiftExpression { [(AST.JSExpressionBinary " instanceof " $1 $3)]}
-- Strictly speaking should have all the NoIn variants of expressions,
-- but we assume syntax is checked so no problem. Cross fingers.
| RelationalExpression 'in' ShiftExpression { [(AST.JSExpressionBinary " in " $1 $3)]}
-- <Equality Expression> ::= <Relational Expression>
-- | <Equality Expression> '==' <Relational Expression>
-- | <Equality Expression> '!=' <Relational Expression>
-- | <Equality Expression> '===' <Relational Expression>
-- | <Equality Expression> '!==' <Relational Expression>
EqualityExpression :: { [AST.JSNode] }
EqualityExpression : RelationalExpression { $1 {- EqualityExpression -} }
| EqualityExpression '==' RelationalExpression { [(AST.JSExpressionBinary "==" $1 $3)]}
| EqualityExpression '!=' RelationalExpression { [(AST.JSExpressionBinary "!=" $1 $3)]}
| EqualityExpression '===' RelationalExpression { [(AST.JSExpressionBinary "===" $1 $3)]}
| EqualityExpression '!==' RelationalExpression { [(AST.JSExpressionBinary "!==" $1 $3)]}
-- <Bitwise And Expression> ::= <Equality Expression>
-- | <Bitwise And Expression> '&' <Equality Expression>
BitwiseAndExpression :: { [AST.JSNode] }
BitwiseAndExpression : EqualityExpression { $1 {- BitwiseAndExpression -} }
| BitwiseAndExpression '&' EqualityExpression { [(AST.JSExpressionBinary "&" $1 $3)]}
-- <Bitwise XOr Expression> ::= <Bitwise And Expression>
-- | <Bitwise XOr Expression> '^' <Bitwise And Expression>
BitwiseXOrExpression :: { [AST.JSNode] }
BitwiseXOrExpression : BitwiseAndExpression { $1 {- BitwiseXOrExpression -} }
| BitwiseXOrExpression '^' BitwiseAndExpression { [(AST.JSExpressionBinary "^" $1 $3)]}
-- <Bitwise Or Expression> ::= <Bitwise XOr Expression>
-- | <Bitwise Or Expression> '|' <Bitwise XOr Expression>
BitwiseOrExpression :: { [AST.JSNode] }
BitwiseOrExpression : BitwiseXOrExpression { $1 {- BitwiseOrExpression -} }
| BitwiseOrExpression '|' BitwiseXOrExpression { [(AST.JSExpressionBinary "|" $1 $3)]}
-- <Logical And Expression> ::= <Bitwise Or Expression>
-- | <Logical And Expression> '&&' <Bitwise Or Expression>
LogicalAndExpression :: { [AST.JSNode] }
LogicalAndExpression : BitwiseOrExpression { $1 {- LogicalAndExpression -} }
| LogicalAndExpression '&&' BitwiseOrExpression { [(AST.JSExpressionBinary "&&" $1 $3)]}
-- <Logical Or Expression> ::= <Logical And Expression>
-- | <Logical Or Expression> '||' <Logical And Expression>
LogicalOrExpression :: { [AST.JSNode] }
LogicalOrExpression : LogicalAndExpression { $1 {- LogicalOrExpression -} }
| LogicalOrExpression '||' LogicalAndExpression { [(AST.JSExpressionBinary "||" $1 $3)]}
-- <Conditional Expression> ::= <Logical Or Expression>
-- | <Logical Or Expression> '?' <Assignment Expression> ':' <Assignment Expression>
ConditionalExpression :: { [AST.JSNode] }
ConditionalExpression : LogicalOrExpression { $1 {- ConditionalExpression -} }
| LogicalOrExpression '?' AssignmentExpression ':' AssignmentExpression
{ [AST.JSExpressionTernary $1 $3 $5] }
-- <Assignment Expression> ::= <Conditional Expression>
-- | <Left Hand Side Expression> <Assignment Operator> <Assignment Expression>
AssignmentExpression :: { [AST.JSNode] }
AssignmentExpression : ConditionalExpression { $1 {- AssignmentExpression -}}
| LeftHandSideExpression AssignmentOperator AssignmentExpression
{ ($1++[$2]++$3) }
-- <Assignment Operator> ::= '=' | '*=' | '/=' | '%=' | '+=' | '-=' | '<<=' | '>>=' | '>>>=' | '&=' | '^=' | '|='
AssignmentOperator :: { AST.JSNode }
AssignmentOperator : 'assign' { AST.JSOperator (token_literal $1) }
| '=' { AST.JSOperator "=" }
-- <Expression> ::= <Assignment Expression>
-- | <Expression> ',' <Assignment Expression>
Expression :: { AST.JSNode }
Expression : AssignmentExpression { AST.JSExpression $1 {- Expression -} }
| Expression ',' AssignmentExpression { flattenExpression $1 $3 }
ExpressionOpt :: { [AST.JSNode] }
ExpressionOpt : Expression { [$1] {- ExpressionOpt -}}
| { [] {- ExpressionOpt -}}
-- <Statement> ::= <Block>
-- | <Variable Statement>
-- | <Empty Statement>
-- | <If Statement>
-- | <If Else Statement>
-- | <Iteration Statement>
-- | <Continue Statement>
-- | <Break Statement>
-- | <Return Statement>
-- | <With Statement>
-- | <Labelled Statement>
-- | <Switch Statement>
-- | <Throw Statement>
-- | <Try Statement>
-- | <Expression>
Statement :: { AST.JSNode }
Statement : StatementNoEmpty { $1 {- Statement1 -}}
| EmptyStatement { $1 {- Statement3 -}}
StatementNoEmpty :: { AST.JSNode }
StatementNoEmpty : StatementBlock { $1 {- StatementNoEmpty1 -}}
| VariableStatement { $1 {- StatementNoEmpty2 -}}
-- | EmptyStatement { $1 {- StatementNoEmpty3 -}}
-- | IfStatement { $1 {- StatementNoEmpty4 -}}
| IfElseStatement { $1 {- StatementNoEmpty5 -}}
| IterationStatement { $1 {- StatementNoEmpty6 -}}
| ContinueStatement { $1 {- StatementNoEmpty7 -}}
| BreakStatement { $1 {- StatementNoEmpty8 -}}
| ReturnStatement { $1 {- StatementNoEmpty9 -}}
| WithStatement { $1 {- StatementNoEmpty10 -}}
| LabelledStatement { $1 {- StatementNoEmpty11 -}}
| SwitchStatement { $1 {- StatementNoEmpty12 -}}
| ThrowStatement { $1 {- StatementNoEmpty13 -}}
| TryStatement { $1 {- StatementNoEmpty14 -}}
| Expression { $1 {- StatementNoEmpty15 -}}
StatementBlock : '{' '}' { (AST.JSLiteral ";") }
| '{' StatementList '}' { (if ($2 == AST.JSStatementList [AST.JSLiteral ";"]) then (AST.JSLiteral ";") else (AST.JSBlock $2)) }
Block : '{' '}' { (AST.JSBlock (AST.JSStatementList [])) }
| '{' StatementList '}' { (AST.JSBlock $2) }
StatementList :: { AST.JSNode }
StatementList : Statement { (AST.JSStatementList [$1]) }
| StatementList Statement { (combineStatements $1 $2) }
-- <Variable Statement> ::= var <Variable Declaration List> ';'
VariableStatement : 'var' VariableDeclarationList AutoSemi { AST.JSVariables "var" $2 }
| 'const' VariableDeclarationList AutoSemi { AST.JSVariables "const" $2 }
-- <Variable Declaration List> ::= <Variable Declaration>
-- | <Variable Declaration List> ',' <Variable Declaration>
VariableDeclarationList :: { [AST.JSNode] }
VariableDeclarationList : VariableDeclaration { [$1] {- VariableDeclarationList -}}
| VariableDeclarationList ',' VariableDeclaration { ($1 ++ [$3]) {- VariableDeclarationList -}}
VariableDeclaration :: { AST.JSNode }
VariableDeclaration : Identifier { (AST.JSVarDecl $1 [])}
| Identifier Initializer { (AST.JSVarDecl $1 $2)}
-- <Initializer> ::= '=' <Assignment Expression>
Initializer : '=' AssignmentExpression { $2 {- Initializer -}}
EmptyStatement : ';' { (AST.JSLiteral ";") }
{-
-- <If Statement> ::= 'if' '(' <Expression> ')' <Statement>
IfStatement : 'if' '(' Expression ')' Statement { (AST.JSIf $3 $5) }
-- <If Else Statement> ::= 'if' '(' <Expression> ')' <Statement> 'else' <Statement>
IfElseStatement : 'if' '(' Expression ')' StatementSemi 'else' Statement { (AST.JSIfElse $3 $5 $7) }
-}
{-
IfElseStatement : 'if' '(' Expression ')' Statement ';' 'else' Statement
{ (AST.JSIfElse $3 (AST.JSBlock (AST.JSStatementList [$5])) $8) }
| 'if' '(' Expression ')' Statement 'else' Statement
{ (AST.JSIfElse $3 $5 $7) }
| 'if' '(' Expression ')' Statement { (AST.JSIf $3 $5) }
-}
IfElseStatement :: { AST.JSNode }
IfElseStatement : 'if' '(' Expression ')' StatementSemi IfElseRest
{ (if ($6 /= []) then
(if (length $6 == 1) then (AST.JSIfElse $3 $5 (head $6))
else (AST.JSIfElse $3 (AST.JSBlock (AST.JSStatementList [$5])) (last $6)))
else (AST.JSIf $3 $5)) }
IfElseRest :: { [AST.JSNode] }
IfElseRest : -- ';' 'else' Statement { [$3,$3] } -- Horrible, but a type-compliant signal nevertheless
{- | -} 'else' Statement { [$2] }
| { [] }
{-
ElsePart : 'else' { 1 }
| ';' 'else' { 2 }
-}
StatementSemi : StatementNoEmpty ';' { (AST.JSBlock (AST.JSStatementList [$1])) }
| StatementNoEmpty { $1 {- StatementSemi -}}
| ';' { AST.JSLiteral ";" }
-- <Iteration Statement> ::= 'do' <Statement> 'while' '(' <Expression> ')' ';'
-- | 'while' '(' <Expression> ')' <Statement>
-- | 'for' '(' <Expression> ';' <Expression> ';' <Expression> ')' <Statement>
-- | 'for' '(' 'var' <Variable Declaration List> ';' <Expression> ';' <Expression> ')' <Statement>
-- | 'for' '(' <Left Hand Side Expression> in <Expression> ')' <Statement>
-- | 'for' '(' 'var' <Variable Declaration> in <Expression> ')' <Statement>
IterationStatement :: { AST.JSNode }
IterationStatement : 'do' Statement 'while' '(' Expression ')' AutoSemi { (AST.JSDoWhile $2 $5 $7) }
| 'while' '(' Expression ')' Statement { (AST.JSWhile $3 $5) }
| 'for' '(' ExpressionOpt ';' ExpressionOpt ';' ExpressionOpt ')' Statement { (AST.JSFor $3 $5 $7 $9) }
| 'for' '(' 'var' VariableDeclarationList ';' ExpressionOpt ';' ExpressionOpt ')' Statement
{ (AST.JSForVar $4 $6 $8 $10) }
| 'for' '(' LeftHandSideExpression 'in' Expression ')' Statement
{ (AST.JSForIn $3 $5 $7) }
| 'for' '(' 'var' VariableDeclaration 'in' Expression ')' Statement
{ (AST.JSForVarIn $4 $6 $8) }
-- <Continue Statement> ::= 'continue' ';'
-- | 'continue' Identifier ';'
ContinueStatement : 'continue' AutoSemi { (AST.JSContinue [$2]) }
| 'continue' Identifier AutoSemi { (AST.JSContinue [$2,$3]) }
-- <Break Statement> ::= 'break' ';'
-- | 'break' Identifier ';'
BreakStatement : 'break' AutoSemi { (AST.JSBreak [] [$2]) }
| 'break' Identifier AutoSemi { (AST.JSBreak [$2] [$3]) }
-- <Return Statement> ::= 'return' ';'
-- | 'return' <Expression> ';'
ReturnStatement : 'return' AutoSemi { (AST.JSReturn [$2]) }
| 'return' Expression AutoSemi { (AST.JSReturn [$2,$3]) }
-- <With Statement> ::= 'with' '(' <Expression> ')' <Statement> ';'
WithStatement : 'with' '(' Expression ')' Statement AutoSemi { (AST.JSWith $3 [$5,$6]) }
-- <Switch Statement> ::= 'switch' '(' <Expression> ')' <Case Block>
SwitchStatement : 'switch' '(' Expression ')' CaseBlock { (AST.JSSwitch $3 $5) }
-- <Case Block> ::= '{' '}'
-- | '{' <Case Clauses> '}'
-- | '{' <Case Clauses> <Default Clause> '}'
-- | '{' <Case Clauses> <Default Clause> <Case Clauses> '}'
-- | '{' <Default Clause> <Case Clauses> '}'
-- | '{' <Default Clause> '}'
CaseBlock :: { [AST.JSNode] }
CaseBlock : '{' '}' { [] }
| '{' CaseClauses '}' { $2 {- CaseBlock2 -}}
| '{' CaseClauses DefaultClause '}' { ($2++[$3]) {- CaseBlock3 -}}
| '{' CaseClauses DefaultClause CaseClauses '}' { ($2++($3:$4)) {- CaseBlock4 -}}
| '{' DefaultClause CaseClauses '}' { ($2:$3) {- CaseBlock5 -}}
| '{' DefaultClause '}' { [$2] {- CaseBlock6 -}}
-- <Case Clauses> ::= <Case Clause>
-- | <Case Clauses> <Case Clause>
CaseClauses :: { [AST.JSNode] }
CaseClauses : CaseClause { [$1] {- CaseClauses1 -}}
| CaseClauses CaseClause { ($1++[$2]) {- CaseClauses2 -}}
-- <Case Clause> ::= 'case' <Expression> ':' <Statement List>
-- | 'case' <Expression> ':'
CaseClause :: { AST.JSNode }
CaseClause : 'case' Expression ':' StatementList { (AST.JSCase $2 $4) }
| 'case' Expression ':' { (AST.JSCase $2 (AST.JSStatementList [])) }
-- <Default Clause> ::= 'default' ':'
-- | 'default' ':' <Statement List>
DefaultClause :: { AST.JSNode }
DefaultClause : 'default' ':' { (AST.JSDefault (AST.JSStatementList [])) }
| 'default' ':' StatementList { (AST.JSDefault $3) }
-- <Labelled Statement> ::= Identifier ':' <Statement>
LabelledStatement : Identifier ':' Statement { (AST.JSLabelled $1 $3) }
-- <Throw Statement> ::= 'throw' <Expression>
ThrowStatement : 'throw' Expression { (AST.JSThrow $2) }
-- Note: worked in updated syntax as per https://developer.mozilla.org/en/JavaScript/Reference/Statements/try...catch
-- i.e., 0 or more catches, then an optional finally
-- <Try Statement> ::= 'try' <Block> <Catch>
-- | 'try' <Block> <Finally>
-- | 'try' <Block> <Catch> <Finally>
TryStatement : 'try' Block Catches { (AST.JSTry $2 $3) {- TryStatement1 -} }
| 'try' Block Finally { (AST.JSTry $2 [$3]) {- TryStatement2 -} }
| 'try' Block Catches Finally { (AST.JSTry $2 ($3++[$4])) {- TryStatement3 -} }
Catches :: { [AST.JSNode] }
Catches : Catch { [$1] {- Catches 1 -} }
| Catches Catch { ($1++[$2]) {- Catches 2 -} }
-- Note: worked in updated syntax as per https://developer.mozilla.org/en/JavaScript/Reference/Statements/try...catch
-- <Catch> ::= 'catch' '(' Identifier ')' <Block>
-- becomes
-- <Catch> ::= 'catch' '(' Identifier ')' <Block>
-- | 'catch' '(' Identifier 'if' ConditionalExpression ')' <Block>
Catch : 'catch' '(' Identifier ')' Block { (AST.JSCatch $3 [] $5) }
| 'catch' '(' Identifier 'if' ConditionalExpression ')' Block { (AST.JSCatch $3 $5 $7) }
-- <Finally> ::= 'finally' <Block>
Finally : 'finally' Block { (AST.JSFinally $2) }
-- <Function Declaration> ::= 'function' Identifier '(' <Formal Parameter List> ')' '{' <Function Body> '}'
-- | 'function' Identifier '(' ')' '{' <Function Body> '}'
FunctionDeclaration :: { AST.JSNode }
FunctionDeclaration : 'function' Identifier '(' FormalParameterList ')' '{' FunctionBody '}'
{ (AST.JSFunction $2 $4 $7) }
| 'function' Identifier '(' ')' '{' FunctionBody '}'
{ (AST.JSFunction $2 [] $6) }
-- <Function Expression> ::= 'function' '(' ')' '{' <Function Body> '}'
-- | 'function' '(' <Formal Parameter List> ')' '{' <Function Body> '}'
FunctionExpression :: { AST.JSNode }
FunctionExpression : 'function' IdentifierOpt '(' ')' '{' FunctionBody '}' { (AST.JSFunctionExpression $2 [] $6) }
| 'function' IdentifierOpt '(' FormalParameterList ')' '{' FunctionBody '}' { (AST.JSFunctionExpression $2 $4 $7) }
IdentifierOpt :: { [AST.JSNode] }
IdentifierOpt : Identifier { [$1] {- IdentifierOpt -}}
| { [] {- IdentifierOpt -}}
-- <Formal Parameter List> ::= Identifier
-- | <Formal Parameter List> ',' Identifier
FormalParameterList :: { [AST.JSNode] }
FormalParameterList : Identifier { [$1] {- FormalParameterList -}}
| FormalParameterList ',' Identifier { ($1++[$3]) }
-- <Function Body> ::= <Source Elements>
-- |
FunctionBody :: { AST.JSNode }
FunctionBody : SourceElements { (AST.JSFunctionBody [$1]) }
| { (AST.JSFunctionBody []) }
-- <Program> ::= <Source Elements>
Program : SourceElementsTop { $1 {- Program -}}
-- <Source Elements> ::= <Source Element>
-- | <Source Elements> <Source Element>
SourceElements :: { AST.JSNode }
SourceElements : SourceElement { (AST.JSSourceElements [$1]) }
| SourceElements SourceElement { (combineSourceElements $1 $2) }
SourceElementsTop :: { AST.JSNode }
SourceElementsTop : SourceElement { (AST.JSSourceElementsTop [$1]) }
| SourceElementsTop SourceElement { (combineSourceElementsTop $1 $2) }
-- <Source Element> ::= <Statement>
-- | <Function Declaration>
SourceElement :: { AST.JSNode }
SourceElement : Statement { $1 {- SourceElement1 -} }
| FunctionDeclaration { $1 {- SourceElement2 -} }
{
combineSourceElements :: AST.JSNode -> AST.JSNode -> AST.JSNode
combineSourceElements (AST.JSSourceElements xs) x = (AST.JSSourceElements (xs++[x]) )
combineSourceElementsTop :: AST.JSNode -> AST.JSNode -> AST.JSNode
combineSourceElementsTop (AST.JSSourceElementsTop xs) x = (AST.JSSourceElementsTop (xs++[x]) )
combineStatements :: AST.JSNode -> AST.JSNode -> AST.JSNode
combineStatements (AST.JSStatementList xs) (AST.JSStatementList ys) = (AST.JSStatementList (xs++ys) )
combineStatements (AST.JSStatementList xs) y = (AST.JSStatementList (xs++[y]) )
parseError :: Token -> P a
parseError = throwError . UnexpectedToken
flattenExpression :: AST.JSNode -> [AST.JSNode] -> AST.JSNode
flattenExpression (AST.JSExpression xs) e = AST.JSExpression (xs++litComma++e)
where
litComma :: [AST.JSNode]
litComma = [(AST.JSLiteral ",")]
}
-- Set emacs mode
-- Local Variables:
-- mode:haskell
-- End: