-
Notifications
You must be signed in to change notification settings - Fork 1
/
grammar.ebnf
214 lines (146 loc) · 4.38 KB
/
grammar.ebnf
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
(*
the indentation level is only recorded for the first token on each line,
for example:
if true:
print "test"
would produce the token stream:
Token { kind: If, indent: Some(0), lexeme: "if" }
Token { kind: True, indent: None, lexeme: "true" }
Token { kind: Colon, indent: None, lexeme: ":" }
Token { kind: Print, indent: Some(2), lexeme: "print" }
Token { kind: String, indent: None, lexeme: "\"test\"" }
the parser maintains a stack of indentation levels,
and supports the following operations on that stack:
{+} -> expects indentation == Some(indent_stack[0] + N), and pushes N onto the indent_stack
{-} -> expects indentation == Some(indent_stack[1]), and pops the top of the indent_stack
{=} -> expects indentation == Some(indent_stack[0])
{0} -> expects indentation == Some(0)
{_} -> expects indentation == None
in certain contexts (such as a grouping expression, or call args), the indentation
is ignored, meaning that any indentation checks always pass, and the indentation levels
don't change.
*)
module = ({0} top_level_stmt)* ;
top_level_stmt = {=} stmt ;
stmt = scoped_stmt | simple_stmt ;
simple_stmt =
| pass_stmt
| return_stmt
| continue_stmt
| break_stmt
| yield_stmt
| print_stmt
| assign_stmt
;
scoped_stmt =
| import_stmt
| if_stmt
| for_stmt
| while_stmt
| loop_stmt
| fn_stmt
| class_stmt
;
pass_stmt = "pass" ;
return_stmt = "return" ({_} expr)? ;
continue_stmt = "continue" ;
break_stmt = "break" ;
yield_stmt = "yield" ({_} expr)? ;
print_stmt = "print" {_} expr ({_} "," {_} expr)? ;
assign_stmt = assign_target {_} assign_op {_} expr ;
assign_target =
| var_expr
| field_expr
| index_expr
;
assign_op =
| ":="
| "="
| "+="
| "-="
| "*="
| "/="
| "%="
| "**="
| "??="
;
import_stmt =
| "import" {_} import_path ({_} "as" identifier)?
| "from" {_} import_path {_} "import" {_} import_symbol_list
;
import_path = identifier ({_} "." {_} identifier)* ;
import_symbol_list = indentifier ({_} "," {_} identifier)* ;
if_stmt =
"if" {_} expr {_} ":" block
({=} "elif" {_} expr {_} ":" block)*
({=} "else" ":" block)?
;
for_stmt = "for" {_} identifier {_} "in" {_} for_iter {_} ":" block ;
for_iter =
| expr {_} ".." {_} expr (* range *)
| expr (* iterable *)
;
while_stmt = "while" {_} expr {_} ":" block ;
loop_stmt = "loop" {_} ":" block ;
fn_stmt = "fn" {_} identifier {_} "(" (param ("," param)*)? ")" {_} ":" block ;
param = identifier ({_} "=" {_} expr)? ;
class_stmt = "class" {_} identifier ({_} "(" identifier ")") {_} ":" class_members ;
class_members =
| {_} pass_stmt
| {+} class_member_list {-}
;
class_member_list =
(class_field ({=} class_field)*)?
(class_method ({=} class_method)*)?
;
class_field = identifier "=" expr ;
class_method = fn_stmt ;
block =
| {_} simple_stmt
| {+} stmt ({=} stmt)*
;
expr = maybe_expr ;
maybe_expr = or_expr ({_} "??" {_} or_expr)* ;
or_expr = and_expr ({_} "||" {_} and_expr)* ;
and_expr = eq_expr ({_} "&&" {_} eq_expr)* ;
eq_expr = comp_expr ({_} ("==" | "!=") {_} comp_expr)* ;
comp_expr = add_expr ({_} ("<" | "<=" | ">" | ">=") {_} add_expr)* ;
add_expr = mul_expr ({_} ("+" | "-") {_} mul_expr)* ;
mul_expr = pow_expr ({_} ("*" | "/" | "%") {_} pow_expr)* ;
pow_expr = unary_expr ({_} "**" {_} unary_expr)* ;
unary_expr = ("-" | "+" | "!" | "?") {_} (unary_expr | postfix_expr) ;
postfix_expr = call_expr | index_expr | field_expr | primary_expr ;
call_expr = postfix_expr {_} "(" (expr ("," expr)*)? ")" ;
index_expr = postfix_expr {_} "[" expr "]" ;
field_expr = postfix_expr {_} "." {_} identifier ;
primary_expr =
| none_expr
| bool_expr
| int_expr
| float_expr
| string_expr
| list_expr
| table_expr
| self_expr
| super_expr
| var_expr
| group_expr
;
none_expr = "none" ;
bool_expr = "true" | "false" ;
(* NOTE: `int_expr` takes precedence over `float_expr` *)
int_expr = (* regex *) "[0-9]([0-9_]*[0-9])?" ;
float_expr = (* regex *) "[0-9]+(\.[0-9]+)?([Ee][+-]?[0-9]+)?" ;
string_expr = "\"" (* regex *) "([^\"\\]|\\.)*" "\"" ;
list_expr = "[" (expr ("," expr)*)? "]" ;
table_expr = "{" (table_field ("," table_field)*)? "}" ;
table_field = table_key ":" expr ;
table_key =
| identifier
| "[" expr "]"
;
self_expr = "self" ;
super_expr = "super" ;
var_expr = identifier ;
group_expr = "(" expr ")" ;
identifier = (* regex *) "[a-zA-Z_][a-zA-Z0-9_]*" ;