forked from richardhundt/shine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.lua
426 lines (334 loc) · 9.59 KB
/
parser.lua
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
local util = require("util")
local re = require('re')
local defs = require('parser.defs')
local lpeg = require('lpeg')
lpeg.setmaxstack(1024)
local patt = [[
chunk <- {|
s (<main_stmt> (<sep> s <main_stmt>)* <sep>?)? s (!. / '' => error)
|} -> chunk
close <- ']' =eq ']' / . close
lcomment <- (!%nl %s)* "--" (!%nl .)* %nl
bcomment <- ('--[' {:eq: '='* :} '[' <close>)
comment <- <bcomment> / <lcomment>
idsafe <- !(%alnum / "_")
s <- (<comment> / %s)*
S <- (<comment> / %s)+
hs <- (!%nl %s)*
HS <- (!%nl %s)+
digits <- %digit (%digit / (&('_' %digit) '_') %digit)*
word <- (%alpha / "_") (%alnum / "_")*
keyword <- (
"local" / "function" / "class" / "module" / "static"
/ "nil" / "true" / "false" / "return" / "end"
/ "yield" / "break" / "continue" / "not" / "throw"
/ "while" / "do" / "for" / "in" / "of" / "and" / "or"
/ "super" / "import" / "export" / "try" / "catch" / "finally"
/ "if" / "elseif" / "else" / "then" / "is" / "typeof"
/ "repeat" / "until"
) <idsafe>
sep <- <bcomment>? (%nl / ";" / &"}" / <lcomment>) / %s <sep>?
astring <- "'" { (!"'" .)* } "'"
qstring <- '"' { (!'"' .)* } '"'
lstring <- ('[' {:eq: '='* :} '[' <close>)
special <- "\n" "\$" / "\\" / "\" .
rstring <- {|
'`' (
<raw_expr> / { (<special> / !(<raw_expr> / '`') .)+ }
)* '`'
|} -> rawString
raw_expr <- (
"${" s <expr> s "}"
) -> rawExpr
string <- (
<qstring> / <astring> / <lstring>
) -> string
hexnum <- "-"? "0x" %xdigit+
decexp <- ("e"/"E") "-"? <digits>
decimal <- "-"? <digits> ("." <digits> <decexp>? / <decexp>)
integer <- "-"? <digits>
octal <- {~ { "-"? "0" [0-7]+ } -> octal ~}
number <- {~
<hexnum> / <octal> / <decimal> / <integer>
~} -> tonumber
boolean <- (
{"true"/"false"} <idsafe>
) -> boolean
literal <- ( <number> / <string> / <boolean> ) -> literal
main_stmt <- (
<module_decl>
/ <import_stmt>
/ <export_decl>
/ <stmt>
)
in <- "in" <idsafe>
end <- "end" <idsafe>
do <- "do" <idsafe>
module_decl <- (
"module" <idsafe> s <ident> s
{| (<main_stmt> (<sep> s <main_stmt>)*)? |} s
<end>
) -> moduleDecl
export_decl <- (
"export" <idsafe> s (<decl_stmt> / <module_decl>)
) -> exportDecl
import_stmt <- (
"import" <idsafe> s {| {"*"} / "{" s <ident> (s "," s <ident>)* s "}" |} s
"from" <idsafe> s <string>
) -> importStmt
stmt <- ({} (
<if_stmt>
/ <while_stmt>
/ <repeat_stmt>
/ <for_stmt>
/ <for_in_stmt>
/ <do_stmt>
/ <expr_stmt>
/ <decl_stmt>
/ <return_stmt>
/ <try_stmt>
/ <throw_stmt>
/ <break_stmt>
/ <continue_stmt>
/ <yield_stmt>
)) -> stmt
stmt_list <- {|
(<stmt> (<sep> s <stmt>)* <sep>?)?
|}
break_stmt <- (
"break" <idsafe>
) -> breakStmt
continue_stmt <- (
"continue" <idsafe>
) -> continueStmt
yield_stmt <- (
"yield" <idsafe> s {| <expr_list>? |}
) -> yieldStmt
return_stmt <- (
"return" <idsafe> s {| <expr_list>? |}
) -> returnStmt
throw_stmt <- (
"throw" <idsafe> s <expr>
) -> throwStmt
try_stmt <- (
"try" <idsafe> s <block_stmt>
{| <catch_clause>* |} (s "finally" <idsafe> s <block_stmt>)?
s <end>
) -> tryStmt
catch_clause <- (
s "catch" <idsafe> s "(" s
<ident> (s "if" <idsafe> s <expr>)? s ")" s <block_stmt> s
) -> catchClause
decl_stmt <- (
<local_decl> / <coro_decl> / <func_decl> / <class_decl>
)
local_decl <- (
"local" <idsafe> s {| <name_list> |} (s "=" s {| <expr_list> |})?
) -> localDecl
patt <- (
<array_patt> / <table_patt> / <member_expr>
)
array_patt <- (
"[" s {| <patt> (s "," s <patt>)* |} "]"
) -> arrayPatt
table_patt <- (
"{" s {| <table_patt_pair> (s "," s <table_patt_pair>)* |} "}"
) -> tablePatt
table_patt_pair <- (
(<literal> / <ident>) s ":" s <patt>
)
name_list <- (
<ident> (s "," s <ident>)*
)
expr_list <- (
<expr> (s "," s <expr>)*
)
func_path <- {|
<ident> (s {"."/"::"} s <ident>)*
|}
func_decl <- (
"function" <idsafe> s <func_path> s <func_head> s <func_body>
) -> funcDecl
func_head <- (
"(" s {| <param_list>? |} s ")"
)
func_expr <- (
"function" <idsafe> s <func_head> s <func_body>
/ (<func_head> / {| |}) s "=>" s <func_body>
) -> funcExpr
func_body <- <block_stmt> s <end> / <expr>
coro_expr <- (
"function*" s <func_head> s <func_body>
/ "*" <func_head> s "=>" s <func_body>
) -> coroExpr
coro_decl <- (
"function*" s <func_path> s <func_head> s <func_body>
) -> coroDecl
coro_prop <- (
({"get"/"set"} <idsafe> s / '' -> "init") "*" <ident> s
<func_head> s <func_body>
) -> coroProp
class_decl <- (
"class" <idsafe> s <ident> (s <class_heritage>)? s <class_body> s <end>
) -> classDecl
class_body <- {|
(<class_body_stmt> (<sep> s <class_body_stmt>)* <sep>?)?
|}
class_body_stmt <- (
<class_member> / !<return_stmt> <stmt>
)
class_member <- (
({"static"} <idsafe> s / '' -> "virt") (<coro_prop> / <prop_defn>)
) -> classMember
class_heritage <- (
"extends" <idsafe> s <expr> / {| |}
)
prop_defn <- (
({"get"/"set"} <idsafe> s / '' -> "init") <ident> s
<func_head> s <func_body>
) -> propDefn
param <- {|
{:name: <ident> :} (s "=" s {:default: <expr> :})?
|}
param_list <- (
<param> s "," s <param_list>
/ <param> s "," s <param_rest>
/ <param>
/ <param_rest>
)
param_rest <- {| "..." {:name: <ident> :} {:rest: '' -> 'true' :} |}
block_stmt <- (
{| (<stmt> (<sep> s <stmt>)* <sep>?)? |}
) -> blockStmt
if_stmt <- (
"if" <idsafe> s <expr> s "then" <idsafe> s <block_stmt> s (
"else" <if_stmt>
/ "else" <idsafe> s <block_stmt> s <end>
/ <end>
)
) -> ifStmt
for_stmt <- (
"for" <idsafe> s <ident> s "=" s <expr> s "," s <expr>
(s "," s <expr> / ('' -> '1') -> tonumber) s
<loop_body>
) -> forStmt
for_in_stmt <- (
"for" <idsafe> s {| <name_list> |} s <in> s <expr> s
<loop_body>
) -> forInStmt
loop_body <- <do> s <block_stmt> s <end>
do_stmt <- <loop_body> -> doStmt
while_stmt <- (
"while" <idsafe> s <expr> s <loop_body>
) -> whileStmt
repeat_stmt <- (
"repeat" <idsafe> s <block_stmt> s "until" <idsafe> s <expr>
) -> repeatStmt
ident <- (
!<keyword> { <word> }
) -> identifier
term <- (
<coro_expr>
/ <func_expr>
/ <nil_expr>
/ <super_expr>
/ <comp_expr>
/ <table_expr>
/ <array_expr>
/ <regex_expr>
/ <ident>
/ <literal>
/ <rstring>
/ "(" s <expr> s ")"
)
expr <- <infix_expr> / <spread_expr>
spread_expr <- (
"..." <postfix_expr>
) -> spreadExpr
nil_expr <- (
"nil" <idsafe>
) -> nilExpr
super_expr <- (
"super" <idsafe>
) -> superExpr
expr_stmt <- (
{} (<assign_expr> / <update_expr> / <expr>)
) -> exprStmt
binop <- {
"+" / "-" / "~" / "/" / "**" / "*" / "%" / "^" / "|" / "&"
/ ">>>" / ">>" / ">=" / ">" / "<<" / "<=" / "<" / ".."
/ "!=" / "==" / ("or" / "and" / "is") <idsafe>
}
infix_expr <- (
{| <prefix_expr> (s <binop> s <prefix_expr>)+ |}
) -> infixExpr / <prefix_expr>
prefix_expr <- (
{ "#" / "~" / "+" / "-" / "!" / ("not" / "typeof") <idsafe> } s <prefix_expr>
) -> prefixExpr / <postfix_expr>
postfix_expr <- {|
<term> <postfix_tail>+
|} -> postfixExpr / <term>
postfix_tail <- {|
s { "." } s <ident>
/ { "::" } s (<ident> / '' => error)
/ { "[" } s <expr> s ("]" / '' => error)
/ { "(" } s {| <expr_list>? |} s (")" / '' => error)
/ {~ HS -> "(" ~} {| !<binop> <expr_list> |}
|}
member_expr <- {|
<term> <member_next>?
|} -> postfixExpr / <term>
member_next <- (
<postfix_tail> <member_next> / <member_tail>
)
member_tail <- {|
s { "." } s <ident>
/ { "::" } s <ident>
/ { "[" } s <expr> s ("]" / '' => error)
|}
assop <- {
"+=" / "-=" / "~=" / "**=" / "*=" / "/=" / "%="
/ "|=" / "&=" / "^=" / "<<=" / ">>>=" / ">>="
}
left_expr <- (
<patt> / <ident>
-- <member_expr> / <ident>
)
assign_expr <- (
{| <left_expr> (s "," s <left_expr>)* |} s "=" s {| <expr_list> |}
) -> assignExpr
update_expr <- (
<left_expr> s <assop> s <expr>
) -> updateExpr
array_expr <- (
"[" s {| <array_elements>? |} s "]"
) -> arrayExpr
array_elements <- <expr> (s "," s <expr>)* (s ",")?
table_expr <- (
"{" s {| <table_members>? |} s "}"
) -> tableExpr
table_members <- (
<table_member> (hs (","/";"/%nl) s <table_member>)* (hs (","/";"/%nl))?
)
table_member <- ((<coro_prop> / <prop_defn>) / {|
{:key: ("[" s <expr> s "]" / <ident>) :} s "=" s {:value: <expr> :}
|} / <ident>) -> tableMember
comp_expr <- (
"[" s {| <comp_block>+ |}
"yield" <idsafe> s <expr> s "]"
) -> compExpr
comp_block <- (
"for" <idsafe> s {| <name_list> |} s <in> s <expr>
(s "if" <idsafe> s <expr>)? s
) -> compBlock
regex_expr <- (
"/" { ( "\\" / "\/" / !("/" / %nl) .)* } "/" {[gmi]*}
) -> regexExpr
]]
local grammar = re.compile(patt, defs)
local function parse(src)
return grammar:match(src)
end
return {
parse = parse
}