forked from toggledbits/lexpjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrammar.jison
430 lines (395 loc) · 16.1 KB
/
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
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
/** Grammar for lexpjs. Copyright (C) 2020,2021 Patrick H. Rigney
* See https://github.com/toggledbits/lexpjs
*
* This Software is offered under the MIT LICENSE open source license. See https://opensource.org/licenses/MIT
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
* to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of
* the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
* THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* NOTA BENE: REQUIRED MODIFICATION TO JISON-LEX FOR UNICODE IDENTIFIERS: See README-lexer.md.
* If you don't need Unicode-friendly identifiers, use the non-Unicode lex pattern.
* Search for "IDENTIFIER" (ALL CAPS) in this file to find the relevant lines.
*/
/* Lexical analysis/rules. First come first served! */
%lex
%x STRD
%x STRS
%x STRB
%%
["] { this.begin("STRD"); buffer = ""; }
['] { this.begin("STRS"); buffer = ""; }
[`] { this.begin("STRB"); buffer = ""; }
<STRD,STRS,STRB>\\x[0-9a-fA-F]{2} { buffer += String.fromCharCode( parseInt( yytext.substring( 2 ), 16 ) ); }
<STRD,STRS,STRB>\\u[0-9a-fA-F]{4} { buffer += String.fromCodePoint( parseInt( yytext.substring( 2 ), 16 ) ); }
<STRD,STRS,STRB>\\u\{[0-9a-fA-F]{1,6}\} { buffer += String.fromCodePoint( parseInt( yytext.slice( 3, -1 ), 16 ) ); }
<STRD,STRS,STRB>"\\0" { buffer += "\0"; }
<STRD,STRS,STRB>"\\'" { buffer += "'"; }
<STRD,STRS,STRB>"\\\"" { buffer += '"'; }
<STRD,STRS,STRB>"\\`" { buffer += '`'; }
<STRD,STRS,STRB>"\\\\" { buffer += "\\"; }
<STRD,STRS,STRB>"\\n" { buffer += "\n"; }
<STRD,STRS,STRB>"\\r" { buffer += "\r"; }
<STRD,STRS,STRB>"\\v" { buffer += "\v"; }
<STRD,STRS,STRB>"\\t" { buffer += "\t"; }
<STRD,STRS,STRB>"\\b" { buffer += "\b"; }
<STRD,STRS,STRB>"\\f" { buffer += "\f"; }
<STRD,STRS,STRB>[\\](\r\n|\r|\n)\s* { /* escape EOL: discard */ }
<STRD,STRS,STRB>\\. { buffer += yytext.charAt( 1 ); /* bogus escape => literal char */ }
<STRD,STRS,STRB>(\r\n|\r|\n)+ { buffer += yytext; }
<STRD,STRS,STRB><<EOF>> { return 'EOF_IN_STRING'; }
<STRD>["] { this.popState(); return 'QSTR'; }
<STRS>['] { this.popState(); return 'QSTR'; }
<STRB>[`] { this.popState(); return 'QSTR'; }
<STRD,STRS,STRB>. { buffer += yytext; }
\s+ { /* skip whitespace */ }
\r { /* skip */ }
\n { /* skip */ }
"," { return 'COMMA'; }
";" { return 'EXPRSEP'; }
"local" { return 'LOCAL'; }
"global" { return 'GLOBAL'; }
"define" { return 'DEF'; }
"true" { return 'TRUE'; }
"false" { return 'FALSE'; }
"null" { return 'NULL'; }
"first" { return 'FIRST'; }
"with" { return 'WITH'; }
"each" { return 'EACH'; }
"NaN" { return 'NAN'; }
"Infinity" { return 'INF'; }
"if" { return 'IF'; }
"then" { return 'THEN'; }
"elif" { return 'ELIF'; }
"elsif" { return 'ELIF'; }
"elseif" { return 'ELIF'; }
"else" { return 'ELSE'; }
"endif" { return 'ENDIF'; }
"case" { return 'CASE'; }
"when" { return 'WHEN'; }
"end" { return 'END'; }
"in" { return 'IN'; }
"done" { return 'DONE'; }
"do" { return 'DO'; }
"and" { return 'LAND'; }
"or" { return 'LOR'; }
"not" { return 'LNOT'; }
/* NOTE: Only ONE of the two patterns that follow should be uncommented. */
/* Use this line if you don't want/need Unicode-friendly identifiers. */
/* [A-Za-z_$][A-Za-z0-9_$]*\b { return 'IDENTIFIER'; } */
/* Use this line for Unicode-friendly identifiers. */
[\p{Alphabetic}_$][\p{Alphabetic}0-9_$]*\b { return 'IDENTIFIER'; }
[0-9]+("."[0-9]+)?([eE][+-]?[0-9]+)?\b { return 'NUMBER'; }
0x[0-9A-Fa-f]+\b { return 'HEXNUM'; }
0o[0-7]+\b { return 'OCTNUM'; }
0b[01]+\b { return 'BINNUM'; }
\.\. { return 'RANGE'; }
":" { return 'COLON'; }
"**" { return 'POW'; }
"*" { return '*'; }
"/" { return '/'; }
"%" { return 'MOD'; }
"-" { return '-'; }
"+" { return '+'; }
">>>" { return '>>>'; }
"<<" { return '<<'; }
">>" { return '>>'; }
"<=" { return '<='; }
">=" { return '>='; }
"<" { return '<'; }
">" { return '>'; }
"===" { return '==='; }
"==" { return '=='; }
"!==" { return '!=='; }
"!=" { return '!='; }
"<>" { return '!='; }
"^" { return 'BXOR'; }
"&&" { return 'LAND'; }
"||" { return 'LOR'; }
"!" { return 'LNOT'; }
"&" { return 'BAND'; }
"|" { return 'BOR'; }
"~" { return 'BNOT'; }
"??" { return 'COALESCE'; }
"?#" { return 'COALESCENAN' }
"?." { return 'QDOT'; }
"?[" { return 'QBRACKET'; }
"?" { return '?'; }
"=" { return 'ASSIGN'; }
"." { return 'DOT'; }
"[" { return '['; }
"]" { return ']'; }
"(" { return '('; }
")" { return ')'; }
"{" { return 'LCURLY'; }
"}" { return 'RCURLY'; }
\#[^\r\n]* { /* skip comment */ }
<<EOF>> { return 'EOF'; }
/lex
/* Operator associativity and precedence */
%right ASSIGN
%right DEF LOCAL GLOBAL
%right FIRST EACH WITH
%right '?'
%right COLON
%left COALESCE COALESCENAN
%left LOR
%left LAND
%left BOR
%left BXOR
%left BAND
%nonassoc '==' '===' '!=' '!=='
%nonassoc IN
%nonassoc '<' '<=' '>' '>='
%left RANGE
%left '<<' '>>' '>>>'
%left '+' '-'
%left '*' '/' MOD
%right POW
%left UMINUS
%right BNOT LNOT
%left DOT QDOT QBRACKET
%start expressions
%{
/* Grammar 22307 */
var buffer = "", qsep = "";
function is_atom( v, typ ) {
return null !== v && "object" === typeof( v ) &&
"undefined" !== typeof v.__atom &&
( !typ || v.__atom === typ );
}
function atom( t, vs ) {
var a = { __atom: t };
Object.keys(vs || {}).forEach( function( key ) {
a[key] = vs[key];
});
return a;
}
function D( ...args ) {
console.log( ...args );
}
%}
%%
/* Here's the grammar. */
expressions
: expr_list EOF
{ return $1; }
;
elif_list
: elif_list ELIF e THEN expr_list
{ $1.push( { test: $3, tc: $5 } ); $$ = $1; }
| ELIF e THEN expr_list
{ $$ = [ { test: $2, tc: $4 } ]; }
;
expr_list
: expr_list COMMA e
{ $1.expr.push( $3 ); $$ = $1; }
| expr_list EXPRSEP e
{ $1.expr.push( $3 ); $$ = $1; }
| e
{ $$ = atom( 'list', { expr: [ $1 ] } ); }
;
/* arg_list - function arguments: expr_list or nothing */
arg_list
: expr_list
{ $$ = $1; }
|
{ $$ = atom( 'list', { expr: [] } ); }
;
/** A reference expression is an expression referring to a value or a member of a value (presumed to be an object or array).
* Certain expressions, such as function calls and parenthesized results, are considered reference expressions. This is a
* slight constraint to help disambiguate certain constructions that may otherwise be possible but usually aren't used
* (e.g. what does "A-OK"[3] mean?)
*/
ref_expr
: IDENTIFIER
{ $$ = atom( 'vref', { name: $1 } ); }
| ref_expr DOT IDENTIFIER
{ $$ = atom( 'deref', { context: $1, member: $3, locs: [@1, @3] } ); }
| ref_expr '[' e ']'
{ $$ = atom( 'deref', { context: $1, member: $3, locs: [@1, @3] } ); }
| ref_expr QDOT IDENTIFIER
{ $$ = atom( 'deref', { context: $1, member: $3, locs: [@1, @3], op: $2 } ); }
| ref_expr QBRACKET e ']'
{ $$ = atom( 'deref', { context: $1, member: $3, locs: [@1, @3], op: $2 } ); }
| IDENTIFIER '(' arg_list ')'
{ $$ = atom( 'fref', { name: $1, args: is_atom( $3, 'list') ? ($3).expr : [ $3 ], locs: [@1] } ); }
| '(' e ')'
{ $$ = $2; }
;
quoted_string : QSTR { $$ = buffer; } ; /* creates necessary indirection (vs using QSTR below directly) */
dict_element
: IDENTIFIER COLON e
{ $$ = { key: $1, value: $3 }; }
| '[' quoted_string ']' COLON e
{ $$ = { key: $2, value: $5 }; }
| quoted_string COLON e
{ $$ = { key: $1, value: $3 }; }
;
dict_elements
: dict_elements COMMA dict_element
{ ($1)[($3).key] = ($3).value; $$ = $1; }
| dict_element
{ $$ = { [($1).key]: ($1).value }; }
;
element_list
: dict_elements
{ $$ = $1; }
|
{ $$ = {}; }
;
array_elements
: array_elements COMMA e
{ $1.push( $3 ); $$ = $1; }
| e
{ $$ = [ $1 ]; }
;
/* Should this build a list atom? ??? */
array_list
: array_elements
{ $$ = $1; }
|
{ $$ = []; }
;
assignment
: GLOBAL IDENTIFIER ASSIGN e
{ $$ = atom( 'binop', { 'op': $3, v1: atom( 'vref', { name: $2 } ), v2: $4, global: true, locs: [@2, @4] } ); }
| LOCAL IDENTIFIER ASSIGN e
{ $$ = atom( 'binop', { 'op': $3, v1: atom( 'vref', { name: $2 } ), v2: $4, local: true, locs: [@2, @4] } ); }
| ref_expr ASSIGN e
{ $$ = atom( 'binop', { 'op': $2, v1: $1, v2: $3, locs: [@1, @3] } ); }
;
when_list
: when_list WHEN e COLON e ELSE e
{ $1.expr.push( atom( 'if', { test: $3, tc: $5, fc: $7, locs: [@3, @5, @7] } ) ); $$ = $1; }
| when_list WHEN e COLON e
{ $1.expr.push( atom( 'if', { test: $3, tc: $5, locs: [@3, @5] } ) ); $$ = $1; }
| WHEN e COLON e
{ $$ = atom( 'list', { expr: [ atom( 'if', { test: $2, tc: $4, locs: [@2, @4] } ) ] } ); }
;
e
: '-' e %prec UMINUS
{ $$ = atom( 'unop', { op: '-', val: $2 } ); }
| LNOT e
{ $$ = atom( 'unop', { op: '!', val: $2 } ); }
| BNOT e
{ $$ = atom( 'unop', { op: '~', val: $2 } ); }
| e POW e
{ $$ = atom( 'binop', { op: $2, v1: $1, v2: $3, locs: [@1,@3] } ); }
| e RANGE e
{ $$ = atom( 'binop', { op: $2, v1: $1, v2: $3, locs: [@1,@3] } ); }
| e '*' e
{ $$ = atom( 'binop', { op: $2, v1: $1, v2: $3, locs: [@1,@3] } ); }
| e '/' e
{ $$ = atom( 'binop', { op: $2, v1: $1, v2: $3, locs: [@1,@3] } ); }
| e MOD e
{ $$ = atom( 'binop', { op: $2, v1: $1, v2: $3, locs: [@1,@3] } ); }
| e '+' e
{ $$ = atom( 'binop', { op: $2, v1: $1, v2: $3, locs: [@1,@3] } ); }
| e '-' e
{ $$ = atom( 'binop', { op: $2, v1: $1, v2: $3, locs: [@1,@3] } ); }
| e '<<' e
{ $$ = atom( 'binop', { op: $2, v1: $1, v2: $3, locs: [@1,@3] } ); }
| e '>>' e
{ $$ = atom( 'binop', { op: $2, v1: $1, v2: $3, locs: [@1,@3] } ); }
| e '>>>' e
{ $$ = atom( 'binop', { op: $2, v1: $1, v2: $3, locs: [@1,@3] } ); }
| e BAND e
{ $$ = atom( 'binop', { op: $2, v1: $1, v2: $3, locs: [@1,@3] } ); }
| e BOR e
{ $$ = atom( 'binop', { op: $2, v1: $1, v2: $3, locs: [@1,@3] } ); }
| e BXOR e
{ $$ = atom( 'binop', { op: $2, v1: $1, v2: $3, locs: [@1,@3] } ); }
| e LAND e
{ $$ = atom( 'binop', { op: '&&', v1: $1, v2: $3, locs: [@1,@3] } ); }
| e LOR e
{ $$ = atom( 'binop', { op: '||', v1: $1, v2: $3, locs: [@1,@3] } ); }
| e '==' e
{ $$ = atom( 'binop', { op: $2, v1: $1, v2: $3, locs: [@1,@3] } ); }
| e '!=' e
{ $$ = atom( 'binop', { op: $2, v1: $1, v2: $3, locs: [@1,@3] } ); }
| e '===' e
{ $$ = atom( 'binop', { op: $2, v1: $1, v2: $3, locs: [@1,@3] } ); }
| e '!==' e
{ $$ = atom( 'binop', { op: $2, v1: $1, v2: $3, locs: [@1,@3] } ); }
| e IN e
{ $$ = atom( 'binop', { op: $2, v1: $1, v2: $3, locs: [@1,@3] } ); }
| e '<' e
{ $$ = atom( 'binop', { op: $2, v1: $1, v2: $3, locs: [@1,@3] } ); }
| e '<=' e
{ $$ = atom( 'binop', { op: $2, v1: $1, v2: $3, locs: [@1,@3] } ); }
| e '>' e
{ $$ = atom( 'binop', { op: $2, v1: $1, v2: $3, locs: [@1,@3] } ); }
| e '>=' e
{ $$ = atom( 'binop', { op: $2, v1: $1, v2: $3, locs: [@1,@3] } ); }
| e COALESCE e
{ $$ = atom( 'binop', { op: $2, v1: $1, v2: $3, locs: [@1,@3] } ); }
| e COALESCENAN e
{ $$ = atom( 'binop', { op: $2, v1: $1, v2: $3, locs: [@1,@3] } ); }
| e '?' e COLON e
{ $$ = atom( 'if', { test: $1, tc: $3, fc: $5, locs: [@1, @3, @5] } ); }
| LCURLY element_list RCURLY
{ $$ = $2; }
| '[' array_list ']'
{ $$ = $2; }
| NUMBER
{ $$ = Number(yytext); }
| HEXNUM
{ $$ = parseInt( yytext.substr( 2 ), 16 ); }
| OCTNUM
{ $$ = parseInt( yytext.substr( 2 ), 8 ); }
| BINNUM
{ $$ = parseInt( yytext.substr( 2 ), 2 ); }
| quoted_string
{ $$ = $1; }
| IF e THEN expr_list elif_list ELSE expr_list ENDIF
{ $$ = atom( 'if', { test: $2, tc: $4, alts: $5, fc: $7, locs: [@2, @4, @5, @7] } ); }
| IF e THEN expr_list elif_list ENDIF
{ $$ = atom( 'if', { test: $2, tc: $4, alts: $5, locs: [@2, @4, @5] } ); }
| IF e THEN expr_list ELSE expr_list ENDIF
{ $$ = atom( 'if', { test: $2, tc: $4, fc: $6, locs: [@2, @4, @6] } ); }
| IF e THEN expr_list ENDIF
{ $$ = atom( 'if', { test: $2, tc: $4, locs: [@2, @4] } ); }
| TRUE
{ $$ = true; }
| FALSE
{ $$ = false; }
| NULL
{ $$ = null; }
| NAN
{ $$ = NaN; }
| INF
{ $$ = Infinity; }
| ref_expr
{ $$ = $1; }
| assignment
{ $$ = $1; }
| EACH IDENTIFIER COMMA IDENTIFIER IN e COLON e
{ $$ = atom( 'iter', { value: $2, key: $4, context: $6, exec: $8 } ); }
| EACH IDENTIFIER IN e COLON e
{ $$ = atom( 'iter', { value: $2, context: $4, exec: $6 } ); }
| FIRST IDENTIFIER COMMA IDENTIFIER IN e WITH e COLON e
{ $$ = atom( 'search', { value: $2, key: $4, context: $6, exec: $8, result: $10 } ); }
| FIRST IDENTIFIER IN e WITH e COLON e
{ $$ = atom( 'search', { value: $2, context: $4, exec: $6, result: $8 } ); }
| FIRST IDENTIFIER COMMA IDENTIFIER IN e WITH e
{ $$ = atom( 'search', { value: $2, key: $4, context: $6, exec: $8 } ); }
| FIRST IDENTIFIER IN e WITH e
{ $$ = atom( 'search', { value: $2, context: $4, exec: $6 } ); }
| DO expr_list DONE
{ $$ = atom( 'block', { block: $2 } ); }
| DEF IDENTIFIER '(' arg_list ')' e
{ $$ = atom( 'fdef', { name: $2, args: $4, list: $6 } ); }
| CASE when_list END
{ $$ = atom( 'case', { when_list: $2 } ); }
;