-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathNotBasicParser.vb
More file actions
1407 lines (1205 loc) · 60 KB
/
NotBasicParser.vb
File metadata and controls
1407 lines (1205 loc) · 60 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
Imports VBF.Compilers
Imports VBF.Compilers.Scanners
Imports VBF.Compilers.Parsers
Imports VBF.Compilers.Scanners.RegularExpression
Imports System.Globalization
Public Class NotBasicParser
Inherits ParserBase(Of CompilationUnit)
'Lexer states
Private m_identifierLexerIndex As Integer
Private m_keywordLexerIndex As Integer
Private m_declareLexerIndex As Integer
Private m_propertyLexerIndex As Integer
'keywords
Private StringKeyword As Token
Private IntKeyword As Token
Private ShortKeyword As Token
Private ByteKeyword As Token
Private CharKeyword As Token
Private LongKeyword As Token
Private BoolKeyword As Token
Private SingleKeyword As Token
Private DoubleKeyword As Token
Private IfKeyword As Token
Private ThenKeyword As Token
Private ElseKeyword As Token
Private ElseIfKeyword As Token
Private EndKeyword As Token
Private TrueKeyword As Token
Private FalseKeyword As Token
Private NewKeyword As Token
Private OperatorKeyword As Token
Private FunctionKeyword As Token
Private ReturnKeyword As Token
Private ExitKeyword As Token
Private TryKeyword As Token
Private CatchKeyword As Token
Private FinallyKeyword As Token
Private ThrowKeyword As Token
Private EachKeyword As Token
Private DoKeyword As Token
Private WhileKeyword As Token
Private UntilKeyword As Token
Private LoopKeyword As Token
Private ContinueKeyword As Token
Private ForKeyword As Token
Private NextKeyword As Token
Private SelectKeyword As Token
Private CaseKeyword As Token
Private NothingKeyword As Token
Private ObjectKeyword As Token
Private AndKeyword As Token
Private OrKeyword As Token
Private NotKeyword As Token
Private XorKeyword As Token
Private ModKeyword As Token
Private ConceptKeyword As Token
Private ConcreteKeyword As Token
Private WhereKeyword As Token
Private TypeKeyword As Token
Private ToKeyword As Token
Private StepKeyword As Token
Private InKeyword As Token
Private EnumKeyword As Token
Private MeKeyword As Token
Private VoidKeyword As Token
'contextural keywords
Private GetKeyword As Token
Private SetKeyword As Token
Private DeclareKeyword As Token
Private Identifier As Token
Private EscapedIdentifier As Token
Private IntegerLiteral As Token
Private FloatLiteral As Token
Private RawStringLiteral As Token
Private CharLiteral As Token
'punctuations
Private Colon As Token ':
Private Comma As Token ',
Private LeftPth As Token '(
Private RightPth As Token ')
Private Semicolon As Token ';
Private LeftBrck As Token '[
Private RightBrck As Token ']
Private LeftBrce As Token '{
Private RightBrce As Token '}
Private AtSymbol As Token '@
Private PlusSymbol As Token '+
Private MinusSymbol As Token '-
Private Asterisk As Token '*
Private Slash As Token '/
Private CapSymbol As Token '^
Private EqualSymbol As Token '=
Private LessSymbol As Token '<
Private GreaterSymbol As Token '>
Private LessEqual As Token '<=
Private GreaterEqual As Token '>=
Private ShiftLeft As Token '<<
Private Dot As Token '.
Private Arrow As Token '=>
Private PoundSymbol As Token
Private LineTerminator As Token
'trivias
Private WhiteSpace As Token
Private Comment As Token
Public Sub New(errorManager As CompilationErrorManager)
MyBase.New(errorManager)
End Sub
Protected Overrides Sub OnDefineLexer(lexicon As Lexicon, triviaTokens As ICollection(Of Token))
'identifier lexer without keyword
Dim identifierLexer = lexicon.Lexer
'lexer with reserved keywords
Dim keywordLexer = identifierLexer.CreateSubLexer()
'lexer for property body (get/set proc)
Dim propertyLexer = keywordLexer.CreateSubLexer()
m_identifierLexerIndex = identifierLexer.Index
m_keywordLexerIndex = keywordLexer.Index
m_propertyLexerIndex = propertyLexer.Index
Dim lettersCategories As New HashSet(Of UnicodeCategory)() From
{
UnicodeCategory.LetterNumber,
UnicodeCategory.LowercaseLetter,
UnicodeCategory.ModifierLetter,
UnicodeCategory.OtherLetter,
UnicodeCategory.TitlecaseLetter,
UnicodeCategory.UppercaseLetter
}
Dim combiningCategories As New HashSet(Of UnicodeCategory)() From
{
UnicodeCategory.NonSpacingMark,
UnicodeCategory.SpacingCombiningMark
}
Dim lineTerminators As New HashSet(Of Char) From
{
ChrW(&HD), ChrW(&HA), ChrW(&H85), ChrW(&H2028), ChrW(&H2029)
}
Dim letterChar As RegularExpression = Nothing
Dim combiningChar As RegularExpression = Nothing
Dim decimalDigitChar As RegularExpression = Nothing
Dim connectingChar As RegularExpression = Nothing
Dim formattingChar As RegularExpression = Nothing
Dim spaceChar As RegularExpression = Nothing
Dim inputChar As RegularExpression = Nothing
Dim strictStringChar As RegularExpression = Nothing
Dim charSetBuilder As New CharSetExpressionBuilder()
charSetBuilder.DefineCharSet(Function(c) lettersCategories.Contains(Char.GetUnicodeCategory(c)), Sub(re) letterChar = re)
charSetBuilder.DefineCharSet(Function(c) combiningCategories.Contains(Char.GetUnicodeCategory(c)), Sub(re) combiningChar = re)
charSetBuilder.DefineCharSet(Function(c) Char.GetUnicodeCategory(c) = UnicodeCategory.SpaceSeparator, Sub(re) spaceChar = re)
charSetBuilder.DefineCharSet(Function(c) Char.GetUnicodeCategory(c) = UnicodeCategory.DecimalDigitNumber, Sub(re) decimalDigitChar = re)
charSetBuilder.DefineCharSet(Function(c) Char.GetUnicodeCategory(c) = UnicodeCategory.ConnectorPunctuation, Sub(re) connectingChar = re)
charSetBuilder.DefineCharSet(Function(c) Char.GetUnicodeCategory(c) = UnicodeCategory.Format, Sub(re) formattingChar = re)
charSetBuilder.DefineCharSet(Function(c) Not lineTerminators.Contains(c), Sub(re) inputChar = re)
charSetBuilder.DefineCharSet(Function(c) (c <> """"c) AndAlso (Not lineTerminators.Contains(c)), Sub(re) strictStringChar = re Or Literal(""""""))
charSetBuilder.Build()
Dim lineTerminatorChar = CharSet(lineTerminators)
Dim whitespaceChar = spaceChar Or Symbol(ChrW(&H9)) Or Symbol(ChrW(&HB)) Or Symbol(ChrW(&HC))
'========== define identifiers ==========
With identifierLexer
Dim identifierStartChar = letterChar Or Symbol("_"c)
Dim identifierPartChar = letterChar Or decimalDigitChar Or connectingChar Or combiningChar Or formattingChar
Identifier = .DefineToken(identifierStartChar & identifierPartChar.Many(), "identifier")
EscapedIdentifier = .DefineToken(Symbol("$"c) & identifierStartChar & identifierPartChar.Many(), "identifier")
End With
'========== define literals ==========
With identifierLexer
Dim digit = CharSet("0123456789")
Dim hexDigit = CharSet("0123456789abcdefABCDEF")
Dim octalDigit = CharSet("01234567")
Dim intLiteral = digit.Many1()
Dim hexLiteral = Literal("&H") & hexDigit.Many1()
Dim octalLiteral = Literal("&O") & octalDigit.Many1()
Dim shortChar = CharSet("sS")
Dim longChar = CharSet("lL")
Dim integralTypeChar = shortChar Or longChar
Dim integralLiteralValue = intLiteral Or hexLiteral Or octalLiteral
IntegerLiteral = .DefineToken(integralLiteralValue & integralTypeChar.Optional(), "integral literal")
Dim singleChar = CharSet("fF")
Dim doubleChar = CharSet("rR")
Dim floatTypeChar = singleChar Or doubleChar
Dim sign = CharSet("+-")
Dim exponent = CharSet("eE") & sign & intLiteral
Dim floatLiteralValue = (intLiteral & Symbol("."c) & intLiteral & exponent.Optional()) Or
(Symbol("."c) & intLiteral & exponent.Optional()) Or
(intLiteral & exponent)
FloatLiteral = .DefineToken((floatLiteralValue & floatTypeChar.Optional()) Or (intLiteral & floatTypeChar), "float point literal")
'Dim embeddedExpEndSymbol = Literal("#>")
Dim doubleQuote = Symbol(""""c)
Dim stringChar = strictStringChar Or lineTerminatorChar
RawStringLiteral = .DefineToken(
doubleQuote & stringChar.Many() & doubleQuote,
"string literal")
Dim charChar = CharSet("cC")
Dim textCharLiteral = doubleQuote & strictStringChar & doubleQuote & charChar
Dim unicodeCharLiteral = Literal("&U+") & hexDigit.Many1()
CharLiteral = .DefineToken(textCharLiteral Or unicodeCharLiteral, "char literal")
End With
'========== define punctuations ==========
With identifierLexer
Colon = .DefineToken(Symbol(":"c))
Comma = .DefineToken(Symbol(","c))
LeftPth = .DefineToken(Symbol("("c))
RightPth = .DefineToken(Symbol(")"c))
Semicolon = .DefineToken(Symbol(";"c))
LeftBrck = .DefineToken(Symbol("["c))
RightBrck = .DefineToken(Symbol("]"c))
LeftBrce = .DefineToken(Symbol("{"c))
RightBrce = .DefineToken(Symbol("}"c))
AtSymbol = .DefineToken(Symbol("@"c))
PlusSymbol = .DefineToken(Symbol("+"c))
MinusSymbol = .DefineToken(Symbol("-"c))
Asterisk = .DefineToken(Symbol("*"c))
Slash = .DefineToken(Symbol("/"c))
CapSymbol = .DefineToken(Symbol("^"c))
EqualSymbol = .DefineToken(Symbol("="c))
LessSymbol = .DefineToken(Symbol("<"c))
GreaterSymbol = .DefineToken(Symbol(">"c))
LessEqual = .DefineToken(Literal("<="))
GreaterEqual = .DefineToken(Literal(">="))
ShiftLeft = .DefineToken(Literal("<<"))
Dot = .DefineToken(Symbol("."c))
Arrow = .DefineToken(Literal("=>"))
PoundSymbol = .DefineToken(Symbol("#"c))
LineTerminator = .DefineToken(lineTerminatorChar Or Literal(vbCrLf), "line terminator")
End With
'========== define trivias ==========
With identifierLexer
WhiteSpace = .DefineToken(whitespaceChar.Many1(), "white space")
Comment = .DefineToken((Symbol("'"c) & inputChar.Many()), "comment")
End With
'========== Define reserved keywords ==========
With keywordLexer
StringKeyword = .DefineToken(Literal("string"))
IntKeyword = .DefineToken(Literal("int"))
ShortKeyword = .DefineToken(Literal("short"))
ByteKeyword = .DefineToken(Literal("byte"))
CharKeyword = .DefineToken(Literal("char"))
LongKeyword = .DefineToken(Literal("long"))
BoolKeyword = .DefineToken(Literal("bool"))
SingleKeyword = .DefineToken(Literal("single"))
DoubleKeyword = .DefineToken(Literal("double"))
IfKeyword = .DefineToken(Literal("if"))
ThenKeyword = .DefineToken(Literal("then"))
ElseKeyword = .DefineToken(Literal("else"))
ElseIfKeyword = .DefineToken(Literal("elseif"))
EndKeyword = .DefineToken(Literal("end"))
TrueKeyword = .DefineToken(Literal("true"))
FalseKeyword = .DefineToken(Literal("false"))
NewKeyword = .DefineToken(Literal("new"))
OperatorKeyword = .DefineToken(Literal("operator"))
FunctionKeyword = .DefineToken(Literal("fun"))
ReturnKeyword = .DefineToken(Literal("return"))
ExitKeyword = .DefineToken(Literal("exit"))
TryKeyword = .DefineToken(Literal("try"))
CatchKeyword = .DefineToken(Literal("catch"))
FinallyKeyword = .DefineToken(Literal("finally"))
ThrowKeyword = .DefineToken(Literal("throw"))
EachKeyword = .DefineToken(Literal("each"))
DoKeyword = .DefineToken(Literal("do"))
WhileKeyword = .DefineToken(Literal("while"))
UntilKeyword = .DefineToken(Literal("until"))
LoopKeyword = .DefineToken(Literal("loop"))
ContinueKeyword = .DefineToken(Literal("continue"))
ForKeyword = .DefineToken(Literal("for"))
NextKeyword = .DefineToken(Literal("next"))
SelectKeyword = .DefineToken(Literal("select"))
CaseKeyword = .DefineToken(Literal("case"))
NothingKeyword = .DefineToken(Literal("nothing"))
ObjectKeyword = .DefineToken(Literal("object"))
AndKeyword = .DefineToken(Literal("and"))
OrKeyword = .DefineToken(Literal("or"))
NotKeyword = .DefineToken(Literal("not"))
XorKeyword = .DefineToken(Literal("xor"))
ModKeyword = .DefineToken(Literal("mod"))
ConceptKeyword = .DefineToken(Literal("concept"))
ConcreteKeyword = .DefineToken(Literal("concrete"))
WhereKeyword = .DefineToken(Literal("where"))
TypeKeyword = .DefineToken(Literal("type"))
ToKeyword = .DefineToken(Literal("to"))
StepKeyword = .DefineToken(Literal("step"))
InKeyword = .DefineToken(Literal("in"))
EnumKeyword = .DefineToken(Literal("enum"))
DeclareKeyword = .DefineToken(Literal("decl"))
MeKeyword = .DefineToken(Literal("me"))
VoidKeyword = .DefineToken(Literal("void"))
End With
'define contextual keywords for property declaration
With propertyLexer
GetKeyword = .DefineToken(Literal("get"))
SetKeyword = .DefineToken(Literal("set"))
End With
triviaTokens.Add(Comment)
triviaTokens.Add(WhiteSpace)
End Sub
Private ReferenceIdentifier As New Production(Of UnifiedIdentifer)
Private QualifiedIdentifier As New Production(Of UnifiedIdentifer)
Private StatementTerminator As New Production(Of SourceSpan)
Private DeclaringIdentifier As New Production(Of UnifiedIdentifer)
Private TypeName As New Production(Of TypeName)
Private ArrayTypeName As New Production(Of TypeName)
Private PrimitiveTypeName As New Production(Of TypeName)
Private QualifiedTypeName As New Production(Of TypeName)
Private TypeSpecifier As New Production(Of TypeSpecifier)
Private TypeParameter As New Production(Of TypeParameter)
Private TypeParameters As New Production(Of IEnumerable(Of TypeParameter))
Private TypeArguments As New Production(Of IEnumerable(Of TypeName))
Private FunctionTypeName As New Production(Of TypeName)
Private Program As New Production(Of CompilationUnit)
Private TopLevelStructure As New Production(Of Definition)
Private TypeDefinition As New Production(Of TypeDefinition)
Private FieldDefinition As New Production(Of FieldDefinition)
Private EnumDefinition As New Production(Of EnumDefinition)
Private ParameterList As New Production(Of IEnumerable(Of ParameterDeclaration))
Private MethodParameterList As New Production(Of IEnumerable(Of ParameterDeclaration))
Private ParameterDeclaration As New Production(Of ParameterDeclaration)
Private ExtensionMethodParameterDeclaration As New Production(Of ParameterDeclaration)
Private FunctionSignature As New Production(Of FunctionSignature)
Private FunctionDefinition As New Production(Of FunctionDefinition)
Private LambdaParameterList As New Production(Of IEnumerable(Of ParameterDeclaration))
Private LambdaParameterDeclaration As New Production(Of ParameterDeclaration)
Private OperatorSignature As New Production(Of OperatorSignature)
Private OperatorDefinition As New Production(Of OperatorDefinition)
Private ShiftRightOperator As New Production(Of LexemeValue)
Private NotEqualOperator As New Production(Of LexemeValue)
Private OverloadableOperator As New Production(Of LexemeValue)
Private ConceptDeclaration As New Production(Of ConceptDeclaration)
Private ConceptDefinition As New Production(Of ConceptDefinition)
Private ConstraintClauses As New Production(Of IEnumerable(Of ConstraintClause))
Private ConceptConstraintClause As New Production(Of ConstraintClause)
Private TypeConstraintClause As New Production(Of ConstraintClause)
Private ConcreteDeclaration As New Production(Of ConcreteDeclaration)
Private ConcreteDefinition As New Production(Of ConcreteDefinition)
Private ProcedureDeclaration As New Production(Of ProcedureDeclaration)
Private ProcedureDefinition As New Production(Of Definition)
Private Statements As New Production(Of IEnumerable(Of Statement))
Private Statement As New Production(Of Statement)
Private SingleLineStatement As New Production(Of Statement)
Private SingleLineOpenStatement As New Production(Of Statement)
Private SingleLineClosedStatement As New Production(Of Statement)
Private BlockStatement As New Production(Of Statement)
Private StatementsBlock As New Production(Of IEnumerable(Of Statement))
Private ReturnStatement As New Production(Of Statement)
Private AssignmentStatement As New Production(Of Statement)
Private ExpressionStatement As New Production(Of Statement)
Private CallStatement As New Production(Of Statement)
Private IfThenStatement As New Production(Of Statement)
Private IfThenElseOpenStatement As New Production(Of Statement)
Private IfThenElseClosedStatement As New Production(Of Statement)
Private IfBlockStatement As New Production(Of Statement)
Private DoStatement As New Production(Of Statement)
Private TryStatement As New Production(Of Statement)
Private ForStatement As New Production(Of Statement)
Private ForEachStatement As New Production(Of Statement)
Private ExitStatement As New Production(Of Statement)
Private ContinueStatement As New Production(Of Statement)
Private SelectCaseStatement As New Production(Of Statement)
Private Expression As New Production(Of Expression)
Private PrimaryExpression As New Production(Of Expression)
Private PrimaryExpressionNonNewArray As New Production(Of Expression)
Private FactorExpression As New Production(Of Expression)
Private TermExpression As New Production(Of Expression)
Private ComparandExpression As New Production(Of Expression)
Private ComparisonExpression As New Production(Of Expression)
Private EqualityExpression As New Production(Of Expression)
Private AndExpression As New Production(Of Expression)
Private OrExpression As New Production(Of Expression)
Private XorExpression As New Production(Of Expression)
Private ShiftingExpression As New Production(Of Expression)
Private TypeSpecifiedExpression As New Production(Of Expression)
Private UnaryExpression As New Production(Of Expression)
Private IntegerLiteralExpression As New Production(Of Expression)
Private FloatLiteralExpression As New Production(Of Expression)
Private NumericLiteralExpression As New Production(Of Expression)
Private BooleanLiteralExpression As New Production(Of Expression)
Private StringLiteralExpression As New Production(Of Expression)
Private CharLiteralExpression As New Production(Of Expression)
Private NothingExpression As New Production(Of Expression)
Private NewArrayExpression As New Production(Of Expression)
Private ReferenceExpression As New Production(Of Expression)
Private MemberAccessExpression As New Production(Of Expression)
Private BracketExpression As New Production(Of Expression)
Private CallExpression As New Production(Of Expression)
Private LambdaExpression As New Production(Of Expression)
Private LambdaBody As New Production(Of LambdaBody)
Private LambdaSignature As New Production(Of LambdaSignature)
Private ArrayLiteralExpression As New Production(Of Expression)
Private ArgumentList As New Production(Of IEnumerable(Of Argument))
Protected Overrides Sub OnDefineParserErrors(errorDefinition As SyntaxErrors, errorManager As CompilationErrorManager)
With errorDefinition
.LexicalErrorId = ErrorCode.InvalidToken
.TokenMissingId = ErrorCode.MissingToken
.TokenUnexpectedId = ErrorCode.UnexpectedToken
.TokenMistakeId = ErrorCode.TokenMistake
.OtherErrorId = ErrorCode.GeneralSyntaxError
End With
MyBase.OnDefineParserErrors(errorDefinition, errorManager)
errorManager.DefineError(ErrorCode.RightShiftSymbolError, 0, CompilationStage.Parsing, "Spaces between >> operator are not allowed")
errorManager.DefineError(ErrorCode.NotEqualSymbolError, 0, CompilationStage.Parsing, "Spaces between <> operator are not allowed")
errorManager.DefineError(ErrorCode.CaseIsNotAllowedAfterCaseElse, 0, CompilationStage.Parsing, "'case' clause is not allowed after 'case else'")
errorManager.DefineError(ErrorCode.CaseElseCanHaveOnlyOne, 0, CompilationStage.Parsing, "Only one 'case else' clause is allowed")
errorManager.DefineError(ErrorCode.ExpressionExpected, 0, CompilationStage.Parsing, "Expression expected")
End Sub
Protected Overrides Function OnDefineGrammar() As ProductionBase(Of CompilationUnit)
'DONE: concept/concret definition
'DONE: user defined type
'DONE: for/foreach statement
'DONE: try/catch statement
'DONE: lambda expression/function type
'DONE: select case statement
'DONE: if then else statement ambiguity gramma
'DONE: String literals
'DONE: array access ambiguity gramma
'DONE: enum
'DONE: type inheritence
'DONE: dispatch method
'DONE: concept default implementation
'DONE: array literal
'DONE: array type specifier
'DONE: type constraint clause
'DONE: object type
'DONE: nothing literal
'TODO: new object expression?
'TODO: void type
StatementTerminator.Rule =
From terminator In (LineTerminator.AsTerminal() Or Semicolon.AsTerminal())
Select If(terminator Is Nothing, Nothing, terminator.Value.Span)
Dim LineContinuation = LineTerminator.Optional()
'Statement terminator
Dim ST = StatementTerminator.Many1()
'=======================================================================
' Basic Structures
'=======================================================================
DeclaringIdentifier.Rule =
(From id In Identifier
Select New UnifiedIdentifer(id.Value, False)) Or
(From eid In EscapedIdentifier
Select New UnifiedIdentifer(eid.Value, True))
ReferenceIdentifier.Rule =
DeclaringIdentifier
QualifiedIdentifier.Rule =
(From id In Identifier.AsTerminal()
Select New UnifiedIdentifer(id.Value, False)) Or
(From eid In EscapedIdentifier
Select New UnifiedIdentifer(eid.Value, True))
ArrayTypeName.Rule =
From baseType In TypeName
From _lbk In LeftBrck
From _rbk In RightBrck
Select DirectCast(New ArrayTypeName(baseType), TypeName)
TypeName.Rule =
QualifiedTypeName Or
PrimitiveTypeName Or
FunctionTypeName Or
ArrayTypeName
QualifiedTypeName.Rule =
From id In ReferenceIdentifier
From typeArgs In TypeArguments.Optional
Select DirectCast(New QualifiedTypeName(id, typeArgs), TypeName)
'<Type, Type,...>
TypeArguments.Rule =
From _lt In LessSymbol
From _lc1 In LineContinuation
From types In TypeName.Many1(Comma.AsTerminal().SuffixedBy(LineContinuation))
From _lc2 In LineContinuation
From _gt In GreaterSymbol
Select types
Dim typeParameterDimension =
From _lt In LessSymbol
From commas In Comma.AsTerminal().Many()
From _gt In GreaterSymbol
Select New Nullable(Of Integer)(commas.Count() + 1)
TypeParameter.Rule =
From name In DeclaringIdentifier
From dimension In typeParameterDimension.Optional
Select New TypeParameter(name, dimension)
'<identifier, identifier<>>
TypeParameters.Rule =
From _lt In LessSymbol
From _lc1 In LineContinuation
From typeParams In TypeParameter.Many1(Comma.AsTerminal().SuffixedBy(LineContinuation))
From _lc2 In LineContinuation
From _gt In GreaterSymbol
Select typeParams
PrimitiveTypeName.Rule =
From typeKeyword In Grammar.Union(IntKeyword,
BoolKeyword,
SingleKeyword,
DoubleKeyword,
ShortKeyword,
ByteKeyword,
LongKeyword,
CharKeyword,
StringKeyword,
ObjectKeyword,
VoidKeyword)
Select DirectCast(New PrimitiveTypeName(typeKeyword.Value), TypeName)
'fun(paramType)returnType
FunctionTypeName.Rule =
From _fun In FunctionKeyword
From _lpth In LeftPth
From _lc1 In LineContinuation
From paramTypes In TypeName.Many(Comma.AsTerminal().SuffixedBy(LineContinuation))
From _lc2 In LineContinuation
From _rpth In RightPth
From returnType In TypeName.Optional
Select DirectCast(New FunctionTypeName(_fun.Value.Span, paramTypes, returnType), TypeName)
'=======================================================================
' Program Entry
'=======================================================================
TopLevelStructure.Rule =
FunctionDefinition.Select(Function(d) d.ToBase()) Or
OperatorDefinition.Select(Function(d) d.ToBase()) Or
ConceptDefinition.Select(Function(d) d.ToBase()) Or
ConcreteDefinition.Select(Function(d) d.ToBase()) Or
TypeDefinition.Select(Function(d) d.ToBase()) Or
EnumDefinition.Select(Function(d) d.ToBase()) Or
ProcedureDeclaration.Select(Function(d) d.ToBase())
Program.Rule =
From _emptylines In StatementTerminator.Many
From definitions In TopLevelStructure.Many()
Select New CompilationUnit(definitions)
'=======================================================================
' Types
'=======================================================================
FieldDefinition.Rule =
From fieldName In DeclaringIdentifier
From typeSp In TypeSpecifier
From _st In ST
Select New FieldDefinition(fieldName, typeSp)
TypeDefinition.Rule =
From _type In TypeKeyword
From typeName In DeclaringIdentifier
From typeParams In TypeParameters.Optional
From baseType In TypeSpecifier.Optional
From whereClauses In ConstraintClauses.Optional
From _st1 In ST
From fields In FieldDefinition.Many
From _end In EndKeyword
From _st2 In ST
Select New TypeDefinition(_type.Value.Span, _end.Value.Span, typeName, typeParams, baseType, whereClauses, fields)
Dim EnumElementValue =
From _eq In EqualSymbol
From num In IntegerLiteral
Select num.Value
Dim EnumElement =
From elementName In DeclaringIdentifier
From elementValue In EnumElementValue.Optional
Select New EnumElement(elementName, elementValue)
EnumDefinition.Rule =
From _enum In EnumKeyword
From enumName In DeclaringIdentifier
From _st1 In ST
From elements In EnumElement.SuffixedBy(ST).Many1
From _end In EndKeyword
From _st2 In ST
Select New EnumDefinition(_enum.Value.Span, _end.Value.Span, enumName, elements)
'=======================================================================
' Functions
'=======================================================================
ParameterList.Rule =
ParameterDeclaration.Many(Comma.AsTerminal().SuffixedBy(LineContinuation))
MethodParameterList.Rule =
ParameterList Or
From extparam In ExtensionMethodParameterDeclaration
From restParams In (
From _1 In Comma.AsTerminal
From _lc In LineContinuation
From params In ParameterDeclaration.Many1(Comma.AsTerminal().SuffixedBy(LineContinuation))
Select params).Optional()
Select {extparam}.Concat(If(restParams, {}))
Dim ParamPrefix =
From prefix In Grammar.Union(SelectKeyword, CaseKeyword)
Select prefix.Value
ParameterDeclaration.Rule =
From prefix In ParamPrefix.Optional
From did In DeclaringIdentifier
From typesp In TypeSpecifier.Optional()
Select New NormalParameterDeclaration(did, typesp, If(prefix IsNot Nothing, New ParameterPrefix(prefix), Nothing)).ToBase
ExtensionMethodParameterDeclaration.Rule =
From prefix In ParamPrefix.Optional
From _me In MeKeyword
From typesp In TypeSpecifier.Optional()
Select New ExtensionParameterDeclaration(typesp, If(prefix IsNot Nothing, New ParameterPrefix(prefix), Nothing)).ToBase
LambdaParameterList.Rule =
LambdaParameterDeclaration.Many(Comma.AsTerminal().SuffixedBy(LineContinuation))
LambdaParameterDeclaration.Rule =
From did In DeclaringIdentifier
From typesp In TypeSpecifier.Optional()
Select New NormalParameterDeclaration(did, typesp, Nothing).ToBase
TypeSpecifier.Rule =
From _colon In Colon
From _nl In LineContinuation
From spTypeName In TypeName
Select New TypeSpecifier(spTypeName)
'FunctionDeclaration := fun name ( arglist ) <st>
FunctionSignature.Rule =
From _fun In FunctionKeyword
From name In DeclaringIdentifier
From typeParams In TypeParameters.Optional
From _lpth In LeftPth
From _nl1 In LineContinuation
From paramlist In MethodParameterList
From _nl2 In LineContinuation
From _rpth In RightPth
From returnTypeSp In TypeSpecifier.Optional()
From whereClauses In ConstraintClauses.Optional()
From _st In ST
Select New FunctionSignature(_fun.Value.Span, name, paramlist, returnTypeSp, typeParams, whereClauses)
FunctionDefinition.Rule =
From decl In FunctionSignature
From ss In Statements
From endfun In EndKeyword
From _st2 In ST
Select New FunctionDefinition(decl, ss, endfun.Value.Span)
'=======================================================================
' Operators
'=======================================================================
OverloadableOperator.Rule =
NotEqualOperator Or ShiftRightOperator Or
From op In Grammar.Union(MinusSymbol, PlusSymbol, NotKeyword,
Asterisk, Slash, ModKeyword, ShiftLeft,
GreaterSymbol, GreaterEqual, LessSymbol, LessEqual, EqualSymbol,
AndKeyword, XorKeyword, OrKeyword)
Select op.Value
'OperatorDeclaration := operator op ( arglist ) <st>
OperatorSignature.Rule =
From _operator In OperatorKeyword
From op In OverloadableOperator
From typeParams In TypeParameters.Optional
From _lpth In LeftPth
From _nl1 In LineContinuation
From paramlist In ParameterList
From _nl2 In LineContinuation
From _rpth In RightPth
From returnTypeSp In TypeSpecifier.Optional()
From whereClauses In ConstraintClauses.Optional()
From _st In ST
Select New OperatorSignature(_operator.Value.Span, op, paramlist, returnTypeSp, typeParams, whereClauses)
OperatorDefinition.Rule =
From decl In OperatorSignature
From statements In statements
From endfun In EndKeyword
From _st In ST
Select New OperatorDefinition(decl, statements, endfun.Value.Span)
'=======================================================================
' Concepts
'=======================================================================
ConceptDeclaration.Rule =
From _concept In ConceptKeyword
From name In DeclaringIdentifier
From typeParams In TypeParameters
From whereClauses In ConstraintClauses.Optional
From _st In ST
Select New ConceptDeclaration(_concept.Value.Span, name, typeParams, whereClauses)
Dim ConstraintClause = ConceptConstraintClause Or TypeConstraintClause
ConstraintClauses.Rule =
From _where In WhereKeyword
From _lc In LineContinuation
From constraints In ConstraintClause.Many1(Comma.AsTerminal().SuffixedBy(LineContinuation))
Select constraints
ConceptConstraintClause.Rule =
From conceptName In ReferenceIdentifier
From typeArgs In TypeArguments
Select New ConceptConstraintClause(conceptName, typeArgs).ToBase
'(A,B => C,D)
TypeConstraintClause.Rule =
From _lph In LeftPth
From _lc1 In LineContinuation
From leftTypes In ReferenceIdentifier.Many1(Comma.Concat(LineContinuation))
From _arrow In Arrow
From _lc2 In LineContinuation
From rightTypes In ReferenceIdentifier.Many1(Comma.Concat(LineContinuation))
From _rph In RightPth
Select New TypeConstraintClause(leftTypes, rightTypes).ToBase
ConcreteDeclaration.Rule =
From _concrete In ConcreteKeyword
From typeParams In TypeParameters.Optional
From conceptName In ReferenceIdentifier
From typeArgs In TypeArguments
From whereClauses In ConstraintClauses.Optional
From _st In ST
Select New ConcreteDeclaration(_concrete.Value.Span, typeParams, conceptName, typeArgs, whereClauses)
Dim ProcedureDeclarationOrDefinition =
ProcedureDeclaration.Select(Function(d) d.ToBase) Or
ProcedureDefinition
ConceptDefinition.Rule =
From decl In ConceptDeclaration
From procedures In ProcedureDeclarationOrDefinition.Many()
From _end In EndKeyword
From _st In ST
Select New ConceptDefinition(decl, procedures, _end.Value.Span)
Dim ProcedureSignature =
FunctionSignature.Select(Function(s) s.ToBase) Or
OperatorSignature.Select(Function(s) s.ToBase)
ProcedureDeclaration.Rule =
From _decl In DeclareKeyword
From signature In ProcedureSignature
Select New ProcedureDeclaration(_decl.Value.Span, signature)
ProcedureDefinition.Rule =
FunctionDefinition.Select(Function(d) d.ToBase()) Or
OperatorDefinition.Select(Function(d) d.ToBase())
ConcreteDefinition.Rule =
From decl In ConcreteDeclaration
From procedures In ProcedureDefinition.Many()
From _end In EndKeyword
From _st In ST
Select New ConcreteDefinition(decl, procedures, _end.Value.Span)
'=======================================================================
' Statements
'=======================================================================
Statements.Rule =
Statement.Many1(ST).SuffixedBy(ST) Or
Grammar.Empty(Of IEnumerable(Of Statement))(Nothing)
StatementsBlock.Rule =
From _lbr In LeftBrce
From _st1 In ST.Optional()
From s In Statement.Many(ST)
From _st2 In ST.Optional()
From _rbr In RightBrce
Select s
Statement.Rule =
SingleLineStatement Or
BlockStatement
SingleLineStatement.Rule =
SingleLineOpenStatement Or
SingleLineClosedStatement
SingleLineClosedStatement.Rule =
ReturnStatement Or
AssignmentStatement Or
IfThenElseClosedStatement Or
ExpressionStatement Or
ContinueStatement Or
ExitStatement
SingleLineOpenStatement.Rule =
IfThenStatement Or
IfThenElseOpenStatement
BlockStatement.Rule =
IfBlockStatement Or
DoStatement Or
ForStatement Or
ForEachStatement Or
TryStatement Or
SelectCaseStatement
ReturnStatement.Rule =
From keyword In ReturnKeyword
From _lc In LineContinuation
From returnValue In Expression.Optional()
Select New ReturnStatement(keyword.Value.Span, returnValue).ToBase
AssignmentStatement.Rule =
From id In ReferenceIdentifier
From _eq In EqualSymbol
From _lc In LineContinuation
From value In Expression
Select New AssignmentStatement(id, value).ToBase
ExpressionStatement.Rule =
From exp In CallExpression
Select New ExpressionStatement(exp).ToBase
IfThenStatement.Rule =
From _if In IfKeyword
From condition In Expression
From _then In ThenKeyword
From trueStatement In SingleLineStatement
Select New IfThenStatement(_if.Value.Span, condition, trueStatement, Nothing).ToBase
IfThenElseOpenStatement.Rule =
From _if In IfKeyword
From condition In Expression
From _then In ThenKeyword
From trueStatement In SingleLineClosedStatement
From elsePart In (
From _else In ElseKeyword
From elseStatement In SingleLineOpenStatement
Select elseStatement)
Select New IfThenStatement(_if.Value.Span, condition, trueStatement, elsePart).ToBase
IfThenElseClosedStatement.Rule =
From _if In IfKeyword
From condition In Expression
From _then In ThenKeyword
From trueStatement In SingleLineClosedStatement
From elsePart In (
From _else In ElseKeyword
From elseStatement In SingleLineClosedStatement
Select elseStatement)
Select New IfThenStatement(_if.Value.Span, condition, trueStatement, elsePart).ToBase
Dim ElseIfBlock =
From _elseif In ElseIfKeyword
From condition In Expression
From _st In ST
From elseIfTruePart In Statements
Select New ElseIfBlock(_elseif.Value.Span, condition, elseIfTruePart)
Dim ElseBlock =
From _else In ElseKeyword
From _st In ST
From elsePart In Statements
Select New ElseBlock(_else.Value.Span, elsePart)
IfBlockStatement.Rule =
From _if In IfKeyword
From condition In Expression
From _st1 In ST
From truePart In Statements
From elseIfBlocks In ElseIfBlock.Many
From elseBlockOpt In ElseBlock.Optional
From _end In EndKeyword
Select New IfBlockStatement(_if.Value.Span, _end.Value.Span, condition, truePart, elseIfBlocks, elseBlockOpt).ToBase
Dim DoLoopForm =
From _do In DoKeyword
From _st In ST
From loopBody In Statements
From _loop In LoopKeyword
Select DoLoopStatement.DoLoopFrom(_do.Value.Span, _loop.Value.Span, loopBody)
Dim DoWhileLoopForm =
From _do In DoKeyword
From _while In WhileKeyword
From condition In Expression
From _st In ST
From loopBody In Statements
From _loop In LoopKeyword
Select DoLoopStatement.DoWhileLoopFrom(_do.Value.Span, _while.Value.Span, _loop.Value.Span, condition, loopBody)
Dim DoUntilLoopForm =
From _do In DoKeyword
From _until In UntilKeyword
From condition In Expression
From _st In ST
From loopBody In Statements
From _loop In LoopKeyword
Select DoLoopStatement.DoUntilLoopFrom(_do.Value.Span, _until.Value.Span, _loop.Value.Span, condition, loopBody)
Dim DoLoopWhileForm =
From _do In DoKeyword
From _st In ST
From loopBody In Statements
From _loop In LoopKeyword
From _while In WhileKeyword
From condition In Expression
Select DoLoopStatement.DoLoopWhileFrom(_do.Value.Span, _while.Value.Span, _loop.Value.Span, condition, loopBody)
Dim DoLoopUntilForm =
From _do In DoKeyword
From _st In ST
From loopBody In Statements
From _loop In LoopKeyword
From _until In UntilKeyword
From condition In Expression
Select DoLoopStatement.DoLoopUntilFrom(_do.Value.Span, _until.Value.Span, _loop.Value.Span, condition, loopBody)
DoStatement.Rule =
DoLoopForm Or
DoWhileLoopForm Or
DoUntilLoopForm Or
DoLoopWhileForm Or
DoLoopUntilForm
ContinueStatement.Rule =
From _continue In ContinueKeyword
From loopStruct In Grammar.Union(ForKeyword, DoKeyword)
Select New ContinueStatement(_continue.Value.Span, loopStruct.Value).ToBase
ExitStatement.Rule =
From _exit In ExitKeyword
From exitStruct In Grammar.Union(ForKeyword, DoKeyword, TryKeyword, SelectKeyword, FunctionKeyword)
Select New ExitStatement(_exit.Value.Span, exitStruct.Value).ToBase
Dim CatchWithTypeBlock =
From _catch In CatchKeyword
From exceptVar In DeclaringIdentifier.Optional
From exceptType In TypeSpecifier
From _st In ST
From catchBody In Statements
Select New CatchBlock(_catch.Value.Span, exceptVar, exceptType, catchBody)