-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgremlins.jison
391 lines (330 loc) · 9.39 KB
/
gremlins.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
%lex
%%
// Ignorables
([/][/][^\n]*\n) // Ignore comments
([/][*](.|\n)*[*][/]) // Ignore comments
(\s+) // Ignore Whitespace
"," return "COMMA"
":" return "COLON"
";" return "SEMI"
// Literals
([0][x][0-9a-fA-F]+) return "NUMBER"
([0][b][01]+) return "NUMBER"
([0-9]+) return "NUMBER"
"true" return "BOOL"
"false" return "BOOL"
"null" return "NULL"
// ("0x"[0-9a-f]+) return "BYTES"
//(["]([^\]|[\].)+["]) return "STRING"
(["]([^\\]|\\.)*["]) return "STRING"
// Operators
// Maths
([*/%][=]) return "ASSIGN"
([*/%]) return "MULOP"
([+-][=]) return "ASSIGN"
"+" return "PLUS"
"-" return "MINUS"
("<<"|">>"|">>>") return "SHIFTOP"
("<"|">"|"<="|">=") return "RELOP"
("=="|"!=") return "EQOP"
"&&" return "LOGICAND"
"||" return "LOGICOR"
"&" return "AND"
"|" return "OR"
"^" return "XOR"
"!" return "NOT"
// Assignment Ops
"=" return "EQUALS"
//([!][/][?]?[=]) return "ASSIGN"
//((<<|>>|>>>)=) return "ASSIGN"
// Brackets
"(" return "OPEN_PAREN"
")" return "CLOSE_PAREN"
"<" return "OPEN_TRIANGLE"
"<" return "CLOSE_TRIANGLE"
"[" return "OPEN_BRACKET"
"]" return "CLOSE_BRACKET"
"{" return "OPEN_BRACE"
"}" return "CLOSE_BRACE"
// Keywords
"const" return "DECLARE"
"let" return "DECLARE"
"function" return "FUNCTION"
"interface" return "INTERFACE"
"if" return "IF"
"else" return "ELSE"
"while" return "WHILE"
"try" return "TRY"
"catch" return "CATCH"
"return" return "RETURN"
// Solidity-specific Types
(("bytes"|"int"|"uint")[0-9]+) return "TYPE_SOLC"
// Types
"address" return "ADDRESS"
"boolean" return "BOOLEAN"
"bytes" return "BYTES"
"hash" return "HASH"
"int" return "INT"
"string" return "STRING"
"uint" return "UINT"
// ID; this should be parsed last
([A-Za-z_][A-Za-z0-9_]*) return "ID"
// Special
<<EOF>> return "EOF"
return "INVALID"
/lex
%start program
%%
program
: top_statements EOF
{ {
const result = { type: "program", interfaces: [ ], functions: [ ], statements: [ ] };
const targets = { interface: result.interfaces, func: result.functions };
for (const stmt of $1) {
(targets[stmt.type] || result.statements).push(stmt);
}
return result;
} }
;
top_statements
: /* empty */
{ $$ = [ ]; }
| top_statement top_statements
{ $$ = [ $1, ...$2 ]; }
;
top_statement
: statement
{ $$ = $1; }
| interface_solc
{ $$ = $1; }
| func
{ $$ = $1; }
;
interface_solc
: INTERFACE ID OPEN_BRACE methods_solc CLOSE_BRACE
{ $$ = { type: "interface", id: $2, methods: $4 }; }
;
methods_solc
: /* empty */
{ $$ = [ ]; }
| method_solc methods_solc
{ $$ = [ $1, ...$2 ]; }
;
method_solc
: ID OPEN_PAREN params_solc CLOSE_PAREN COLON type_solc SEMI
{ $$ = { type: "method", id: $1, params: $3, returns: $6 }; }
| ID OPEN_PAREN CLOSE_PAREN COLON type_solc SEMI
{ $$ = { type: "method", id: $1, params: [ ], returns: $5 }; }
;
params_solc
: param_solc
{ $$ = [ $1 ]; }
| param_solc COMMA params_solc
{ $$ = [ $1, ...$3 ]; }
;
param_solc
: type_solc
${ $$ = { type: $1 }; }
| ID COLON type_solc
${ $$ = { id: $1, type: $3 }; }
;
// Solidity types overlap with Gremlin types, so this includes
// the overlap and the Solidity-specific
type_solc
: ADDRESS
${ $$ = $1; }
| BOOLEAN
${ $$ = $1; }
| BYTES
${ $$ = $1; }
| INT
${ $$ = $1; }
| STRING
${ $$ = $1; }
| UINT
${ $$ = $1; }
| TYPE_SOLC
${ $$ = $1; }
;
func
: FUNCTION ID OPEN_PAREN CLOSE_PAREN COLON type OPEN_BRACE statements CLOSE_BRACE
{ $$ = { type: "func", id: $2, body: $8, returns: $6 }; }
| FUNCTION ID OPEN_PAREN params CLOSE_PAREN COLON type OPEN_BRACE statements CLOSE_BRACE
{ $$ = { type: "func", id: $2, params: $4, body: $9, returns: $7 }; }
;
params
: param
${ $$ = [ $1 ]; }
| param COMMA params
${ $$ = [ $1, ...$3 ]; }
;
param
: ID
{ $$ = { id: $1 }; }
| ID COLON type
{ $$ = { id: $1, type: $3 }; }
;
type
: ADDRESS
{ $$ = $1; }
| BOOLEAN
{ $$ = $1; }
| BYTES
{ $$ = $1; }
| HASH
{ $$ = $1; }
| INT
{ $$ = $1; }
| STRING
{ $$ = $1; }
| UINT
{ $$ = $1; }
;
statements
: /* empty */
{ $$ = [ ]; }
| statement statements
{ $$ = [ $1, ...$2 ]; }
;
statement_if
: IF OPEN_PAREN expr CLOSE_PAREN OPEN_BRACE statements CLOSE_BRACE
{ $$ = { type: "stmt-if", branches: [ { cond: $3, body: $6 } ] }; }
;
statement_elseif
: statement_if
{ $$ = $1; }
| statement_elseif ELSE IF OPEN_PAREN expr CLOSE_PAREN OPEN_BRACE statements CLOSE_BRACE
{ {
const branches = $1.branches;
branches.push({ cond: $5, body: $8 });
$$ = { type: "stmt-if", branches };
} }
;
statement_else
: statement_elseif
{ $$ = $1; }
| statement_elseif ELSE OPEN_BRACE statements CLOSE_BRACE
{ {
const branches = $1.branches;
branches.push({ body: $4 });
$$ = { type: "stmt-if", branches } ;
} }
;
statement
: ID ASSIGN expr SEMI
{ $$ = { type: "stmt-assign", target: $1, op: $2, expr: $3 }; }
| ID EQUALS expr SEMI
{ $$ = { type: "stmt-assign", target: $1, op: "=", expr: $3 }; }
| DECLARE ID EQUALS expr SEMI
{ $$ = { type: "stmt-assign", declare: $1, target: $1, op: $2, expr: $3 }; }
| expr SEMI
{ $$ = { type: "stmt-expr", expr: $1 }; }
| WHILE OPEN_PAREN expr CLOSE_PAREN OPEN_BRACE statements CLOSE_BRACE
{ $$ = { type: "stmt-while", cond: $3, body: $6 }; }
| statement_else
{ $$ = $1; }
| RETURN SEMI
{ $$ = { type: "return" }; }
| RETURN expr SEMI
{ $$ = { type: "return", value: $2 }; }
;
primary_expr
: ID
{ $$ = { type: "id", value: yytext }; }
| NUMBER
{ {
let v = yytext;
if (v.substring(0, 2) === "0b") {
v = parseInt(v.substring(2), 2);
}
$$ = { type: "literal", value: parseInt(v) };
} }
| STRING
{ $$ = { type: "literal", value: yytext }; }
| BOOL
{ $$ = { type: "literal", value: (yytext === "true") }; }
| OPEN_PAREN expr CLOSE_PAREN
{ $$ = $2; }
;
unary_op
: NOT { $$ = $1; }
| MINUS { $$ = $1; }
;
unary_expr
: primary_expr
{ $$ = $1; }
| OPEN_TRIANGE TYPE CLOSE_TRIANGLE primary_expr
{ $$ = { type: "cast", cast: $2, expr: $4 }; }
| unary_op primary_expr
{ $$ = { type: "unary", op: $1, expr: $2 }; }
;
mul_expr
: unary_expr
{ $$ = $1; }
| mul_expr MULOP unary_expr
{ $$ = { type: "binary-expr", op: $2, left: $1, right: $3 }; }
;
add_op
: PLUS { $$ = $1; }
| MINUS { $$ = $1; }
;
add_expr
: mul_expr
{ $$ = $1; }
| add_expr add_op mul_expr
{ $$ = { type: "binary-expr", op: $2, left: $1, right: $3 }; }
;
shift_expr
: add_expr
{ $$ = $1; }
| shift_expr SHIFTOP add_expr
{ $$ = { type: "binary-expr", op: $2, left: $1, right: $3 }; }
;
rel_expr
: shift_expr
{ $$ = $1; }
| rel_expr RELOP shift_expr
{ $$ = { type: "binary-expr", op: $2, left: $1, right: $3 }; }
;
eq_expr
: rel_expr
{ $$ = $1; }
| eq_expr EQOP rel_expr
{ $$ = { type: "binary-expr", op: $2, left: $1, right: $3 }; }
;
and_expr
: eq_expr
{ $$ = $1; }
| and_expr AND eq_expr
{ $$ = { type: "binary-expr", op: $2, left: $1, right: $3 }; }
;
xor_expr
: and_expr
{ $$ = $1; }
| xor_expr XOR and_expr
{ $$ = { type: "binary-expr", op: $2, left: $1, right: $3 }; }
;
or_expr
: xor_expr
{ $$ = $1; }
| or_expr OR xor_expr
{ $$ = { type: "binary-expr", op: $2, left: $1, right: $3 }; }
;
logic_and_expr
: or_expr
{ $$ = $1; }
| logic_and_expr LOGICAND or_expr
{ $$ = { type: "binary-expr", op: $2, left: $1, right: $3 }; }
;
logic_or_expr
: logic_and_expr
{ $$ = $1; }
| logic_or_expr LOGICOR logic_and_expr
{ $$ = { type: "binary-expr", op: $2, left: $1, right: $3 }; }
;
expr
: logic_or_expr
;
%%
function loc(yy, start, end, statement) {
}