-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyadrol-grammar.jison
383 lines (302 loc) · 12 KB
/
yadrol-grammar.jison
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
%lex
%options ranges
%x string
%%
<<EOF>> return 'EOF';
\s+ /* skip */
"//".*\n /* skip */
"//".*<<EOF>> /* skip */
\$[^\$]+\$ return 'PLACEHOLDER';
[-]{3,} return 'BREAK';
";" return 'SEMICOLON';
"import" return 'IMPORT';
"sample" return 'OUTPUT';
"roll" return 'OUTPUT';
"as" return 'AS';
"if" return 'IF';
"then" return 'THEN';
"else" return 'ELSE';
"for" return 'FOR';
"repeat" return 'REPEAT';
"while" return 'WHILE';
"limit" return 'LIMIT';
"in" return 'IN';
"count" return 'COUNT';
"#" return 'COUNT';
"string" return 'CONVERT';
"boolean" return 'CONVERT';
"number" return 'CONVERT';
"list" return 'CONVERT';
"map" return 'CONVERT';
"sort" return 'REORDER';
"reverse" return 'REORDER';
"shuffle" return 'REORDER';
"or" return 'OR';
"and" return 'AND';
"not" return 'NOT';
"<<" return 'APPEND';
"===" return 'GEN_COMP';
"!==" return 'GEN_COMP';
"==" return 'NUM_COMP';
"!=" return 'NUM_COMP';
"<=" return 'NUM_COMP';
">=" return 'NUM_COMP';
"<" return 'NUM_COMP';
">" return 'NUM_COMP';
".." return 'RANGE';
"+" return 'PLUS';
"-" return 'PLUS';
"*" return 'MULT';
"/" return 'MULT';
"%" return 'MULT';
"=" return 'ASSIGN';
"highest" return 'BEST';
"lowest" return 'BEST';
"first" return 'BEST';
"last" return 'BEST';
"of" return 'OF';
"draw" return 'DRAW';
"from" return 'FROM';
"d" return 'DICE';
[A-Z]"d" return 'UPPER_DICE';
"d"[A-Z]\w* return 'DICE_UPPER';
[A-Z]"d"[A-Z]\w* return 'UPPER_DICE_UPPER';
"d"\d+ return 'DICE_NUMBER';
[A-Z]"d"\d+ return 'UPPER_DICE_NUMBER';
"(" return 'LPAREN';
")" return 'RPAREN';
"[" return 'LBRACKET';
"]" return 'RBRACKET';
"," return 'COMMA';
":" return 'COLON';
"." return 'DOT';
"local" return 'SCOPE';
"outer" return 'SCOPE';
"global" return 'SCOPE';
"this" return 'THIS';
"undef" return 'UNDEF';
"false" return 'BOOLEAN';
"true" return 'BOOLEAN';
[0-9]+ return 'NUMBER';
"fun" return 'FUN';
'{' return 'LCURLY';
'}' return 'RCURLY';
[A-Z_a-z]\w* return 'IDENTIFIER';
"\"" this.begin('string'); return 'STR_START';
<string><<EOF>> throw new Error('unterminated string literal');
<string>"\"" this.popState(); return 'STR_END';
<string>\\n return 'STR_NL';
<string>\\\" return 'STR_DQ';
<string>[^\\\"]* return 'STR_CONST';
/lex
%left BREAK
%left SEMICOLON
%nonassoc IMPORT OUTPUT
%nonassoc ASSIGN
%nonassoc IF WHILE LIMIT FOR
%left OR
%left AND
%left NOT
%nonassoc GEN_COMP NUM_COMP
%left IN
%nonassoc APPEND
%nonassoc RANGE
%left PLUS
%left MULT
%nonassoc SIGN
%nonassoc BEST
%nonassoc DRAW
%left DICE DICE_UPPER UPPER_DICE DICE_NUMBER
%nonassoc COUNT REORDER CONVERT
%left LPAREN LBRACKET DOT
%start top
%%
top
: expressionList EOF { return $1; }
;
expressionList
: expression optSemicolon { $$ = [$1]; }
| expression optSemicolon BREAK expressionList { $4.unshift($1); $$ = $4; }
;
expression
: LPAREN expression RPAREN
{ $$ = $2; }
| UNDEF
{ $$ = new Constant(Location.fromLexer(yy.sourceFile, @1, @1), undefined); }
| BOOLEAN
{ $$ = new Constant(Location.fromLexer(yy.sourceFile, @1, @1), (yytext == 'true')); }
| STR_START STR_END
{ $$ = new Constant(Location.fromLexer(yy.sourceFile, @1, @1), ''); }
| STR_START string STR_END
{ $$ = new StringInterpolation(Location.fromLexer(yy.sourceFile, @1, @3), $2); }
| NUMBER
{ $$ = new Constant(Location.fromLexer(yy.sourceFile, @1, @1), Number(yytext)); }
| LBRACKET list RBRACKET
{ $$ = new ContainerConstructor(Location.fromLexer(yy.sourceFile, @1, @3), $2, 'list'); }
| LCURLY map RCURLY
{ $$ = new ContainerConstructor(Location.fromLexer(yy.sourceFile, @1, @3), new YadrolMap($2), 'map'); }
| FUN LPAREN lambdaArgs RPAREN LCURLY expression RCURLY
{ $$ = new Lambda(Location.fromLexer(yy.sourceFile, @1, @7), new YadrolMap($3), $6); }
| IDENTIFIER
{ $$ = new Variable(Location.fromLexer(yy.sourceFile, @1, @1), yytext); }
| SCOPE
{ $$ = new ScopeVariables(Location.fromLexer(yy.sourceFile, @1, @1), ScopeVariables[yytext.toUpperCase()]); }
| THIS
{ $$ = new This(Location.fromLexer(yy.sourceFile, @1, @1)); }
| expression DOT IDENTIFIER
{ $$ = new Subscript(Location.fromLexer(yy.sourceFile, @1, @3), $1, new Constant(Location.fromLexer(yy.sourceFile, @3, @3), $3)); }
| expression LBRACKET expression RBRACKET
{ $$ = new Subscript(Location.fromLexer(yy.sourceFile, @1, @4), $1, $3); }
| expression LPAREN callArgs RPAREN
{ var posArgs = yy.extractPositionalArgs($3); $$ = new Call(Location.fromLexer(yy.sourceFile, @1, @4), $1, posArgs, new YadrolMap($3)); }
| COUNT expression
{ $$ = new Count(Location.fromLexer(yy.sourceFile, @1, @2), $2); }
| REORDER expression
{ $$ = new ListReorder(Location.fromLexer(yy.sourceFile, @1, @2), ListReorder[$1.toUpperCase()], $2); }
| CONVERT expression
{ $$ = new Convert(Location.fromLexer(yy.sourceFile, @1, @2), $2, $1); }
| expression DICE expression
{ $$ = new Dice(Location.fromLexer(yy.sourceFile, @1, @3), $1, $3, yy.recordLogger); }
| expression DICE_UPPER
{ $$ = new Dice(Location.fromLexer(yy.sourceFile, @1, @2), $1, new Variable(Location.fromLexer(yy.sourceFile, @2, @2), $2.slice(1)), yy.recordLogger); }
| expression DICE_NUMBER
{ $$ = new Dice(Location.fromLexer(yy.sourceFile, @1, @2), $1, new Constant(Location.fromLexer(yy.sourceFile, @2, @2), Number($2.slice(1))), yy.recordLogger); }
| UPPER_DICE expression
{ $$ = new Dice(Location.fromLexer(yy.sourceFile, @1, @2), new Variable(Location.fromLexer(yy.sourceFile, @1, @1), $1.slice(0, 1)), $2, yy.recordLogger); }
| UPPER_DICE_UPPER
{ $$ = new Dice(Location.fromLexer(yy.sourceFile, @1, @1), new Variable(Location.fromLexer(yy.sourceFile, @1, @1), $1.slice(0, 1)), new Variable(Location.fromLexer(yy.sourceFile, @1, @1), $1.slice(2)), yy.recordLogger); }
| UPPER_DICE_NUMBER
{ $$ = new Dice(Location.fromLexer(yy.sourceFile, @1, @1), new Variable(Location.fromLexer(yy.sourceFile, @1, @1), $1.slice(0, 1)), new Constant(Location.fromLexer(yy.sourceFile, @1, @1), Number($1.slice(2))), yy.recordLogger); }
| DICE expression
{ $$ = new Die(Location.fromLexer(yy.sourceFile, @1, @2), $2, yy.recordLogger); }
| DICE_UPPER
{ $$ = new Die(Location.fromLexer(yy.sourceFile, @1, @1), new Variable(Location.fromLexer(yy.sourceFile, @1, @1), $1.slice(1)), yy.recordLogger); }
| DICE_NUMBER
{ $$ = new Die(Location.fromLexer(yy.sourceFile, @1, @1), new Constant(Location.fromLexer(yy.sourceFile, @1, @1), Number($1.slice(1))), yy.recordLogger); }
| DRAW expression FROM expression
{ $$ = new DrawMultiple(Location.fromLexer(yy.sourceFile, @1, @4), $2, $4); }
| DRAW FROM expression
{ $$ = new Draw(Location.fromLexer(yy.sourceFile, @1, @3), $3); }
| BEST expression OF expression
{ $$ = new BestMultiple(Location.fromLexer(yy.sourceFile, @1, @4), Best[$1.toUpperCase()], $2, $4); }
| BEST OF expression
{ $$ = new Best(Location.fromLexer(yy.sourceFile, @1, @3), Best[$1.toUpperCase()], $3); }
| PLUS expression %prec SIGN
{ $$ = new Sign(Location.fromLexer(yy.sourceFile, @1, @2), yy.getSignOperator($1), $2); }
| expression MULT expression
{ $$ = new Arithmetic(Location.fromLexer(yy.sourceFile, @1, @3), yy.getArithmeticOperator($2), $1, $3); }
| expression PLUS expression
{ $$ = new Arithmetic(Location.fromLexer(yy.sourceFile, @1, @3), yy.getArithmeticOperator($2), $1, $3); }
| expression RANGE expression
{ $$ = new Range(Location.fromLexer(yy.sourceFile, @1, @3), $1, $3); }
| expression APPEND expression
{ $$ = new Append(Location.fromLexer(yy.sourceFile, @1, @3), $1, $3); }
| expression GEN_COMP expression
{ $$ = new GeneralComparison(Location.fromLexer(yy.sourceFile, @1, @3), yy.getGeneralComparisonOperator($2), $1, $3); }
| expression NUM_COMP expression
{ $$ = new NumberComparison(Location.fromLexer(yy.sourceFile, @1, @3), yy.getNumberComparisonOperator($2), $1, $3); }
| expression IN expression
{ $$ = new IndexOf(Location.fromLexer(yy.sourceFile, @1, @3), $1, $3); }
| NOT expression
{ $$ = new BooleanNot(Location.fromLexer(yy.sourceFile, @1, @2), $2); }
| expression AND expression
{ $$ = new BooleanAnd(Location.fromLexer(yy.sourceFile, @1, @3), $1, $3); }
| expression OR expression
{ $$ = new BooleanOr(Location.fromLexer(yy.sourceFile, @1, @3), $1, $3); }
| IF expression THEN expression ELSE expression
{ $$ = new Conditional(Location.fromLexer(yy.sourceFile, @1, @6), $2, $4, $6); }
| WHILE expression REPEAT expression limit
{ $$ = new Repeat(Location.fromLexer(yy.sourceFile, @1, @5), $4, $2, true, $5); }
| REPEAT expression WHILE expression limit
{ $$ = new Repeat(Location.fromLexer(yy.sourceFile, @1, @5), $2, $4, false, $5); }
| REPEAT expression IF expression
{ $$ = new Repeat(Location.fromLexer(yy.sourceFile, @1, @4), $2, $4, false, 1); }
| FOR loopVars IN expression IF expression
{ $$ = new ForLoop(Location.fromLexer(yy.sourceFile, @1, @6), $2[0], $2[1], new Variable(Location.fromLexer(yy.sourceFile, @2, @2), $2[1]), $4, $6); }
| expression FOR loopVars IN expression forLoopCondition
{ $$ = new ForLoop(Location.fromLexer(yy.sourceFile, @1, @6), $3[0], $3[1], $1, $5, $6); }
| expression ASSIGN expression
{ $$ = new Assign(Location.fromLexer(yy.sourceFile, @1, @3), $1, $3); }
| IMPORT STR_START string STR_END
{ $$ = new Import(Location.fromLexer(yy.sourceFile, @1, @2), new StringInterpolation(Location.fromLexer(yy.sourceFile, @2, @4), $3), undefined, yy.recordLogger); }
| IMPORT IDENTIFIER ASSIGN STR_START string STR_END
{ $$ = new Import(Location.fromLexer(yy.sourceFile, @1, @4), new StringInterpolation(Location.fromLexer(yy.sourceFile, @4, @6), $5), $2, yy.recordLogger); }
| OUTPUT expression
{ $$ = new Output(Location.fromLexer(yy.sourceFile, @1, @2), undefined, $2, yy.recordLogger.defaultType, Output[$1.toUpperCase()], yy.recordLogger); }
| OUTPUT expression AS CONVERT
{ $$ = new Output(Location.fromLexer(yy.sourceFile, @1, @4), undefined, $2, $4, Output[$1.toUpperCase()], yy.recordLogger); }
| OUTPUT expression AS STR_START string STR_END
{ $$ = new Output(Location.fromLexer(yy.sourceFile, @1, @6), new StringInterpolation(Location.fromLexer(yy.sourceFile, @1, @5), $5), $2, yy.recordLogger.defaultType, Output[$1.toUpperCase()], yy.recordLogger); }
| OUTPUT expression AS CONVERT STR_START string STR_END
{ $$ = new Output(Location.fromLexer(yy.sourceFile, @1, @7), new StringInterpolation(Location.fromLexer(yy.sourceFile, @1, @6), $6), $2, $4, Output[$1.toUpperCase()], yy.recordLogger); }
| OUTPUT expression AS STR_START string STR_END CONVERT
{ $$ = new Output(Location.fromLexer(yy.sourceFile, @1, @7), new StringInterpolation(Location.fromLexer(yy.sourceFile, @1, @4), $4), $2, $6, Output[$1.toUpperCase()], yy.recordLogger); }
| expression semicolon expression %prec SEMICOLON
{ $$ = new Sequence(Location.fromLexer(yy.sourceFile, @1, @3), $1, $3); }
;
list
: { $$ = []; }
| expression { $$ = [$1]; }
| expression COMMA list { $3.unshift($1); $$ = $3; }
;
map
: { $$ = []; }
| mapEntry { $$ = [$1]; }
| mapEntry COMMA map { $3.unshift($1); $$ = $3; }
;
mapEntry
: IDENTIFIER COLON expression { $$ = [ $1, $3 ]; }
;
lambdaArgs
: { $$ = []; }
| lambdaArg { $$ = [$1]; }
| lambdaArg COMMA lambdaArgs { $3.unshift($1); $$ = $3; }
;
lambdaArg
: IDENTIFIER lambdaArgValue { $$ = [$1, $2]; }
;
lambdaArgValue
: { $$ = new Constant(Location.NONE, undefined); }
| COLON expression { $$ = $2; }
;
callArgs
: { $$ = []; }
| callArg { $$ = [$1]; }
| callArg COMMA callArgs { $3.unshift($1); $$ = $3; }
;
callArg
: IDENTIFIER COLON expression { $$ = [$1, $3]; }
| expression { $$ = [$1]; }
;
limit
: { $$ = Number.MAX_VALUE; }
| LIMIT NUMBER { $$ = Number($2); }
;
loopVars
: IDENTIFIER { $$ = [undefined, $1]; }
| IDENTIFIER COMMA IDENTIFIER { $$ = [$1, $3]; }
;
forLoopCondition
: { $$ = new Constant(Location.NONE, true) }
| IF expression { $$ = $2; }
;
semicolon
: SEMICOLON
| SEMICOLON semicolon
;
optSemicolon
:
| SEMICOLON optSemicolon
;
string
: stringElement { $$ = [$1]; }
| stringElement string { $2.unshift($1); $$ = $2; }
;
stringElement
: STR_NL { $$ = new Constant(Location.fromLexer(yy.sourceFile, @1), '\n'); }
| STR_DQ { $$ = new Constant(Location.fromLexer(yy.sourceFile, @1), '"'); }
| STR_CONST { $$ = new Constant(Location.fromLexer(yy.sourceFile, @1), $1); }
;
placeholder
: PLACEHOLDER
;