-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSyntaxScanner.cs
More file actions
386 lines (325 loc) · 16.5 KB
/
Copy pathSyntaxScanner.cs
File metadata and controls
386 lines (325 loc) · 16.5 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
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using NETMetaCoder.Abstractions;
using NETMetaCoder.SyntaxEnvelope;
namespace NETMetaCoder
{
/// <summary>
/// This type encapsulates the logic for building a <see cref="SyntaxEnvelope"/>, by scanning the syntax tree of a
/// compilation unit.
/// </summary>
public static class SyntaxScanner
{
/// <summary>
/// Constructs a new <see cref="SyntaxScanner"/> instance.
/// </summary>
/// <param name="tree"></param>
/// <param name="attributeNames"></param>
/// <returns></returns>
public static SyntaxEnvelope.SyntaxEnvelope
ScanSyntaxTree(SyntaxTree tree, IEnumerable<string> attributeNames) =>
new SyntaxScannerWithContext(tree, attributeNames).ScanSyntaxTree();
private sealed class SyntaxScannerWithContext : CSharpSyntaxWalker
{
private readonly SyntaxTree _tree;
private readonly IEnumerable<string> _attributeNames;
private readonly SyntaxEnvelope.SyntaxEnvelope _syntaxEnvelope = new SyntaxEnvelope.SyntaxEnvelope();
private ushort _nodeIndex;
private readonly Stack<NamespaceSyntaxEnvelope> _namespaceSyntaxEnvelopeStack =
new Stack<NamespaceSyntaxEnvelope>();
private readonly Stack<ClassOrStructSyntaxEnvelope> _classOrStructSyntaxEnvelopeStack =
new Stack<ClassOrStructSyntaxEnvelope>();
public SyntaxScannerWithContext(SyntaxTree tree, IEnumerable<string> attributeNames)
{
_tree = tree;
_attributeNames = attributeNames;
}
public SyntaxEnvelope.SyntaxEnvelope ScanSyntaxTree()
{
if (_tree.GetDiagnostics().Any(d => d.Severity == DiagnosticSeverity.Error))
{
throw new NETMetaCoderException("The syntax tree has errors and no scanning will take place.");
}
Visit(_tree.GetCompilationUnitRoot());
_syntaxEnvelope.Prune();
return _syntaxEnvelope;
}
public override void VisitUsingDirective(UsingDirectiveSyntax node) =>
_syntaxEnvelope.AddUsingDirectiveSyntax(node);
public override void VisitNamespaceDeclaration(NamespaceDeclarationSyntax node)
{
_nodeIndex++;
UpdateNamespaceSyntaxEnvelopeStack(node);
foreach (var childNode in node.ChildNodes())
{
Visit(childNode);
}
}
public override void VisitClassDeclaration(ClassDeclarationSyntax node)
{
_nodeIndex++;
UpdateClassOrStructSyntaxEnvelopeStack(node);
foreach (var childNode in node.ChildNodes())
{
Visit(childNode);
}
}
public override void VisitStructDeclaration(StructDeclarationSyntax node)
{
_nodeIndex++;
UpdateClassOrStructSyntaxEnvelopeStack(node);
foreach (var childNode in node.ChildNodes())
{
Visit(childNode);
}
}
public override void VisitMethodDeclaration(MethodDeclarationSyntax node)
{
_nodeIndex++;
AttributeSyntax methodObsoletion = null;
var attributeNamesFound = node.FindAttributes(_attributeNames,
(_, referenceAttributeName) =>
{
_syntaxEnvelope.AddAttributeNameFound(referenceAttributeName);
},
attributeSyntax =>
{
if (attributeSyntax.IsMethodObsoletionAttribute())
{
methodObsoletion = attributeSyntax;
}
});
if (attributeNamesFound.Any())
{
UpdateClassOrStructSyntaxEnvelopeStackHead(node, attributeNamesFound.ToImmutableHashSet(),
methodObsoletion);
}
}
private static bool IsSameNode(NamespaceDeclarationSyntax a, NamespaceDeclarationSyntax b)
{
if (a == null && b == null)
{
return true;
}
var canBeTheSameNode = a != null && b != null && a.Name.ToString() == b.Name.ToString();
if (canBeTheSameNode)
{
if (a.Parent is CompilationUnitSyntax && b.Parent is CompilationUnitSyntax)
{
return true;
}
if (a.Parent is NamespaceDeclarationSyntax aa && b.Parent is NamespaceDeclarationSyntax bb)
{
return IsSameNode(aa, bb);
}
}
return false;
}
private static bool IsSameNode(TypeDeclarationSyntax a, TypeDeclarationSyntax b)
{
if (a == null && b == null)
{
return true;
}
if (a is ClassDeclarationSyntax && !(b is ClassDeclarationSyntax) ||
a is StructDeclarationSyntax && !(b is StructDeclarationSyntax))
{
return false;
}
var canBeTheSameNode = a != null && b != null && a.Identifier.ToString() == b.Identifier.ToString();
if (canBeTheSameNode)
{
if (a.Parent is ClassDeclarationSyntax ac && b.Parent is ClassDeclarationSyntax bc)
{
return IsSameNode(ac, bc);
}
if (a.Parent is StructDeclarationSyntax @as && b.Parent is StructDeclarationSyntax bs)
{
return IsSameNode(@as, bs);
}
if (a.Parent is NamespaceDeclarationSyntax an && b.Parent is NamespaceDeclarationSyntax bn)
{
return IsSameNode(an, bn);
}
if (a.Parent is CompilationUnitSyntax && b.Parent is CompilationUnitSyntax)
{
return true;
}
}
return false;
}
private void UpdateNamespaceSyntaxEnvelopeStack(NamespaceDeclarationSyntax node)
{
// A new namespace means that the current class or struct nesting level must be abandoned.
_classOrStructSyntaxEnvelopeStack.Clear();
{
// While no common parent can be found, pop the namespace syntax stack,
while (_namespaceSyntaxEnvelopeStack.Any())
{
var namespaceSyntaxEnvelope = _namespaceSyntaxEnvelopeStack.Peek();
// if the current namespace syntax node is the same as the new syntax node's parent, then we
// have found the node under which we need to place the new syntax node.
if (node.Parent is NamespaceDeclarationSyntax namespaceSyntaxParent &&
IsSameNode(namespaceSyntaxParent, namespaceSyntaxEnvelope.NamespaceDeclarationSyntax))
{
break;
}
_namespaceSyntaxEnvelopeStack.Pop();
}
}
{
NamespaceSyntaxEnvelope newNamespaceSyntaxEnvelope;
// If we have any namespace syntax nodes left in the stack,
if (_namespaceSyntaxEnvelopeStack.Any())
{
var namespaceSyntaxEnvelope = _namespaceSyntaxEnvelopeStack.Peek();
// we place the new syntax node under it.
newNamespaceSyntaxEnvelope = namespaceSyntaxEnvelope.AddNamespaceSyntax(node, _nodeIndex);
}
// Else,
else
{
// we place the new syntax node under the compilation unit's root.
newNamespaceSyntaxEnvelope = _syntaxEnvelope.AddNamespaceSyntax(node, _nodeIndex);
}
_namespaceSyntaxEnvelopeStack.Push(newNamespaceSyntaxEnvelope);
}
}
private void UpdateClassOrStructSyntaxEnvelopeStack(TypeDeclarationSyntax node)
{
{
var foundParentClassOrStructSyntax = false;
// While no common parent can be found, pop the class syntax stack,
while (_classOrStructSyntaxEnvelopeStack.Any())
{
var classOrStructSyntaxEnvelope = _classOrStructSyntaxEnvelopeStack.Peek();
// if the current class or struct syntax node is the same as the new syntax node's parent, then
// we have found the node under which we need to place the new syntax node.
if ((node.Parent is ClassDeclarationSyntax classSyntaxParent &&
IsSameNode(classSyntaxParent, classOrStructSyntaxEnvelope.ClassDeclarationSyntax)) ||
(node.Parent is StructDeclarationSyntax structSyntaxParent &&
IsSameNode(structSyntaxParent, classOrStructSyntaxEnvelope.StructDeclarationSyntax)))
{
foundParentClassOrStructSyntax = true;
break;
}
_classOrStructSyntaxEnvelopeStack.Pop();
}
if (!foundParentClassOrStructSyntax)
{
// While no common parent can be found, pop the namespace syntax stack,
while (_namespaceSyntaxEnvelopeStack.Any())
{
var namespaceSyntaxEnvelope = _namespaceSyntaxEnvelopeStack.Peek();
// if the current namespace syntax node is the same as the new syntax node's parent, then we
// have found the node under which we need to place the new syntax node.
if (node.Parent is NamespaceDeclarationSyntax namespaceSyntaxParent &&
IsSameNode(namespaceSyntaxParent, namespaceSyntaxEnvelope.NamespaceDeclarationSyntax))
{
break;
}
_namespaceSyntaxEnvelopeStack.Pop();
}
}
}
{
ClassOrStructSyntaxEnvelope newClassOrStructSyntaxEnvelope;
// If we have any class syntax nodes left in the stack,
if (_classOrStructSyntaxEnvelopeStack.Any())
{
var classOrStructSyntaxEnvelope = _classOrStructSyntaxEnvelopeStack.Peek();
// we place the new syntax node under it.
if (node is ClassDeclarationSyntax classDeclarationSyntax)
{
newClassOrStructSyntaxEnvelope =
classOrStructSyntaxEnvelope.AddClassSyntax(classDeclarationSyntax, _nodeIndex);
}
else if (node is StructDeclarationSyntax structDeclarationSyntax)
{
newClassOrStructSyntaxEnvelope =
classOrStructSyntaxEnvelope.AddStructSyntax(structDeclarationSyntax, _nodeIndex);
}
else
{
throw new ArgumentException(
$"The syntax node must be either of type {nameof(ClassDeclarationSyntax)}, or " +
$"{nameof(StructDeclarationSyntax)}.");
}
}
// Else, if we have any namespace syntax nodes left in the stack,
else if (_namespaceSyntaxEnvelopeStack.Any())
{
var namespaceSyntaxEnvelope = _namespaceSyntaxEnvelopeStack.Peek();
// we place the new syntax node under it.
if (node is ClassDeclarationSyntax classDeclarationSyntax)
{
newClassOrStructSyntaxEnvelope =
namespaceSyntaxEnvelope.AddClassSyntax(classDeclarationSyntax, _nodeIndex);
}
else if (node is StructDeclarationSyntax structDeclarationSyntax)
{
newClassOrStructSyntaxEnvelope =
namespaceSyntaxEnvelope.AddStructSyntax(structDeclarationSyntax, _nodeIndex);
}
else
{
throw new ArgumentException(
$"The syntax node must be either of type {nameof(ClassDeclarationSyntax)}, or " +
$"{nameof(StructDeclarationSyntax)}.");
}
}
// Else,
else
{
// we place the new syntax node under the compilation unit's root.
if (node is ClassDeclarationSyntax classDeclarationSyntax)
{
newClassOrStructSyntaxEnvelope =
_syntaxEnvelope.AddClassSyntax(classDeclarationSyntax, _nodeIndex);
}
else if (node is StructDeclarationSyntax structDeclarationSyntax)
{
newClassOrStructSyntaxEnvelope =
_syntaxEnvelope.AddStructSyntax(structDeclarationSyntax, _nodeIndex);
}
else
{
throw new ArgumentException(
$"The syntax node must be either of type {nameof(ClassDeclarationSyntax)}, or " +
$"{nameof(StructDeclarationSyntax)}.");
}
}
_classOrStructSyntaxEnvelopeStack.Push(newClassOrStructSyntaxEnvelope);
}
}
private void UpdateClassOrStructSyntaxEnvelopeStackHead(MethodDeclarationSyntax node,
ImmutableHashSet<string> attributeNamesFound, AttributeSyntax methodObsoletion)
{
// While no common parent can be found, pop the class or struct syntax stack,
while (_classOrStructSyntaxEnvelopeStack.Any())
{
var classOrStructSyntaxEnvelope = _classOrStructSyntaxEnvelopeStack.Peek();
// if the current class or struct syntax node is the same as the new syntax node's parent, then we
// have found the node under which we need to place the new syntax node.
if ((node.Parent is ClassDeclarationSyntax classSyntaxParent &&
IsSameNode(classSyntaxParent, classOrStructSyntaxEnvelope.ClassDeclarationSyntax)) ||
(node.Parent is StructDeclarationSyntax structSyntaxParent &&
IsSameNode(structSyntaxParent, classOrStructSyntaxEnvelope.StructDeclarationSyntax)))
{
break;
}
_classOrStructSyntaxEnvelopeStack.Pop();
}
// This call should never fail because we check for error diagnostics before we begin and syntactically,
// it is not possible to have a method outside a class or struct.
_classOrStructSyntaxEnvelopeStack.Peek()
.AddMethodSyntax(node, _nodeIndex, attributeNamesFound, methodObsoletion);
}
}
}
}