-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathparser.lua
More file actions
2795 lines (1909 loc) · 69.6 KB
/
parser.lua
File metadata and controls
2795 lines (1909 loc) · 69.6 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
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
--[[
____ _ _ ___ ___ ____ ___ ___ __ ____ _ _ _ ___ _ _ ____
F ___J FJ LJ F _ ", F _ ", F ___J F __". F __". FJ F __ ] F L L] /.\ F __". FJ L] F___ J
J |___: J \/ F J `-' |J `-'(| J |___: J (___| J (___| J L J |--| L J \| L //_\\ J |--\ LJ | | L `-__| L
| _____| / \ | __/F| _ L | _____| J\___ \ J\___ \ | | | | | | | |\ | / ___ \ | | J |J J F L |__ (
F L____: / /\ \ F |__/ F |_\ L F L____: .--___) \.--___) \ F J F L__J J F L\\ J / L___J \ F L__J |J\ \/ /F .-____] J
J________LJ__//\\__LJ__| J__| \\__LJ________LJ\______JJ\______JJ____LJ\______/FJ__L \\__L J__L J__LJ______/F \\__// J\______/F
|________||__/ \__||__L |__| J__||________| J______F J______F|____| J______F |__L J__| |__L J__||______F \__/ J______F
::Base Parser::
```````````````
A parser is the logical structure used to turn tokens into instructions that.
:::Syntax Grammar:::
```````````````````
I have based this off the one from E2.
:::Key:::
* ε is the end-of-file
* E? matches zero or one occurrences of T (and will always match one if possible)
* E* matches zero or more occurrences of T (and will always match as many as possible)
* E F matches E (and then whitespace) and then F
* E / F tries matching E, if it fails it matches F (from the start location)
* &E matches E, but does not consume any input.
* !E matches everything except E, and does not consume any input.
:::Root:::
Root ← Stmt1((";" / " ") Stmt1)* ε
:::Statements:::
Stmt1 ← ("try" Block "(" Var ")" Block)? Stmt2
Stmt2 ← ("if" Cond Block Stmt3)? Stmt5
Stmt3 ← ("elseif" Cond Block Stmt3)? Stmt4
Stmt4 ← ("else" Block)
Stmt5 ← (("for" "(" Type "=" Expr1 ")" Block) / ("while" Cond Block))? Stmt6
Stmt6 ← (("server" / "client") Block)? Stmt7
Stmt7 ← "global"? (type (Var("," Var)* "="? (Expr1? ("," Expr1)*)))? Stmt8
Stmt8 ← (type (Var("," Var)* ("=" / "+=" / "-=" / "/=" / "*=")? (Expr1? ("," Expr1)*)))? Stmt9
Stmt9 ← ("delegate" "(" (Type ((",")?)*)?) ")" ("{")? "return" Num ("}")?)? Stmt10
Stmt10 ← (("return" (Expr1 ((","")?)*)?) / "continue", "break")?
:::Expressions:::
Expr1 ← (Expr1 "?" Expr1 ":" Expr1)? Expr2
Expr2 ← (Expr3 "||" Expr3)? Expr3
Expr3 ← (Expr4 "&&" Expr4)? Expr4
Expr4 ← (Expr5 "^^" Expr5)? Expr5
Expr5 ← (Expr6 "|" Expr6)? Expr6
Expr6 ← (Expr7 "&" Expr7)? Expr7
Expr7 ← (Expr8 ("==" / "!=") (Values / Expr1))? Expr8
Expr8 ← (Epxr9 (">" / "<" / " >=" / "<=") Expr1)? Expr9
Expr9 ← (Epxr10 "<<" Expr10)? Expr10
Expr10 ← (Epxr11 ">>" Expr11)? Expr11
Expr11 ← (Epxr12 "+" Expr12)? Expr12
Expr12 ← (Epxr13 "-" Expr13)? Expr13
Expr13 ← (Epxr14 "/" Expr14)? Expr14
Expr14 ← (Epxr15 "*" Expr15)? Expr15
Expr15 ← (Epxr16 "^" Expr16)? Expr16
Expr16 ← (Epxr17 "%" Expr17)? Expr17
Expr17 ← (Epxr18 "instanceof" Type)? Expr18
Expr18 ← ("+" Expr24)? Exp19
Expr19 ← ("-" Expr24)? Exp20
Expr2 ← ("!" Expr24)? Expr21
Expr21 ← ("#" Expr24)? Expr22
Expr22 ← (("$" / "~") Var)? Expr23
Expr23 ← ("("type")" Expr1)? Expr24
Expr24 ← (Params2 "=>" Block1)? Expr25
Expr25 ← ("(" Expr1 ")" (Trailing)?)? Expr26
Expr26 ← (Library "." Function "(" (Expr1 ((",")?)*)?) ")")? Expr27
Expr27 ← (Var (Trailing)?)? Expr28
Expr28 ← ("new" Type "(" (Expr1 ((","")?)*)?) ")")? Expr29
Expr29 ← ("Function" Params2 Block1)? Expr30
Expr30 ← Expr31? Error
Expr31 ← (String / Number / "true" / "false", "void")?
:::Syntax:::
Cond ← "(" Expr1 ")"
Block ← "{" (Stmt1 ((";" / " ") Stmt1)*)? "}"
Values ← "[" Expr1 ("," Expr1)* "]"
Raw ← (Str / Num / Bool)? Table
Trailing ← (Method / Get /Call)?
Method ← (("." Method "(" (Expr1 ((","")?)*)?) ")")
Get ← ("[" Expr1 ("," Type)? "]")
Call ← ("(" (Expr1 ((","")?)*)?) ")")?
Params ← ("(" (Type Var (("," Type Var)*)?)? ")")
Params2 ← ( "{" Params "}" )? Params
]]
local function name(id)
local obj = EXPR_LIB.GetClass(id);
return obj and obj.name or id;
end
--[[
]]
local PARSER = {};
PARSER.__index = PARSER;
function PARSER.New()
return setmetatable({}, PARSER);
end
function PARSER.Initialize(this, instance, files)
this.__pos = 0;
this.__depth = 0;
this.__scopeID = 0;
this.__scope = {[0] = {classes = {}}};
this.__scopeData = this.__scope;
this.__instructions = {};
this.stmt_deph = 0;
this.__token = instance.tokens[0];
this.__next = instance.tokens[1];
this.__total = #instance.tokens;
this.__tokens = instance.tokens;
this.__script = instance.script;
--this.__tasks = {};
this.__directives = {};
this.__directives.inport = {};
this.__directives.outport = {};
this.__directives.includes = {};
this.__directives.synced = { };
this.__files = files;
end
--[[
]]
function PARSER.Yield(this)
end
function PARSER.Run(this)
--TODO: PcallX for stack traces on internal errors?
local status, result = pcall(this._Run, this);
if (status) then
return true, result;
end
if (type(result) == "table") then
return false, result;
end
local err = {};
err.state = "internal";
err.msg = result;
return false, err;
end
function PARSER._Run(this)
local result = {};
result.instruction = this:Root();
result.script = this.__script;
--result.tasks = this.__tasks
result.tokens = this.__tokens;
result.directives = this.__directives;
return result;
end
function PARSER.Throw(this, token, msg, fst, ...)
local err = {};
if (fst) then
msg = string.format(msg, fst, ...);
end
err.state = "parser";
err.char = token.char;
err.line = token.line;
err.msg = msg;
error(err,0);
end
--[[
]]
function PARSER.PushScope(this)
this.__scope = {};
this.__scope.classes = {};
this.__scopeID = this.__scopeID + 1;
this.__scopeData[this.__scopeID] = this.__scope;
end
function PARSER.PopScope(this)
this.__scopeData[this.__scopeID] = nil;
this.__scopeID = this.__scopeID - 1;
this.__scope = this.__scopeData[this.__scopeID];
end
function PARSER.SetOption(this, option, value, deep)
if (not deep) then
this.__scope[option] = value;
else
for i = this.__scopeID, 0, -1 do
local v = this.__scopeData[i][option];
if (v) then
this.__scopeData[i][option] = value;
break;
end
end
end
end
--[[
]]
function PARSER.GetOption(this, option, nonDeep)
if (this.__scope[option]) then
return this.__scope[option];
end
if (not nonDeep) then
for i = this.__scopeID, 0, -1 do
local v = this.__scopeData[i][option];
if (v) then
return v;
end
end
end
end
function PARSER.SetUserObject(this, name, scope)
if (not scope) then
scope = this.__scopeID;
end
if (not name) then debug.Trace() end
local class = {};
class.name = name;
class.scope = scope;
this.__scopeData[scope].classes[name] = class;
return scope, class;
end
function PARSER.GetUserObject(this, name, scope, nonDeep)
if (not scope) then
scope = this.__scopeID;
end
local v = this.__scopeData[scope].classes[name];
if (v) then
return v.scope, v;
end
if (not nonDeep) then
for i = scope, 0, -1 do
local v = this.__scopeData[i].classes[name];
if (v) then
return v.scope, v;
end
end
end
end
--[[
]]
function PARSER.Next(this)
this.__pos = this.__pos + 1;
this.__token = this.__tokens[this.__pos];
this.__next = this.__tokens[this.__pos + 1];
if (this.__pos > this.__total) then
return false;
end
return true;
end
function PARSER.HasTokens(this)
return this.__next ~= nil;
end
function PARSER.CheckCurrentToken(this, type, ...)
local tkn = this.__next;
if (tkn) then
for _, t in pairs({type, ...}) do
local tokenType = tkn.type;
if (tokenType == "var" and this:GetUserObject(tkn.data)) then
tokenType = "typ";
end
if (tokenType == t) then
return true;
end
end
end
return false;
end
function PARSER.CheckToken(this, type, ...)
if (this.__pos < this.__total) then
local tkn = this.__next;
for _, t in pairs({type, ...}) do
local tokenType = tkn.type;
if (tokenType == "var" and this:GetUserObject(tkn.data)) then
tokenType = "typ";
end
if (tokenType == t) then
return true;
end
end
end
return false;
end
function PARSER.Accept(this, type, ...)
if (this:CheckToken(type, ...)) then
--print("Accept(" .. type .. ", " .. this.__next.data .. ") -> ", this.cur_instruction and this.cur_instruction.type or "?")
this:Next();
return true;
end
return false;
end
function PARSER.AcceptWithData(this, type, data)
if (this:CheckToken(type) and this.__next.data == data) then
--print("AcceptWithData(" .. type .. ", " .. data .. ") -> ", this.cur_instruction and this.cur_instruction.type or "?")
this:Next();
return true;
end
return false;
end
function PARSER.GetTokenData(this)
return this.__token.data
end
function PARSER.GetToken(this, pos)
if (pos >= this.__total) then
return this.__tokens[pos];
end
end
function PARSER.OffsetToken(this, token, offset)
local pos = token.index + offset;
local token = this.__tokens[pos];
return token;
end
function PARSER.StepBackward(this, steps)
if (not steps) then
steps = 1;
end
local pos = this.__pos - (steps + 1);
if (pos > this.__total) then
pos = this.__total;
end
this.__pos = pos;
this:Next();
end
function PARSER.GotoToken(this, token, offset)
if (not offset) then
offset = 0;
end
local pos = token.index - (offset + 1);
if (pos == 0) then
this.__pos = 0;
this.__token = this.__tokens[0];
this.__next = this.__tokens[1];
return;
end
if (pos > this.__total) then
pos = this.__total;
end
this.__pos = pos;
this:Next();
end
function PARSER.CheckForSequence(this, type, ...)
local tkn = this.__token;
if (not tkn) then
return false;
end
if (not this:Accept(type)) then
return false;
end
local res = true;
local types = {...};
for i = 1, #types do
res = this:Accept(types[i]);
if (not res) then
break;
end
end
this:GotoToken(tkn);
return res;
end
function PARSER.GetFirstTokenOnLine(this)
for i = this.__pos, 1, -1 do
local tkn = this.__tokens[i];
if (tkn.newLine) then
return tkn;
end
end
return this.__tokens[1];
end
function PARSER.StatmentContains(this, token, type)
local i = this.__pos;
while (i < this.__total) do
local tkn = this.__tokens[i];
if (not tkn) then
return;
end
if (tkn.type == "sep" or tkn.line ~= token.line) then
return;
end
if (tkn.type == type) then
return tkn;
end
i = i + 1;
end
end
function PARSER.LastInStatment(this, token, type, endType)
local last;
local i = token.index;
while (i <= this.__total) do
local tkn = this.__tokens[i];
if (not tkn) then
break;
end
if (tkn.type == "sep" or tkn.line ~= token.line) then
break;
end
if (tkn.type == type) then
last = tkn;
elseif (endType and tkn.type == endType) then
break;
end
i = i + 1;
end
return last;
end
--[[
]]
function PARSER.RequireAt( this, token, type, msg, ... )
if (not this:Accept(type)) then
msg = string.gsub(msg, "$GOT", this.__next and (this.__next.type or "nothing") or "nothing");
this:Throw( token or this.__token, msg, ... );
end; return this.__token;
end
function PARSER.Require( this, type, msg, ... )
return this:RequireAt( nil, type, msg, ... );
end
function PARSER.Exclude( this, tpye, msg, ... )
if (this:Accept(type)) then
this:Throw( this.__token, msg, ... )
end
end
function PARSER.ExcludeWhiteSpace(this, msg, ...)
if (not this:HasTokens()) then
this:Throw( this.__token, msg, ... )
end
end
--[[
]]
function PARSER.StartInstruction(this, _type, token, perfhandler)
if (not istable(token)) then
debug.Trace();
error("PARSER:StartInstruction got bad token type " .. tostring(token));
elseif (type(_type) ~= "string") then
debug.Trace();
error("PARSER:StartInstruction got bad instruction type " .. tostring(_type));
elseif (type(token) ~= "table") then
debug.Trace();
error("PARSER:StartInstruction got bad instruction token " .. tostring(token));
end
local inst = {};
inst.type = _type;
inst.perfhandler = perfhandler;
inst.rCount = 0;
inst.result = "void";
inst.token = token;
inst.start = token;
inst.char = token.char;
inst.line = token.line;
--inst.tokens = {all = {}};
--inst.instructions = {all = {}};
inst.parent = this.cur_instruction;
inst.depth = this.__depth;
inst.scope = this.__scope;
this.cur_instruction = inst;
this.__depth = this.__depth + 1;
this.stmt_deph = this.stmt_deph + 1;
return inst;
end
function PARSER.SetEndResults(this, inst, type, count)
inst.result = type;
inst.rCount = count or 1;
end
function PARSER.EndInstruction(this, inst, data)
inst.data = data or {};
inst.final = this.__token;
this.cur_instruction = inst.parent;
this.__depth = this.__depth - 1;
this.stmt_deph = this.stmt_deph + 1;
return inst;
end
--[[
]]
function PARSER.Root(this)
local seq = this:StartInstruction("root", this.__tokens[1], true);
local stmts = this:Statements(false);
return this:EndInstruction(seq, {stmts = stmts});
end
function PARSER.Block_1(this, _end, lcb, returnable)
this:ExcludeWhiteSpace( "Further input required at end of code, incomplete statement" )
if (this:Accept("lcb")) then
local stmts;
local seq = this:StartInstruction("seq", this.__token, true);
if (not this:CheckToken("rcb")) then
this:PushScope();
stmts = this:Statements(true);
this:PopScope();
end
if (not this:Accept("rcb")) then
this:Throw(this.__token, "Right curly bracket (}) missing, to close block got " .. this.__token.type);
end
return this:EndInstruction(seq, {stmts = stmts});
end
do
local seq = this:StartInstruction("seq", this.__next);
this:PushScope()
local stmt = this:Statment_1();
this:Accept("sep");
this:PopScope()
return this:EndInstruction(seq, {stmts = {stmt}});
end
end
function PARSER.Block_2(this)
this:ExcludeWhiteSpace( "Further input required at end of code, incomplete statement" )
this:Require("lcb", "Left curly bracket ({) missing for constructor");
local stmts;
local seq = this:StartInstruction("seq", this.__token, true);
if (not this:CheckToken("rcb")) then
this:PushScope();
stmts = this:Statements(true, this.ConstructorStatment);
this:PopScope();
end
if (not this:Accept("rcb")) then
this:Throw(this.__token, "Right curly bracket (}) missing, to close constructor got " .. this.__token.type);
end
return this:EndInstruction(seq, {stmts = stmts});
end
function PARSER.ConstructorStatment(this, stmtc)
if (this:Accept("sup")) then
if (stmtc > 0) then
this:Throw(this.__token, "Super constructor can not appear here.");
end
local seq = this:StartInstruction("supconst", this.__token, true);
this:Require("lpa", "Left parenthesis (( ) expected to open super constructor parameters.")
local expressions = {};
if (not this:CheckToken("rpa")) then
expressions[1] = this:Expression_1();
while(this:Accept("com")) do
this:Exclude("rpa", "Expression or value expected after comma (,).");
local expr = this:Expression_1( )
if expr then table.insert( expressions, expr ) end
end
end
this:Require("rpa", "Right parenthesis ( )) expected to close super constructor parameters.");
return this:EndInstruction(seq, {expressions = expressions});
end
return this:Statment_0();
end
--[[
]]
function PARSER.Directive_SERVER(this, token, directive)
if (this.FirstStatment) then
this:Throw(token, "Directive @server must appear towards the top of your code");
elseif (this.__directives.client) then
this:Throw(token, "Directive @server conflicts with directive @client.");
elseif (this.__directives.server) then
this:Throw(token, "Directive @server must not appear twice.");
end
this.__directives.server = true;
end
function PARSER.Directive_CLIENT(this, token, directive)
if (this.FirstStatment) then
this:Throw(token, "Directive @client must appear towards the top of your code");
elseif (this.__directives.server) then
this:Throw(token, "Directive @client conflicts with directive @server.");
elseif (this.__directives.client) then
this:Throw(token, "Directive @client must not appear twice.");
end
this.__directives.client = true;
end
function PARSER.Directive_NAME(this, token, directive)
this:Require("str", "String expected to follow directive @name");
if (this.FirstStatment) then
this:Throw(token, "Directive @name must appear towards the top of your code");
elseif (this.__directives.name) then
this:Throw(token, "Directive @name must not appear twice.");
end
this.__directives.name = this.__token.data;
end
function PARSER.Directive_MODEL(this, token, directive)
this:Require("str", "String expected to follow directive @model");
if (this.FirstStatment) then
this:Throw(token, "Directive @model must appear towards the top of your code");
elseif (this.__directives.model) then
this:Throw(token, "Directive @model must not appear twice.");
end
this.__directives.model = this.__token.data;
end
function PARSER.Directive_INCLUDE(this, token, directive)
this:Require("str", "String expected to follow directive @include");
local inst = this:StartInstruction("include", token);
if (this.FirstStatment) then
this:Throw(token, "Directive @include must appear towards the top of your code");
end
local includes = this.__directives.includes or {};
local file_path = string.sub(this.__token.data, 2, -2);
local exists;
if (CLIENT) then
exists = file.Exists("golem/" .. file_path .. ".txt", "DATA");
elseif (SERVER) then
exists = this.__files[file_path] ~= nil;
end
if (not exists) then
this:Throw(token, "No sutch file %s", file_path);
end
if (includes[file_path]) then
this:Throw(token, "File %s, allready included.", file_path);
end
includes[file_path] = file_path;
return this:EndInstruction(inst, {file = file_path});
end
function PARSER.Directive_INPUT(this, token, directive)
this:Require("typ", "Class expected for inport type, after @input");
local inst = this:StartInstruction("inport", token);
local port_type = this.__token.data;
local class_obj = EXPR_LIB.GetClass(port_type);
if (not class_obj.wire_in_class) then
this:Throw(token, "Invalid wire port, class %s can not be used for wired input.", class_obj.name);
end
local variables = {};
this:Require("var", "Variable('s) expected after class for inport name.");
variables[1] = this.__token;
while (this:Accept("com")) do
this:Require("var", "Variable expected after comma (,).");
table.insert( variables, this.__token );
end
return this:EndInstruction(inst, {class = class_obj.id, variables = variables, wire_type = class_obj.wire_in_class, wire_func = class_obj.wire_in_func, sync_sv = class_obj.wire_sync_sv, sync_cl = class_obj.wire_sync_cl});
end
function PARSER.Directive_OUTPUT(this, token, directive)
this:Require("typ", "Class expected for outport type, after @output");
local inst = this:StartInstruction("outport", token);
local port_class = this.__token.data;
local class_obj = EXPR_LIB.GetClass(port_class);
if (not class_obj.wire_out_class) then
this:Throw(token, "Invalid wire port, class %s can not be used for wired output.", class_obj.name);
end
local variables = {};
this:Require("var", "Variable('s) expected after class for output name.");
variables[1] = this.__token;
while (this:Accept("com")) do
this:Require("var", "Variable expected after comma (,).");
table.insert( variables, this.__token );
end
return this:EndInstruction(inst, {class = class_obj.id, variables = variables, wire_type = class_obj.wire_out_class, wire_func = class_obj.wire_out_func, wire_func2 = class_obj.wire_out_func, sync_sv = class_obj.wire_sync_sv, sync_cl = class_obj.wire_sync_cl});
end
function PARSER.Directive_SYNCED(this, token, directive)
this:Require("typ", "Class expected for synced variable type, after @synced");
local inst = this:StartInstruction("synced", token);
local class_type = this.__token.data;
local class_obj = EXPR_LIB.GetClass(class_type);
if (not class_obj.wire_sync_sv or not class_obj.wire_sync_cl) then
this:Throw(token, "Synced Variables cant not be of type %s.", name(class_obj.id));
end
local variables = {};
this:Require("var", "Variable('s) expected after class for synced variable name.");
variables[1] = this.__token;
while (this:Accept("com")) do
this:Require("var", "Variable expected after comma (,).");
table.insert( variables, this.__token );
end
return this:EndInstruction(inst, {class = class_obj.id, variables = variables, sync_sv = class_obj.wire_sync_sv, sync_cl = class_obj.wire_sync_cl});
end
--[[
]]
function PARSER.Statements(this, block, call)
local sep = false;
local stmts = {};
call = call or this.Statment_0;
while true do
local stmt = call(this, #stmts);
table.insert( stmts, stmt )
local seperated = this:Accept("sep");
if (not stmt) then
break;
end
if (block and this:CheckToken("rcb")) then
break;
end
if (not this:HasTokens()) then
break;
end
local pre = stmts[#stmts - 1];
if (pre) and (pre.line == stmt.line and not sep) then
this:Throw(stmt.token, "Statements must be separated by semicolon (;) or newline")
end
if (stmt.type == "return") then
this:Throw(stmt.final, "Statement can not appear after return.")
elseif (stmt.type == "continue") then
this:Throw(stmt.final, "Statement can not appear after continue.")
elseif (stmt.type == "break") then
this:Throw(stmt.final, "Statement can not appear after break.")
end
sep = seperated;
end
return stmts;
end
--[[
]]
function PARSER.Statment_0(this)
local sep;
local dirLine;
while this:Accept("dir") do
local token = this.__token;
dirLine = this.__token.line;
if (not this:Accept("var", "sv", "cl")) then
this:Throw(token, "Directive name exspected after @");
end
local directive = this.__token.data;
local func = this["Directive_" .. string.upper(directive)]
if (not func) then
this:Throw(token, "No such directive @%s", directive);
end
local instr = func(this, token, directive);
sep = this:Accept("sep");
if (instr) then
return instr
end
if (!this:HasTokens()) then
return;
end
end
if (this:CheckToken("cls")) then
return this:ClassStatment_0();
end
if (this:CheckToken("itf")) then
return this:InterfaceStatment_0();
end
local stmt = this:Statment_1();
if (dirLine and (not sep or direLine == stmt.line)) then
this:Throw(stmt.token, "Statements must be separated by semicolon (;) or newline")
end
return stmt;
end;
function PARSER.Statment_1(this)
this:Yield();
if (this:Accept("try")) then
local inst = this:StartInstruction("try", this.__token);
local block1 = this:Block_1(true, "function()");
this:Require("cth", "Catch expected after try statment, for try catch");
this:Require("lpa", "Left parenthesis (( ) expected after catch.");
local var = this:Require("var", "Variable expected for error object, catch(variable)");
this:Require("rpa", "Right parenthesis ( )) expected to end catch.");
local block2 = this:Block_1(false, "then");
return this:EndInstruction(inst, {block1 = block1; var = var; block2 = block2});
end
return this:Statment_2();
end
function PARSER.Statment_2(this)
if (this:Accept("if")) then
local inst = this:StartInstruction("if", this.__token);
local condition = this:GetCondition();
local block = this:Block_1(false, "then");
local eif = { this:Statment_3() };