-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCsGrammar.cs
executable file
·3318 lines (2777 loc) · 110 KB
/
CsGrammar.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
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
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CSharpCodeParser
{
public class CsGrammar
{
public Rule r_compilationUnit { get; private set; }
readonly Id EOF, IDENTIFIER, NAME, LITERAL;
public int tokenIdentifier,
tokenName,
tokenLiteral,
tokenAttribute,
tokenStatement,
tokenClassBody,
tokenStructBody,
tokenInterfaceBody,
tokenNamespaceBody,
tokenBinaryOperator,
tokenExpectedType,
tokenMemberInitializer,
tokenNamedParameter,
tokenEOF;
void InitializeTokenCategories()
{
tokenIdentifier = TokenToId("IDENTIFIER");
tokenName = TokenToId("NAME");
tokenLiteral = TokenToId("LITERAL");
tokenAttribute = TokenToId(".ATTRIBUTE");
tokenStatement = TokenToId(".STATEMENT");
tokenClassBody = TokenToId(".CLASSBODY");
tokenStructBody = TokenToId(".STRUCTBODY");
tokenInterfaceBody = TokenToId(".INTERFACEBODY");
tokenNamespaceBody = TokenToId(".NAMESPACEBODY");
tokenBinaryOperator = TokenToId(".BINARYOPERATOR");
tokenExpectedType = TokenToId(".EXPECTEDTYPE");
tokenMemberInitializer = TokenToId(".MEMBERINITIALIZER");
tokenNamedParameter = TokenToId(".NAMEDPARAMETER");
tokenEOF = TokenToId("EOF");
}
public Parser parser{ get; private set; }
public CsGrammar()
{
EOF = new Id("EOF");
IDENTIFIER = new Id("IDENTIFIER");
LITERAL = new Id("LITERAL");
NAME = new NameId();
var externAliasDirective = new Id("externAliasDirective");
var usingDirective = new Id("usingDirective");
//var globalAttribute = new Id("globalAttribute");
var namespaceMemberDeclaration = new Id("namespaceMemberDeclaration");
var namespaceDeclaration = new Id("namespaceDeclaration");
var qualifiedIdentifier = new Id("qualifiedIdentifier");
var namespaceBody = new Id("namespaceBody");
//var typeDeclaration = new Id("typeDeclaration");
var attributes = new Id("attributes");
var modifiers = new Id("modifiers");
var typeParameterList = new Id("typeParameterList");
var typeParameter = new Id("typeParameter");
var classBase = new Id("classBase");
var typeParameterConstraintsClauses = new Id("typeParameterConstraintsClauses");
var typeParameterConstraintsClause = new Id("typeParameterConstraintsClause");
var classBody = new Id("classBody");
var classMemberDeclaration = new Id("classMemberDeclaration");
var attribute = new Id("attribute");
var typeName = new Id("typeName");
var argumentList = new Id("argumentList");
var attributeArgumentList = new Id("attributeArgumentList");
var expression = new Id("expression");
var constantExpression = new Id("constantExpression");
var primaryExpression = new Id("primaryExpression");
var arrayCreationExpression = new Id("arrayCreationExpression");
var implicitArrayCreationExpression = new Id("implicitArrayCreationExpression");
var constantDeclaration = new Id("constantDeclaration");
var fieldDeclaration = new Id("fieldDeclaration");
var methodDeclaration = new Id("methodDeclaration");
var propertyDeclaration = new Id("propertyDeclaration");
var eventDeclaration = new Id("eventDeclaration");
var indexerDeclaration = new Id("indexerDeclaration");
var operatorDeclaration = new Id("operatorDeclaration");
var constructorDeclaration = new Id("constructorDeclaration");
var destructorDeclaration = new Id("destructorDeclaration");
var constantDeclarators = new Id("constantDeclarators");
var constantDeclarator = new Id("constantDeclarator");
var type = new Id("type");
var type2 = new Id("type2");
var predefinedType = new Id("predefinedType");
var variableDeclarators = new Id("variableDeclarators");
var variableDeclarator = new Id("variableDeclarator");
var arrayInitializer = new Id("arrayInitializer");
var variableInitializer = new Id("variableInitializer");
var variableInitializerList = new Id("variableInitializerList");
var simpleType = new Id("simpleType");
var exceptionClassType = new Id("exceptionClassType");
var nonArrayType = new Id("nonArrayType");
var rankSpecifier = new Id("rankSpecifier");
var numericType = new Id("numericType");
var integralType = new Id("integralType");
var floatingPointType = new Id("floatingPointType");
r_compilationUnit = new Rule("compilationUnit",
new Many(externAliasDirective) - new Many(usingDirective)
//new Many(globalAttribute),
- new Many(namespaceMemberDeclaration) - EOF
)
{ semantics = SemanticFlags.CompilationUnitScope };
parser = new Parser(r_compilationUnit, this);
parser.Add(new Rule("externAliasDirective",
new Seq("extern", "alias", IDENTIFIER, ";")
)
{ semantics = SemanticFlags.ExternAlias });
var usingNamespaceDirective = new Id("usingNamespaceDirective");
var usingAliasDirective = new Id("usingAliasDirective");
var globalNamespace = new Id("globalNamespace");
var namespaceName = new Id("namespaceName");
var namespaceOrTypeName = new Id("namespaceOrTypeName");
var PARTIAL = new Id("PARTIAL");
parser.Add(new Rule("usingDirective",
"using" - (new If(IDENTIFIER - "=", usingAliasDirective) | usingNamespaceDirective) - ";"
));
parser.Add(new Rule("PARTIAL",
new If(s => s.Current.text == "partial", IDENTIFIER) | "partial"
)
{ contextualKeyword = true });
parser.Add(new Rule("namespaceMemberDeclaration",
namespaceDeclaration
| (attributes - modifiers - new Alt(
new Seq(
new Opt(PARTIAL),
new Id("classDeclaration") | new Id("structDeclaration") | new Id("interfaceDeclaration")),
new Id("enumDeclaration"),
new Id("delegateDeclaration"))
)
| ".NAMESPACEBODY"
));
parser.Add(new Rule("namespaceDeclaration",
"namespace" - qualifiedIdentifier - namespaceBody - new Opt(";")
)
{ semantics = SemanticFlags.NamespaceDeclaration });
parser.Add(new Rule("qualifiedIdentifier",
NAME - new Many(new Lit(".") - NAME)
));
parser.Add(new Rule("namespaceBody",
"{" - new Many(externAliasDirective) - new Many(usingDirective) - new Many(namespaceMemberDeclaration) - "}"
)
{ semantics = SemanticFlags.NamespaceBodyScope });
parser.Add(new Rule("usingNamespaceDirective",
namespaceName
)
{ semantics = SemanticFlags.UsingNamespace });
parser.Add(new Rule("namespaceName",
namespaceOrTypeName
));
parser.Add(new Rule("usingAliasDirective",
IDENTIFIER - "=" - namespaceOrTypeName
)
{ semantics = SemanticFlags.UsingAlias });
parser.Add(new Rule("classDeclaration",
new Seq(
"class",
NAME,
new Opt(typeParameterList),
new Opt(classBase),
new Opt(typeParameterConstraintsClauses),
classBody,
new Opt(";"))
)
{ semantics = SemanticFlags.ClassDeclaration | SemanticFlags.TypeDeclarationScope });
parser.Add(new Rule("typeParameterList",
"<" - attributes - typeParameter - new Many("," - attributes - typeParameter) - ">"
));
parser.Add(new Rule("typeParameter",
NAME
)
{ semantics = SemanticFlags.TypeParameterDeclaration });
var interfaceTypeList = new Id("interfaceTypeList");
parser.Add(new Rule("classBase",
":" - interfaceTypeList
)
{ semantics = SemanticFlags.ClassBaseScope });
parser.Add(new Rule("interfaceTypeList",
(typeName | "object") - new Many("," - typeName)
)
{ semantics = SemanticFlags.BaseListDeclaration });
parser.Add(new Rule("classBody",
"{" - new Many(classMemberDeclaration) - "}"
)
{ semantics = SemanticFlags.ClassBodyScope });
var interfaceDeclaration = new Id("interfaceDeclaration");
var classDeclaration = new Id("classDeclaration");
var structDeclaration = new Id("structDeclaration");
var memberName = new Id("memberName");
var qid = new Id("qid");
var enumDeclaration = new Id("enumDeclaration");
//var enumDeclarator = new Id("enumDeclarator");
var delegateDeclaration = new Id("delegateDeclaration");
var conversionOperatorDeclaration = new Id("conversionOperatorDeclaration");
parser.Add(new Rule("memberName",
qid
));
parser.Add(new Rule("classMemberDeclaration",
new Seq(
attributes,
modifiers,
constantDeclaration
| "void" - methodDeclaration
| new If(PARTIAL, PARTIAL - ("void" - methodDeclaration | classDeclaration | structDeclaration | interfaceDeclaration))
| new If(IDENTIFIER - "(", constructorDeclaration)
| new Seq(
type,
new If(memberName - "(", methodDeclaration)
| new If(memberName - "{", propertyDeclaration)
| new If(typeName - "." - "this", typeName - "." - indexerDeclaration)
| new If(predefinedType - "." - "this", predefinedType - "." - indexerDeclaration)
| indexerDeclaration
| fieldDeclaration
| operatorDeclaration)
| classDeclaration
| structDeclaration
| interfaceDeclaration
| enumDeclaration
| delegateDeclaration
| eventDeclaration
| conversionOperatorDeclaration)
| destructorDeclaration
| ".CLASSBODY"
) /*{ semantics = SemanticFlags.MemberDeclarationScope }*/);
var constructorDeclarator = new Id("constructorDeclarator");
var constructorBody = new Id("constructorBody");
var constructorInitializer = new Id("constructorInitializer");
var destructorDeclarator = new Id("destructorDeclarator");
var destructorBody = new Id("destructorBody");
var arguments = new Id("arguments");
var attributeArguments = new Id("attributeArguments");
var formalParameterList = new Id("formalParameterList");
var block = new Id("block");
var statementList = new Id("statementList");
parser.Add(new Rule("constructorDeclaration",
constructorDeclarator - constructorBody
)
{ semantics = SemanticFlags.MethodDeclarationScope | SemanticFlags.ConstructorDeclarator });
parser.Add(new Rule("constructorDeclarator",
IDENTIFIER - "(" - new Opt(formalParameterList) - ")" - new Opt(constructorInitializer)
));
parser.Add(new Rule("constructorInitializer",
":" - (new Lit("base") | "this") - arguments
)
{ semantics = SemanticFlags.ConstructorInitializerScope });
parser.Add(new Rule("constructorBody",
"{" - statementList - "}"
| ";"
)
{ semantics = SemanticFlags.MethodBodyScope });
parser.Add(new Rule("destructorDeclaration",
destructorDeclarator - destructorBody
)
{ semantics = SemanticFlags.MethodDeclarationScope | SemanticFlags.DestructorDeclarator });
parser.Add(new Rule("destructorDeclarator",
"~" - new Opt("extern") - IDENTIFIER - "(" - ")"
));
parser.Add(new Rule("destructorBody",
"{" - statementList - "}"
| ";"
)
{ semantics = SemanticFlags.MethodBodyScope });
parser.Add(new Rule("constantDeclaration",
"const" - type - constantDeclarators - ";"
));
parser.Add(new Rule("constantDeclarators",
constantDeclarator - new Many("," - constantDeclarator)
));
parser.Add(new Rule("constantDeclarator",
NAME - "=" - constantExpression
)
{ semantics = SemanticFlags.ConstantDeclarator });
parser.Add(new Rule("constantExpression",
expression
| ".EXPECTEDTYPE"
));
var methodHeader = new Id("methodHeader");
var methodBody = new Id("methodBody");
var formalParameter = new Id("formalParameter");
var fixedParameter = new Id("fixedParameter");
var parameterModifier = new Id("parameterModifier");
var defaultArgument = new Id("defaultArgument");
var parameterArray = new Id("parameterArray");
var statement = new Id("statement");
var typeVariableName = new Id("typeVariableName");
var typeParameterConstraintList = new Id("typeParameterConstraintList");
var secondaryConstraintList = new Id("secondaryConstraintList");
var secondaryConstraint = new Id("secondaryConstraint");
var constructorConstraint = new Id("constructorConstraint");
var WHERE = new Id("WHERE");
parser.Add(new Rule("methodDeclaration",
methodHeader - methodBody
)
{ semantics = SemanticFlags.MethodDeclarationScope | SemanticFlags.MethodDeclarator });
parser.Add(new Rule("methodHeader",
memberName - "(" - new Opt(formalParameterList) - ")" - new Opt(typeParameterConstraintsClauses)
));
parser.Add(new Rule("typeParameterConstraintsClauses",
typeParameterConstraintsClause - new Many(typeParameterConstraintsClause)
));//{ semantics = SemanticFlags.TypeParameterConstraintsScope });
parser.Add(new Rule("WHERE",
new If(s => s.Current.text == "where", IDENTIFIER) | "where"
)
{ contextualKeyword = true });
parser.Add(new Rule("typeParameterConstraintsClause",
WHERE - typeVariableName - ":" - typeParameterConstraintList
));//{ semantics = SemanticFlags.TypeParameterConstraint });
parser.Add(new Rule("typeParameterConstraintList",
(new Lit("class") | "struct") - new Opt("," - secondaryConstraintList)
| secondaryConstraintList
));
parser.Add(new Rule("secondaryConstraintList",
secondaryConstraint - new Opt("," - new Id("secondaryConstraintList"))
| constructorConstraint
));
parser.Add(new Rule("secondaryConstraint",
typeName // | typeVariableName
));
parser.Add(new Rule("typeVariableName",
IDENTIFIER
));
parser.Add(new Rule("constructorConstraint",
new Lit("new") - "(" - ")"
));
parser.Add(new Rule("methodBody",
"{" - statementList - "}"
| ";"
)
{ semantics = SemanticFlags.MethodBodyScope });
parser.Add(new Rule("block",
"{" - statementList - "}"
| ";"
)
{ semantics = SemanticFlags.CodeBlockScope });
parser.Add(new Rule("statementList",
new Many(new IfNot(new Seq("default", ":"), statement))
));
// var declarationStatement = new Id("declarationStatement");
var labeledStatement = new Id("labeledStatement");
var embeddedStatement = new Id("embeddedStatement");
var selectionStatement = new Id("selectionStatement");
var iterationStatement = new Id("iterationStatement");
var jumpStatement = new Id("jumpStatement");
var tryStatement = new Id("tryStatement");
var lockStatement = new Id("lockStatement");
var usingStatement = new Id("usingStatement");
var yieldStatement = new Id("yieldStatement");
var expressionStatement = new Id("expressionStatement");
var breakStatement = new Id("breakStatement");
var continueStatement = new Id("continueStatement");
var gotoStatement = new Id("gotoStatement");
var returnStatement = new Id("returnStatement");
var throwStatement = new Id("throwStatement");
var checkedStatement = new Id("checkedStatement");
var uncheckedStatement = new Id("uncheckedStatement");
var localVariableDeclaration = new Id("localVariableDeclaration");
var localVariableType = new Id("localVariableType");
var localVariableDeclarators = new Id("localVariableDeclarators");
var localVariableDeclarator = new Id("localVariableDeclarator");
var localVariableInitializer = new Id("localVariableInitializer");
//var stackallocInitializer = new Id("stackallocInitializer");
var localConstantDeclaration = new Id("localConstantDeclaration");
//var unmanagedType = new Id("unmanagedType");
var resourceAcquisition = new Id("resourceAcquisition");
var VAR = new Id("VAR");
parser.Add(new Rule("VAR",
//new If(s => s.Current.text == "var", IDENTIFIER)
IDENTIFIER | "var"
)
{ contextualKeyword = true });
parser.Add(new Rule("statement",
new If((type | "var") - IDENTIFIER - (new Lit(";") | "=" | "[" | ","), localVariableDeclaration - ";")
| new If(IDENTIFIER - ":", labeledStatement)
| localConstantDeclaration
| embeddedStatement
));
parser.Add(new Rule("localVariableDeclaration",
localVariableType - localVariableDeclarators
));
parser.Add(new Rule("localVariableType",
new If(s => s.Current.text == "var", VAR)
| type
));
parser.Add(new Rule("localVariableDeclarators",
localVariableDeclarator - new Many("," - localVariableDeclarator)
));
parser.Add(new Rule("localVariableDeclarator",
NAME - new Opt("=" - localVariableInitializer)
)
{ semantics = SemanticFlags.LocalVariableDeclarator });
parser.Add(new Rule("localVariableInitializer",
expression | arrayInitializer //| stackallocInitializer
| ".EXPECTEDTYPE"
)
{ semantics = SemanticFlags.LocalVariableInitializerScope });
parser.Add(new Rule("localConstantDeclaration",
"const" - type - constantDeclarators - ";"
));
parser.Add(new Rule("labeledStatement",
IDENTIFIER - ":" - statement
)
{ semantics = SemanticFlags.LabeledStatement });
var YIELD = new Id("YIELD");
parser.Add(new Rule("YIELD",
//new If(s => s.Current.text == "yield", IDENTIFIER)
IDENTIFIER | "yield"
)
{ contextualKeyword = true });
parser.Add(new Rule("embeddedStatement",
block
| selectionStatement // if, switch
| iterationStatement // while, do, for, foreach
| jumpStatement // break, continue, goto, return, throw
| tryStatement
| lockStatement
| usingStatement
| new If(
s => {
if (s.Current.text != "yield")
return false;
string next = s.Lookahead(1).text;
return next == "return" || next == "break";
}, yieldStatement)
| "yield return" | "yield break;" // auto-completion hint only, while the actual code will be handled by the previous line
| new If(new Lit("checked") - "{", checkedStatement)
| new If(new Lit("unchecked") - "{", uncheckedStatement)
| expressionStatement // expression!
| ".STATEMENT"
)
{ semantics = SemanticFlags.CodeBlockScope });
parser.Add(new Rule("lockStatement",
new Seq("lock", "(", expression, ")", embeddedStatement)
));
parser.Add(new Rule("checkedStatement",
new Seq("checked", block)
));
parser.Add(new Rule("uncheckedStatement",
new Seq("unchecked", block)
));
parser.Add(new Rule("usingStatement",
new Seq("using", "(", resourceAcquisition, ")", embeddedStatement)
)
{ semantics = SemanticFlags.UsingStatementScope });
parser.Add(new Rule("resourceAcquisition",
new If(localVariableType - IDENTIFIER, localVariableDeclaration)
| expression
));
parser.Add(new Rule("yieldStatement",
YIELD -
(("return" - expression - ";")
| (new Lit("break") - ";"))
));
var ifStatement = new Id("ifStatement");
var elseStatement = new Id("elseStatement");
var switchStatement = new Id("switchStatement");
var booleanExpression = new Id("booleanExpression");
var switchBlock = new Id("switchBlock");
var switchSection = new Id("switchSection");
var switchLabel = new Id("switchLabel");
var statementExpression = new Id("statementExpression");
parser.Add(new Rule("selectionStatement",
ifStatement | switchStatement
));
parser.Add(new Rule("ifStatement",
new Lit("if") - "(" - booleanExpression - ")" - embeddedStatement - new Opt(elseStatement)
));
parser.Add(new Rule("elseStatement",
"else" - embeddedStatement
));
parser.Add(new Rule("switchStatement",
new Lit("switch") - "(" - expression - ")" - switchBlock
));
parser.Add(new Rule("switchBlock",
"{" - new Many(switchSection) - "}"
)
{ semantics = SemanticFlags.SwitchBlockScope });
parser.Add(new Rule("switchSection",
new Many(switchLabel) - statement - statementList
));
parser.Add(new Rule("switchLabel",
"case" - constantExpression - ":"
| new Seq("default", ":")
));
parser.Add(new Rule("expressionStatement",
statementExpression - ";"
));
parser.Add(new Rule("jumpStatement",
breakStatement
| continueStatement
| gotoStatement
| returnStatement
| throwStatement
));
parser.Add(new Rule("breakStatement",
new Seq("break", ";")
));
parser.Add(new Rule("continueStatement",
new Seq("continue", ";")
));
parser.Add(new Rule("gotoStatement",
"goto" - (
IDENTIFIER
| "case" - constantExpression
| "default")
- ";"
));
parser.Add(new Rule("returnStatement",
"return" - new Opt(expression) - ";"
));
parser.Add(new Rule("throwStatement",
"throw" - new Opt(expression) - ";"
));
var catchClauses = new Id("catchClauses");
var finallyClause = new Id("finallyClause");
var specificCatchClauses = new Id("specificCatchClauses");
var specificCatchClause = new Id("specificCatchClause");
var catchExceptionIdentifier = new Id("catchExceptionIdentifier");
var generalCatchClause = new Id("generalCatchClause");
parser.Add(new Rule("tryStatement",
"try" - block - (catchClauses - new Opt(finallyClause) | finallyClause)
));
parser.Add(new Rule("catchClauses",
new If(new Seq("catch", "("), specificCatchClauses - new Opt(generalCatchClause))
| generalCatchClause
));
parser.Add(new Rule("specificCatchClauses",
specificCatchClause - new Many(new If(new Seq("catch", "("), specificCatchClause))
));
parser.Add(new Rule("specificCatchClause",
new Lit("catch") - "(" - exceptionClassType - new Opt(catchExceptionIdentifier) - ")" - block
)
{ semantics = SemanticFlags.SpecificCatchScope });
parser.Add(new Rule("catchExceptionIdentifier",
NAME
)
{ semantics = SemanticFlags.CatchExceptionParameterDeclaration });
parser.Add(new Rule("generalCatchClause",
"catch" - block
));
parser.Add(new Rule("finallyClause",
"finally" - block
));
parser.Add(new Rule("formalParameterList",
formalParameter - new Many("," - formalParameter)
)
{ semantics = SemanticFlags.FormalParameterListScope });
parser.Add(new Rule("formalParameter",
attributes - (fixedParameter | parameterArray)
// | "__arglist"
));
parser.Add(new Rule("fixedParameter",
new Opt(parameterModifier) - type - NAME - new Opt(defaultArgument)
)
{ semantics = SemanticFlags.FixedParameterDeclaration });
parser.Add(new Rule("parameterModifier",
new Lit("ref") | "out" | "this"
));
parser.Add(new Rule("defaultArgument",
"=" - (expression | ".EXPECTEDTYPE")
));
parser.Add(new Rule("parameterArray",
"params" - type - NAME
)
{ semantics = SemanticFlags.ParameterArrayDeclaration });
var whileStatement = new Id("whileStatement");
var doStatement = new Id("doStatement");
var forStatement = new Id("forStatement");
var foreachStatement = new Id("foreachStatement");
var forInitializer = new Id("forInitializer");
var forIterator = new Id("forIterator");
var statementExpressionList = new Id("statementExpressionList");
parser.Add(new Rule("iterationStatement",
whileStatement | doStatement | forStatement | foreachStatement
));
parser.Add(new Rule("whileStatement",
new Seq("while", "(", booleanExpression, ")", embeddedStatement)
));
parser.Add(new Rule("doStatement",
"do" - embeddedStatement - "while" - "(" - booleanExpression - ")" - ";"
));
parser.Add(new Rule("forStatement",
new Seq("for", "(", new Opt(forInitializer), ";", new Opt(booleanExpression), ";",
new Opt(forIterator), ")", embeddedStatement)
)
{ semantics = SemanticFlags.ForStatementScope });
parser.Add(new Rule("forInitializer",
new If(localVariableType - IDENTIFIER, localVariableDeclaration)
| statementExpressionList
));
parser.Add(new Rule("forIterator",
statementExpressionList
));
parser.Add(new Rule("foreachStatement",
new Lit("foreach") - "(" - localVariableType - NAME - "in" - expression - ")" - embeddedStatement
)
{ semantics = SemanticFlags.ForStatementScope | SemanticFlags.ForEachVariableDeclaration });
parser.Add(new Rule("statementExpressionList",
new Seq(statementExpression, new Many(new Seq(",", statementExpression)))
));
// TODO: should be assignment, call, increment, decrement, and new object expressions
parser.Add(new Rule("statementExpression",
expression
));
var accessorDeclarations = new Id("accessorDeclarations");
var getAccessorDeclaration = new Id("getAccessorDeclaration");
var setAccessorDeclaration = new Id("setAccessorDeclaration");
var accessorModifiers = new Id("accessorModifiers");
var accessorBody = new Id("accessorBody");
parser.Add(new Rule("indexerDeclaration",
new Lit("this") - "[" - formalParameterList - "]" - "{" - accessorDeclarations - "}"
)
{ semantics = SemanticFlags.IndexerDeclaration | SemanticFlags.AccessorsListScope });
parser.Add(new Rule("propertyDeclaration",
memberName - "{" - accessorDeclarations - "}"
)
{ semantics = SemanticFlags.PropertyDeclaration | SemanticFlags.AccessorsListScope });
parser.Add(new Rule("accessorModifiers",
"internal" - new Opt("protected")
| "protected" - new Opt("internal")
| "public"
| "private"
));
var GET = new Id("GET");
var SET = new Id("SET");
parser.Add(new Rule("GET",
IDENTIFIER | "get"
)
{ contextualKeyword = true });
parser.Add(new Rule("SET",
IDENTIFIER | "set"
)
{ contextualKeyword = true });
parser.Add(new Rule("accessorDeclarations",
attributes - new Opt(accessorModifiers)
- (new If(s => s.Current.text == "get", getAccessorDeclaration - new Opt(attributes - new Opt(accessorModifiers) - setAccessorDeclaration))
| setAccessorDeclaration - new Opt(attributes - new Opt(accessorModifiers) - getAccessorDeclaration))
));
parser.Add(new Rule("getAccessorDeclaration",
GET - accessorBody
)
{ semantics = SemanticFlags.GetAccessorDeclaration });
parser.Add(new Rule("setAccessorDeclaration",
SET - accessorBody
)
{ semantics = SemanticFlags.SetAccessorDeclaration });
parser.Add(new Rule("accessorBody",
"{" - statementList - "}"
| ";"
)
{ semantics = SemanticFlags.AccessorBodyScope });
var eventDeclarators = new Id("eventDeclarators");
var eventDeclarator = new Id("eventDeclarator");
var eventWithAccessorsDeclaration = new Id("eventWithAccessorsDeclaration");
var eventAccessorDeclarations = new Id("eventAccessorDeclarations");
var addAccessorDeclaration = new Id("addAccessorDeclaration");
var removeAccessorDeclaration = new Id("removeAccessorDeclaration");
parser.Add(new Rule("eventDeclaration",
"event" - typeName - (
new If(memberName - "{",
eventWithAccessorsDeclaration)
| eventDeclarators - ";")
));
parser.Add(new Rule("eventWithAccessorsDeclaration",
memberName - "{" - eventAccessorDeclarations - "}"
)
{ semantics = SemanticFlags.EventWithAccessorsDeclaration | SemanticFlags.AccessorsListScope });
parser.Add(new Rule("eventDeclarators",
eventDeclarator - new Many("," - eventDeclarator)
));
parser.Add(new Rule("eventDeclarator",
NAME - new Opt("=" - variableInitializer)
)
{ semantics = SemanticFlags.EventDeclarator });
parser.Add(new Rule("eventAccessorDeclarations",
attributes - (
new If(s => s.Current.text == "add", addAccessorDeclaration - attributes - removeAccessorDeclaration)
| removeAccessorDeclaration - attributes - addAccessorDeclaration)
));
var ADD = new Id("ADD");
var REMOVE = new Id("REMOVE");
parser.Add(new Rule("ADD",
IDENTIFIER | "add"
)
{ contextualKeyword = true });
parser.Add(new Rule("REMOVE",
IDENTIFIER | "remove"
)
{ contextualKeyword = true });
parser.Add(new Rule("addAccessorDeclaration",
ADD - accessorBody
)
{ semantics = SemanticFlags.AddAccessorDeclaration });
parser.Add(new Rule("removeAccessorDeclaration",
REMOVE - accessorBody
)
{ semantics = SemanticFlags.RemoveAccessorDeclaration });
parser.Add(new Rule("fieldDeclaration",
variableDeclarators - ";"
));
parser.Add(new Rule("variableDeclarators",
new Seq(variableDeclarator, new Many(new Seq(",", variableDeclarator)))
));
parser.Add(new Rule("variableDeclarator",
NAME - new Opt("=" - variableInitializer)
)
{ semantics = SemanticFlags.VariableDeclarator });
parser.Add(new Rule("variableInitializer",
expression | arrayInitializer
| ".EXPECTEDTYPE"
));
parser.Add(new Rule("modifiers",
new Many(
new Lit("new") | "public" | "protected" | "internal" | "private" | "abstract"
| "sealed" | "static" | "readonly" | "volatile" | "virtual" | "override" | "extern"
)
));
var operatorDeclarator = new Id("operatorDeclarator");
var operatorBody = new Id("operatorBody");
var operatorParameter = new Id("operatorParameter");
var binaryOperatorPart = new Id("binaryOperatorPart");
var unaryOperatorPart = new Id("unaryOperatorPart");
var overloadableBinaryOperator = new Id("overloadableBinaryOperator");
var overloadableUnaryOperator = new Id("overloadableUnaryOperator");
var conversionOperatorDeclarator = new Id("conversionOperatorDeclarator");
parser.Add(new Rule("operatorDeclaration",
operatorDeclarator - operatorBody
));
parser.Add(new Rule("operatorDeclarator",
new Seq("operator",
new Seq(new Lit("+") | "-", "(", operatorParameter, binaryOperatorPart | unaryOperatorPart)
| overloadableUnaryOperator - "(" - operatorParameter - unaryOperatorPart
| overloadableBinaryOperator - "(" - operatorParameter - binaryOperatorPart)
)
{ semantics = SemanticFlags.OperatorDeclarator });
parser.Add(new Rule("operatorParameter",
type - NAME
)
{ semantics = SemanticFlags.FixedParameterDeclaration });
parser.Add(new Rule("unaryOperatorPart",
")"
));
parser.Add(new Rule("binaryOperatorPart",
"," - operatorParameter - ")"
));
parser.Add(new Rule("overloadableUnaryOperator",
/*'+' | '-' | */ new Lit("!") | "~" | "++" | "--" | "true" | "false"
));
parser.Add(new Rule("overloadableBinaryOperator",
/*'+' | '-' | */
// >> check needed
new Lit("*") | "/" | "%" | "&" | "|" | "^" | "<<" | new If(new Seq(">", ">"), new Seq(">", ">")) | "==" | "!=" | ">" | "<" | ">=" | "<="
));
parser.Add(new Rule("conversionOperatorDeclaration",
conversionOperatorDeclarator - operatorBody
));
parser.Add(new Rule("conversionOperatorDeclarator",
(new Lit("implicit") | "explicit") - "operator" - type - "(" - operatorParameter - ")"
)
{ semantics = SemanticFlags.ConversionOperatorDeclarator });
parser.Add(new Rule("operatorBody",
"{" - statementList - "}"
| ";"
)
{ semantics = SemanticFlags.MethodBodyScope });
var typeOrGeneric = new Id("typeOrGeneric");
var typeArgumentList = new Id("typeArgumentList");
var unboundTypeRank = new Id("unboundTypeRank");
var structInterfaces = new Id("structInterfaces");
var structBody = new Id("structBody");
var structMemberDeclaration = new Id("structMemberDeclaration");
parser.Add(new Rule("structDeclaration",
"struct" - NAME - new Opt(typeParameterList) - new Opt(structInterfaces)
- new Opt(typeParameterConstraintsClauses) - structBody - new Opt(";")
)
{ semantics = SemanticFlags.StructDeclaration | SemanticFlags.TypeDeclarationScope });
parser.Add(new Rule("structInterfaces",
":" - interfaceTypeList
)
{ semantics = SemanticFlags.StructInterfacesScope });
parser.Add(new Rule("structBody",
"{" - new Many(structMemberDeclaration) - "}"
)
{ semantics = SemanticFlags.StructBodyScope });
parser.Add(new Rule("structMemberDeclaration",
new Seq(
attributes,
modifiers,
constantDeclaration
| "void" - methodDeclaration
| new If(PARTIAL, PARTIAL - ("void" - methodDeclaration | classDeclaration | structDeclaration | interfaceDeclaration))
| new If(IDENTIFIER - "(", constructorDeclaration)
| new Seq(
type,
new If(memberName - "(", methodDeclaration)
| new If(memberName - "{", propertyDeclaration)
| new If(memberName - "." - "this", typeName - "." - indexerDeclaration)
| new If(predefinedType - "." - "this", predefinedType - "." - indexerDeclaration)
| indexerDeclaration
| fieldDeclaration
| operatorDeclaration)
| classDeclaration
| structDeclaration
| interfaceDeclaration
| enumDeclaration
| delegateDeclaration
| eventDeclaration
| conversionOperatorDeclaration
| ".STRUCTBODY")
));
var interfaceBase = new Id("interfaceBase");
var interfaceBody = new Id("interfaceBody");
var interfaceMemberDeclaration = new Id("interfaceMemberDeclaration");
var interfaceMethodDeclaration = new Id("interfaceMethodDeclaration");
var interfaceEventDeclaration = new Id("interfaceEventDeclaration");
var interfacePropertyDeclaration = new Id("interfacePropertyDeclaration");
var interfaceIndexerDeclaration = new Id("interfaceIndexerDeclaration");
var interfaceAccessorDeclarations = new Id("interfaceAccessorDeclarations");
var interfaceGetAccessorDeclaration = new Id("interfaceGetAccessorDeclaration");
var interfaceSetAccessorDeclaration = new Id("interfaceSetAccessorDeclaration");
parser.Add(new Rule("interfaceDeclaration",
"interface" - NAME - new Opt(typeParameterList) - new Opt(interfaceBase) -
new Opt(typeParameterConstraintsClauses) - interfaceBody - new Opt(";")
)
{ semantics = SemanticFlags.InterfaceDeclaration | SemanticFlags.TypeDeclarationScope });