-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathagate.y
447 lines (374 loc) · 7.65 KB
/
agate.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
// SPDX-License-Identifier: MIT
// Copyright (c) 2022 Julien Bernard
%{
#include <stdio.h>
#include <stdlib.h>
#include "agate-lexer.h"
extern int line;
static int yyerror(const char *message) {
printf("line %d: %s\n", line, message);
return 0;
}
%}
%token EOM 0 "end of module file"
%token EOL "end of line"
%token ERROR "error"
%token CHAR_LITERAL "char literal"
%token INT_LITERAL "integer literal"
%token FLOAT_LITERAL "float literal"
%token STRING_LITERAL "string literal"
%token INTERPOLATION_LITERAL "interpolation literal"
%token IDENTIFIER "identifier"
%token FIELD_INSTANCE "instance field"
%token FIELD_CLASS "class field"
/* keywords */
%token KW_AS "as"
%token KW_ASSERT "assert"
%token KW_BREAK "break"
%token KW_CLASS "class"
%token KW_CONSTRUCT "construct"
%token KW_CONTINUE "continue"
%token KW_DEF "def"
%token KW_ELSE "else"
%token KW_FALSE "false"
%token KW_FOR "for"
%token KW_FOREIGN "foreign"
%token KW_IF "if"
%token KW_IMPORT "import"
%token KW_IN "in"
%token KW_IS "is"
%token KW_LOOP "loop"
%token KW_MIXIN "mixin"
%token KW_NIL "nil"
%token KW_ONCE "once"
%token KW_RETURN "return"
%token KW_STATIC "static"
%token KW_SUPER "super"
%token KW_THIS "this"
%token KW_TRUE "true"
%token KW_WHILE "while"
/* operators */
%token OP_EQ "=="
%token OP_EXCL_RANGE "..."
%token OP_GEQ ">="
%token OP_INCL_RANGE ".."
%token OP_LAND "&&"
%token OP_LEQ "<="
%token OP_LOGICAL_RSHIFT ">>>"
%token OP_LOR "||"
%token OP_LSHIFT "<<"
%token OP_NE "!="
%token OP_RSHIFT ">>"
%start unit
%define parse.error verbose
%debug
%defines
%verbose
%%
unit:
maybe_eol declarations
;
declarations:
/* empty */
| declarations decl eol
;
decl:
import
| decl_function
| decl_variable
| decl_class
| statement
;
import:
KW_IMPORT STRING_LITERAL
| KW_IMPORT STRING_LITERAL KW_FOR identifiers
;
identifiers:
id
| identifiers ',' id
;
id:
IDENTIFIER
| IDENTIFIER KW_AS IDENTIFIER
;
block:
'{' eol declarations '}'
| '{' expr '}'
| '{' '}'
;
decl_function:
KW_DEF IDENTIFIER '(' parameters ')' block
;
parameters:
/* empty */
| IDENTIFIER
| parameters ',' IDENTIFIER
;
subscript_parameters:
IDENTIFIER
| parameters ',' IDENTIFIER
;
decl_variable:
KW_DEF IDENTIFIER
| KW_DEF IDENTIFIER '=' expr
;
decl_class:
KW_CLASS IDENTIFIER class_body
| KW_FOREIGN KW_CLASS IDENTIFIER class_body
| KW_CLASS IDENTIFIER KW_IS expr_primary class_body
| KW_FOREIGN KW_CLASS IDENTIFIER KW_IS expr_primary class_body
;
class_body:
'{' eol methods '}'
| '{' '}'
;
methods:
/* empty */
| methods method eol
;
method:
KW_CONSTRUCT IDENTIFIER '(' parameters ')' method_body
| decorator method_signature method_body
| decorator KW_MIXIN IDENTIFIER
;
method_body:
block
| KW_FOREIGN
;
method_signature:
IDENTIFIER '(' parameters ')'
| IDENTIFIER
| IDENTIFIER '=' '(' IDENTIFIER ')'
| binary_operator '(' IDENTIFIER ')'
| unary_operator
| '[' subscript_parameters ']'
| '[' subscript_parameters ']' '=' '(' IDENTIFIER ')'
| '(' parameters ')'
| '?' '(' IDENTIFIER ',' IDENTIFIER ')'
;
decorator:
/* empty */
| KW_STATIC
;
binary_operator:
'&'
| '/'
| OP_EQ
| OP_EXCL_RANGE
| '>'
| OP_GEQ
| OP_INCL_RANGE
| KW_IS
| '<'
| OP_LEQ
| '-'
| '%'
| OP_NE
| '|'
| '+'
| OP_LSHIFT
| OP_RSHIFT
| OP_LOGICAL_RSHIFT
| '*'
| '^'
;
unary_operator:
'+'
| '-'
| '!'
| '~'
;
statement:
stmt_once
| stmt_loop
| stmt_assert
| stmt_if
| stmt_for
| stmt_while
| KW_BREAK
| KW_CONTINUE
| stmt_return
| expr
;
stmt_once:
KW_ONCE block
;
stmt_loop:
KW_LOOP block
;
stmt_assert:
KW_ASSERT '(' expr ',' expr ')'
;
stmt_if:
KW_IF '(' expr ')' block
| KW_IF '(' expr ')' block KW_ELSE block
| KW_IF '(' expr ')' block KW_ELSE stmt_if
;
stmt_for:
KW_FOR '(' IDENTIFIER KW_IN expr ')' block
;
stmt_while:
KW_WHILE '(' expr ')' block
;
stmt_return:
KW_RETURN
| KW_RETURN expr
;
expr:
expr_assignment
;
expr_assignment:
expr_postfix '=' expr
| expr_conditional
;
expr_conditional:
expr_logical_or '?' expr ':' expr_conditional
| expr_logical_or
;
expr_logical_or:
expr_logical_or OP_LOR expr_logical_and
| expr_logical_and
;
expr_logical_and:
expr_logical_and OP_LAND expr_equality
| expr_equality
;
expr_equality:
expr_equality OP_EQ expr_is
| expr_equality OP_NE expr_is
| expr_is
;
expr_is:
expr_is KW_IS expr_comparison
| expr_comparison
;
expr_comparison:
expr_comparison '<' expr_bitwise_or
| expr_comparison '>' expr_bitwise_or
| expr_comparison OP_LEQ expr_bitwise_or
| expr_comparison OP_GEQ expr_bitwise_or
| expr_bitwise_or
;
expr_bitwise_or:
expr_bitwise_or '|' expr_bitwise_xor
| expr_bitwise_xor
;
expr_bitwise_xor:
expr_bitwise_xor '^' expr_bitwise_and
| expr_bitwise_and
;
expr_bitwise_and:
expr_bitwise_and '&' expr_shift
| expr_shift
;
expr_shift:
expr_shift OP_LSHIFT expr_range
| expr_shift OP_RSHIFT expr_range
| expr_shift OP_LOGICAL_RSHIFT expr_range
| expr_range
;
expr_range:
expr_range OP_INCL_RANGE expr_addition
| expr_range OP_EXCL_RANGE expr_addition
| expr_addition
;
expr_addition:
expr_addition '+' expr_multiplication
| expr_addition '-' expr_multiplication
| expr_multiplication
;
expr_multiplication:
expr_multiplication '*' expr_unary
| expr_multiplication '/' expr_unary
| expr_multiplication '%' expr_unary
| expr_unary
;
expr_unary:
expr_postfix
| '+' expr_unary
| '-' expr_unary
| '!' expr_unary
| '~' expr_unary
;
expr_postfix:
expr_primary
| expr_postfix '[' arguments ']'
| expr_postfix call_arguments
| expr_postfix '.' IDENTIFIER
| expr_postfix '.' IDENTIFIER lambda
;
call_arguments:
'(' ')'
| '(' ')' lambda
| '(' arguments ')'
| '(' arguments ')' lambda
;
lambda:
'{' eol declarations '}'
| '{' expr '}'
| '{' '|' lambda_parameters '|' eol declarations '}'
| '{' '|' lambda_parameters '|' expr '}'
| '{' '}'
;
lambda_parameters:
IDENTIFIER
| lambda_parameters ',' IDENTIFIER
;
arguments:
expr
| arguments ',' expr
;
expr_primary:
IDENTIFIER
| FIELD_INSTANCE
| FIELD_CLASS
| '.' IDENTIFIER
| KW_NIL
| KW_TRUE
| KW_FALSE
| KW_THIS
| KW_SUPER
| CHAR_LITERAL
| INT_LITERAL
| FLOAT_LITERAL
| string
| '(' expr ')'
| compound
;
string:
STRING_LITERAL
| INTERPOLATION_LITERAL expr string
;
compound:
'[' maybe_eol ']'
| '[' elements maybe_eol ']'
| '[' elements ',' maybe_eol ']'
| '{' maybe_eol '}'
| '{' members maybe_eol '}'
| '{' members ',' maybe_eol '}'
| '(' components ')'
;
elements:
maybe_eol expr
| elements ',' maybe_eol expr
;
members:
maybe_eol member
| members ',' maybe_eol member
;
member:
expr_unary ':' maybe_eol expr
;
components:
expr ',' expr
| components ',' expr
;
eol:
EOL
| eol EOL
;
maybe_eol:
/* empty */
| eol
;
%%