/
SyntaxTriviaList.Enumerator.cs
164 lines (138 loc) · 5.3 KB
/
SyntaxTriviaList.Enumerator.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace Microsoft.CodeAnalysis
{
public partial struct SyntaxTriviaList
{
[StructLayout(LayoutKind.Auto)]
public struct Enumerator
{
private SyntaxToken _token;
private GreenNode? _singleNodeOrList;
private int _baseIndex;
private int _count;
private int _index;
private GreenNode? _current;
private int _position;
internal Enumerator(in SyntaxTriviaList list)
{
_token = list.Token;
_singleNodeOrList = list.Node;
_baseIndex = list.Index;
_count = list.Count;
_index = -1;
_current = null;
_position = list.Position;
}
// PERF: Passing SyntaxToken by ref since it's a non-trivial struct
private void InitializeFrom(in SyntaxToken token, GreenNode greenNode, int index, int position)
{
_token = token;
_singleNodeOrList = greenNode;
_baseIndex = index;
_count = greenNode.IsList ? greenNode.SlotCount : 1;
_index = -1;
_current = null;
_position = position;
}
// PERF: Used to initialize an enumerator for leading trivia directly from a token.
// This saves constructing an intermediate SyntaxTriviaList. Also, passing token
// by ref since it's a non-trivial struct
internal void InitializeFromLeadingTrivia(in SyntaxToken token)
{
Debug.Assert(token.Node is object);
var node = token.Node.GetLeadingTriviaCore();
Debug.Assert(node is object);
InitializeFrom(in token, node, 0, token.Position);
}
// PERF: Used to initialize an enumerator for trailing trivia directly from a token.
// This saves constructing an intermediate SyntaxTriviaList. Also, passing token
// by ref since it's a non-trivial struct
internal void InitializeFromTrailingTrivia(in SyntaxToken token)
{
Debug.Assert(token.Node is object);
var leading = token.Node.GetLeadingTriviaCore();
int index = 0;
if (leading != null)
{
index = leading.IsList ? leading.SlotCount : 1;
}
var trailingGreen = token.Node.GetTrailingTriviaCore();
int trailingPosition = token.Position + token.FullWidth;
if (trailingGreen != null)
{
trailingPosition -= trailingGreen.FullWidth;
}
Debug.Assert(trailingGreen is object);
InitializeFrom(in token, trailingGreen, index, trailingPosition);
}
public bool MoveNext()
{
int newIndex = _index + 1;
if (newIndex >= _count)
{
// invalidate iterator
_current = null;
return false;
}
_index = newIndex;
if (_current != null)
{
_position += _current.FullWidth;
}
Debug.Assert(_singleNodeOrList is object);
_current = GetGreenNodeAt(_singleNodeOrList, newIndex);
return true;
}
public SyntaxTrivia Current
{
get
{
if (_current == null)
{
throw new InvalidOperationException();
}
return new SyntaxTrivia(_token, _current, _position, _baseIndex + _index);
}
}
internal bool TryMoveNextAndGetCurrent(out SyntaxTrivia current)
{
if (!MoveNext())
{
current = default;
return false;
}
current = new SyntaxTrivia(_token, _current, _position, _baseIndex + _index);
return true;
}
}
private class EnumeratorImpl : IEnumerator<SyntaxTrivia>
{
private Enumerator _enumerator;
// SyntaxTriviaList is a relatively big struct so is passed as ref
internal EnumeratorImpl(in SyntaxTriviaList list)
{
_enumerator = new Enumerator(in list);
}
public SyntaxTrivia Current => _enumerator.Current;
object IEnumerator.Current => _enumerator.Current;
public bool MoveNext()
{
return _enumerator.MoveNext();
}
public void Reset()
{
throw new NotSupportedException();
}
public void Dispose()
{
}
}
}
}