-
Notifications
You must be signed in to change notification settings - Fork 2
/
Parser.vb
5707 lines (4428 loc) · 211 KB
/
Parser.vb
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
'
' Visual Basic .NET Parser
'
' Copyright (C) 2004, Microsoft Corporation. All rights reserved.
'
' THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
' EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
' MERCHANTIBILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
'
''' <summary>
''' A parser for the Visual Basic .NET language based on the grammar
''' documented in the Language Specification.
''' </summary>
Public NotInheritable Class Parser
Implements IDisposable
Private Enum PrecedenceLevel
None
[Xor]
[Or]
[And]
[Not]
Relational
Shift
Concatenate
Plus
Modulus
IntegralDivide
Multiply
Negate
Power
End Enum
Private NotInheritable Class ExternalSourceContext
Public Start As Location
Public File As String
Public Line As Long
End Class
Private NotInheritable Class RegionContext
Public Start As Location
Public Description As String
End Class
Private NotInheritable Class ConditionalCompilationContext
Public BlockActive As Boolean
Public AnyBlocksActive As Boolean
Public SeenElse As Boolean
End Class
' The scanner we're going to be using.
Private Scanner As Scanner
' The error table for the parsing
Private ErrorTable As ArrayList
' External line mappings
Private ExternalLineMappings As ArrayList
' Source regions
Private SourceRegions As ArrayList
' Conditional compilation constants
Private ConditionalCompilationConstants As Hashtable
' Whether there is an error in the construct
Private ErrorInConstruct As Boolean
' Whether we're at the beginning of a line
Private AtBeginningOfLine As Boolean
' Whether we're doing preprocessing or not
Private Preprocess As Boolean
' The current stack of block contexts
Private BlockContextStack As Stack = New Stack
' The current external source context
Private CurrentExternalSourceContext As ExternalSourceContext
' The current stack of region contexts
Private RegionContextStack As Stack = New Stack
' The current stack of conditional compilation states
Private ConditionalCompilationContextStack As Stack = New Stack
' Determine whether we have been disposed already or not
Private Disposed As Boolean = False
''' <summary>
''' Disposes the parser.
''' </summary>
Public Sub Dispose() Implements IDisposable.Dispose
If Not Disposed Then
Disposed = True
Scanner.Close()
End If
End Sub
'*
'* Token reading functions
'*
Private Function Peek() As Token
Return Scanner.Peek()
End Function
Private Function PeekAheadFor(ByVal ParamArray tokens() As TokenType) As TokenType
Dim Start As Token = Peek()
Dim Current As Token = Start
While Not CanEndStatement(Current.Type)
For Each Token As TokenType In tokens
If Current.Type = Token Then
Backtrack(Start)
Return Current.Type
End If
Next
Current = Read()
End While
Backtrack(Start)
Return TokenType.None
End Function
Private Function Read() As Token
Return Scanner.Read()
End Function
Private Function ReadLocation() As Location
Return Read().Span.Start
End Function
Private Sub Backtrack(ByVal token As Token)
Scanner.Seek(token)
End Sub
Private Sub ResyncAt(ByVal ParamArray tokenTypes() As TokenType)
Dim CurrentToken As Token = Peek()
While CurrentToken.Type <> TokenType.Colon AndAlso _
CurrentToken.Type <> TokenType.EndOfStream AndAlso _
CurrentToken.Type <> TokenType.LineTerminator AndAlso _
Not BeginsStatement(CurrentToken.Type)
For Each TokenType As TokenType In tokenTypes
' CONSIDER: Need to check for non-reserved tokens?
If CurrentToken.Type = TokenType Then
Return
End If
Next
Read()
CurrentToken = Peek()
End While
End Sub
Private Function ParseTrailingComments() As CommentCollection
Dim Comments As ArrayList = New ArrayList
' Link in comments that follow the statement
While Peek().Type = TokenType.Comment
Dim CommentToken As CommentToken = CType(Scanner.Read(), CommentToken)
Comments.Add(New Comment(CommentToken.Comment, CommentToken.IsREM, CommentToken.Span))
End While
If Comments.Count > 0 Then
Dim NewList As CommentCollection = New CommentCollection(Comments)
Comments.Clear()
Return NewList
End If
End Function
'*
'* Helpers
'*
Private Sub PushBlockContext(ByVal type As TreeType)
BlockContextStack.Push(type)
End Sub
Private Sub PopBlockContext()
BlockContextStack.Pop()
End Sub
Private Function CurrentBlockContextType() As TreeType
If BlockContextStack.Count = 0 Then
Return TreeType.SyntaxError
Else
Return DirectCast(BlockContextStack.Peek(), TreeType)
End If
End Function
Private Shared Function GetBinaryOperator(ByVal type As TokenType) As OperatorType
Select Case type
Case TokenType.Ampersand
Return OperatorType.Concatenate
Case TokenType.Star
Return OperatorType.Multiply
Case TokenType.ForwardSlash
Return OperatorType.Divide
Case TokenType.BackwardSlash
Return OperatorType.IntegralDivide
Case TokenType.Caret
Return OperatorType.Power
Case TokenType.Plus
Return OperatorType.Plus
Case TokenType.Minus
Return OperatorType.Minus
Case TokenType.LessThan
Return OperatorType.LessThan
Case TokenType.LessThanEquals
Return OperatorType.LessThanEquals
Case TokenType.Equals
Return OperatorType.Equals
Case TokenType.NotEquals
Return OperatorType.NotEquals
Case TokenType.GreaterThan
Return OperatorType.GreaterThan
Case TokenType.GreaterThanEquals
Return OperatorType.GreaterThanEquals
Case TokenType.LessThanLessThan
Return OperatorType.ShiftLeft
Case TokenType.GreaterThanGreaterThan
Return OperatorType.ShiftRight
Case TokenType.Mod
Return OperatorType.Modulus
Case TokenType.Or
Return OperatorType.Or
Case TokenType.OrElse
Return OperatorType.OrElse
Case TokenType.And
Return OperatorType.And
Case TokenType.AndAlso
Return OperatorType.AndAlso
Case TokenType.Xor
Return OperatorType.Xor
Case TokenType.Like
Return OperatorType.Like
Case TokenType.Is
Return OperatorType.Is
Case Else
Return OperatorType.None
End Select
End Function
Private Shared Function GetOperatorPrecedence(ByVal operator As OperatorType) As PrecedenceLevel
Select Case operator
Case OperatorType.Power
Return PrecedenceLevel.Power
Case OperatorType.Negate, OperatorType.UnaryPlus
Return PrecedenceLevel.Negate
Case OperatorType.Multiply, OperatorType.Divide
Return PrecedenceLevel.Multiply
Case OperatorType.IntegralDivide
Return PrecedenceLevel.IntegralDivide
Case OperatorType.Modulus
Return PrecedenceLevel.Modulus
Case OperatorType.Plus, OperatorType.Minus
Return PrecedenceLevel.Plus
Case OperatorType.Concatenate
Return PrecedenceLevel.Concatenate
Case OperatorType.ShiftLeft, OperatorType.ShiftRight
Return PrecedenceLevel.Shift
Case OperatorType.Equals, _
OperatorType.NotEquals, _
OperatorType.LessThan, _
OperatorType.GreaterThan, _
OperatorType.GreaterThanEquals, _
OperatorType.LessThanEquals, _
OperatorType.Is, _
OperatorType.Like
Return PrecedenceLevel.Relational
Case OperatorType.Not
Return PrecedenceLevel.Not
Case OperatorType.And, OperatorType.AndAlso
Return PrecedenceLevel.And
Case OperatorType.Or, OperatorType.OrElse
Return PrecedenceLevel.Or
Case OperatorType.Xor
Return PrecedenceLevel.Xor
Case Else
Return PrecedenceLevel.None
End Select
End Function
Private Shared Function GetAssignmentOperator(ByVal tokenType As TokenType, Optional ByRef compoundOperator As OperatorType = OperatorType.None) As TreeType
Dim Type As TreeType
Select Case tokenType
Case tokenType.Equals
Type = TreeType.AssignmentStatement
compoundOperator = OperatorType.None
Case tokenType.PlusEquals
compoundOperator = OperatorType.Plus
Case tokenType.AmpersandEquals
compoundOperator = OperatorType.Concatenate
Case tokenType.StarEquals
compoundOperator = OperatorType.Multiply
Case tokenType.MinusEquals
compoundOperator = OperatorType.Minus
Case tokenType.ForwardSlashEquals
compoundOperator = OperatorType.Divide
Case tokenType.BackwardSlashEquals
compoundOperator = OperatorType.IntegralDivide
Case tokenType.CaretEquals
compoundOperator = OperatorType.Power
Case tokenType.LessThanLessThanEquals
compoundOperator = OperatorType.ShiftLeft
Case tokenType.GreaterThanGreaterThanEquals
compoundOperator = OperatorType.ShiftRight
Case Else
Type = TreeType.SyntaxError
compoundOperator = OperatorType.None
End Select
If compoundOperator <> TreeType.SyntaxError Then
Type = TreeType.CompoundAssignmentStatement
End If
Return Type
End Function
Private Shared Function IsRelationalOperator(ByVal type As TokenType) As Boolean
Return type >= TokenType.LessThan AndAlso type <= TokenType.GreaterThanEquals
End Function
Private Shared Function GetExitType(ByVal tokenType As TokenType) As BlockType
Select Case tokenType
Case tokenType.Do
Return BlockType.Do
Case tokenType.For
Return BlockType.For
Case tokenType.While
Return BlockType.While
Case tokenType.Select
Return BlockType.Select
Case tokenType.Sub
Return BlockType.Sub
Case tokenType.Function
Return BlockType.Function
Case tokenType.Property
Return BlockType.Property
Case tokenType.Try
Return BlockType.Try
Case Else
Return BlockType.None
End Select
End Function
Private Shared Function GetBlockType(ByVal type As TokenType) As BlockType
Select Case type
Case TokenType.While
Return BlockType.While
Case TokenType.Select
Return BlockType.Select
Case TokenType.If
Return BlockType.If
Case TokenType.Try
Return BlockType.Try
Case TokenType.SyncLock
Return BlockType.SyncLock
Case TokenType.With
Return BlockType.With
Case TokenType.Sub
Return BlockType.Sub
Case TokenType.Function
Return BlockType.Function
Case TokenType.Get
Return BlockType.Get
Case TokenType.Set
Return BlockType.Set
Case TokenType.Property
Return BlockType.Property
Case TokenType.Class
Return BlockType.Class
Case TokenType.Structure
Return BlockType.Structure
Case TokenType.Module
Return BlockType.Module
Case TokenType.Interface
Return BlockType.Interface
Case TokenType.Enum
Return BlockType.Enum
Case TokenType.Namespace
Return BlockType.Namespace
Case Else
Return BlockType.None
End Select
End Function
Private Sub ReportSyntaxError(ByVal syntaxError As SyntaxError)
If ErrorInConstruct Then
Return
End If
ErrorInConstruct = True
If Not ErrorTable Is Nothing Then
ErrorTable.Add(syntaxError)
End If
End Sub
Private Sub ReportSyntaxError(ByVal errorType As SyntaxErrorType, ByVal span As Span)
ReportSyntaxError(New SyntaxError(errorType, span))
End Sub
Private Sub ReportSyntaxError(ByVal errorType As SyntaxErrorType, ByVal firstToken As Token, ByVal lastToken As Token)
' A lexical error takes precedence over the parser error
If firstToken.Type = TokenType.LexicalError Then
ReportSyntaxError(CType(firstToken, ErrorToken).SyntaxError)
Else
ReportSyntaxError(errorType, SpanFrom(firstToken, lastToken))
End If
End Sub
Private Sub ReportSyntaxError(ByVal errorType As SyntaxErrorType, ByVal token As Token)
ReportSyntaxError(errorType, token, token)
End Sub
Private Function StatementEndsBlock(ByVal blockStatementType As TreeType, ByVal endStatement As Statement) As Boolean
Select Case blockStatementType
Case TreeType.DoBlockStatement
Return endStatement.Type = TreeType.LoopStatement
Case TreeType.ForBlockStatement, TreeType.ForEachBlockStatement
Return endStatement.Type = TreeType.NextStatement
Case TreeType.WhileBlockStatement
Return (endStatement.Type = TreeType.EndBlockStatement) AndAlso CType(endStatement, EndBlockStatement).EndType = BlockType.While
Case TreeType.SyncLockBlockStatement
Return (endStatement.Type = TreeType.EndBlockStatement) AndAlso CType(endStatement, EndBlockStatement).EndType = BlockType.SyncLock
Case TreeType.WithBlockStatement
Return (endStatement.Type = TreeType.EndBlockStatement) AndAlso CType(endStatement, EndBlockStatement).EndType = BlockType.With
Case TreeType.TryBlockStatement, TreeType.CatchBlockStatement
Return endStatement.Type = TreeType.CatchStatement OrElse _
endStatement.Type = TreeType.FinallyStatement OrElse _
(endStatement.Type = TreeType.EndBlockStatement AndAlso CType(endStatement, EndBlockStatement).EndType = BlockType.Try)
Case TreeType.FinallyBlockStatement
Return endStatement.Type = TreeType.EndBlockStatement AndAlso CType(endStatement, EndBlockStatement).EndType = BlockType.Try
Case TreeType.SelectBlockStatement, TreeType.CaseBlockStatement
Return endStatement.Type = TreeType.CaseStatement OrElse _
endStatement.Type = TreeType.CaseElseStatement OrElse _
(endStatement.Type = TreeType.EndBlockStatement AndAlso CType(endStatement, EndBlockStatement).EndType = BlockType.Select)
Case TreeType.CaseElseBlockStatement
Return endStatement.Type = TreeType.EndBlockStatement AndAlso CType(endStatement, EndBlockStatement).EndType = BlockType.Select
Case TreeType.IfBlockStatement, TreeType.ElseIfBlockStatement
Return endStatement.Type = TreeType.ElseIfStatement OrElse _
endStatement.Type = TreeType.ElseStatement OrElse _
(endStatement.Type = TreeType.EndBlockStatement AndAlso CType(endStatement, EndBlockStatement).EndType = BlockType.If)
Case TreeType.ElseBlockStatement
Return endStatement.Type = TreeType.EndBlockStatement AndAlso CType(endStatement, EndBlockStatement).EndType = BlockType.If
Case TreeType.LineIfBlockStatement
Return False
Case TreeType.SubDeclaration, TreeType.ConstructorDeclaration
Return endStatement.Type = TreeType.EndBlockStatement AndAlso CType(endStatement, EndBlockStatement).EndType = BlockType.Sub
Case TreeType.FunctionDeclaration
Return endStatement.Type = TreeType.EndBlockStatement AndAlso CType(endStatement, EndBlockStatement).EndType = BlockType.Function
Case TreeType.GetDeclaration
Return endStatement.Type = TreeType.EndBlockStatement AndAlso CType(endStatement, EndBlockStatement).EndType = BlockType.Get
Case TreeType.SetDeclaration
Return endStatement.Type = TreeType.EndBlockStatement AndAlso CType(endStatement, EndBlockStatement).EndType = BlockType.Set
Case TreeType.PropertyDeclaration
Return endStatement.Type = TreeType.EndBlockStatement AndAlso CType(endStatement, EndBlockStatement).EndType = BlockType.Property
Case TreeType.ClassDeclaration
Return endStatement.Type = TreeType.EndBlockStatement AndAlso CType(endStatement, EndBlockStatement).EndType = BlockType.Class
Case TreeType.StructureDeclaration
Return endStatement.Type = TreeType.EndBlockStatement AndAlso CType(endStatement, EndBlockStatement).EndType = BlockType.Structure
Case TreeType.ModuleDeclaration
Return endStatement.Type = TreeType.EndBlockStatement AndAlso CType(endStatement, EndBlockStatement).EndType = BlockType.Module
Case TreeType.InterfaceDeclaration
Return endStatement.Type = TreeType.EndBlockStatement AndAlso CType(endStatement, EndBlockStatement).EndType = BlockType.Interface
Case TreeType.EnumDeclaration
Return endStatement.Type = TreeType.EndBlockStatement AndAlso CType(endStatement, EndBlockStatement).EndType = BlockType.Enum
Case TreeType.NamespaceDeclaration
Return endStatement.Type = TreeType.EndBlockStatement AndAlso CType(endStatement, EndBlockStatement).EndType = BlockType.Namespace
Case Else
Debug.Fail("Unexpected.")
End Select
Return False
End Function
Private Function DeclarationEndsBlock(ByVal blockDeclarationType As TreeType, ByVal endDeclaration As EndBlockDeclaration) As Boolean
Select Case blockDeclarationType
Case TreeType.SubDeclaration, TreeType.ConstructorDeclaration
Return endDeclaration.EndType = BlockType.Sub
Case TreeType.FunctionDeclaration
Return endDeclaration.EndType = BlockType.Function
Case TreeType.PropertyDeclaration
Return endDeclaration.EndType = BlockType.Property
Case TreeType.GetDeclaration
Return endDeclaration.EndType = BlockType.Get
Case TreeType.SetDeclaration
Return endDeclaration.EndType = BlockType.Set
Case TreeType.ClassDeclaration
Return endDeclaration.EndType = BlockType.Class
Case TreeType.StructureDeclaration
Return endDeclaration.EndType = BlockType.Structure
Case TreeType.ModuleDeclaration
Return endDeclaration.EndType = BlockType.Module
Case TreeType.InterfaceDeclaration
Return endDeclaration.EndType = BlockType.Interface
Case TreeType.EnumDeclaration
Return endDeclaration.EndType = BlockType.Enum
Case TreeType.NamespaceDeclaration
Return endDeclaration.EndType = BlockType.Namespace
Case Else
Debug.Fail("Unexpected.")
End Select
Return False
End Function
Private Function ValidInContext(ByVal blockType As TreeType, ByVal declarationType As TreeType) As Boolean
Select Case declarationType
Case TreeType.OptionDeclaration, TreeType.ImportsDeclaration, TreeType.AttributeDeclaration
Return blockType = TreeType.File
Case TreeType.NamespaceDeclaration
Return blockType = TreeType.NamespaceDeclaration OrElse _
blockType = TreeType.File
Case TreeType.ClassDeclaration, TreeType.StructureDeclaration, TreeType.InterfaceDeclaration, _
TreeType.DelegateSubDeclaration, TreeType.DelegateFunctionDeclaration, TreeType.EnumDeclaration
Return blockType = TreeType.ClassDeclaration OrElse _
blockType = TreeType.StructureDeclaration OrElse _
blockType = TreeType.ModuleDeclaration OrElse _
blockType = TreeType.InterfaceDeclaration OrElse _
blockType = TreeType.NamespaceDeclaration OrElse _
blockType = TreeType.File
Case TreeType.ModuleDeclaration
Return blockType = TreeType.NamespaceDeclaration OrElse _
blockType = TreeType.File
Case TreeType.EventDeclaration, TreeType.SubDeclaration, TreeType.FunctionDeclaration, _
TreeType.PropertyDeclaration
Return blockType = TreeType.ClassDeclaration OrElse _
blockType = TreeType.StructureDeclaration OrElse _
blockType = TreeType.ModuleDeclaration OrElse _
blockType = TreeType.InterfaceDeclaration
Case TreeType.VariableListDeclaration, TreeType.ExternalSubDeclaration, _
TreeType.ExternalFunctionDeclaration
Return blockType = TreeType.ClassDeclaration OrElse _
blockType = TreeType.StructureDeclaration OrElse _
blockType = TreeType.ModuleDeclaration
Case TreeType.ConstructorDeclaration
Return blockType = TreeType.ClassDeclaration OrElse _
blockType = TreeType.StructureDeclaration
Case TreeType.GetDeclaration, TreeType.SetDeclaration
Return blockType = TreeType.PropertyDeclaration
Case TreeType.InheritsDeclaration
Return blockType = TreeType.ClassDeclaration OrElse _
blockType = TreeType.InterfaceDeclaration
Case TreeType.ImplementsDeclaration
Return blockType = TreeType.ClassDeclaration OrElse _
blockType = TreeType.StructureDeclaration
Case TreeType.EnumValueDeclaration
Return blockType = TreeType.EnumDeclaration
Case TreeType.EmptyDeclaration
Return True
Case Else
Debug.Fail("Unexpected.")
End Select
End Function
Private Function ValidDeclaration(ByVal blockType As TreeType, ByVal declaration As Declaration, ByVal declarations As ArrayList) As SyntaxErrorType
If Not ValidInContext(blockType, declaration.Type) Then
Return InvalidDeclarationTypeError(blockType, declaration)
End If
If declaration.Type = TreeType.InheritsDeclaration Then
For Each ExistingDeclaration As Declaration In declarations
If blockType = TreeType.ClassDeclaration OrElse ExistingDeclaration.Type <> TreeType.InheritsDeclaration Then
Return SyntaxErrorType.InheritsMustBeFirst
End If
Next
If CType(declaration, InheritsDeclaration).InheritedTypes.Count > 1 AndAlso blockType <> TreeType.InterfaceDeclaration Then
Return SyntaxErrorType.NoMultipleInheritance
End If
End If
If declaration.Type = TreeType.ImplementsDeclaration Then
For Each ExistingDeclaration As Declaration In declarations
If ExistingDeclaration.Type <> TreeType.InheritsDeclaration AndAlso _
ExistingDeclaration.Type <> TreeType.ImplementsDeclaration Then
Return SyntaxErrorType.ImplementsInWrongOrder
End If
Next
End If
If declaration.Type = TreeType.OptionDeclaration Then
For Each ExistingDeclaration As Declaration In declarations
If ExistingDeclaration.Type <> TreeType.OptionDeclaration Then
Return SyntaxErrorType.OptionStatementWrongOrder
End If
Next
End If
If declaration.Type = TreeType.ImportsDeclaration Then
For Each ExistingDeclaration As Declaration In declarations
If ExistingDeclaration.Type <> TreeType.OptionDeclaration AndAlso _
Existingdeclaration.Type <> TreeType.ImportsDeclaration Then
Return SyntaxErrorType.ImportsStatementWrongOrder
End If
Next
End If
If declaration.Type = TreeType.AttributeDeclaration Then
For Each ExistingDeclaration As Declaration In declarations
If ExistingDeclaration.Type <> TreeType.OptionDeclaration AndAlso _
ExistingDeclaration.Type <> TreeType.ImportsDeclaration AndAlso _
ExistingDeclaration.Type <> TreeType.AttributeDeclaration Then
Return SyntaxErrorType.AttributesStatementWrongOrder
End If
Next
End If
Return SyntaxErrorType.None
End Function
Private Sub ReportMismatchedEndError(ByVal blockType As TreeType, ByVal actualEndSpan As Span)
Dim ErrorType As SyntaxErrorType
Select Case blockType
Case TreeType.DoBlockStatement
ErrorType = SyntaxErrorType.ExpectedLoop
Case TreeType.ForBlockStatement, TreeType.ForEachBlockStatement
ErrorType = SyntaxErrorType.ExpectedNext
Case TreeType.WhileBlockStatement
ErrorType = SyntaxErrorType.ExpectedEndWhile
Case TreeType.SelectBlockStatement, TreeType.CaseBlockStatement, TreeType.CaseElseBlockStatement
ErrorType = SyntaxErrorType.ExpectedEndSelect
Case TreeType.SyncLockBlockStatement
ErrorType = SyntaxErrorType.ExpectedEndSyncLock
Case TreeType.IfBlockStatement, TreeType.ElseIfBlockStatement, TreeType.ElseBlockStatement
ErrorType = SyntaxErrorType.ExpectedEndIf
Case TreeType.TryBlockStatement, TreeType.CatchBlockStatement, TreeType.FinallyBlockStatement
ErrorType = SyntaxErrorType.ExpectedEndTry
Case TreeType.WithBlockStatement
ErrorType = SyntaxErrorType.ExpectedEndWith
Case TreeType.SubDeclaration, TreeType.ConstructorDeclaration
ErrorType = SyntaxErrorType.ExpectedEndSub
Case TreeType.FunctionDeclaration
ErrorType = SyntaxErrorType.ExpectedEndFunction
Case TreeType.PropertyDeclaration
ErrorType = SyntaxErrorType.ExpectedEndProperty
Case TreeType.GetDeclaration
ErrorType = SyntaxErrorType.ExpectedEndGet
Case TreeType.SetDeclaration
ErrorType = SyntaxErrorType.ExpectedEndSet
Case TreeType.ClassDeclaration
ErrorType = SyntaxErrorType.ExpectedEndClass
Case TreeType.StructureDeclaration
ErrorType = SyntaxErrorType.ExpectedEndStructure
Case TreeType.ModuleDeclaration
ErrorType = SyntaxErrorType.ExpectedEndModule
Case TreeType.InterfaceDeclaration
ErrorType = SyntaxErrorType.ExpectedEndInterface
Case TreeType.EnumDeclaration
ErrorType = SyntaxErrorType.ExpectedEndEnum
Case TreeType.NamespaceDeclaration
ErrorType = SyntaxErrorType.ExpectedEndNamespace
Case Else
Debug.Fail("Unexpected.")
End Select
ReportSyntaxError(ErrorType, actualEndSpan)
End Sub
Private Sub ReportMissingBeginStatementError(ByVal blockStatementType As TreeType, ByVal endStatement As Statement)
Dim ErrorType As SyntaxErrorType
Select Case endStatement.Type
Case TreeType.LoopStatement
ErrorType = SyntaxErrorType.LoopWithoutDo
Case TreeType.NextStatement
ErrorType = SyntaxErrorType.NextWithoutFor
Case TreeType.EndBlockStatement
Select Case CType(endStatement, EndBlockStatement).EndType
Case BlockType.While
ErrorType = SyntaxErrorType.EndWhileWithoutWhile
Case BlockType.Select
ErrorType = SyntaxErrorType.EndSelectWithoutSelect
Case BlockType.SyncLock
ErrorType = SyntaxErrorType.EndSyncLockWithoutSyncLock
Case BlockType.If
ErrorType = SyntaxErrorType.EndIfWithoutIf
Case BlockType.Try
ErrorType = SyntaxErrorType.EndTryWithoutTry
Case BlockType.With
ErrorType = SyntaxErrorType.EndWithWithoutWith
Case Else
Debug.Fail("Unexpected.")
End Select
Case TreeType.CatchStatement
If blockStatementType = TreeType.FinallyBlockStatement Then
ErrorType = SyntaxErrorType.CatchAfterFinally
Else
ErrorType = SyntaxErrorType.CatchWithoutTry
End If
Case TreeType.FinallyStatement
If blockStatementType = TreeType.FinallyBlockStatement Then
ErrorType = SyntaxErrorType.FinallyAfterFinally
Else
ErrorType = SyntaxErrorType.FinallyWithoutTry
End If
Case TreeType.CaseStatement
If blockStatementType = TreeType.CaseElseBlockStatement Then
ErrorType = SyntaxErrorType.CaseAfterCaseElse
Else
ErrorType = SyntaxErrorType.CaseWithoutSelect
End If
Case TreeType.CaseElseStatement
If blockStatementType = TreeType.CaseElseBlockStatement Then
ErrorType = SyntaxErrorType.CaseElseAfterCaseElse
Else
ErrorType = SyntaxErrorType.CaseElseWithoutSelect
End If
Case TreeType.ElseIfStatement
If blockStatementType = TreeType.ElseBlockStatement Then
ErrorType = SyntaxErrorType.ElseIfAfterElse
Else
ErrorType = SyntaxErrorType.ElseIfWithoutIf
End If
Case TreeType.ElseStatement
If blockStatementType = TreeType.ElseBlockStatement Then
ErrorType = SyntaxErrorType.ElseAfterElse
Else
ErrorType = SyntaxErrorType.ElseWithoutIf
End If
Case Else
Debug.Fail("Unexpected.")
End Select
ReportSyntaxError(ErrorType, endStatement.Span)
End Sub
Private Sub ReportMissingBeginDeclarationError(ByVal endDeclaration As EndBlockDeclaration)
Dim ErrorType As SyntaxErrorType
Select Case endDeclaration.EndType
Case BlockType.Sub
ErrorType = SyntaxErrorType.EndSubWithoutSub
Case BlockType.Function
ErrorType = SyntaxErrorType.EndFunctionWithoutFunction
Case BlockType.Property
ErrorType = SyntaxErrorType.EndPropertyWithoutProperty
Case BlockType.Get
ErrorType = SyntaxErrorType.EndGetWithoutGet
Case BlockType.Set
ErrorType = SyntaxErrorType.EndSetWithoutSet
Case BlockType.Class
ErrorType = SyntaxErrorType.EndClassWithoutClass
Case BlockType.Structure
ErrorType = SyntaxErrorType.EndStructureWithoutStructure
Case BlockType.Module
ErrorType = SyntaxErrorType.EndModuleWithoutModule
Case BlockType.Interface
ErrorType = SyntaxErrorType.EndInterfaceWithoutInterface
Case BlockType.Enum
ErrorType = SyntaxErrorType.EndEnumWithoutEnum
Case BlockType.Namespace
ErrorType = SyntaxErrorType.EndNamespaceWithoutNamespace
Case Else
Debug.Fail("Unexpected.")
End Select
ReportSyntaxError(ErrorType, endDeclaration.Span)
End Sub
Private Function InvalidDeclarationTypeError(ByVal blockType As TreeType, ByVal badDeclaration As Declaration) As SyntaxErrorType
Dim ErrorType As SyntaxErrorType
Select Case blockType
Case TreeType.PropertyDeclaration
ErrorType = SyntaxErrorType.InvalidInsideProperty
Case TreeType.ClassDeclaration
ErrorType = SyntaxErrorType.InvalidInsideClass
Case TreeType.StructureDeclaration
ErrorType = SyntaxErrorType.InvalidInsideStructure
Case TreeType.ModuleDeclaration
ErrorType = SyntaxErrorType.InvalidInsideModule
Case TreeType.InterfaceDeclaration
ErrorType = SyntaxErrorType.InvalidInsideInterface
Case TreeType.EnumDeclaration
ErrorType = SyntaxErrorType.InvalidInsideEnum
Case TreeType.NamespaceDeclaration, TreeType.File
ErrorType = SyntaxErrorType.InvalidInsideNamespace
Case Else
Debug.Fail("Unexpected.")
End Select
Return ErrorType
End Function
Private Sub HandleUnexpectedToken(ByVal type As TokenType)
Dim ErrorType As SyntaxErrorType
Select Case type
Case TokenType.Comma
ErrorType = SyntaxErrorType.ExpectedComma
Case TokenType.LeftParenthesis
ErrorType = SyntaxErrorType.ExpectedLeftParenthesis
Case TokenType.RightParenthesis
ErrorType = SyntaxErrorType.ExpectedRightParenthesis
Case TokenType.Equals
ErrorType = SyntaxErrorType.ExpectedEquals
Case TokenType.As
ErrorType = SyntaxErrorType.ExpectedAs
Case TokenType.RightCurlyBrace
ErrorType = SyntaxErrorType.ExpectedRightCurlyBrace
Case TokenType.Period
ErrorType = SyntaxErrorType.ExpectedPeriod
Case TokenType.Minus
ErrorType = SyntaxErrorType.ExpectedMinus
Case TokenType.Is
ErrorType = SyntaxErrorType.ExpectedIs
Case TokenType.GreaterThan
ErrorType = SyntaxErrorType.ExpectedGreaterThan
Case Else
Debug.Fail("Should give a more specific error.")
ErrorType = SyntaxErrorType.SyntaxError
End Select