-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
input.lua
1051 lines (1021 loc) · 25.4 KB
/
input.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
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
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
-- a
-- b
--[[c]]
print"hi"
print"hi"
A = 123
local function a(p1,p2,...)
print("b")
end
local function a()
print("b")
a,b = 1,2,3
end
--[[ LOGGING ]]--
local TostringValue,TupleToString,List,pprint do
local tostring,pcall = tostring,pcall
local function doPathKey(key)
if type(key) == "string" and key:find("^[%a_][%w_]*$") then
return "."..key
end return "["..TostringValue(key,true).."]"
end local prims = {boolean=true,number=true,string=true,userdata=true}
function TostringValue(val,short,tab,inline)
tab = tab or 0
local typ = type(val)
local tss,ts = pcall(tostring,val)
ts = tss and ts or "<tostring errored>"
if type(ts) ~= "string" then return "__tostring_returned_non_string" end
local realpre = (" "):rep(tab)
local prefix = inline and "" or realpre
short = short or {path={"root"}}
if typ == "string" then
return prefix..("%q"):format(val)
elseif val == nil or typ == "boolean" or typ == "number" then
return prefix..ts
elseif typ == "table" then
if short == true then return prefix..ts end
if short[val] then return prefix..short[val] end
short[val] = table.concat(short.path)
local pc,ipc,prim = 0,0,true
for k,v in pairs(val) do
pc,prim=pc+1,prim and prims[type(k)] and prims[type(v)]
end
for k,v in ipairs(val) do
ipc,prim=ipc+1,prim and prims[type(k)] and prims[type(v)]
end if pc == 0 then return prefix.."{}" end
local idk,pre = {},(" "):rep(tab+1)
local bigsep = prim and "" or "\n"
local sep = prim and "," or ";"
for i=1,ipc do
idk[i] = TostringValue(val[i],short,prim and 0 or tab+1)..sep..bigsep
end if ipc ~= 0 and prim then idk[#idk] = idk[#idk]:sub(1,-2) end
local row = (prim and "" or pre).."[%s] = %s"..sep..bigsep
for k,v in pairs(val) do
if type(k) ~= "number" or type(k) == "number" and k > ipc then
local key = TostringValue(k,true) short.path[#short.path+1] = doPathKey(k)
table.insert(idk,row:format(key,TostringValue(v,short,prim and 0 or tab+1,true)))
short.path[#short.path] = nil
end
end if pc-ipc ~= 0 and prim and idk[1] then idk[#idk] = idk[#idk]:sub(1,-2) end
return prefix.."{"..bigsep..table.concat(idk)..(prim and "" or realpre).."}"
elseif typ == "Instance" then
return prefix..(val.Parent and val:GetFullName() or "Instance "..ts)
else
return prefix..typ.." "..ts
end
end
function TupleToString(...)
local res = {...}
for i=1,select("#",...) do
res[i] = TostringValue(res[i])
end return table.concat(res,", ")
end
function List(tab) print()
for k,v in pairs(tab) do
print("- ["..TostringValue(k,true).."] =",TostringValue(v,true)..";")
end
end
local function path()
local res,trace = {},debug.traceback()
for line in trace:gmatch("[^\n]+") do
local ln,fn = line:match("(%d+): in function '(%w+)'")
if ln and tonumber(ln) > 100 then
res[#res+1] = fn
elseif line:find("...",1,true) then
res[#res+1] = "..."
end
end trace = {}
for i=1,#res do
trace[i] = res[#res-i+1]
end return table.concat(trace,">")
end
local function level()
for i=1,200 do
if not debug.getinfo(i) then
return i
end
end
end
BLOCKS = 0
function pprint(...)
print("["..level().."|"..BLOCKS.."]",TupleToString(...))
end
end
function a()
out:write()
end
local function cprint(txt,col,maxl)
local out = io.open("out.ps1","w")
local ln,nl = 1
txt = txt:gsub("\n"," \n")
for line in txt:gmatch("[^\n]+") do
if maxl and ln > maxl then break end
if nl then out:write("write-host\n") end nl = true
line = line:gsub('"','\96"') ln = ln + 1
if line == " " then line = "" end
out:write("write-host -NoNewline -Background "..col.." \""..line.."\"\n")
end
out:close()
os.execute("powershell -ExecutionPolicy Bypass -File out.ps1")
--os.execute("powershell write-host -NoNewline -foreground "..col.." "..test)
end
local function splitprint(txt,i)
local begin,lns = txt:sub(1,i-1),{}
for line in begin:gmatch("[^\n]+") do
lns[#lns+1] = line
end begin = table.concat(lns,"\n",math.max(#lns-5,1))
cprint(begin,"darkgreen" or "green")
cprint(txt:sub(i),"darkred" or "red",3) print()
end
--[[ PARSER ]]--
local function map(tab)
local res = {}
for i=1,#tab do
res[tab[i]] = true
end return res
end
local Keywords = map{
"do","end","while","repeat","until",
"if","then","elseif","else","for","in",
"function","local","return","break",
"false","true","nil","not","and","or"
}
local Binops,BinopsPriority = {
"+","-","*","/","^","%","..",
"<=","<",">=",">","==","~="
},{
["^"] = 1;
-- unary operators = 2
["*"] = 3, ["/"] = 3, ["%"] = 3;
["+"] = 4, ["-"] = 4;
[".."] = 5;
["<"] = 6, ["<="] = 6, [">"] = 6, [">="] = 6, ["=="] = 6, ["~="] = 6;
-- or/and = 7
}
local Parser = {}
function Parser.new(str)
local lines = {1}
for i=1,#str do
if str:byte(i,i) == 10 then
lines[#lines+1] = i+1
end
end
local res = {str=str,i=1,lines=lines}
for k,v in pairs(Parser) do
res[k] = v
end return res
end
do -- Parser stuff
function Parser:Line(i)
i = i or self.i
local lines = self.lines
for line=#lines,1,-1 do
local start = lines[line]
if start <= i then
return line,i-start+1
end
end error()
end
function Parser:Peek(func,...)
local old = self.i
return self[func](self,...),old
end
function Parser:ActualTrim()
local a,b = self.str:find("%s+",self.i)
if a ~= self.i then return self.i,true end
b = b + 1 self.i = b return b,false
end
function Parser:Comment()
self:ActualTrim()
local eq = self:Match("%-%-%[=*%[",true)
if eq then
local start = self:Line()
eq = string.rep("=",#eq - 4)
local start,stop = self:Find("]"..eq.."]",true)
assert(start,"unfinished long comment starting at line "..start)
self.i = stop + 1 return true
elseif self:String("--",true) then
local p = self:Find("\n",true)
if not p then return end
self.i = p + 1 return true
end
end
function Parser:Trim()
while self:Comment() do end
return self:ActualTrim()
end
function Parser:String(str,noTrim)
local i = noTrim and self.i or self:Trim()
if self.str:sub(i,i+#str-1) == str then
self.i = i+#str return str
end
end
function Parser:Match(pat,noTrim)
local i = noTrim and self.i or self:Trim()
local a,b = self.str:find(pat,i)
if a == i then
self.i = b + 1
return self.str:sub(a,b)
end
end
function Parser:Find(pat,plain)
return self.str:find(pat,self.i,plain)
end
function Parser:Keyword(word)
local i,str = self:Trim(),self.str
local a,b = str:find("%w+",i)
if a == i then
local found = str:sub(a,b)
if found == word then
self.i = b + 1 return found
elseif not word and Keywords[found] then
self.i = b + 1 return found
end
end
end
end
do -- Error handling
function Parser:Assert(cond,err,...)
if cond then return end
err = err:gsub("%%l",self:Line())
error(err:format(...))
end
end
--[[ Syntax structs
Scope {
Parent = Scope
Locals = Local[]
Upvalues = Upvalue[]
}
Chunk {
Block = Stat[]
Scope = Scope
}
FuncBody : Chunk {
Block = Stat[]
Parameters = <name>[]
}
Stat {
Line = <number>
Type = <statType>
<statType-specific fields>
}
Stat types:
Vararg {}
Break {}
Return {
Expressions = Expression[]
}
Variable {
Scope = Scope;
ScopePosition = <number>
Name = <name>
}
Field {
Name = <name> -- THIS OR
Expression = Expression -- THIS
Base = Expression
}
Method {
Name = <name>
Base = Expression
}
Do {
Block = Stat[]
}
While {
Block = Stat[]
Condition = Expression
}
Repeat {
Block = Stat[]
Condition = Expression
}
If {
Blocks = {Expression,Stat[]}[]
Otherwise = Stat[]
}
NumericFor {
Block = Stat[]
Name = <name>
Var = Expression
Limit = Expression
Step = Expression/nil
}
GenericFor {
Block = Stat[]
Names = <name>[]
Expressions = Expression[]
}
Assignment {
Variables = Expression[] (Variable/Field)
Expressions = Expression[]
Locals = <true/nil>
}
UnaryOp {
Operation = <unop>
Priority = 2
Expression = Expression
}
BinaryOp {
Operation = <binop>
Priority = <priority>
Left = Expression
Right = Expression
}
FunctionCall {
--Method = <boolean>
Target = Expression
Arguments = Expression[]
}
FunctionSelfCall {
Name = <name>
Base = Expression
Arguments = Expression[]
}
Brackets {
Expression = Expression
}
Constant {
Value = <string/number/boolean/nil>
}
Table {
Content = {Key=Expression/nil,Value=Expression}[]
}
Function {
Chunk = Chunk
Parameters = <name>[]
Variable = Variable/Field/Method/<nil>
Local = <true/nil>
}
]]
do -- Syntax parsing
function Parser:Block()
BLOCKS = BLOCKS + 1
local scope = {
Parent = self.scope,
Locals = {},
Upvalues = {}
}
self.scope = scope
local stats = {}
local prev = self.block
self.block = stats
local stat,last = self:Stat()
while stat do
stats[#stats+1] = stat
self:String(";")
if last then break end
stat,last = self:Stat()
end
if self:Keyword("break") then
stats[#stats+1] = {
Line = self:Line();
Type = "Break";
}
elseif self:Keyword("return") then
local start = self:Line()
stats[#stats+1] = {
Line = start;
Type = "Return";
Expressions = self:Explist() or {};
}
end
self.block = prev
self.scope = scope.Parent
BLOCKS = BLOCKS - 1
return stats,scope
end
function Parser:Chunk()
local chunk = {}
self.base = self.base or chunk
local prev = self.chunk
self.chunk = chunk
chunk.Block,chunk.Scope = self:Block()
self.chunk = prev
return chunk
end
function Parser:Parse()
local ch = self:Chunk()
assert(self.i > #self.str,"Unexpected symbol")
return ch
end
--[[
stat ::=
varlist `=� explist |
functioncall |
do block end |
while exp do block end |
repeat block until exp |
if exp then block {elseif exp then block} [else block] end |
for Name `=� exp `,� exp [`,� exp] do block end |
for namelist in explist do block end |
function funcname funcbody |
local function Name funcbody |
local namelist [`=� explist]
]]
function Parser:Functionname()
local start = self:Line()
local name = self:Name()
assert(name,"Expected a name")
local expr = {
Line = start;
Type = "Variable";
Scope = self.scope;
ScopePosition = #self.scope.Locals;
Name = name;
}
while self:String(".") do
name = self:Name()
assert(name,"Expected a name")
expr = {
Line = start;
Type = "Field";
Name = name;
Base = expr;
}
end
if self:String(":") then
name = self:Name()
assert(name,"Expected a name")
expr = {
Line = start;
Type = "Method";
Name = name;
Base = expr;
}
end return expr
end
function Parser:Stat()
local keyword,old = self:Peek("Keyword")
local start = self:Line()
if keyword == "do" then
local block = self:Block()
assert(self:Keyword("end"),"Expected `end` to close `do` (line "..start..")")
return {
Line = start;
Type = "Do";
Block = block;
}
elseif keyword == "while" then
local expr = self:Expression()
assert(self:Keyword("do"),"Expected `do`")
local block = self:Block()
assert(self:Keyword("end"),"Expected `end` to close `while` (line "..start..")")
--self.block[#self.block+1] =
return {
Line = start;
Type = "While";
Condition = expr;
Block = block;
}
elseif keyword == "repeat" then
local block = self:Block()
assert(self:Keyword("until"),"Expected `until` to close `repeat` (line "..start..")")
local expr = self:Expression()
self.block[#self.block+1] = {
Line = start;
Type = "Repeat";
Condition = expr;
Block = block;
}
elseif keyword == "if" then
local expr = self:Expression()
assert(expr,"Expected an expression")
assert(self:Keyword("then"),"Expected `then`")
local blocks = {{expr,(self:Block())}}
while self:Keyword("elseif") do
expr = self:Expression()
assert(expr,"Expected an expression")
assert(self:Keyword("then"),"Expected `then`")
blocks[#blocks+1] = {expr,(self:Block())}
end local otherwise
if self:Keyword("else") then
otherwise = self:Block()
end
assert(self:Keyword("end"),"Expected `end` to close `if` (line "..start..")")
--self.block[#self.block+1] =
return {
Line = start;
Type = "If";
Blocks = blocks;
Otherwise = otherwise;
}
elseif keyword == "for" then
local names = self:Namelist()
assert(names,"Expected a name")
if #names == 1 and self:String("=") then
local var = self:Expression()
assert(var,"Expected an expression")
assert(self:String(","),"Expected a `,`")
local limit = self:Expression()
assert(limit,"Expected an expression")
local step
if self:String(",") then
step = self:Expression()
assert(step,"Expected an expression")
end
assert(self:Keyword("do"),"Expected `do`")
local block = self:Block()
assert(self:Keyword("end"),"Expected `end` to close `for` (line "..start..")")
return {
Line = start;
Type = "NumericFor";
Block = block;
Name = names[1];
Var = var;
Limit = limit;
Step = step;
}
end
assert(self:Keyword("in"),"Expected `in`")
local exprs = self:Explist()
assert(exprs,"Expected an expression")
assert(self:Keyword("do"),"Expected `do`")
local block = self:Block()
assert(self:Keyword("end"),"Expected `end` to close `for` (line "..start..")")
return {
Line = start;
Type = "GenericFor";
Block = block;
Names = names;
Expressions = exprs;
}
elseif keyword == "function" then
local name = self:Functionname()
assert(name,"Expected a name")
local func = self:Funcbody()
--[[func.Variable = {
Line = start;
Type = "Variable";
Scope = self.scope;
ScopePosition = #self.scope.Locals;
Name = name;
}]]
func.Variable = name
if name.Type == "Method" then
--table.insert(func.Parameters,1,"self")
table.insert(func.Chunk.Scope.Locals,1,"self")
end
--self.block[#self.block+1] = func
return func
elseif keyword == "local" then
if self:Keyword("function") then
local name = self:Name()
assert(name,"Expected a name")
local locals = self.scope.Locals
locals[#locals+1] = name
local func = self:Funcbody()
func.Variable = {
Line = start;
Type = "Variable";
Scope = self.scope;
ScopePosition = #self.scope.Locals;
Name = name;
}
func.Local = true;
--self.block[#self.block+1] = func
return func
else
local vars,exprs = self:Namelist()
assert(vars,"Expected a name")
if self:String("=") then
exprs = self:Explist()
end
local locals = self.scope.Locals
for i=1,#vars do
local name = vars[i]
vars[i] = {
Line = start;
Type = "Variable";
Name = name;
Scope = self.scope;
ScopePosition = #self.scope.Locals;
}
locals[#locals+1] = name
end
return {
Line = start;
Type = "Assignment";
Variables = vars;
Expressions = exprs or {};
Locals = true;
}
end error("hi")
else--if not keyword then
self.i = old
local expr,o = self:Peek("Expression")
if expr and (expr.Type == "FunctionCall" or expr.Type == "FunctionSelfCall") then
return expr
end self.i = o
local vars = self:Varlist()
if vars then
assert(self:String("="),"Expected `=`")
local exprs = self:Explist()
assert(exprs,"Expected an expression")
return {
Line = start;
Type = "Assignment";
Variables = vars;
Expressions = exprs;
}
end self.i = o
end
self.i = old
end
function Parser:CheckBinop(expr)
local left = expr.Left
if left.Priority then
if left.Priority < expr.Priority then
end
end
return self:PostExpression(expr)
end
local IsPrefix = {
Variable = true;
Field = true;
FunctionCall = true;
FunctionSelfCall = true;
Brackets = true;
}
function Parser:PostExpression(prev)
local start = self:Line()
local binop,old = self:Peek("Binop")
local isPrefix = IsPrefix[prev.Type]
if binop then
local right = self:Expression()
assert(right,"Expected an expression")
return self:CheckBinop{
Line = start;
Type = "BinaryOp";
Operation = binop;
Priority = BinopsPriority[binop];
Left = prev;
Right = right;
}
elseif self:String("[") then
assert(isPrefix,"Unexpected symbol `[`")
start = self:Line()
local expr = self:Expression()
assert(expr,"Expected an expression")
assert(self:String("]"),"Expected `]` to close `[` (line "..start..")")
return self:PostExpression{
Line = start;
Type = "Field";
Expression = expr;
Base = prev;
}
elseif self:String(".") then
assert(isPrefix,"Unexpected symbol `.`")
local name = self:Name()
assert(name,"Expected a name")
return self:PostExpression{
Line = start;
Type = "Field";
Name = name;
Base = prev;
}
elseif self:String(":") then
assert(isPrefix,"Unexpected symbol `:`")
local name = self:Name()
assert(name,"Expected a name")
return self:PostExpression{
Line = start;
Type = "Method";
Name = name;
Base = prev;
}
end
local args = self:Args()
if args then
if prev.Type == "Method" then
return self:PostExpression{
Line = self:Line();
Type = "FunctionSelfCall";
Name = prev.Name;
Base = prev.Base;
Arguments = args;
}
end
assert(isPrefix,"Unexpected symbol")
return self:PostExpression{
Line = self:Line();
Type = "FunctionCall";
Target = prev;
Arguments = args;
}
end return prev
end
function Parser:Expression()
local start = self:Line()
if self:String("(") then
local expr = self:Expression()
assert(self:String(")"),"Expected `)` to close `(` (line "..start..")")
return self:PostExpression{
Line = start;
Type = "Brackets";
Expression = expr;
}
elseif self:String("...") then
return self:PostExpression{
Line = line;
Type = "Vararg";
}
elseif self:Keyword("function") then
local func = self:Funcbody()
return self:PostExpression(func)
end
local op,o = self:Peek("Match","[%-#]")
op = op or self:Keyword("not")
if op then
local expr = self:Expression()
assert(expr,"Expected an expression")
return self:PostExpression({
Line = start;
Type = "UnaryOp";
Operation = op;
Expression = expr;
Priority = 2;
})
end self.i = o
local keyword = self:Keyword()
if keyword == "nil" then
return self:PostExpression{Line=line,Type="Constant"}
elseif keyword == "true" then
return self:PostExpression{Line=line,Type="Constant",Value=true}
elseif keyword == "false" then
return self:PostExpression{Line=line,Type="Constant",Value=false}
end self.i = o
local number = self:Number()
if number then return self:PostExpression(number) end
local str = self:StringConstant()
if str then return self:PostExpression(str) end
local tab = self:Table()
if tab then return self:PostExpression(tab) end
local name,old = self:Peek("Name")
if name then
return self:PostExpression{
Line = start;
Type = "Variable";
Name = name;
Scope = self.scope;
ScopePosition = #self.scope.Locals;
}
end self.i = old
end
local Setters = {
Field = true;
Variable = true;
}
function Parser:Varlist()
local var,o = self:Peek("Expression")
if not var then self.i=o return end
if not Setters[var.Type] then self.i=o return end
local res = {var}
while self:String(",") do
var = self:Expression()
assert(var,"Expected a variable")
assert(Setters[var.Type],"Expected a variable/field")
res[#res+1] = var
end return res
end
function Parser:Number()
if self:String("0x") then
local mat = self:Match("%w+")
mat = tonumber(mat,16)
assert(mat,"Malformed number")
return {
Line = self:Line();
Type = "Constant";
Value = mat;
}
end
local value = self:Match("%d+")
if self:String(".",true) then
local n = self:Match("%d+",true)
value = (value or "").."."..n
elseif not value then
return nil
end
if self:Match("[eE]",true) then
local n = self:Match("[%-%+]?%d+",true)
assert(n,"Malformed number")
value = value.."e"..n
end
return {
Line = self:Line();
Type = "Constant";
Value = tonumber(value);
}
end
local escapes = {a="\a",b="\b",f="\f",n="\n",r="\r",t="\t",v="\v",["\r"] = "\n",["\\"] = "\\\\"}
local function escaped(str)
str = str:gsub("\\(%d%d?%d?)",string.char)
return str:gsub("\\x(%d+%d+)",function(c)
return string.char(tonumber(c,16))
end):gsub("\\(.)",function(c) return escapes[c] or c end)
end
function Parser:StringConstant()
local q = self:Match("['\"]")
if q then
local pos,str,esc = self.i,self.str
for i=pos,#str do
local c = str:sub(i,i)
if esc then
esc = nil
elseif c == "\\" then
esc = true
elseif c == q then
self.i = i + 1
esc = nil
return {
Line = self:Line();
Type = "Constant";
Value = escaped(str:sub(pos,i-1));
}
end
end
error("Unfinished string")
end
q = self:Match("%[=*%[")
if not q then return end
local eqs,pos = q:sub(2,-2),self.i
local a,b = self:Find("]"..eqs.."]",true)
self.i = b + 1
return {
Line = self:Line();
Type = "Constant";
Value = self.str:sub(pos,a-1);
}
end
function Parser:Binop()
local kw = self:Keyword("and")
kw = kw or self:Keyword("or")
if kw then return kw,7 end
for i=1,#Binops do
local op = Binops[i]
if self:String(op) then
return op
end
end
end
function Parser:Field()
if self:String("[") then
local start = self:Line()
local key = self:Expression()
assert(key,"Expected an expression")
assert(self:String("]"),"Expected `]` to close `[` (line "..start..")")
assert(self:String("="),"Expected `=`")
local val = self:Expression()
assert(val,"Expected an expression")
return {Key=key,Value=val}
end local name,old = self:Peek("Name")
if name and self:String("=") then
local start = self:Line()
local expr = self:Expression()
assert(expr,"Expected an expression")
return {
Key = {
Line = start;
Type = "Constant";
Value = name;
}, Value = expr
}
end self.i = old
local expr = self:Expression()
return expr and {Value=expr}
end
function Parser:Table()
if not self:String("{") then return end
local start = self:Line()
local content,sep = {},true
local field = self:Field()
while field do
assert(sep,"Expected `,` or `;`")
content[#content+1] = field
sep = self:Match("[,;]")
field = self:Field()
end
assert(self:String("}"),"Expected `}` to close `{` (line "..start..")")
return {
Line = start;
Type = "Table";
Content = content;
}
end
function Parser:Explist()
local expr = self:Expression()
if not expr then return end
local res = {expr}
while self:String(",") do
expr = self:Expression()
assert(expr,"Unexpected `,`")
res[#res+1] = expr
end
return res
end
function Parser:Args()
local res = self:StringConstant()
if res then return {res} end
res = self:Table()
if res then return {res} end
if not self:String("(") then return end
local start = self:Line()
res = self:Explist()
assert(self:String(")"),"Expected `)` to close `(` (line "..start..")")
return res or {}
end
function Parser:Functioncall()
local old = self.i
local prefix = self:Prefixexp()
if not prefix then self.i = old return end
local methodname
if self:String(":") then
methodname = self:Name()
assert(methodname,"Expected a name")
end
local args = self:Args()
if not args then
if prefix.Arguments then
return prefix
end self.i = old return
end
return {
Line = self:Line();
Type = "FunctionCall";
Target = prefix;
Method = method;
Arguments = args;
}
end
function Parser:Name()
local m,o = self:Peek("Match","[_%a][_%w]*")
if not m then return end
if Keywords[m] then
self.i = o return
end return m
end
function Parser:Namelist(vararg)
local name = self:Name()
local names = {name}
if name then
while self:String(",") do
if vararg and self:String("...") then
names[#names+1] = "..." break
end name = self:Name()
assert(name,"Expected a name")
names[#names+1] = name
end return names
end
end
function Parser:Parlist()
local namelist = self:Namelist(true)
if namelist then
if self:String(",") then
assert(self:String("..."),"Expected `...`")
namelist[#namelist+1] = "..." return namelist
end return namelist
elseif self:String("...") then