-
Notifications
You must be signed in to change notification settings - Fork 1
/
grammar.rustpeg
319 lines (263 loc) · 10.2 KB
/
grammar.rustpeg
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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use ast::*;
pub program -> Vec<StmtNode>
= __ terminators? s:statements { s }
pub statements -> Vec<StmtNode>
= statement_node*
statement_node -> StmtNode
= __ lpos:#position s:statement rpos:#position __ { StmtNode { pos: (lpos, rpos), data: s } }
statement -> Stmt
= s:if_statement { s }
/ l:loop_statement { l }
/ BREAK __ TERMINATOR { Stmt::Break }
/ CONTINUE __ TERMINATOR { Stmt::Continue }
/ RETURN __ e:expr_node? __ TERMINATOR { Stmt::Return(e) }
/ a:assignment_statement { a }
/ v:variable_declaration { v }
/ e:expr_node __ TERMINATOR { Stmt::Expr(e) }
/ f:function_definition_node { Stmt::Expr(f) }
/ b:block { b }
/ TERMINATOR { Stmt::Empty }
assignment_statement -> Stmt
= lpos:#position i:identifier rpos:#position __ EQUALS __ e:expr_node __ TERMINATOR {
Stmt::Assign(LhsExprNode { pos: (lpos, rpos), data: LhsExpr::Identifier(i) }, e)
}
/ lpos:#position i:identifier rpos:#position __ op:assign_with_op __ e:expr_node __ TERMINATOR {
Stmt::AssignOp(LhsExprNode { pos: (lpos, rpos), data: LhsExpr::Identifier(i) }, op, e)
}
assign_with_op -> BinOp
= OP_PLUS __ EQUALS { BinOp::Add }
/ OP_MINUS __ EQUALS { BinOp::Sub }
/ OP_ASTERISK __ EQUALS { BinOp::Mul }
/ OP_SLASH __ EQUALS { BinOp::Div }
/ OP_MOD __ EQUALS { BinOp::Mod }
variable_declaration -> Stmt
= b:binding_type __ i:identifier __ EQUALS __ e:expr_node __ TERMINATOR {
Stmt::VarDecl(
Variable::Identifier(b, i), e
)
}
binding_type -> BindingType
= VAR { BindingType::Mutable }
loop_statement -> Stmt
= LOOP __ lpos:#position b:block rpos:#position __ { Stmt::Loop(Box::new(StmtNode { pos: (lpos, rpos), data: b })) }
if_statement -> Stmt
= IF __ e:expr_node __ lpos1:#position b1:block rpos1:#position __ ELSE __ lpos2:#position b2:block rpos2:#position __ {
Stmt::IfThen(
IfThenStmt {
cond: e,
then_block: Box::new(StmtNode { pos: (lpos1, rpos1), data: b1 }),
maybe_else_block: Some(Box::new(StmtNode { pos: (lpos2, rpos2), data: b2 })),
}
)
}
/ IF __ e:expr_node __ lpos1:#position b1:block rpos1:#position __ ELSE __ lpos2:#position elseif:if_statement rpos2:#position __ {
Stmt::IfThen(
IfThenStmt {
cond: e,
then_block: Box::new(StmtNode { pos: (lpos1, rpos1), data: b1 }),
maybe_else_block: Some(Box::new(StmtNode { pos: (lpos2, rpos2), data: elseif })),
}
)
}
/ IF e:expr_node lpos:#position b:block rpos:#position {
Stmt::IfThen(
IfThenStmt {
cond: e,
then_block: Box::new(StmtNode { pos: (lpos, rpos), data: b }),
maybe_else_block: None
}
)
}
block -> Stmt
= #quiet<_block> / #expected("block")
_block -> Stmt
= OPENING_BRACE __ terminators? __ s:statements __ terminators? __ CLOSING_BRACE { Stmt::Block(s) }
function_definition_node -> ExprNode
= __ lpos:#position f:function_definition rpos:#position __ { ExprNode { pos: (lpos, rpos), data: f } }
function_definition -> Expr
= FN __ i:identifier? __ OPEN_PAREN __ params:param_list __ COMMA? __ CLOSE_PAREN __ lpos:#position body:block rpos:#position __ {
Expr::FnDef(
FnDefExpr {
maybe_id: i,
params: params,
body: Box::new(StmtNode { pos: (lpos, rpos), data: body } ),
}
)
}
param_list -> Vec<String>
= identifier_with_whitespace ** COMMA
identifier_with_whitespace -> String
= __ id:identifier __ { id }
expr_node -> ExprNode
= e:binary_expr_node { e }
/ s:single_expr_node { s }
comma_args -> Vec<ExprNode>
= expr_node ** COMMA
binary_expr_node -> ExprNode
= binary_expr
// __ lpos:#position e:binary_expr rpos:#position __ {
// // hackily check if e is a proper binary expr
// if e.pos == (0, 0) {
// ExprNode { pos: (lpos, rpos), data: e.data }
// } else {
// // e is actually a single_expr, so just propagate
// e
// }
// }
binary_expr -> ExprNode = #infix<single_expr_node> {
#L x AND y { ExprNode { pos: (x.pos.0, y.pos.1), data: Expr::BinaryLogical(Box::new(x), LogicalBinOp::And, Box::new(y)) } }
x OR y { ExprNode { pos : (x.pos.0, y.pos.1), data: Expr::BinaryLogical(Box::new(x), LogicalBinOp::Or, Box::new(y)) } }
#L x OP_STRICT_EQUALS y { ExprNode { pos : (x.pos.0, y.pos.1), data: Expr::Binary(Box::new(x), BinOp::Eq, Box::new(y)) } }
#L x OP_LESS_THAN y { ExprNode { pos : (x.pos.0, y.pos.1), data: Expr::Binary(Box::new(x), BinOp::Lt, Box::new(y)) } }
x OP_LESS_THAN_OR_EQUAL y { ExprNode { pos : (x.pos.0, y.pos.1), data: Expr::Binary(Box::new(x), BinOp::Lte, Box::new(y)) } }
x OP_GREATER_THAN y { ExprNode { pos : (x.pos.0, y.pos.1), data: Expr::Binary(Box::new(x), BinOp::Gt, Box::new(y)) } }
x OP_GREATER_THAN_OR_EQUAL y { ExprNode { pos : (x.pos.0, y.pos.1), data: Expr::Binary(Box::new(x), BinOp::Gte, Box::new(y)) } }
#L x OP_PLUS y { ExprNode { pos : (x.pos.0, y.pos.1), data: Expr::Binary(Box::new(x), BinOp::Add, Box::new(y)) } }
x OP_MINUS y { ExprNode { pos : (x.pos.0, y.pos.1), data: Expr::Binary(Box::new(x), BinOp::Sub, Box::new(y)) } }
#L x OP_ASTERISK y { ExprNode { pos : (x.pos.0, y.pos.1), data: Expr::Binary(Box::new(x), BinOp::Mul, Box::new(y)) } }
x OP_SLASH y { ExprNode { pos : (x.pos.0, y.pos.1), data: Expr::Binary(Box::new(x), BinOp::Div, Box::new(y)) } }
x OP_MOD y { ExprNode { pos : (x.pos.0, y.pos.1), data: Expr::Binary(Box::new(x), BinOp::Mod, Box::new(y)) } }
}
single_expr_node -> ExprNode
= __ OPEN_PAREN e:expr_node CLOSE_PAREN __ { e }
/ e:expr_with_suffix { e }
expr_with_suffix -> ExprNode
// TODO: This sort of messes up position annotation for the ExprNode
= __ lpos:#position e:simple_expr_node suffixes:expr_suffix* rpos:#position __ {
if suffixes.is_empty() {
e
} else {
let mut expr = e;
for suffix in suffixes {
match suffix {
ExprSuffix::InSquareBrackets(idx_expr) => {
expr = ExprNode {
pos: (lpos, rpos),
data: Expr::MemberByIdx(Box::new(expr), Box::new(idx_expr)),
}
}
ExprSuffix::ListInParens(args_list) => {
expr = ExprNode {
pos: (lpos, rpos),
data: Expr::FnCall(Box::new(expr), args_list),
}
}
}
}
expr
}
}
expr_suffix -> ExprSuffix
= member_access_suffix
/ function_call_suffix
member_access_suffix -> ExprSuffix
= OPEN_SQUARE_BRACKET idx:expr_node CLOSE_SQUARE_BRACKET { ExprSuffix::InSquareBrackets(idx) }
function_call_suffix -> ExprSuffix
= OPEN_PAREN __ args:comma_args __ COMMA? __ CLOSE_PAREN { ExprSuffix::ListInParens(args) }
_tuple -> Expr
= OPEN_PAREN __ args:comma_args __ COMMA? __ CLOSE_PAREN {
Expr::Tuple(args)
}
simple_expr_node -> ExprNode
= __ lpos:#position e:simple_expr rpos:#position __ { ExprNode { pos: (lpos, rpos), data: e } }
simple_expr -> Expr
= OP_MINUS __ e:single_expr_node { Expr::Unary(UnOp::Neg, Box::new(e)) }
/ NOT __ e:expr_node { Expr::UnaryLogical(LogicalUnOp::Not, Box::new(e)) }
/ t:tuple { t }
/ f:function_definition { f }
/ l:literal_node { Expr::Literal(l) }
/ i:identifier { Expr::Identifier(i) }
literal_node -> LiteralNode
= __ lpos:#position l:literal rpos:#position __ {
LiteralNode { pos: (lpos, rpos), data: l }
}
literal -> Literal
= f:float { Literal::Float(f) }
/ i:integer { Literal::Integer(i) }
/ b:boolean { Literal::Bool(b) }
/ s:doubleQuotedString { Literal::String(s) }
tuple -> Expr
= #quiet<_tuple> / #expected("tuple")
doubleQuotedString -> String
= #quiet<_doubleQuotedString> / #expected("string")
_doubleQuotedString -> String
= '"' s:$([^"]*) '"' { s.to_owned() }
integer -> i64
= #quiet<_integer> / #expected("number")
_integer -> i64
= n:$([+-]?[0-9]+) { n.parse().unwrap() }
float -> f64
= #quiet<_float> / #expected("number")
_float -> f64
= f:$([+-]?[0-9]+"."[0-9]+) { f.parse().unwrap() }
boolean -> bool
= #quiet<_boolean> / #expected("bool")
_boolean -> bool
= "true" { true }
/ "false" { false }
identifier -> String
= #quiet<_identifier> / #expected("identifier")
_identifier -> String
= !reserved_identifier i:$([a-zA-Z_][a-zA-Z0-9_]*[!?]?) { i.to_string() }
__ = #quiet<(whitespace / comment)*>
comment = "#" (!eol_char .)*
eol_char = [\n\r]
whitespace = [ \t\n\r]
EQUALS = #quiet<"="> / #expected("equals")
terminators = TERMINATOR+
TERMINATOR -> ()
= ";"
OP_PLUS = "+"
OP_MINUS = "-"
OP_ASTERISK = "*"
OP_SLASH = "/"
OP_MOD = "%"
OP_COLON = ":"
OP_LESS_THAN = "<"
OP_GREATER_THAN = ">"
OP_LESS_THAN_OR_EQUAL = "<="
OP_GREATER_THAN_OR_EQUAL = ">="
OP_STRICT_EQUALS = "=="
OPENING_BRACE = "{"
CLOSING_BRACE = "}"
OPEN_PAREN = "("
CLOSE_PAREN = ")"
OPEN_SQUARE_BRACKET = "["
CLOSE_SQUARE_BRACKET = "]"
COMMA = ","
COLON = ":"
reserved_identifier = VAR
/ IF
/ ELSE
/ AND
/ OR
/ NOT
/ "true"
/ "false"
/ LOOP
/ BREAK
/ CONTINUE
/ FN
/ RETURN;
keyword<E> = E
VAR = keyword<"var">
IF = keyword<"if">
ELSE = keyword<"else">
AND = keyword<"and">
OR = keyword<"or">
NOT = keyword<"not">
LOOP = keyword<"loop">
BREAK = keyword<"break">
CONTINUE = keyword<"continue">
FN = keyword<"fn">
RETURN = keyword<"return">
NUMBER = keyword<"Number">
BOOL = keyword<"Bool">
STRING = keyword<"String">
FUNCTION = keyword<"Function">
TUPLE = keyword<"Tuple">
ANY = keyword<"any">
VOID = keyword<"void">