-
Notifications
You must be signed in to change notification settings - Fork 1
/
grammar.kpeg
436 lines (334 loc) · 13.4 KB
/
grammar.kpeg
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
%% {
def initialize(*args)
super
@precedence = {
"*" => 8,
"/" => 8,
"+" => 7,
"-" => 7,
"==" => 10,
"=" => 0,
"&&" => 3,
"||" => 2
}
@assoc = {
"=" => :right,
"<<" => :left
}
end
def ast
@ast || @result
end
def set_assoc(operator, assoc)
@assoc[operator] = assoc
end
def prec(op)
if level = @precedence[op]
return level
end
return 5
end
def assoc(op)
@assoc[op]
end
# courtesy of alexsuraci (@vito on github and twitter)
#
# when resolving `1 + 2 * 3', the call path is:
#
# resolve(nil, 1, [+, 2, *, 3])
# | resolve(+, 2, [*, 3])
# | | resolve(*, 3, [])
# | | `-> [3, []]
# | `-> resolve(+, binary(2, 3, *), [])
# | `-> [(2 * 3), []]
# `-> resolve(nil, binary(1, (2 * 3), +), [])
# `-> [(1 + (2 * 3)), [])
#
# where `binary(y, z, +)' constructs `y + z', with
# `z' being the expression from the nested resolve
#
def resolve(a, e, chain)
# if the chain is empty, we've got a full expression
# this is guaranteed to occur because we start with `a' as `nil',
# which means it'll keep resolving until the chain is consumed.
return [e, []] if chain.empty?
# pull the next operator out of the chain
b, *rest = chain
# if `a' is non-nil, and `a' and `b' associate to the left,
# stop, returning the expression and the rest of the chain,
# which will get picked up again when building up `a'
if a && (prec(a) > prec(b) || (prec(a) == prec(b) && assoc(a) == :left))
[e, chain]
else
# pull the next expression out to resolve the next operator
e2, *rest2 = rest
# resolve from the next operator onward,
# capturing the unconsumed bits as rest3
r, rest3 = resolve(b, e2, rest2)
# continue with the remaining chain (rest3)
# note that the original arg is passed here,
# with the next one already done
resolve(a, binary(e, r, b), rest3)
end
end
}
%% ast-location = ::Talon::AST
%% comment = ast Comment($text)
%% bracket = ast BracketOperator(receiver, arguments)
%% number = ast Number($value)
%% strlit = ast String($value)
%% bool_true = ast True()
%% bool_false = ast False()
%% ident = ast Identifier($name)
%% typed_ident = ast TypedIdentifier($name, type)
%% lambda_type = ast LambdaType(*arg_types, ret_type)
%% templated_name = ast TemplatedName($name, arguments)
%% method_def = ast MethodDefinition($name, *arguments, body, return_type)
%% method_dec = ast MethodDeclaration($name, *arguments, return_type, attribute)
%% class_def = ast ClassDefinition(name, superclass_name, body)
%% trait_def = ast TraitDefinition(name, body)
%% seq = ast Sequence(*elements)
%% if_node = ast If(condition, then_body, else_body)
%% while_node = ast While(condition, body)
%% case_node = ast Case(condition, whens)
%% when_node = ast When(var, body)
%% call = ast MethodCall(receiver, $method_name, *arguments)
%% unary = ast UnaryOperator(receiver, operator)
%% binary = ast BinaryOperator(receiver, argument, operator)
%% group = ast Grouped(expression)
%% attr = ast Attribute($name, $values)
%% var = ast VariableCreation(identifier, type_decl, expression)
%% var_decl = ast VariableDeclaration(identifier, type_decl)
%% ivar_decl = ast IVarDeclaration(identifier, type_decl)
%% named_type = ast NamedType($identifier)
%% pointer_type = ast PointerType(inner)
%% array_type = ast ArrayType(inner)
%% scoped_type = ast ScopedType(parent, child)
%% templated_type = ast TemplatedType(base, arguments)
%% ivar = ast InstanceVariable($name)
%% assign = ast Assignment(variable, value)
%% ret = ast Return(value)
%% inc = ast IncludeTrait($name)
%% templated_instance = ast TemplatedInstance(name, type, *arguments)
%% import = ast Import(*segements)
%% package = ast Package(*segments)
%% varargs = ast VariableArguments()
%% lambda = ast Lambda(args, captures, body)
%% lambda_stmts = ast LambdaStatements(body)
%% named_arg = ast NamedArgument(name, expr)
%% data = ast Data(name, cases)
%% data_case = ast DataCase(name, *args)
eof = !.
space = " " | "\t"
nl = "\n"
sp = space+
- = space*
comment = "--" < (!nl .)* > nl ~comment(text)
br-sp = (space | nl)*
expr_end = - (comment | ("\n" | ";")) br-sp
number = < ("0" | /[1-9][0-9]*/) > { text }
integer = number:n ~number(n.to_i)
float = number:w "." number:f ~number("#{w}.#{f}".to_f)
true = "true" ~bool_true()
false = "false" ~bool_false()
keyword = "if" | "end" | "else" | "true" | "false" | "when"
word = !keyword < /[a-zA-Z_][a-zA-Z0-9_]*/ > { text }
num_escapes = < /[0-7]{1,3}/ > { [text.to_i(8)].pack("U") }
| "x" < /[a-f\d]{2}/i > { [text.to_i(16)].pack("U") }
string_escapes = "n" { "\n" }
| "s" { " " }
| "r" { "\r" }
| "t" { "\t" }
| "v" { "\v" }
| "f" { "\f" }
| "b" { "\b" }
| "a" { "\a" }
| "e" { "\e" }
| "\\" { "\\" }
| "\"" { "\"" }
| num_escapes
| < . > { text }
string_seq = < /[^\\"]+/ > { text }
string_body = ("\\" string_escapes:s | string_seq:s)*:ary { Array(ary) }
string = "\"" string_body:s "\"" ~strlit(s.join)
identifier = word:w ~ident(w)
attribute_val = "\"" < /[^"]+/ > "\"" { text }
| number:n { n.to_i }
attribute_kv = word:n br-sp "=" br-sp attribute_val:v { { n => v } }
attribute_list = attribute_kv:i br-sp "," br-sp attribute_list:l { i.merge(l) }
| attribute_kv
attribute = "%" word:n "(" attribute_list:l ")" br-sp
~attr(n,l)
template_arg = true | false | integer | type_id
template_args = template_arg:a br-sp "," br-sp template_args:t { [a] + t }
| template_arg:a { [a] }
type_mod = type_mod:t "[]" ~array_type(t)
| type_mod:t "*" ~pointer_type(t)
| type_mod:b "<" template_args:a ">" ~templated_type(b, a)
| "(" - type_list:a - "=>" - type_id:r ")" ~lambda_type(a,r)
| word:w ~named_type(w)
type_id = type_mod:w "::" type_id:t ~scoped_type(w, t)
| type_mod
template_instance = word "<" word ">"
type_array = word "[]"
if = "if" sp expr:c expr_end sequence:t expr_end "end"
~if_node(c,t,nil)
| "if" sp expr:c expr_end sequence:t expr_end
"else" expr_end sequence:f expr_end "end"
~if_node(c,t,f)
while = "while" sp expr:c expr_end sequence:t expr_end "end"
~while_node(c,t)
case = "case" sp expr:c expr_end whens:t expr_end "end"
~case_node(c,t)
typed_ident = word:i br-sp ":" br-sp type_id:t ~typed_ident(i,t)
when = "when" br-sp typed_ident:i expr_end sequence:b
~when_node(i,b)
whens = - comment:e whens:m { [e] + m }
| - when:e expr_end whens:m { [e] + m }
| - when:e { [e] }
maybe_ti = typed_ident | identifier
args_list = maybe_ti:i br-sp "," br-sp args_list:l { [i] + l }
| maybe_ti:i { [i] }
type_list = type_id:i br-sp "," br-sp type_list:l { [i] + l }
| type_id:i { [i] }
id_list = identifier:a br-sp "," br-sp id_list:l { [a] + l }
| identifier:a { [a] }
typed_id_list = typed_ident:a br-sp "," br-sp typed_id_list:l { [a] + l }
| typed_ident:a { [a] }
templated_name = word:w "<" id_list:e ">" ~templated_name(w,e)
| word:w
def_start = "def" sp templated_name:w { w }
def_args = "(" br-sp ")" { nil }
| "(" br-sp "..." br-sp ")" { [varargs] }
| "(" br-sp args_list:l br-sp ")" { l }
| "(" br-sp args_list:l br-sp "," br-sp "..." br-sp ")"
{ l + [varargs] }
ret_type = - ":" - type_id:t { t }
def = def_start:w def_args?:a ret_type?:t expr_end "end"
~method_def(w,a,nil,t)
| def_start:w def_args?:a ret_type?:t expr_end sequence:s expr_end "end"
~method_def(w,a,s,t)
dec = attribute?:ab "dec" sp templated_name:w def_args?:a ret_type:t
~method_dec(w,a,t,ab)
var = "var" sp word:i sp ":" sp type_id:t sp "=" sp expr:e ~var(i,t,e)
| "var" sp word:i sp "=" sp expr:e ~var(i,nil,e)
| "var" sp word:i sp ":" sp type_id:t ~var_decl(i,t)
| word:i sp ":=" sp expr:e ~var(i,nil,e)
ivar_decl = "var" sp "@" word:w sp ":" sp type_id:t ~ivar_decl(w,t)
level2 = dec | def | class | trait | inc | ivar_decl
many_l2 = - comment:e level2:m { [e] + m }
| - level2:e expr_end many_l2:m { [e] + m }
| - level2:e { [e] }
l2_seq = many_l2:e { e.size > 1 ? seq(e) : e.first }
class_start = "class" sp templated_name:w { w }
class_superclass = - ":" - templated_name:s { s }
class = class_start:w class_superclass?:s expr_end "end"
~class_def(w,s,nil)
| class_start:w class_superclass?:s expr_end l2_seq:e expr_end "end"
~class_def(w,s,e)
trait = "trait" sp templated_name:name expr_end "end"
~trait_def(name,nil)
| "trait" sp templated_name:name expr_end l2_seq:e expr_end "end"
~trait_def(name,e)
data = "data" sp templated_name:n - "=" br-sp data_cases:c ~data(n,c)
data_case = word:w "(" typed_id_list:ti ")" ~data_case(w, ti)
| word:w ~data_case(w, nil)
data_cases = data_case:w - "|" br-sp data_cases:c { [w] + c }
| data_case:w { [w] }
inc = "include" sp word:name ~inc(name)
import = "import" sp (word:w ".")*:left word:right ~import(left + [right])
package = "package" sp (word:w ".")*:left word:right ~package(left + [right])
operator_chars = /[~`!#$\%^\&*\\+\-\/?<=>\|]/
unary_operator = < operator_chars > { text }
binary_operator = !"--" < operator_chars+ > { text }
ivar = "@" word:w ~ivar(w)
literal = float
| integer
| true
| false
| string
| !template_instance !type_array identifier
| ivar
| type_id
embed_rhs = literal:r "." word:w ~call(r,w)
| literal
call_np_list = method_np_arg:e - "," br-sp call_np_list:l { [e] + l }
| method_np_arg:e { [e] }
named_arg_np = word:w ":" br-sp method_np_arg:e ~named_arg(w,e)
| method_np_arg
named_arg_list_np = named_arg_np:e br-sp "," br-sp named_arg_list_np:l { [e] + l }
| named_arg_np:e { [e] }
call_args_np = named_arg_list_np | call_np_list
method_call_np = method_np_recv:r "." word:w sp
call_args_np:l ~call(r,w,l)
func_call_np = word:w sp call_args_np:l ~call(nil,w,l)
expr_list = expr:e br-sp "," br-sp expr_list_b:l { [e] + l }
| expr:e { [e] }
named_arg = word:w ":" br-sp expr:e ~named_arg(w,e)
| expr
call_args = named_arg:e br-sp "," br-sp call_args:l { [e] + l }
| named_arg:e { [e] }
method_np_recv = method_np_recv:r "." word:w
"(" br-sp call_args?:l br-sp ")" ~call(r,w,l)
| method_np_recv:r "[" expr_list:l "]" ~bracket(r,l)
| literal
method_np_arg =
lambda
| grouped
| binary_send
| method_np_arg:r "." word:w
"(" br-sp call_args?:l br-sp ")" ~call(r,w,l)
| method_np_arg:r "." word:w ~call(r,w,nil)
| method_np_arg:r "[" expr_list:l "]" ~bracket(r,l)
| word:w "(" call_args?:l ")" ~call(nil,w,l)
| literal
method_call = method_call:r "." word:w
"(" br-sp call_args?:l br-sp ")" ~call(r,w,l)
| method_call:r "." word:w ~call(r,w,nil)
| method_call:r "[" expr_list:l "]" ~bracket(r,l)
| word:w "(" br-sp call_args?:l br-sp ")" ~call(nil,w,l)
| word:w "<" word:t ">" "(" br-sp call_args?:l br-sp ")"
~templated_instance(w,t,l)
| literal
bin_expr = bin_expr:r "." word:w "(" br-sp call_args?:l br-sp ")" ~call(r,w,l)
| bin_expr:r "." word:w ~call(r,w,nil)
| word:w "(" br-sp call_args?:l br-sp ")" ~call(nil,w,l)
| literal
binary_c = (- binary_operator:o br-sp bin_expr:e { [o, e] })+:bs { bs.flatten }
binary_send = bin_expr:l binary_c:c { resolve(nil, l, c).first }
assignable = assignable:r "[" expr_list:l "]" ~bracket(r,l)
| assignable:r "." word:w ~call(r,w,nil)
| identifier
| ivar
assign = assignable:i - "=" br-sp call:e ~assign(i, e)
ret = "return" br-sp expr:e ~ret(e)
capture_list = "*" word:w - "," - capture_list:l { [w] + l }
| "*" word:w { [w] }
lambda_body = expr | "{" br-sp many_stmts:e br-sp "}" ~lambda_stmts(e)
lambda = "(" - typed_id_list:a - ","
- capture_list:c - ")" - "->" - lambda_body:e ~lambda(a, c, e)
| "(" - typed_id_list:a - ")" - "->" - lambda_body:e ~lambda(a, nil, e)
| "(" - id_list:a - ")" - "->" - lambda_body:e ~lambda(a, nil, e)
| "(" - capture_list:c - ")" - "->" - lambda_body:e ~lambda(nil, c, e)
| identifier:i - "->" - lambda_body:e ~lambda([i], nil, e)
| "()" - "->" - lambda_body:e ~lambda(nil, nil, e)
call = unary_operator:o expr:r ~unary(r,o)
| !template_instance binary_send
| func_call_np
| method_call_np
| method_call
many_stmts = - comment:e many_stmts:m { [e] + m }
| - stmt:e expr_end many_stmts:m { [e] + m }
| - stmt:e { [e] }
sequence = many_stmts:e
{ e.size > 1 ? seq(e) : e.first }
stmt = if | while | case | var | assign | ret | expr
expr = lambda | call
grouped = "(" expr:e ")" ~group(e)
toplevel = dec | def | class | trait | data | import | package | inc
many_top = - comment:e many_top:m { [e] + m }
| - toplevel:e expr_end many_top:m { [e] + m }
| - toplevel:e { [e] }
top_seq = many_top:e
{ e.size > 1 ? seq(e) : e.first }
root = expr_end* top_seq:e br-sp eof { @ast = e }