-
Notifications
You must be signed in to change notification settings - Fork 4k
/
SyntaxList.vb
891 lines (709 loc) · 32.9 KB
/
SyntaxList.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
' Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
Imports System
Imports System.Collections
Imports System.Collections.Generic
Imports System.Linq
Imports System.Runtime.CompilerServices
Imports System.Text
Imports Microsoft.CodeAnalysis
Imports Microsoft.CodeAnalysis.Text
Imports Microsoft.CodeAnalysis.VisualBasic.Symbols
Imports Microsoft.CodeAnalysis.VisualBasic.Syntax
Namespace Microsoft.CodeAnalysis.VisualBasic.Syntax.InternalSyntax
Friend MustInherit Class SyntaxList
Inherits VisualBasicSyntaxNode
Protected Sub New(errors As DiagnosticInfo(), annotations As SyntaxAnnotation())
MyBase.New(SyntaxKind.List, errors, annotations)
End Sub
Friend Sub New(reader As ObjectReader)
MyBase.New(reader)
End Sub
Protected Sub New()
MyBase.New(SyntaxKind.List)
End Sub
Friend Shared Function List(child As VisualBasicSyntaxNode) As VisualBasicSyntaxNode
Return child
End Function
Friend Shared Function List(child0 As VisualBasicSyntaxNode, child1 As VisualBasicSyntaxNode) As WithTwoChildren
Dim hash As Integer
Dim cached As GreenNode = SyntaxNodeCache.TryGetNode(SyntaxKind.List, child0, child1, hash)
If cached IsNot Nothing Then
Return DirectCast(cached, WithTwoChildren)
End If
Dim result = New WithTwoChildren(child0, child1)
If hash >= 0 Then
SyntaxNodeCache.AddNode(result, hash)
End If
Return result
End Function
Friend Shared Function List(child0 As VisualBasicSyntaxNode, child1 As VisualBasicSyntaxNode, child2 As VisualBasicSyntaxNode) As WithThreeChildren
Dim hash As Integer
Dim cached As GreenNode = SyntaxNodeCache.TryGetNode(SyntaxKind.List, child0, child1, child2, hash)
If cached IsNot Nothing Then
Return DirectCast(cached, WithThreeChildren)
End If
Dim result = New WithThreeChildren(child0, child1, child2)
If hash >= 0 Then
SyntaxNodeCache.AddNode(result, hash)
End If
Return result
End Function
Friend Shared Function List(nodes As ArrayElement(Of VisualBasicSyntaxNode)()) As SyntaxList
' "WithLotsOfChildren" list will alocate a separate array to hold
' precomputed node offsets. It may not be worth it for smallish lists.
If nodes.Length < 10 Then
Return New WithManyChildren(nodes)
Else
Return New WithLotsOfChildren(nodes)
End If
End Function
Friend Shared Function List(nodes As VisualBasicSyntaxNode()) As SyntaxList
Return List(nodes, nodes.Length)
End Function
Friend Shared Function List(nodes As VisualBasicSyntaxNode(), count As Integer) As SyntaxList
Dim array = New ArrayElement(Of VisualBasicSyntaxNode)(count - 1) {}
Debug.Assert(array.Length = count)
For i = 0 To count - 1
array(i).Value = nodes(i)
Debug.Assert(array(i).Value IsNot Nothing)
Next
Return List(array)
End Function
Friend MustOverride Sub CopyTo(array As ArrayElement(Of VisualBasicSyntaxNode)(), offset As Integer)
Friend Shared Function Concat(left As VisualBasicSyntaxNode, right As VisualBasicSyntaxNode) As VisualBasicSyntaxNode
If (left Is Nothing) Then
Return right
End If
If (right Is Nothing) Then
Return left
End If
Dim tmp As ArrayElement(Of VisualBasicSyntaxNode)()
Dim leftList As SyntaxList = TryCast(left, SyntaxList)
Dim rightList As SyntaxList = TryCast(right, SyntaxList)
If leftList IsNot Nothing Then
If rightList IsNot Nothing Then
tmp = New ArrayElement(Of VisualBasicSyntaxNode)(left.SlotCount + right.SlotCount - 1) {}
leftList.CopyTo(tmp, 0)
rightList.CopyTo(tmp, left.SlotCount)
Return SyntaxList.List(tmp)
End If
tmp = New ArrayElement(Of VisualBasicSyntaxNode)((left.SlotCount + 1) - 1) {}
leftList.CopyTo(tmp, 0)
tmp(left.SlotCount).Value = right
Return SyntaxList.List(tmp)
End If
If rightList IsNot Nothing Then
tmp = New ArrayElement(Of VisualBasicSyntaxNode)((rightList.SlotCount + 1) - 1) {}
tmp(0).Value = left
rightList.CopyTo(tmp, 1)
Return SyntaxList.List(tmp)
End If
Return SyntaxList.List(left, right)
End Function
Friend NotInheritable Class WithTwoChildren
Inherits SyntaxList
Private _child0 As VisualBasicSyntaxNode
Private _child1 As VisualBasicSyntaxNode
Private Sub New(errors As DiagnosticInfo(), annotations As SyntaxAnnotation(), child0 As VisualBasicSyntaxNode, child1 As VisualBasicSyntaxNode)
MyBase.New(errors, annotations)
MyBase._slotCount = 2
MyBase.AdjustFlagsAndWidth(child0)
Me._child0 = child0
MyBase.AdjustFlagsAndWidth(child1)
Me._child1 = child1
End Sub
Friend Sub New(child0 As VisualBasicSyntaxNode, child1 As VisualBasicSyntaxNode)
MyBase.New()
MyBase._slotCount = 2
MyBase.AdjustFlagsAndWidth(child0)
Me._child0 = child0
MyBase.AdjustFlagsAndWidth(child1)
Me._child1 = child1
End Sub
Friend Sub New(reader As ObjectReader)
MyBase.New(reader)
MyBase._slotCount = 2
Me._child0 = DirectCast(reader.ReadValue(), VisualBasicSyntaxNode)
MyBase.AdjustFlagsAndWidth(_child0)
Me._child1 = DirectCast(reader.ReadValue(), VisualBasicSyntaxNode)
MyBase.AdjustFlagsAndWidth(_child1)
End Sub
Friend Overrides Function GetReader() As Func(Of ObjectReader, Object)
Return Function(r) New WithTwoChildren(r)
End Function
Friend Overrides Sub WriteTo(writer As ObjectWriter)
MyBase.WriteTo(writer)
writer.WriteValue(Me._child0)
writer.WriteValue(Me._child1)
End Sub
Friend Overrides Sub CopyTo(array As ArrayElement(Of VisualBasicSyntaxNode)(), offset As Integer)
array(offset).Value = Me._child0
array((offset + 1)).Value = Me._child1
End Sub
Friend Overrides Function GetSlot(index As Integer) As GreenNode
Select Case index
Case 0
Return Me._child0
Case 1
Return Me._child1
End Select
Return Nothing
End Function
Friend Overrides Function CreateRed(parent As SyntaxNode, startLocation As Integer) As SyntaxNode
Return New VisualBasic.Syntax.SyntaxList.WithTwoChildren(Me, parent, startLocation)
End Function
Friend Overrides Function SetDiagnostics(errors() As DiagnosticInfo) As GreenNode
Return New WithTwoChildren(errors, Me.GetAnnotations(), Me._child0, Me._child1)
End Function
Friend Overrides Function SetAnnotations(annotations() As SyntaxAnnotation) As GreenNode
Return New WithTwoChildren(Me.GetDiagnostics(), annotations, Me._child0, Me._child1)
End Function
End Class
Friend NotInheritable Class WithThreeChildren
Inherits SyntaxList
Private _child0 As VisualBasicSyntaxNode
Private _child1 As VisualBasicSyntaxNode
Private _child2 As VisualBasicSyntaxNode
Private Sub New(errors As DiagnosticInfo(), annotations As SyntaxAnnotation(), child0 As VisualBasicSyntaxNode, child1 As VisualBasicSyntaxNode, child2 As VisualBasicSyntaxNode)
MyBase.New(errors, annotations)
MyBase._slotCount = 3
MyBase.AdjustFlagsAndWidth(child0)
Me._child0 = child0
MyBase.AdjustFlagsAndWidth(child1)
Me._child1 = child1
MyBase.AdjustFlagsAndWidth(child2)
Me._child2 = child2
End Sub
Friend Sub New(child0 As VisualBasicSyntaxNode, child1 As VisualBasicSyntaxNode, child2 As VisualBasicSyntaxNode)
MyBase.New()
MyBase._slotCount = 3
MyBase.AdjustFlagsAndWidth(child0)
Me._child0 = child0
MyBase.AdjustFlagsAndWidth(child1)
Me._child1 = child1
MyBase.AdjustFlagsAndWidth(child2)
Me._child2 = child2
End Sub
Friend Sub New(reader As ObjectReader)
MyBase.New(reader)
MyBase._slotCount = 3
Me._child0 = DirectCast(reader.ReadValue(), VisualBasicSyntaxNode)
MyBase.AdjustFlagsAndWidth(_child0)
Me._child1 = DirectCast(reader.ReadValue(), VisualBasicSyntaxNode)
MyBase.AdjustFlagsAndWidth(_child1)
Me._child2 = DirectCast(reader.ReadValue(), VisualBasicSyntaxNode)
MyBase.AdjustFlagsAndWidth(_child2)
End Sub
Friend Overrides Function GetReader() As Func(Of ObjectReader, Object)
Return Function(r) New WithThreeChildren(r)
End Function
Friend Overrides Sub WriteTo(writer As ObjectWriter)
MyBase.WriteTo(writer)
writer.WriteValue(Me._child0)
writer.WriteValue(Me._child1)
writer.WriteValue(Me._child2)
End Sub
Friend Overrides Sub CopyTo(array As ArrayElement(Of VisualBasicSyntaxNode)(), offset As Integer)
array(offset).Value = Me._child0
array(offset + 1).Value = Me._child1
array(offset + 2).Value = Me._child2
End Sub
Friend Overrides Function GetSlot(index As Integer) As GreenNode
Select Case index
Case 0
Return Me._child0
Case 1
Return Me._child1
Case 2
Return Me._child2
End Select
Return Nothing
End Function
Friend Overrides Function CreateRed(parent As SyntaxNode, startLocation As Integer) As SyntaxNode
Return New VisualBasic.Syntax.SyntaxList.WithThreeChildren(Me, parent, startLocation)
End Function
Friend Overrides Function SetDiagnostics(errors() As DiagnosticInfo) As GreenNode
Return New WithThreeChildren(errors, Me.GetAnnotations(), Me._child0, Me._child1, Me._child2)
End Function
Friend Overrides Function SetAnnotations(annotations() As SyntaxAnnotation) As GreenNode
Return New WithThreeChildren(Me.GetDiagnostics(), annotations, Me._child0, Me._child1, Me._child2)
End Function
End Class
Friend MustInherit Class WithManyChildrenBase
Inherits SyntaxList
Protected ReadOnly _children As ArrayElement(Of VisualBasicSyntaxNode)()
Protected Sub New(errors As DiagnosticInfo(), annotations As SyntaxAnnotation(), children As ArrayElement(Of VisualBasicSyntaxNode)())
MyBase.New(errors, annotations)
Me._children = children
InitChildren()
End Sub
Friend Sub New(children As ArrayElement(Of VisualBasicSyntaxNode)())
MyBase.New()
Me._children = children
InitChildren()
End Sub
Private Sub InitChildren()
Dim n = _children.Length
If (n < Byte.MaxValue) Then
Me._slotCount = CByte(n)
Else
Me._slotCount = Byte.MaxValue
End If
For i = 0 To _children.Length - 1
Dim child = _children(i)
MyBase.AdjustFlagsAndWidth(child)
Next i
End Sub
Protected Overrides Function GetSlotCount() As Integer
Return Me._children.Length
End Function
Protected Sub New(reader As ObjectReader)
MyBase.New(reader)
Dim length = reader.ReadInt32()
Me._children = New ArrayElement(Of VisualBasicSyntaxNode)(length - 1) {}
For i = 0 To length - 1
Me._children(i).Value = DirectCast(reader.ReadValue(), VisualBasicSyntaxNode)
Next
InitChildren()
End Sub
Friend Overrides Sub WriteTo(writer As ObjectWriter)
MyBase.WriteTo(writer)
' PERF Write the array out manually.Profiling shows that this Is cheaper than converting to
' an array in order to use writer.WriteValue.
writer.WriteInt32(Me._children.Length)
For i = 0 To Me._children.Length - 1
writer.WriteValue(Me._children(i).Value)
Next
End Sub
Friend Overrides Sub CopyTo(nodes As ArrayElement(Of VisualBasicSyntaxNode)(), offset As Integer)
Array.Copy(Me._children, 0, nodes, offset, Me._children.Length)
End Sub
Friend Overrides Function GetSlot(index As Integer) As GreenNode
Return Me._children(index)
End Function
'TODO: weakening heuristic may need some tuning.
Private Shared Function ShouldMakeWeakList(parent As SyntaxNode) As Boolean
Return parent IsNot Nothing AndAlso TypeOf parent Is VisualBasic.Syntax.MethodBlockBaseSyntax
End Function
Friend Overrides Function CreateRed(parent As SyntaxNode, startLocation As Integer) As SyntaxNode
Dim isSeparated = SlotCount > 1 AndAlso HasNodeTokenPattern()
If ShouldMakeWeakList(parent) Then
If isSeparated Then
Return New VisualBasic.Syntax.SyntaxList.WeakSeparatedWithManyChildren(Me, parent, startLocation)
End If
Return New VisualBasic.Syntax.SyntaxList.WeakWithManyChildren(Me, parent, startLocation)
Else
If isSeparated Then
Return New VisualBasic.Syntax.SyntaxList.SeparatedWithManyChildren(Me, parent, startLocation)
End If
Return New VisualBasic.Syntax.SyntaxList.WithManyChildren(Me, parent, startLocation)
End If
End Function
Private Function HasNodeTokenPattern() As Boolean
For i = 0 To Me.SlotCount - 1
' even slots must not be tokens and odd slots must be
If Me.GetSlot(i).IsToken = ((i And 1) = 0) Then
Return False
End If
Next
Return True
End Function
Friend MustOverride Overrides Function SetDiagnostics(newErrors() As DiagnosticInfo) As GreenNode
Friend MustOverride Overrides Function SetAnnotations(annotations() As SyntaxAnnotation) As GreenNode
End Class
Friend NotInheritable Class WithManyChildren
Inherits WithManyChildrenBase
Friend Sub New(children As ArrayElement(Of VisualBasicSyntaxNode)())
MyBase.New(children)
End Sub
Private Sub New(errors As DiagnosticInfo(), annotations As SyntaxAnnotation(), children As ArrayElement(Of VisualBasicSyntaxNode)())
MyBase.New(errors, annotations, children)
End Sub
Friend Sub New(reader As ObjectReader)
MyBase.New(reader)
End Sub
Friend Overrides Function GetReader() As Func(Of ObjectReader, Object)
Return Function(r) New WithManyChildren(r)
End Function
Friend Overrides Function SetDiagnostics(errors() As DiagnosticInfo) As GreenNode
Return New WithManyChildren(errors, Me.GetAnnotations(), Me._children)
End Function
Friend Overrides Function SetAnnotations(annotations() As SyntaxAnnotation) As GreenNode
Return New WithManyChildren(Me.GetDiagnostics(), annotations, Me._children)
End Function
End Class
Friend NotInheritable Class WithLotsOfChildren
Inherits WithManyChildrenBase
Private ReadOnly _childOffsets As Integer()
Friend Sub New(children As ArrayElement(Of VisualBasicSyntaxNode)())
MyBase.New(children)
_childOffsets = CalculateOffsets(children)
End Sub
Private Sub New(errors As DiagnosticInfo(), annotations As SyntaxAnnotation(), children As ArrayElement(Of VisualBasicSyntaxNode)(), childOffsets As Integer())
MyBase.New(errors, annotations, children)
_childOffsets = childOffsets
End Sub
Friend Sub New(reader As ObjectReader)
MyBase.New(reader)
_childOffsets = CalculateOffsets(_children)
End Sub
Friend Overrides Function GetReader() As Func(Of ObjectReader, Object)
Return Function(r) New WithLotsOfChildren(r)
End Function
Public Overrides Function GetSlotOffset(index As Integer) As Integer
Return _childOffsets(index)
End Function
''' <summary>
''' Find the slot that contains the given offset.
''' </summary>
''' <param name="offset">The target offset. Must be between 0 and <see cref="GreenNode.FullWidth"/>.</param>
''' <returns>The slot index of the slot containing the given offset.</returns>
''' <remarks>
''' This implementation uses a binary search to find the first slot that contains
''' the given offset.
''' </remarks>
Public Overrides Function FindSlotIndexContainingOffset(offset As Integer) As Integer
Debug.Assert(offset >= 0 AndAlso offset < FullWidth)
Return _childOffsets.BinarySearchUpperBound(offset) - 1
End Function
Friend Overrides Function SetDiagnostics(errors() As DiagnosticInfo) As GreenNode
Return New WithLotsOfChildren(errors, Me.GetAnnotations(), Me._children, Me._childOffsets)
End Function
Friend Overrides Function SetAnnotations(annotations() As SyntaxAnnotation) As GreenNode
Return New WithLotsOfChildren(Me.GetDiagnostics(), annotations, Me._children, Me._childOffsets)
End Function
Private Shared Function CalculateOffsets(children As ArrayElement(Of VisualBasicSyntaxNode)()) As Integer()
Dim n = children.Length
Dim childOffsets = New Integer(n - 1) {}
Dim offset = 0
For i = 0 To n - 1
childOffsets(i) = offset
offset += children(i).Value.FullWidth
Next
Return childOffsets
End Function
End Class
End Class
Friend Class SyntaxListBuilder
Private _count As Integer
Private _nodes As ArrayElement(Of VisualBasicSyntaxNode)()
Public Shared Function Create() As SyntaxListBuilder
Return New SyntaxListBuilder(8)
End Function
Public Sub New(size As Integer)
Me._nodes = New ArrayElement(Of VisualBasicSyntaxNode)(size - 1) {}
End Sub
Public Function Add(item As VisualBasicSyntaxNode) As SyntaxListBuilder
EnsureAdditionalCapacity(1)
Return Me.AddUnsafe(item)
End Function
Private Function AddUnsafe(item As GreenNode) As SyntaxListBuilder
Debug.Assert(item IsNot Nothing)
Me._nodes(Me._count).Value = DirectCast(item, VisualBasicSyntaxNode)
Me._count += 1
Return Me
End Function
Public Function AddRange(Of TNode As VisualBasicSyntaxNode)(list As SyntaxList(Of TNode)) As SyntaxListBuilder
Return Me.AddRange(Of TNode)(list, 0, list.Count)
End Function
Public Function AddRange(Of TNode As VisualBasicSyntaxNode)(list As SyntaxList(Of TNode), offset As Integer, length As Integer) As SyntaxListBuilder
EnsureAdditionalCapacity(length - offset)
Dim oldCount = Me._count
For i = offset To offset + length - 1
AddUnsafe(list.ItemUntyped(i))
Next i
Me.Validate(oldCount, Me._count)
Return Me
End Function
Public Function Any(kind As SyntaxKind) As Boolean
Dim i As Integer
For i = 0 To Me._count - 1
If (Me._nodes(i).Value.Kind = kind) Then
Return True
End If
Next i
Return False
End Function
Friend Sub RemoveLast()
Me._count -= 1
Me._nodes(Me._count) = Nothing
End Sub
Public Sub Clear()
Me._count = 0
End Sub
Private Sub EnsureAdditionalCapacity(additionalCount As Integer)
Dim currentSize As Integer = Me._nodes.Length
Dim requiredSize As Integer = Me._count + additionalCount
If requiredSize <= currentSize Then
Return
End If
Dim newSize As Integer =
If(requiredSize < 8, 8,
If(requiredSize >= Integer.MaxValue / 2, Integer.MaxValue,
Math.Max(requiredSize, currentSize * 2))) ' Guaranteed to at least double
Debug.Assert(newSize >= requiredSize)
Array.Resize(Me._nodes, newSize)
End Sub
Friend Function ToArray() As ArrayElement(Of VisualBasicSyntaxNode)()
Dim dst As ArrayElement(Of VisualBasicSyntaxNode)() = New ArrayElement(Of VisualBasicSyntaxNode)(Me._count - 1) {}
'TODO: workaround for range check hoisting bug
' <<< FOR LOOP
Dim i As Integer = 0
GoTo enter
Do
dst(i) = Me._nodes(i)
i += 1
enter:
Loop While i < dst.Length
' >>> FOR LOOP
Return dst
End Function
Friend Function ToListNode() As VisualBasicSyntaxNode
Select Case Me._count
Case 0
Return Nothing
Case 1
Return Me._nodes(0)
Case 2
Return SyntaxList.List(Me._nodes(0), Me._nodes(1))
Case 3
Return SyntaxList.List(Me._nodes(0), Me._nodes(1), Me._nodes(2))
End Select
Return SyntaxList.List(Me.ToArray)
End Function
<Conditional("DEBUG")>
Private Sub Validate(start As Integer, [end] As Integer)
Dim i As Integer
For i = start To [end] - 1
Debug.Assert(Me._nodes(i).Value IsNot Nothing)
Next i
End Sub
Public ReadOnly Property Count As Integer
Get
Return Me._count
End Get
End Property
Default Public Property Item(index As Integer) As VisualBasicSyntaxNode
Get
Return Me._nodes(index)
End Get
Set(value As VisualBasicSyntaxNode)
Me._nodes(index).Value = value
End Set
End Property
Public Function ToList() As SyntaxList(Of VisualBasicSyntaxNode)
Return New SyntaxList(Of VisualBasicSyntaxNode)(ToListNode)
End Function
Public Function ToList(Of TDerived As VisualBasicSyntaxNode)() As SyntaxList(Of TDerived)
Return New SyntaxList(Of TDerived)(ToListNode)
End Function
End Class
Friend Structure SyntaxListBuilder(Of TNode As VisualBasicSyntaxNode)
Private builder As SyntaxListBuilder
Public Shared Function Create() As SyntaxListBuilder(Of TNode)
Return New SyntaxListBuilder(Of TNode)(8)
End Function
Public Sub New(size As Integer)
Me.New(New SyntaxListBuilder(size))
End Sub
Friend Sub New(builder As SyntaxListBuilder)
Me.builder = builder
End Sub
Public ReadOnly Property IsNull As Boolean
Get
Return (Me.builder Is Nothing)
End Get
End Property
Public ReadOnly Property Count As Integer
Get
Return Me.builder.Count
End Get
End Property
Default Public Property Item(index As Integer) As TNode
Get
Return DirectCast(Me.builder.Item(index), TNode)
End Get
Set(value As TNode)
Me.builder.Item(index) = value
End Set
End Property
Friend Sub RemoveLast()
Me.builder.RemoveLast()
End Sub
Public Sub Clear()
Me.builder.Clear()
End Sub
Public Sub Add(node As TNode)
Me.builder.Add(node)
End Sub
Public Sub AddRange(nodes As SyntaxList(Of TNode))
Me.builder.AddRange(Of TNode)(nodes)
End Sub
Public Sub AddRange(nodes As SyntaxList(Of TNode), offset As Integer, length As Integer)
Me.builder.AddRange(Of TNode)(nodes, offset, length)
End Sub
Public Function Any(kind As SyntaxKind) As Boolean
Return Me.builder.Any(kind)
End Function
Public Function ToList() As SyntaxList(Of TNode)
Debug.Assert(Me.builder IsNot Nothing)
Return Me.builder.ToList(Of TNode)()
End Function
Public Function ToList(Of TDerivedNode As TNode)() As SyntaxList(Of TDerivedNode)
Debug.Assert(Me.builder IsNot Nothing)
Return Me.builder.ToList(Of TDerivedNode)()
End Function
Public Shared Widening Operator CType(builder As SyntaxListBuilder(Of TNode)) As SyntaxListBuilder
Return builder.builder
End Operator
Public Shared Widening Operator CType(builder As SyntaxListBuilder(Of TNode)) As SyntaxList(Of TNode)
Return builder.ToList
End Operator
End Structure
Friend Structure SeparatedSyntaxListBuilder(Of TNode As VisualBasicSyntaxNode)
Private builder As SyntaxListBuilder
Public Sub New(size As Integer)
Me.New(New SyntaxListBuilder(size))
End Sub
Friend Sub New(builder As SyntaxListBuilder)
Me.builder = builder
End Sub
Public ReadOnly Property IsNull As Boolean
Get
Return (Me.builder Is Nothing)
End Get
End Property
Public ReadOnly Property Count As Integer
Get
Return Me.builder.Count
End Get
End Property
Default Public Property Item(index As Integer) As VisualBasicSyntaxNode
Get
Return Me.builder.Item(index)
End Get
Set(value As VisualBasicSyntaxNode)
Me.builder.Item(index) = value
End Set
End Property
Public Sub Clear()
Me.builder.Clear()
End Sub
Public Sub Add(node As TNode)
Me.builder.Add(node)
End Sub
Friend Sub AddSeparator(separatorToken As SyntaxToken)
Me.builder.Add(separatorToken)
End Sub
Friend Sub AddRange(nodes As SeparatedSyntaxList(Of TNode), count As Integer)
Dim list = nodes.GetWithSeparators
Me.builder.AddRange(list, Me.Count, Math.Min(count * 2, list.Count))
End Sub
Friend Sub RemoveLast()
Me.builder.RemoveLast()
End Sub
Public Function Any(kind As SyntaxKind) As Boolean
Return Me.builder.Any(kind)
End Function
Public Function ToList() As SeparatedSyntaxList(Of TNode)
Return New SeparatedSyntaxList(Of TNode)(New SyntaxList(Of VisualBasicSyntaxNode)(Me.builder.ToListNode))
End Function
Public Function ToList(Of TDerivedNode As TNode)() As SeparatedSyntaxList(Of TDerivedNode)
Return New SeparatedSyntaxList(Of TDerivedNode)(New SyntaxList(Of VisualBasicSyntaxNode)(Me.builder.ToListNode))
End Function
Public Shared Widening Operator CType(builder As SeparatedSyntaxListBuilder(Of TNode)) As SyntaxListBuilder
Return builder.builder
End Operator
End Structure
Friend Structure SyntaxList(Of TNode As VisualBasicSyntaxNode)
Implements IEquatable(Of SyntaxList(Of TNode))
Private _node As GreenNode
Friend Sub New(node As GreenNode)
Me._node = node
End Sub
Friend ReadOnly Property Node As VisualBasicSyntaxNode
Get
Return DirectCast(Me._node, VisualBasicSyntaxNode)
End Get
End Property
Public ReadOnly Property Count As Integer
Get
Return If((Me._node Is Nothing), 0, If(Me._node.IsList, Me._node.SlotCount, 1))
End Get
End Property
Public ReadOnly Property Last As TNode
Get
Dim node = Me._node
If node.IsList Then
Return DirectCast(node.GetSlot(node.SlotCount - 1), TNode)
End If
Return DirectCast(node, TNode)
End Get
End Property
Default Public ReadOnly Property Item(index As Integer) As TNode
Get
Dim node = Me._node
If node.IsList Then
Return DirectCast(node.GetSlot(index), TNode)
End If
Debug.Assert(index = 0)
Return DirectCast(node, TNode)
End Get
End Property
Friend ReadOnly Property ItemUntyped(index As Integer) As GreenNode
Get
Dim node = Me._node
If node.IsList Then
Return node.GetSlot(index)
End If
Debug.Assert(index = 0)
Return node
End Get
End Property
Public Function Any() As Boolean
Return Me._node IsNot Nothing
End Function
Public Function Any(kind As SyntaxKind) As Boolean
For i = 0 To Me.Count - 1
Dim element = Me.ItemUntyped(i)
If (element.RawKind = kind) Then
Return True
End If
Next
Return False
End Function
Friend ReadOnly Property Nodes As TNode()
Get
Dim arr = New TNode(Me.Count - 1) {}
For i = 0 To Me.Count - 1
arr(i) = Me.Item(i)
Next
Return arr
End Get
End Property
Public Shared Operator =(left As SyntaxList(Of TNode), right As SyntaxList(Of TNode)) As Boolean
Return (left._node Is right._node)
End Operator
Public Shared Operator <>(left As SyntaxList(Of TNode), right As SyntaxList(Of TNode)) As Boolean
Return (Not left._node Is right._node)
End Operator
Public Overrides Function Equals(obj As Object) As Boolean
Return (TypeOf obj Is SyntaxList(Of TNode) AndAlso Me.Equals(DirectCast(obj, SyntaxList(Of TNode))))
End Function
Public Overloads Function Equals(other As SyntaxList(Of TNode)) As Boolean Implements IEquatable(Of SyntaxList(Of TNode)).Equals
Return Me._node Is other._node
End Function
Public Overrides Function GetHashCode() As Integer
Return If((Not Me._node Is Nothing), Me._node.GetHashCode, 0)
End Function
Friend Function AsSeparatedList(Of TOther As VisualBasicSyntaxNode)() As SeparatedSyntaxList(Of TOther)
Return New SeparatedSyntaxList(Of TOther)(New SyntaxList(Of TOther)(Me._node))
End Function
Public Shared Widening Operator CType(node As TNode) As SyntaxList(Of TNode)
Return New SyntaxList(Of TNode)(node)
End Operator
Public Shared Widening Operator CType(nodes As SyntaxList(Of TNode)) As SyntaxList(Of VisualBasicSyntaxNode)
Return New SyntaxList(Of VisualBasicSyntaxNode)(nodes._node)
End Operator
End Structure
End Namespace