-
Notifications
You must be signed in to change notification settings - Fork 1
/
parser.y
457 lines (374 loc) · 9.52 KB
/
parser.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
%code requires {
#include <cstdio>
#include "AST/Node/AstNode.h"
#include "AST/Expression/AstExpression.h"
#include "AST/Statement/AstStatement.h"
#include "AST/Script/AstScript.h"
int yylex();
void yyerror(char *);
}
%union {
char *regex;
char * str;
double decimal;
char *binary;
char *octal;
char *hex;
bool booelan;
char *ident;
Node *root;
StatementList *statementlist;
Statement *statement;
Expression *expression;
}
%token COMMENT NULL_L
%token <regex> REGEX_LITERAL
%token <str> STRING_L
%token <decimal> DECIMAL_L
%token <binary> BINARY_L
%token <octal> OCTAL_L
%token <hex> HEX_L
%token <bool> BooleanLiteral
%token <ident> IDENT
%token BREAK DO IN TYPEOF CASE ELSE INSTANCEOF VAR CATCH EXPORT NEW VOID CLASS EXTENDS RETURN WHILE CONST FINALLY SUPER WITH CONTINUE FOR SWITCH YIELD DEBUGGER FUNCTION THIS DEFAULT IF THROW DELETE IMPORT TRY AWAIT ENUM TDOT LE GE EQ DIFF EQTYPE DFTYPE INCREASE DECREASE LSHIFT RSHIFT URSHIFT LOGAND LOOR ADDASS SUBASS MULASS REMASS LSHIFTASS RSHIFTASS URSHIFTASS BWANDASS BWORASS BWXORASS ARROWF EXP EXPASS DIVASS LINE_TERM
%{
Node *root;
%}
%type <statementlist> StatementList StatementList_opt
%type <root> Script ScriptBody_opt ScriptBody StatementListItem
%type <statement> Statement BlockStatement Block ExpressionStatement IfStatement
%type <expression> Expression AssignmentExpression ConditionalExpression LogicalORExpression LogicalANDExpression BitwiseORExpression BitwiseXORExpression BitwiseANDExpression EqualityExpression RelationalExpression ShiftExpression AdditiveExpression MultiplicativeExpression ExponentiationExpression UnaryExpression UpdateExpression LeftHandSideExpression NewExpression MemberExpression PrimaryExpression
%type <expression> IdentifierReference Literal NumericLiteral Identifier DecimalLiteral IdentifierName
%start Script
%%
Script
: ScriptBody_opt { $$ = new Script($1); root = $$;}
;
ScriptBody_opt
: ScriptBody { $$ = $1 }
| empty
;
ScriptBody
: StatementList { $$ = new ScriptBody($1); }
;
StatementList_opt
: StatementList { $$ = $1; }
| empty
;
StatementList
: StatementListItem { $$ = new StatementList($1); }
| StatementList StatementListItem { $$ = new StatementList($1, $2); }
;
StatementListItem
: Statement { $$ = new StatementListItem($1); }
| Declaration
;
/* Level 1 */
Statement
: BlockStatement
| VariableStatement
| EmptyStatement
| ExpressionStatement { $$ = $1; }
| IfStatement { $$ = $1; }
| BreakableStatement
| ContinueStatement
| BreakStatement
| ReturnStatement
| WithStatement
| LabelledStatement
| ThrowStatement
| TryStatement
| DebuggerStatement
;
Declaration
:
;
/* Level 2 */
BlockStatement
: Block { $$ = new BlockStatement($1); }
;
VariableStatement
:
;
EmptyStatement
:
;
ExpressionStatement
: Expression ';' { $$ = new ExpressionStatement($1); }
;
IfStatement
: IF '(' Expression ')' Statement ELSE Statement { $$ = new IfStatement($3, $5, $7); }
| IF '(' Expression ')' Statement { $$ = new IfStatement($3, $5); }
;
BreakableStatement
:
;
ContinueStatement
:
;
BreakStatement
:
;
ReturnStatement
:
;
WithStatement
:
;
LabelledStatement
:
;
ThrowStatement
:
;
TryStatement
:
;
DebuggerStatement
:
;
/* Level 3 */
Block
: '{' StatementList_opt '}' { $$ = new Block($2); }
;
Expression
: AssignmentExpression { $$ = $1; }
| Expression ',' AssignmentExpression
;
/* Level 4 */
AssignmentExpression
: ConditionalExpression { $$ = new AssignmentExpression($1); }
| YieldExpression
| ArrowFunction
| LeftHandSideExpression '=' AssignmentExpression { $$ = new AssignmentExpression($1, $3); }
| LeftHandSideExpression AssignmentOperator AssignmentExpression
;
/* Level 5 */
ConditionalExpression
: LogicalORExpression { $$ = new ConditionalExpression($1); }
| LogicalORExpression '?' AssignmentExpression ':' AssignmentExpression
;
YieldExpression
:
;
ArrowFunction
:
;
LeftHandSideExpression
: NewExpression { $$ = new LeftHandSideExpression($1); }
| CallExpression
;
/* Level 6 */
NewExpression
: MemberExpression { $$ = new NewExpression($1); }
| NEW NewExpression
;
CallExpression
:
;
LogicalORExpression
: LogicalANDExpression { $$ = new LogicalORExpression($1); }
| LogicalORExpression LOOR LogicalANDExpression
;
/* Level 7 */
MemberExpression
: PrimaryExpression { $$ = new MemberExpression($1); }
| MemberExpression '[' Expression ']'
| MemberExpression '.' IdentifierName
| MemberExpression TemplateLiteral
| SuperProperty
| MetaProperty
| NEW MemberExpression Arguments
;
LogicalANDExpression
: BitwiseORExpression { $$ = new LogicalANDExpression($1); }
| LogicalANDExpression LOGAND BitwiseORExpression
;
/* Level 8 */
PrimaryExpression
: THIS
| IdentifierReference { $$ = new PrimaryExpression($1); }
| Literal { $$ = new PrimaryExpression($1); }
| ArrayLiteral
| ObjectLiteral
| FunctionExpression
| ClassExpression
| GeneratorExpression
| RegularExpressionLiteral
| TemplateLiteral
| CoverParenthesizedExpressionAndArrowParameterList
;
MemberExpression
:
;
SuperProperty
:
;
MetaProperty
:
;
Arguments
:
;
BitwiseORExpression
: BitwiseXORExpression { $$ = new BitwiseORExpression($1); }
| BitwiseORExpression '|' BitwiseXORExpression
;
/* Level 9 */
IdentifierReference
: Identifier { $$ = new IdentifierReference($1); }
| YIELD
;
Literal
: NullLiteral
| BooleanLiteral
| NumericLiteral { $$ = new Literal($1); }
| StringLiteral
;
ArrayLiteral
:
;
ObjectLiteral
:
;
FunctionExpression
:
;
ClassExpression
:
;
GeneratorExpression
:
;
CoverParenthesizedExpressionAndArrowParameterList
:
;
BitwiseXORExpression
: BitwiseANDExpression { $$ = new BitwiseXORExpression($1); }
| BitwiseXORExpression '^' BitwiseANDExpression
;
/* Level 10 */
Identifier
: IdentifierName { $$ = new Identifier($1); }
;
BitwiseANDExpression
: EqualityExpression { $$ = new BitwiseANDExpression($1); }
| BitwiseANDExpression '&' EqualityExpression
;
NumericLiteral
: DecimalLiteral { $$ = new NumericLiteral($1); }
| BinaryIntegerLiteral
| OctalIntegerLiteral
| HexIntegerLiteral
;
/* Level 11 */
EqualityExpression
: RelationalExpression { $$ = new EqualityExpression($1); }
| EqualityExpression EQ RelationalExpression { $$ = new EqualityExpression($1, $3, "=="); }
| EqualityExpression DIFF RelationalExpression { $$ = new EqualityExpression($1, $3, "!="); }
| EqualityExpression EQTYPE RelationalExpression { $$ = new EqualityExpression($1, $3, "==="); }
| EqualityExpression DFTYPE RelationalExpression { $$ = new EqualityExpression($1, $3, "!=="); }
;
/* Level 12 */
RelationalExpression
: ShiftExpression { $$ = new RelationalExpression($1); }
| RelationalExpression '<' ShiftExpression { $$ = new RelationalExpression($1, $3, "<"); }
| RelationalExpression '>' ShiftExpression { $$ = new RelationalExpression($1, $3, ">"); }
| RelationalExpression LE ShiftExpression { $$ = new RelationalExpression($1, $3, "<="); }
| RelationalExpression GE ShiftExpression { $$ = new RelationalExpression($1, $3, ">="); }
| RelationalExpression INSTANCEOF ShiftExpression { $$ = new RelationalExpression($1, $3, "instanceof"); }
| RelationalExpression IN ShiftExpression { $$ = new RelationalExpression($1, $3, "in"); }
;
/* Level 13 */
ShiftExpression
: AdditiveExpression { $$ = new ShiftExpression($1); }
| ShiftExpression LSHIFT AdditiveExpression
| ShiftExpression RSHIFT AdditiveExpression
| ShiftExpression URSHIFT AdditiveExpression
;
/* Level 14 */
AdditiveExpression
: MultiplicativeExpression { $$ = new AdditiveExpression($1); }
| AdditiveExpression '+' MultiplicativeExpression { $$ = new AdditiveExpression($1, $3, "+");}
| AdditiveExpression '-' MultiplicativeExpression
;
/* Level 15 */
MultiplicativeExpression
: ExponentiationExpression { $$ = new MultiplicativeExpression($1); }
| MultiplicativeExpression MultiplicativeOperator ExponentiationExpression
;
/* Level 16 */
ExponentiationExpression
: UnaryExpression { $$ = new ExponentiationExpression($1); }
| UpdateExpression EXP ExponentiationExpression
;
/* Level 17 */
UnaryExpression
: UpdateExpression { $$ = new UnaryExpression($1); }
| DELETE UnaryExpression
| VOID UnaryExpression
| TYPEOF UnaryExpression
| '+' UnaryExpression
| '-' UnaryExpression
| '~' UnaryExpression
| '!' UnaryExpression
;
UpdateExpression
: LeftHandSideExpression { $$ = new UpdateExpression($1); }
| LeftHandSideExpression INCREASE
| LeftHandSideExpression DECREASE
| INCREASE UnaryExpression
| DECREASE UnaryExpression
;
/* END */
IdentifierName
: IDENT { $$ = new IdentifierName($1); }
;
TemplateLiteral
:
;
RegularExpressionLiteral
:
;
NullLiteral
:
;
StringLiteral
:
;
DecimalLiteral
: DECIMAL_L { $$ = new DecimalLiteral($1); }
;
BinaryIntegerLiteral
:
;
OctalIntegerLiteral
:
;
HexIntegerLiteral
:
;
/* Utility */
AssignmentOperator
: ADDASS
| SUBASS
| MULASS
| REMASS
| DIVASS
| LSHIFTASS
| RSHIFTASS
| URSHIFTASS
| BWANDASS
| BWXORASS
| BWORASS
| EXPASS
;
MultiplicativeOperator
: '*'
| '/'
| '%'
;
empty
:
;
%%