-
-
Notifications
You must be signed in to change notification settings - Fork 49
/
SyntaxLocator.cs
195 lines (177 loc) · 7.08 KB
/
SyntaxLocator.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
using System;
using System.Collections.Generic;
namespace Microsoft.Language.Xml
{
public static class SyntaxLocator
{
public static TextSpan GetLeadingTriviaSpan(this SyntaxNode node)
{
var leadingTrivia = node.GetLeadingTrivia().Node;
return leadingTrivia != null ?
new TextSpan(node.Start, leadingTrivia.Width) :
new TextSpan(node.Start, 0);
}
public static TextSpan GetTrailingTriviaSpan(this SyntaxNode node)
{
var trailingTrivia = node.GetTrailingTrivia().Node;
return trailingTrivia != null ?
new TextSpan(node.Start + node.FullWidth - trailingTrivia.Width, trailingTrivia.Width) :
new TextSpan(node.Start + node.FullWidth, 0);
}
public static SyntaxNode FindNode(
this SyntaxNode node,
int position,
Func<SyntaxNode, bool> descendIntoChildren = null,
bool includeTrivia = true,
bool excludeTerminal = false)
{
if (node == null)
{
throw new ArgumentNullException(nameof(node));
}
int offset = position;
bool searchChildren = true;
while (searchChildren)
{
if (descendIntoChildren?.Invoke(node) == false)
{
break;
}
// set to false, so loop will only continue if explicitly
// specified in a later stage
searchChildren = false;
int index;
node.GetIndexAndOffset(offset, out index, out offset);
if (index > 0)
{
index--;
}
var slotCount = includeTrivia ? node.GetSlotCountIncludingTrivia() : node.SlotCount;
for (int i = index; i < slotCount; i++)
{
var child = includeTrivia ? node.GetSlotIncludingTrivia(i) : node.GetNodeSlot(i);
if (child != null)
{
if (child.Start > position)
{
// child is after position
break;
}
else if ((child.Start + child.FullWidth) <= position)
{
// child ends before position, go to next child
continue;
}
else
{
// We have hit a terminal, maybe we should be returning its non-terminal parent
if (child.SlotCount == 0 && excludeTerminal)
return node;
node = child;
searchChildren = true;
break;
}
}
}
}
return node;
}
private struct VisitState
{
public SyntaxNode node;
public int i;
}
public static IEnumerable<SyntaxNode> Tokens(this SyntaxNode root, TextSpan span, Func<SyntaxNode, bool> descendIntoChildren = null)
{
VisitState currentState = new VisitState() { node = root };
Stack<VisitState> stateStack = new Stack<VisitState>();
stateStack.Push(currentState);
bool foundFirst = false;
while (stateStack.Count != 0)
{
currentState = stateStack.Pop();
if (currentState.i == 0 && currentState.node.SlotCount == 0)
{
if (currentState.node.FullSpan.OverlapsWith(span))
{
foundFirst = true;
yield return currentState.node;
}
}
else if (currentState.i != 0 || (descendIntoChildren?.Invoke(currentState.node) != false))
{
if (!foundFirst && currentState.i == 0)
{
int offset;
currentState.node.GetIndexAndOffset(span.Start - currentState.node.Start, out currentState.i, out offset);
if (currentState.i > 0)
{
// The element is the first element to start after the start position. We want
// the first element containing the start position so back track by one to ensure that is
// included.
currentState.i--;
}
}
while (currentState.i < currentState.node.SlotCount)
{
var child = currentState.node.GetNodeSlot(currentState.i);
currentState.i++;
if (child != null)
{
var childSpan = child.Span;
if (childSpan.Start > span.End)
{
break;
}
else if (childSpan.End < span.Start)
{
continue;
}
else
{
stateStack.Push(currentState);
stateStack.Push(new VisitState() { node = child });
break;
}
}
}
}
}
}
private static IEnumerable<SyntaxNode> VisitTerminalsRecursive(this SyntaxNode node, TextSpan span)
{
if (node.SlotCount == 0)
{
if (node.FullSpan.OverlapsWith(span))
{
yield return node;
}
}
else
{
for (int i = 0; i < node.SlotCount; i++)
{
var child = node.GetNodeSlot(i);
if (child != null)
{
if (child.Start > span.End)
{
break;
}
else if (child.FullSpan.End < span.Start)
{
continue;
}
else
{
foreach (var terminal in child.VisitTerminalsRecursive(span))
{
yield return terminal;
}
}
}
}
}
}
}
}