- Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathparser.lua
More file actions
Latest commit
607 lines (559 loc) · 17.3 KB
/
Copy pathparser.lua
File metadata and controls
607 lines (559 loc) · 17.3 KB
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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
localoperator=require("sci-lang.operator")
localLJ_52=false
localEndOfBlock= { TK_else=true, TK_elseif=true, TK_end=true, TK_until=true, TK_eof=true }
localis_algebra_map= {
BinaryAlgebraExpression=true,
UnaryAlgebraExpression=true,
IndexAlgebraExpression=true,
}
localfunctionis_algebra(node)
returnis_algebra_map[node.kind]
end
localfunctionerr_syntax(ls, em)
ls:error(ls.token, em)
end
localfunctionerr_token(ls, token)
ls:error(ls.token, "'%s' expected", ls.token2str(token))
end
localfunctioncheckcond(ls, cond, em)
ifnotcondthenerr_syntax(ls, em) end
end
localfunctionlex_opt(ls, tok)
ifls.token==tokthen
ls:next()
returntrue
end
returnfalse
end
localfunctionlex_check(ls, tok)
ifls.token~=tokthenerr_token(ls, tok) end
ls:next()
end
localfunctionlex_match(ls, what, who, line)
ifnotlex_opt(ls, what) then
ifline==ls.linenumberthen
err_token(ls, what)
else
localtoken2str=ls.token2str
ls:error(ls.token, "%s expected (to close %s at line %d)", token2str(what), token2str(who), line)
end
end
end
localfunctionlex_str(ls)
ifls.token~='TK_name' and (LJ_52orls.token~='TK_goto') then
err_token(ls, 'TK_name')
end
locals=ls.tokenval
ls:next()
returns
end
localexpr_primary, expr, expr_unop, expr_binop, expr_simple
localexpr_list, expr_table
localparse_body, parse_block, parse_args
localfunctionvar_lookup(ast, ls)
localname=lex_str(ls)
returnast:identifier(name)
end
localfunctionexpr_field(ast, ls, v)
ls:next() -- Skip dot or colon.
localkey=lex_str(ls)
returnast:expr_property(v, key)
end
localfunctionexpr_bracket(ast, ls, accept_empty)
ls:next() -- Skip '['.
localv
ifaccept_emptyandlex_opt(ls, ']') then
v=nil
else
v=expr(ast, ls)
lex_check(ls, ']')
end
returnv
end
functionexpr_table(ast, ls)
localline=ls.linenumber
localkvs= {}
lex_check(ls, '{')
whilels.token~='}' do
localkey
ifls.token=='[' then
key=expr_bracket(ast, ls)
lex_check(ls, '=')
elseif (ls.token=='TK_name' or (notLJ_52andls.token=='TK_goto')) andls:lookahead() =='=' then
localname=lex_str(ls)
key=ast:literal(name)
lex_check(ls, '=')
end
localval=expr(ast, ls)
kvs[#kvs+1] = { val, key } -- "key" can be nil.
ifnotlex_opt(ls, ',') andnotlex_opt(ls, ';') thenbreakend
end
lex_match(ls, '}', '{', line)
returnast:expr_table(kvs, line)
end
functionexpr_simple(ast, ls)
localtk, val=ls.token, ls.tokenval
locale
iftk=='TK_number' then
e=ast:literal(val)
elseiftk=='TK_string' then
e=ast:literal(val)
elseiftk=='TK_nil' then
e=ast:literal(nil)
elseiftk=='TK_true' then
e=ast:literal(true)
elseiftk=='TK_false' then
e=ast:literal(false)
elseiftk=='TK_dots' then
ifnotls.fs.varargsthen
err_syntax(ls, "cannot use \"...\" outside a vararg function")
end
e=ast:expr_vararg()
elseiftk=='{' then
returnexpr_table(ast, ls)
elseiftk=='TK_function' then
ls:next()
localargs, body, proto=parse_body(ast, ls, ls.linenumber, false)
returnast:expr_function(args, body, proto)
else
returnexpr_primary(ast, ls)
end
ls:next()
returne
end
functionexpr_list(ast, ls)
localexps= { }
exps[1] =expr(ast, ls)
whilelex_opt(ls, ',') do
exps[#exps+1] =expr(ast, ls)
end
localn=#exps
ifn>0then
exps[n] =ast:set_expr_last(exps[n])
end
returnexps
end
functionexpr_unop(ast, ls)
localtk=ls.token
iftk=='TK_not' ortk=='-' ortk=='#' then
localline=ls.linenumber
ls:next()
localop=ls.token2str(tk)
localv=expr_binop(ast, ls, operator.unary_priority(op))
ifis_algebra(v) then
assert(op=='-', 'not yet implemented')
returnast:expr_algebra_unop(op, v, line)
else
returnast:expr_unop(op, v, line)
end
else
localexp=expr_simple(ast, ls)
iflex_opt(ls, '`') then
localline=ls.linenumber
exp=ast:expr_algebra_unop('`', exp, line)
end
returnexp
end
end
-- Parse binary expressions with priority higher than the limit.
functionexpr_binop(ast, ls, limit)
localv=expr_unop(ast, ls)
localop=ls.token2str(ls.token)
whileoperator.is_binop(op) andoperator.left_priority(op) >limitdo
localline=ls.linenumber
ls:next()
localv2, nextop=expr_binop(ast, ls, operator.right_priority(op))
ifis_algebra(v) oris_algebra(v2) then
v=ast:expr_algebra_binop(op, v, v2, line)
else
v=ast:expr_binop(op, v, v2, line)
end
op=nextop
end
returnv, op
end
functionexpr(ast, ls)
returnexpr_binop(ast, ls, 0) -- Priority 0: parse whole expression.
end
-- Parse primary expression.
functionexpr_primary(ast, ls)
localv, vk
-- Parse prefix expression.
ifls.token=='(' then
localline=ls.linenumber
ls:next()
vk, v='expr', ast:expr_brackets(expr(ast, ls))
lex_match(ls, ')', '(', line)
elseifls.token=='TK_name' or (notLJ_52andls.token=='TK_goto') then
vk, v='var', var_lookup(ast, ls)
else
err_syntax(ls, "unexpected symbol")
end
whiletruedo-- Parse multiple expression suffixes.
localline=ls.linenumber
ifls.token=='.' then
vk, v='indexed', expr_field(ast, ls, v)
elseifls.token=='[' then
localkey=expr_bracket(ast, ls, true)
ifkeythen
vk, v='indexed', ast:expr_index(v, key)
else
vk, v='indexed', ast:expr_algebra_index(v)
end
elseifls.token==':' then
ls:next()
localkey=lex_str(ls)
localargs=parse_args(ast, ls)
vk, v='call', ast:expr_method_call(v, key, args, line)
elseifls.token=='(' orls.token=='TK_string' orls.token=='{' then
localargs=parse_args(ast, ls)
vk, v='call', ast:expr_function_call(v, args, line)
else
break
end
end
returnv, vk
end
-- Parse statements ----------------------------------------------------
-- Parse 'return' statement.
localfunctionparse_return(ast, ls, line)
ls:next() -- Skip 'return'.
ls.fs.has_return=true
localexps
ifEndOfBlock[ls.token] orls.token==';' then-- Base return.
exps= { }
else-- Return with one or more values.
exps=expr_list(ast, ls)
end
returnast:return_stmt(exps, line)
end
-- Parse numeric 'for'.
localfunctionparse_for_num(ast, ls, varname, line)
lex_check(ls, '=')
localinit=expr(ast, ls)
lex_check(ls, ',')
locallast=expr(ast, ls)
localstep
iflex_opt(ls, ',') then
step=expr(ast, ls)
else
step=ast:literal(1)
end
lex_check(ls, 'TK_do')
localbody=parse_block(ast, ls, line)
localvar=ast:identifier(varname)
returnast:for_stmt(var, init, last, step, body, line, ls.linenumber)
end
-- Parse 'for' iterator.
localfunctionparse_for_iter(ast, ls, indexname)
localvars= { ast:identifier(indexname) }
whilelex_opt(ls, ',') do
vars[#vars+1] =ast:identifier(lex_str(ls))
end
lex_check(ls, 'TK_in')
localline=ls.linenumber
localexps=expr_list(ast, ls)
lex_check(ls, 'TK_do')
localbody=parse_block(ast, ls, line)
returnast:for_iter_stmt(vars, exps, body, line, ls.linenumber)
end
-- Parse 'for' statement.
localfunctionparse_for(ast, ls, line)
ls:next() -- Skip 'for'.
localvarname=lex_str(ls) -- Get first variable name.
localstmt
ifls.token=='=' then
stmt=parse_for_num(ast, ls, varname, line)
elseifls.token==',' orls.token=='TK_in' then
stmt=parse_for_iter(ast, ls, varname)
else
err_syntax(ls, "'=' or 'in' expected")
end
lex_match(ls, 'TK_end', 'TK_for', line)
returnstmt
end
localfunctionparse_repeat(ast, ls, line)
ast:fscope_begin()
ls:next() -- Skip 'repeat'.
localbody=parse_block(ast, ls)
locallastline=ls.linenumber
lex_match(ls, 'TK_until', 'TK_repeat', line)
localcond=expr(ast, ls) -- Parse condition.
ast:fscope_end()
returnast:repeat_stmt(cond, body, line, lastline)
end
-- Parse function argument list.
functionparse_args(ast, ls)
localline=ls.linenumber
localargs
ifls.token=='(' then
ifnotLJ_52andline~=ls.lastlinethen
err_syntax(ls, "ambiguous syntax (function call x new statement)")
end
ls:next()
ifls.token~=')' then-- Not f().
args=expr_list(ast, ls)
else
args= { }
end
lex_match(ls, ')', '(', line)
elseifls.token=='{' then
locala=expr_table(ast, ls)
args= { a }
elseifls.token=='TK_string' then
locala=ls.tokenval
ls:next()
args= { ast:literal(a) }
else
err_syntax(ls, "function arguments expected")
end
returnargs
end
localfunctionparse_assignment(ast, ls, vlist, var, vk)
localline=ls.linenumber
checkcond(ls, vk=='var' orvk=='indexed', 'syntax error')
vlist[#vlist+1] =var
iflex_opt(ls, ',') then
localn_var, n_vk=expr_primary(ast, ls)
returnparse_assignment(ast, ls, vlist, n_var, n_vk)
else-- Parse RHS.
lex_check(ls, '=')
localexps=expr_list(ast, ls)
localalgebra=false
fori=1,#vlistdo
algebra=algebraoris_algebra(vlist[i])
end
ifalgebrathen
assert(#vlist==1, 'not yet implemented')
returnast:assignment_algebra_expr(vlist, exps, line)
else
returnast:assignment_expr(vlist, exps, line)
end
end
end
localfunctionparse_call_assign(ast, ls)
localvar, vk=expr_primary(ast, ls)
ifvk=='call' then
returnast:new_statement_expr(var, ls.linenumber)
else
localvlist= { }
returnparse_assignment(ast, ls, vlist, var, vk)
end
end
-- Parse 'local' statement.
localfunctionparse_local(ast, ls)
localline=ls.linenumber
iflex_opt(ls, 'TK_function') then-- Local function declaration.
localname=lex_str(ls)
localargs, body, proto=parse_body(ast, ls, line, false)
returnast:local_function_decl(name, args, body, proto)
else-- Local variable declaration.
localvl= { }
repeat-- Collect LHS.
vl[#vl+1] =lex_str(ls)
untilnotlex_opt(ls, ',')
localexps
iflex_opt(ls, '=') then-- Optional RHS.
exps=expr_list(ast, ls)
else
exps= { }
end
returnast:local_decl(vl, exps, line)
end
end
localfunctionparse_func(ast, ls, line)
localneedself=false
ls:next() -- Skip 'function'.
-- Parse function name.
localv=var_lookup(ast, ls)
whilels.token=='.' do-- Multiple dot-separated fields.
v=expr_field(ast, ls, v)
end
ifls.token==':' then-- Optional colon to signify method call.
needself=true
v=expr_field(ast, ls, v)
end
localargs, body, proto=parse_body(ast, ls, line, needself)
returnast:function_decl(v, args, body, proto)
end
localfunctionparse_while(ast, ls, line)
ls:next() -- Skip 'while'.
localcond=expr(ast, ls)
ast:fscope_begin()
lex_check(ls, 'TK_do')
localbody=parse_block(ast, ls)
locallastline=ls.linenumber
lex_match(ls, 'TK_end', 'TK_while', line)
ast:fscope_end()
returnast:while_stmt(cond, body, line, lastline)
end
localfunctionparse_then(ast, ls, tests, line)
ls:next()
tests[#tests+1] =expr(ast, ls)
lex_check(ls, 'TK_then')
returnparse_block(ast, ls, line)
end
localfunctionparse_if(ast, ls, line)
localtests, blocks= { }, { }
blocks[1] =parse_then(ast, ls, tests, line)
whilels.token=='TK_elseif' do
blocks[#blocks+1] =parse_then(ast, ls, tests, ls.linenumber)
end
localelse_branch
ifls.token=='TK_else' then
localeline=ls.linenumber
ls:next() -- Skip 'else'.
else_branch=parse_block(ast, ls, eline)
end
lex_match(ls, 'TK_end', 'TK_if', line)
returnast:if_stmt(tests, blocks, else_branch, line)
end
localfunctionparse_label(ast, ls)
ls:next() -- Skip '::'.
localname=lex_str(ls)
lex_check(ls, 'TK_label')
-- Recursively parse trailing statements: labels and ';' (Lua 5.2 only).
whiletruedo
ifls.token=='TK_label' then
parse_label(ast, ls)
elseifLJ_52andls.token==';' then
ls:next()
else
break
end
end
returnast:label_stmt(name, ls.linenumber)
end
localfunctionparse_goto(ast, ls)
localline=ls.linenumber
localname=lex_str(ls)
returnast:goto_stmt(name, line)
end
-- Parse a statement. Returns the statement itself and a boolean that tells if it
-- must be the last one in a chunk.
localfunctionparse_stmt(ast, ls)
localline=ls.linenumber
localstmt
ifls.token=='TK_if' then
stmt=parse_if(ast, ls, line)
elseifls.token=='TK_while' then
stmt=parse_while(ast, ls, line)
elseifls.token=='TK_do' then
ls:next()
localbody=parse_block(ast, ls)
locallastline=ls.linenumber
lex_match(ls, 'TK_end', 'TK_do', line)
stmt=ast:do_stmt(body, line, lastline)
elseifls.token=='TK_for' then
stmt=parse_for(ast, ls, line)
elseifls.token=='TK_repeat' then
stmt=parse_repeat(ast, ls, line)
elseifls.token=='TK_function' then
stmt=parse_func(ast, ls, line)
elseifls.token=='TK_local' then
ls:next()
stmt=parse_local(ast, ls, line)
elseifls.token=='TK_return' then
stmt=parse_return(ast, ls, line)
returnstmt, true-- Must be last.
elseifls.token=='TK_break' then
ls:next()
stmt=ast:break_stmt(line)
returnstmt, notLJ_52-- Must be last in Lua 5.1.
elseifLJ_52andls.token==';' then
ls:next()
returnparse_stmt(ast, ls)
elseifls.token=='TK_label' then
stmt=parse_label(ast, ls)
elseifls.token=='TK_goto' then
ifLJ_52orls:lookahead() =='TK_name' then
ls:next()
stmt=parse_goto(ast, ls)
end
end
-- If here 'stmt' is "nil" then ls.token didn't match any of the previous rules.
-- Fall back to call/assign rule.
ifnotstmtthen
stmt=parse_call_assign(ast, ls)
end
returnstmt, false
end
localfunctionparse_params(ast, ls, needself)
lex_check(ls, "(")
localargs= { }
ifneedselfthen
args[1] =ast:var_declare("self")
end
ifls.token~=")" then
repeat
ifls.token=='TK_name' or (notLJ_52andls.token=='TK_goto') then
localname=lex_str(ls)
args[#args+1] =ast:var_declare(name)
elseifls.token=='TK_dots' then
ls:next()
ls.fs.varargs=true
args[#args+1] =ast:expr_vararg()
break
else
err_syntax(ls, "<name> or \"...\" expected")
end
untilnotlex_opt(ls, ',')
end
lex_check(ls, ")")
returnargs
end
localfunctionnew_proto(ls, varargs)
return { varargs=varargs }
end
localfunctionparse_block_stmts(ast, ls)
localfirstline=ls.linenumber
localstmt, islast=nil, false
localbody= { }
whilenotislastandnotEndOfBlock[ls.token] do
stmt, islast=parse_stmt(ast, ls)
body[#body+1] =stmt
lex_opt(ls, ';')
end
returnbody, firstline, ls.linenumber
end
localfunctionparse_chunk(ast, ls)
localbody, firstline, lastline=parse_block_stmts(ast, ls)
returnast:chunk(body, ls.chunkname, 0, lastline)
end
-- Parse body of a function.
functionparse_body(ast, ls, line, needself)
localpfs=ls.fs
ls.fs=new_proto(ls, false)
ast:fscope_begin()
ls.fs.firstline=line
localargs=parse_params(ast, ls, needself)
localbody=parse_block(ast, ls)
ast:fscope_end()
localproto=ls.fs
ifls.token~='TK_end' then
lex_match(ls, 'TK_end', 'TK_function', line)
end
ls.fs.lastline=ls.linenumber
ls:next()
ls.fs=pfs
returnargs, body, proto
end
functionparse_block(ast, ls, firstline)
ast:fscope_begin()
localbody=parse_block_stmts(ast, ls)
body.firstline, body.lastline=firstline, ls.linenumber
ast:fscope_end()
returnbody
end
localfunctionparse(ast, ls)
ls:next()
ls.fs=new_proto(ls, true)
ast:fscope_begin()
localchunk=parse_chunk(ast, ls)
ast:fscope_end()
ifls.token~='TK_eof' then
err_token(ls, 'TK_eof')
end
returnchunk
end
returnparse