forked from fusionlanguage/fut
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CiParser.cs
934 lines (862 loc) · 25.9 KB
/
CiParser.cs
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
// CiParser.cs - Ci parser
//
// Copyright (C) 2011-2022 Piotr Fusik
//
// This file is part of CiTo, see https://github.com/pfusik/cito
//
// CiTo is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// CiTo is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with CiTo. If not, see http://www.gnu.org/licenses/
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text;
namespace Foxoft.Ci
{
public class CiParser : CiLexer
{
public CiProgram Program;
CiLoop CurrentLoop;
CiCondCompletionStatement CurrentLoopOrSwitch;
string XcrementParent = null;
CiException StatementException(CiStatement statement, string message)
{
return new CiException(this.Filename, statement.Line, message);
}
CiException StatementException(CiStatement statement, string format, params object[] args)
{
return StatementException(statement, string.Format(format, args));
}
string ParseId()
{
string id = this.StringValue;
Expect(CiToken.Id);
return id;
}
CiCodeDoc ParseDoc() => See(CiToken.DocComment) ? new CiDocParser(this).ParseCodeDoc() : null;
CiExpr ParseSymbolReference(CiExpr left) => new CiSymbolReference { Line = this.Line, Left = left, Name = ParseId() };
CiExpr[] ParseCollection(CiToken closing)
{
List<CiExpr> items = new List<CiExpr>();
if (!See(closing)) {
do
items.Add(ParseExpr());
while (Eat(CiToken.Comma));
}
Expect(closing);
return items.ToArray();
}
CiExpr ParseConstInitializer()
{
if (Eat(CiToken.LeftBrace))
return new CiAggregateInitializer { Line = this.Line, Items = ParseCollection(CiToken.RightBrace) };
return ParseExpr();
}
void CheckXcrementParent()
{
if (this.XcrementParent != null) {
string op = this.CurrentToken == CiToken.Increment ? "++" : "--";
throw ParseException($"{op} not allowed on the right side of {this.XcrementParent}");
}
}
bool SeeDigit()
{
int c = PeekChar();
return c >= '0' && c <= '9';
}
CiInterpolatedString ParseInterpolatedString()
{
int line = this.Line;
List<CiInterpolatedPart> parts = new List<CiInterpolatedPart>();
do {
string prefix = this.StringValue;
NextToken();
CiExpr arg = ParseExpr();
CiExpr width = null;
char format = ' ';
int precision = -1;
if (Eat(CiToken.Comma))
width = ParseExpr();
if (See(CiToken.Colon)) {
format = (char) ReadChar();
if ("DdEeFfGgXx".IndexOf(format) < 0)
throw ParseException("Invalid format specifier");
if (SeeDigit()) {
precision = ReadChar() - '0';
if (SeeDigit())
precision = precision * 10 + ReadChar() - '0';
}
NextToken();
}
parts.Add(new CiInterpolatedPart(prefix, arg) { WidthExpr = width, Format = format, Precision = precision });
Check(CiToken.RightBrace);
} while (ReadInterpolatedString() == CiToken.InterpolatedString);
string suffix = this.StringValue;
NextToken();
return new CiInterpolatedString(parts.ToArray(), suffix) { Line = line };
}
CiExpr ParseParenthesized()
{
Expect(CiToken.LeftParenthesis);
CiExpr result = ParseExpr();
Expect(CiToken.RightParenthesis);
return result;
}
CiExpr ParsePrimaryExpr()
{
CiExpr result;
switch (this.CurrentToken) {
case CiToken.Increment:
case CiToken.Decrement:
CheckXcrementParent();
goto case CiToken.Minus;
case CiToken.Minus:
case CiToken.Tilde:
case CiToken.ExclamationMark:
return new CiPrefixExpr { Line = this.Line, Op = NextToken(), Inner = ParsePrimaryExpr() };
case CiToken.New:
return new CiPrefixExpr { Line = this.Line, Op = NextToken(), Inner = ParseType() };
case CiToken.LiteralLong:
result = new CiLiteralLong(this.LongValue) { Line = this.Line };
NextToken();
break;
case CiToken.LiteralDouble:
result = new CiLiteralDouble(this.DoubleValue) { Line = this.Line };
NextToken();
break;
case CiToken.LiteralString:
result = new CiLiteralString(this.StringValue) { Line = this.Line };
NextToken();
break;
case CiToken.False:
result = new CiLiteralFalse { Line = this.Line };
NextToken();
break;
case CiToken.True:
result = new CiLiteralTrue { Line = this.Line };
NextToken();
break;
case CiToken.Null:
result = new CiLiteralNull { Line = this.Line };
NextToken();
break;
case CiToken.InterpolatedString:
result = ParseInterpolatedString();
break;
case CiToken.LeftParenthesis:
result = ParseParenthesized();
break;
case CiToken.Id:
result = ParseSymbolReference(null);
break;
case CiToken.Resource:
NextToken();
Expect(CiToken.Less);
if (ParseId() != "byte")
throw ParseException("Expected resource<byte[]>");
Expect(CiToken.LeftBracket);
Expect(CiToken.RightBracket);
Expect(CiToken.Greater);
result = new CiPrefixExpr { Line = this.Line, Op = CiToken.Resource, Inner = ParseParenthesized() };
break;
default:
throw ParseException("Invalid expression");
}
for (;;) {
switch (this.CurrentToken) {
case CiToken.Dot:
NextToken();
result = ParseSymbolReference(result);
break;
case CiToken.LeftParenthesis:
if (!(result is CiSymbolReference symbol))
throw ParseException("Expected a method");
NextToken();
result = new CiCallExpr { Line = this.Line, Method = symbol, Arguments = ParseCollection(CiToken.RightParenthesis) };
break;
case CiToken.LeftBracket:
result = new CiBinaryExpr { Line = this.Line, Left = result, Op = NextToken(), Right = See(CiToken.RightBracket) ? null : ParseExpr() };
Expect(CiToken.RightBracket);
break;
case CiToken.Increment:
case CiToken.Decrement:
CheckXcrementParent();
goto case CiToken.ExclamationMark;
case CiToken.ExclamationMark:
case CiToken.Hash:
result = new CiPostfixExpr { Line = this.Line, Inner = result, Op = NextToken() };
break;
default:
return result;
}
}
}
CiExpr ParseMulExpr()
{
CiExpr left = ParsePrimaryExpr();
for (;;) {
switch (this.CurrentToken) {
case CiToken.Asterisk:
case CiToken.Slash:
case CiToken.Mod:
left = new CiBinaryExpr { Line = this.Line, Left = left, Op = NextToken(), Right = ParsePrimaryExpr() };
break;
default:
return left;
}
}
}
CiExpr ParseAddExpr()
{
CiExpr left = ParseMulExpr();
while (See(CiToken.Plus) || See(CiToken.Minus))
left = new CiBinaryExpr { Line = this.Line, Left = left, Op = NextToken(), Right = ParseMulExpr() };
return left;
}
CiExpr ParseShiftExpr()
{
CiExpr left = ParseAddExpr();
while (See(CiToken.ShiftLeft) || See(CiToken.ShiftRight))
left = new CiBinaryExpr { Line = this.Line, Left = left, Op = NextToken(), Right = ParseAddExpr() };
return left;
}
CiExpr ParseRelExpr()
{
CiExpr left = ParseShiftExpr();
for (;;) {
switch (this.CurrentToken) {
case CiToken.Less:
case CiToken.LessOrEqual:
case CiToken.Greater:
case CiToken.GreaterOrEqual:
left = new CiBinaryExpr { Line = this.Line, Left = left, Op = NextToken(), Right = ParseShiftExpr() };
break;
case CiToken.Is:
CiBinaryExpr isExpr = new CiBinaryExpr { Line = this.Line, Left = left, Op = NextToken(), Right = ParsePrimaryExpr() };
if (See(CiToken.Id))
isExpr.Right = new CiVar { Line = this.Line, TypeExpr = isExpr.Right, Name = ParseId() };
return isExpr;
default:
return left;
}
}
}
CiExpr ParseEqualityExpr()
{
CiExpr left = ParseRelExpr();
while (See(CiToken.Equal) || See(CiToken.NotEqual))
left = new CiBinaryExpr { Line = this.Line, Left = left, Op = NextToken(), Right = ParseRelExpr() };
return left;
}
CiExpr ParseAndExpr()
{
CiExpr left = ParseEqualityExpr();
while (See(CiToken.And))
left = new CiBinaryExpr { Line = this.Line, Left = left, Op = NextToken(), Right = ParseEqualityExpr() };
return left;
}
CiExpr ParseXorExpr()
{
CiExpr left = ParseAndExpr();
while (See(CiToken.Xor))
left = new CiBinaryExpr { Line = this.Line, Left = left, Op = NextToken(), Right = ParseAndExpr() };
return left;
}
CiExpr ParseOrExpr()
{
CiExpr left = ParseXorExpr();
while (See(CiToken.Or))
left = new CiBinaryExpr { Line = this.Line, Left = left, Op = NextToken(), Right = ParseXorExpr() };
return left;
}
CiExpr ParseCondAndExpr()
{
CiExpr left = ParseOrExpr();
while (See(CiToken.CondAnd)) {
string saveXcrementParent = this.XcrementParent;
this.XcrementParent = "&&";
left = new CiBinaryExpr { Line = this.Line, Left = left, Op = NextToken(), Right = ParseOrExpr() };
this.XcrementParent = saveXcrementParent;
}
return left;
}
CiExpr ParseCondOrExpr()
{
CiExpr left = ParseCondAndExpr();
while (See(CiToken.CondOr)) {
string saveXcrementParent = this.XcrementParent;
this.XcrementParent = "||";
left = new CiBinaryExpr { Line = this.Line, Left = left, Op = NextToken(), Right = ParseCondAndExpr() };
this.XcrementParent = saveXcrementParent;
}
return left;
}
CiExpr ParseExpr()
{
CiExpr left = ParseCondOrExpr();
if (See(CiToken.QuestionMark)) {
CiSelectExpr result = new CiSelectExpr { Line = this.Line, Cond = left };
NextToken();
string saveXcrementParent = this.XcrementParent;
this.XcrementParent = "?";
result.OnTrue = ParseExpr();
Expect(CiToken.Colon);
result.OnFalse = ParseExpr();
this.XcrementParent = saveXcrementParent;
return result;
}
return left;
}
CiExpr ParseType()
{
CiExpr left = ParsePrimaryExpr();
if (Eat(CiToken.Range))
return new CiBinaryExpr { Line = this.Line, Left = left, Op = CiToken.Range, Right = ParsePrimaryExpr() };
if (left is CiSymbolReference symbol && Eat(CiToken.Less)) {
CiSymbol klass = CiSystem.Value.TryLookup(symbol.Name);
if (klass == null)
throw StatementException(symbol, "{0} not found", symbol.Name);
int line = this.Line;
bool saveTypeArg = this.ParsingTypeArg;
this.ParsingTypeArg = true;
CiExpr typeArg = ParseType();
if (Eat(CiToken.Comma)) {
CiExpr valueType = ParseType();
if (klass != CiSystem.DictionaryClass && klass != CiSystem.SortedDictionaryClass)
throw StatementException(symbol, "{0} is not a generic class with two type parameters", symbol.Name);
left = new CiSymbolReference { Line = line, Left = new CiAggregateInitializer { Items = new CiExpr[] { typeArg, valueType } }, Symbol = klass };
}
else if (klass != CiSystem.ListClass && klass != CiSystem.StackClass && klass != CiSystem.HashSetClass)
throw StatementException(symbol, "{0} is not a generic class with one type parameter", symbol.Name);
else
left = new CiSymbolReference { Line = line, Left = typeArg, Symbol = klass };
Expect(CiToken.RightAngle);
this.ParsingTypeArg = saveTypeArg;
if (Eat(CiToken.LeftParenthesis)) {
Expect(CiToken.RightParenthesis);
left = new CiCallExpr { Line = this.Line, Method = (CiSymbolReference) left, Arguments = Array.Empty<CiExpr>() };
}
}
return left;
}
CiExpr ParseAssign(bool allowVar)
{
CiExpr left = allowVar ? ParseType() : ParseExpr();
switch (this.CurrentToken) {
case CiToken.Assign:
case CiToken.AddAssign:
case CiToken.SubAssign:
case CiToken.MulAssign:
case CiToken.DivAssign:
case CiToken.ModAssign:
case CiToken.AndAssign:
case CiToken.OrAssign:
case CiToken.XorAssign:
case CiToken.ShiftLeftAssign:
case CiToken.ShiftRightAssign:
return new CiBinaryExpr { Line = this.Line, Left = left, Op = NextToken(), Right = ParseAssign(false) };
case CiToken.Id:
if (allowVar)
return ParseVar(left);
return left;
default:
return left;
}
}
CiExpr ParseInitializer()
{
if (!Eat(CiToken.Assign))
return null;
if (Eat(CiToken.LeftBrace)) {
int startLine = this.Line;
List<CiExpr> fields = new List<CiExpr>();
do {
int line = this.Line;
CiExpr field = ParseSymbolReference(null);
Expect(CiToken.Assign);
fields.Add(new CiBinaryExpr { Line = line, Left = field, Op = CiToken.Assign, Right = ParseExpr() });
} while (Eat(CiToken.Comma));
Expect(CiToken.RightBrace);
return new CiAggregateInitializer { Line = startLine, Items = fields.ToArray() };
}
return ParseExpr();
}
CiVar ParseVar(CiExpr type) => new CiVar { Line = this.Line, TypeExpr = type, Name = ParseId(), Value = ParseInitializer() };
CiVar ParseVar() => ParseVar(ParseType());
CiConst ParseConst()
{
Expect(CiToken.Const);
CiConst konst = new CiConst { Line = this.Line, TypeExpr = ParseType(), Name = ParseId() };
Expect(CiToken.Assign);
konst.Value = ParseConstInitializer();
Expect(CiToken.Semicolon);
return konst;
}
CiBlock ParseBlock()
{
int line = this.Line;
Expect(CiToken.LeftBrace);
List<CiStatement> statements = new List<CiStatement>();
while (!Eat(CiToken.RightBrace))
statements.Add(ParseStatement());
return new CiBlock { Line = line, Statements = statements.ToArray() };
}
CiAssert ParseAssert()
{
CiAssert result = new CiAssert { Line = this.Line };
Expect(CiToken.Assert);
result.Cond = ParseExpr();
if (Eat(CiToken.Comma))
result.Message = ParseExpr();
Expect(CiToken.Semicolon);
return result;
}
CiBreak ParseBreak()
{
if (this.CurrentLoopOrSwitch == null)
throw ParseException("break outside loop or switch");
CiBreak result = new CiBreak(this.CurrentLoopOrSwitch) { Line = this.Line };
Expect(CiToken.Break);
Expect(CiToken.Semicolon);
if (this.CurrentLoopOrSwitch is CiLoop loop)
loop.HasBreak = true;
return result;
}
CiContinue ParseContinue()
{
if (this.CurrentLoop == null)
throw ParseException("continue outside loop");
CiContinue result = new CiContinue(this.CurrentLoop) { Line = this.Line };
Expect(CiToken.Continue);
Expect(CiToken.Semicolon);
return result;
}
void ParseLoopBody(CiLoop loop)
{
CiLoop outerLoop = this.CurrentLoop;
CiCondCompletionStatement outerLoopOrSwitch = this.CurrentLoopOrSwitch;
this.CurrentLoopOrSwitch = this.CurrentLoop = loop;
loop.Body = ParseStatement();
this.CurrentLoopOrSwitch = outerLoopOrSwitch;
this.CurrentLoop = outerLoop;
}
CiDoWhile ParseDoWhile()
{
CiDoWhile result = new CiDoWhile { Line = this.Line };
Expect(CiToken.Do);
ParseLoopBody(result);
Expect(CiToken.While);
result.Cond = ParseParenthesized();
Expect(CiToken.Semicolon);
return result;
}
CiFor ParseFor()
{
CiFor result = new CiFor { Line = this.Line };
Expect(CiToken.For);
Expect(CiToken.LeftParenthesis);
if (!See(CiToken.Semicolon))
result.Init = ParseAssign(true);
Expect(CiToken.Semicolon);
if (!See(CiToken.Semicolon))
result.Cond = ParseExpr();
Expect(CiToken.Semicolon);
if (!See(CiToken.RightParenthesis))
result.Advance = ParseAssign(false);
Expect(CiToken.RightParenthesis);
ParseLoopBody(result);
return result;
}
void ParseForeachIterator(CiForeach result)
{
result.Add(new CiVar { Line = this.Line, TypeExpr = ParseType(), Name = ParseId() });
}
CiForeach ParseForeach()
{
CiForeach result = new CiForeach { Line = this.Line };
Expect(CiToken.Foreach);
Expect(CiToken.LeftParenthesis);
if (Eat(CiToken.LeftParenthesis)) {
ParseForeachIterator(result);
Expect(CiToken.Comma);
ParseForeachIterator(result);
Expect(CiToken.RightParenthesis);
}
else
ParseForeachIterator(result);
Expect(CiToken.In);
result.Collection = ParseExpr();
Expect(CiToken.RightParenthesis);
ParseLoopBody(result);
return result;
}
CiIf ParseIf()
{
CiIf result = new CiIf { Line = this.Line };
Expect(CiToken.If);
result.Cond = ParseParenthesized();
result.OnTrue = ParseStatement();
if (Eat(CiToken.Else))
result.OnFalse = ParseStatement();
return result;
}
CiLock ParseLock()
{
CiLock result = new CiLock { Line = this.Line };
Expect(CiToken.Lock);
result.Lock = ParseParenthesized();
result.Body = ParseStatement();
return result;
}
CiNative ParseNative()
{
int line = this.Line;
Expect(CiToken.Native);
StringBuilder sb = new StringBuilder();
this.CopyTo = sb;
try {
Expect(CiToken.LeftBrace);
int nesting = 1;
for (;;) {
if (See(CiToken.EndOfFile))
throw ParseException("Native block not terminated");
if (See(CiToken.LeftBrace))
nesting++;
else if (See(CiToken.RightBrace) && --nesting == 0)
break;
NextToken();
}
}
finally {
this.CopyTo = null;
}
NextToken();
Trace.Assert(sb[sb.Length - 1] == '}');
sb.Length--;
return new CiNative { Line = line, Content = sb.ToString() };
}
CiReturn ParseReturn()
{
CiReturn result = new CiReturn { Line = this.Line };
NextToken();
if (!See(CiToken.Semicolon))
result.Value = ParseExpr();
Expect(CiToken.Semicolon);
return result;
}
CiSwitch ParseSwitch()
{
CiSwitch result = new CiSwitch { Line = this.Line };
Expect(CiToken.Switch);
result.Value = ParseParenthesized();
Expect(CiToken.LeftBrace);
CiCondCompletionStatement outerLoopOrSwitch = this.CurrentLoopOrSwitch;
this.CurrentLoopOrSwitch = result;
List<CiCase> cases = new List<CiCase>();
while (Eat(CiToken.Case)) {
List<CiExpr> values = new List<CiExpr>();
CiExpr value;
do {
value = ParseExpr();
values.Add(value);
Expect(CiToken.Colon);
} while (Eat(CiToken.Case));
if (See(CiToken.Default))
throw StatementException(value, "Please remove case before default");
CiCase kase = new CiCase { Values = values.ToArray() };
List<CiStatement> statements = new List<CiStatement>();
for (;;) {
statements.Add(ParseStatement());
switch (this.CurrentToken) {
case CiToken.Case:
case CiToken.Default:
case CiToken.RightBrace:
break;
default:
continue;
}
break;
}
kase.Body = statements.ToArray();
cases.Add(kase);
}
if (cases.Count == 0)
throw ParseException("Switch with no cases");
result.Cases = cases.ToArray();
if (Eat(CiToken.Default)) {
Expect(CiToken.Colon);
List<CiStatement> statements = new List<CiStatement>();
do
statements.Add(ParseStatement());
while (!See(CiToken.RightBrace));
result.DefaultBody = statements.ToArray();
}
Expect(CiToken.RightBrace);
this.CurrentLoopOrSwitch = outerLoopOrSwitch;
return result;
}
CiThrow ParseThrow()
{
CiThrow result = new CiThrow { Line = this.Line };
Expect(CiToken.Throw);
result.Message = ParseExpr();
Expect(CiToken.Semicolon);
return result;
}
CiWhile ParseWhile()
{
CiWhile result = new CiWhile { Line = this.Line };
Expect(CiToken.While);
result.Cond = ParseParenthesized();
ParseLoopBody(result);
return result;
}
CiStatement ParseStatement()
{
switch (this.CurrentToken) {
case CiToken.LeftBrace:
return ParseBlock();
case CiToken.Assert:
return ParseAssert();
case CiToken.Break:
return ParseBreak();
case CiToken.Const:
return ParseConst();
case CiToken.Continue:
return ParseContinue();
case CiToken.Do:
return ParseDoWhile();
case CiToken.For:
return ParseFor();
case CiToken.Foreach:
return ParseForeach();
case CiToken.If:
return ParseIf();
case CiToken.Lock:
return ParseLock();
case CiToken.Native:
return ParseNative();
case CiToken.Return:
return ParseReturn();
case CiToken.Switch:
return ParseSwitch();
case CiToken.Throw:
return ParseThrow();
case CiToken.While:
return ParseWhile();
default:
CiExpr expr = ParseAssign(true);
Expect(CiToken.Semicolon);
return expr;
}
}
CiCallType ParseCallType()
{
switch (this.CurrentToken) {
case CiToken.Static:
NextToken();
return CiCallType.Static;
case CiToken.Abstract:
NextToken();
return CiCallType.Abstract;
case CiToken.Virtual:
NextToken();
return CiCallType.Virtual;
case CiToken.Override:
NextToken();
return CiCallType.Override;
case CiToken.Sealed:
NextToken();
return CiCallType.Sealed;
default:
return CiCallType.Normal;
}
}
void ParseMethod(CiMethod method)
{
method.IsMutator = Eat(CiToken.ExclamationMark);
Expect(CiToken.LeftParenthesis);
if (!See(CiToken.RightParenthesis)) {
do {
CiCodeDoc doc = ParseDoc();
CiVar param = ParseVar();
param.Documentation = doc;
method.Parameters.Add(param);
} while (Eat(CiToken.Comma));
}
Expect(CiToken.RightParenthesis);
method.Throws = Eat(CiToken.Throws);
if (method.CallType == CiCallType.Abstract)
Expect(CiToken.Semicolon);
else if (See(CiToken.FatArrow))
method.Body = ParseReturn();
else
method.Body = ParseBlock();
}
public CiClass ParseClass(CiCallType callType)
{
Expect(CiToken.Class);
CiClass klass = new CiClass { Parent = this.Program, Filename = this.Filename, Line = this.Line, CallType = callType, Name = ParseId() };
if (Eat(CiToken.Colon))
klass.BaseClassName = ParseId();
Expect(CiToken.LeftBrace);
List<CiConst> consts = new List<CiConst>();
List<CiField> fields = new List<CiField>();
List<CiMethod> methods = new List<CiMethod>();
while (!Eat(CiToken.RightBrace)) {
CiCodeDoc doc = ParseDoc();
CiVisibility visibility;
switch (this.CurrentToken) {
case CiToken.Internal:
visibility = CiVisibility.Internal;
NextToken();
break;
case CiToken.Protected:
visibility = CiVisibility.Protected;
NextToken();
break;
case CiToken.Public:
visibility = CiVisibility.Public;
NextToken();
break;
default:
visibility = CiVisibility.Private;
break;
}
if (See(CiToken.Const)) {
// const
CiConst konst = ParseConst();
konst.Documentation = doc;
konst.Visibility = visibility;
consts.Add(konst);
continue;
}
callType = ParseCallType();
// \ class | static | normal | abstract | sealed
// method \| | | |
// --------+--------+--------+----------+-------
// static | + | + | + | +
// normal | - | + | + | +
// abstract| - | - | + | -
// virtual | - | + | + | -
// override| - | + | + | +
// sealed | - | + | + | +
if (callType == CiCallType.Static || klass.CallType == CiCallType.Abstract) {
// ok
}
else if (klass.CallType == CiCallType.Static)
throw ParseException("Only static members allowed in a static class");
else if (callType == CiCallType.Abstract)
throw ParseException("Abstract methods allowed only in an abstract class");
else if (klass.CallType == CiCallType.Sealed && callType == CiCallType.Virtual)
throw ParseException("Virtual methods disallowed in a sealed class");
if (visibility == CiVisibility.Private && callType != CiCallType.Static && callType != CiCallType.Normal)
throw ParseException("{0} method cannot be private", callType);
CiExpr type = Eat(CiToken.Void) ? CiSystem.VoidType : ParseType();
if (See(CiToken.LeftBrace) && type is CiCallExpr call) {
// constructor
if (call.Method.Name != klass.Name)
throw ParseException("Constructor name doesn't match class name");
if (callType != CiCallType.Normal)
throw ParseException("Constructor cannot be {0}", callType);
if (call.Arguments.Length != 0)
throw ParseException("Constructor parameters not supported");
if (klass.Constructor != null)
throw ParseException("Duplicate constructor, already defined in line {0}", klass.Constructor.Line);
if (visibility == CiVisibility.Private)
visibility = CiVisibility.Internal; // TODO
klass.Constructor = new CiMethodBase { Line = call.Line, Documentation = doc, Visibility = visibility, Parent = klass, Type = CiSystem.VoidType, Name = klass.Name, Body = ParseBlock() };
continue;
}
int line = this.Line;
string name = ParseId();
if (See(CiToken.LeftParenthesis) || See(CiToken.ExclamationMark)) {
// method
CiMethod method = new CiMethod { Line = line, Documentation = doc, Visibility = visibility, CallType = callType, TypeExpr = type, Name = name };
method.Parameters.Parent = klass;
ParseMethod(method);
methods.Add(method);
continue;
}
// field
if (visibility == CiVisibility.Public)
throw ParseException("Field cannot be public");
if (callType != CiCallType.Normal)
throw ParseException("Field cannot be {0}", callType);
if (type == CiSystem.VoidType)
throw ParseException("Field cannot be void");
CiField field = new CiField { Line = line, Documentation = doc, Visibility = visibility, TypeExpr = type, Name = name, Value = ParseInitializer() };
Expect(CiToken.Semicolon);
fields.Add(field);
}
klass.Consts = consts.ToArray();
klass.Fields = fields.ToArray();
klass.Methods = methods.ToArray();
return klass;
}
public CiEnum ParseEnum()
{
Expect(CiToken.Enum);
bool flags = Eat(CiToken.Asterisk);
CiEnum enu = flags ? new CiEnumFlags() : new CiEnum();
enu.Parent = this.Program;
enu.Filename = this.Filename;
enu.Line = this.Line;
enu.Name = ParseId();
Expect(CiToken.LeftBrace);
do {
CiConst konst = new CiConst { Visibility = CiVisibility.Public, Documentation = ParseDoc(), Line = this.Line, Name = ParseId(), Type = enu };
if (Eat(CiToken.Assign))
konst.Value = ParseExpr();
else if (flags)
throw ParseException("enum* symbol must be assigned a value");
enu.Add(konst);
} while (Eat(CiToken.Comma));
Expect(CiToken.RightBrace);
return enu;
}
public void Parse(string filename, TextReader reader)
{
Open(filename, reader);
while (!See(CiToken.EndOfFile)) {
CiCodeDoc doc = ParseDoc();
CiContainerType type;
bool isPublic = Eat(CiToken.Public);
switch (this.CurrentToken) {
// class
case CiToken.Class:
type = ParseClass(CiCallType.Normal);
break;
case CiToken.Static:
case CiToken.Abstract:
case CiToken.Sealed:
type = ParseClass(ParseCallType());
break;
// enum
case CiToken.Enum:
type = ParseEnum();
break;
// native
case CiToken.Native:
this.Program.TopLevelNatives.Add(ParseNative().Content);
continue;
default:
throw ParseException("Expected class or enum");
}
type.Documentation = doc;
type.IsPublic = isPublic;
this.Program.Add(type);
}
}
}
}