-
Notifications
You must be signed in to change notification settings - Fork 0
/
grammar.rkt
59 lines (35 loc) · 1.55 KB
/
grammar.rkt
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
#lang brag
;; this file defines the Lua grammar as a brag grammar, for use as reference once it can parse all of Lua, which we will then build on to add our own desired additions to the language!
;; lua grammar
chunk: (stat [SEMICOLON])* [laststat [SEMICOLON]]
block: chunk
stat: varlist EQ explist
| functioncall
| DO block END
| WHILE exp DO block END
| REPEAT block UNTIL exp
| IF exp THEN block (ELSEIF exp THEN block)* [ELSE block] END
| FOR VAR EQ exp COMMA exp [COMMA exp] DO block END
| FOR namelist IN explist DO block END
| FUNCTION funcname funcbody
| LOCAL FUNCTION VAR funcbody
| LOCAL namelist [EQ explist]
laststat: RETURN [explist] | BREAK
funcname: VAR (DOT VAR)* [COLON VAR]
varlist: var (COMMA var)*
var: VAR | prefixexp LSQUARE exp RSQUARE | prefixexp PERIOD VAR
namelist: VAR (COMMA VAR)*
explist: (exp COMMA)* exp
exp: NIL | FALSE | TRUE | NUM | STR | VARIADIC | function | prefixexp | tableconstructor | exp binop exp | unop exp
prefixexp: var | functioncall | LPAREN exp RPAREN
functioncall: prefixexp args | prefixexp COLON VAR args
args: LPAREN [explist] RPAREN | tableconstructor | STR
function: FUNCTION funcbody
funcbody: LPAREN [parlist] RPAREN block END
parlist: namelist [COMMA VARIADIC] | VARIADIC
tableconstructor: LBRACKET [fieldlist] RBRACKET
fieldlist: field (fieldsep field)* [fieldsep]
field: LSQUARE exp RSQUARE EQ exp | VAR EQ exp | exp
fieldsep: COMMA | SEMICOLON
binop: ADD | SUB | MUL | DIV | POW | MOD | CONCAT | LT | LTEQ | GT | GTEQ | EQEQ | NEQ | AND | OR
unop: SUB | NOT | LEN