Repository navigation
Expand file tree
/
Copy pathCXNodeExtensions.cs
More file actions
170 lines (140 loc) · 5.32 KB
/
Copy pathCXNodeExtensions.cs
File metadata and controls
170 lines (140 loc) · 5.32 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Discord.CX.Parser;
/// <summary>
/// Contains <see cref="ICXNode"/> related extensions.
/// </summary>
public static class CXNodeExtensions
{
extension(IReadOnlyList<CXToken> tokens)
{
public string ToValueString(bool includeLeadingTrivia = false, bool includeTrailingTrivia = false)
{
if (tokens.Count is 0) return string.Empty;
var sb = new StringBuilder();
for (var i = 0; i < tokens.Count; i++)
{
var token = tokens[i];
var isFirst = i == 0;
var isLast = i == tokens.Count - 1;
if (includeLeadingTrivia || !isFirst)
sb.Append(token.LeadingTrivia);
sb.Append(token.Value);
if (includeTrailingTrivia || !isLast)
sb.Append(token.TrailingTrivia);
}
return sb.ToString();
}
}
extension(ICXNode node)
{
/// <summary>
/// Gets whether this node or any descending nodes contains diagnostics.
/// </summary>
public bool HasDiagnostics
{
get
{
if (node.DiagnosticDescriptors.Count is not 0) return true;
return node.Slots.Any(x => x.HasDiagnostics);
}
}
/// <summary>
/// Gets whether this node contains any tokens that have the missing flag specified.
/// </summary>
public bool HasMissingTokens
{
get
{
if (node is CXToken token) return token.IsMissing;
return node.Slots.Any(x => x.HasMissingTokens);
}
}
/// <summary>
/// Gets the diagnostics reported for this node.
/// </summary>
public IReadOnlyList<CXDiagnostic> Diagnostics
=> [..node.DiagnosticDescriptors.Select(node.CreateDiagnostic)];
/// <summary>
/// Gets all diagnostics from this node and any descending nodes.
/// </summary>
public IReadOnlyList<CXDiagnostic> AllDiagnostics
=> [..node.Diagnostics, ..node.Slots.SelectMany(x => x.AllDiagnostics)];
/// <summary>
/// Gets the parser used to parse this <see cref="ICXNode"/>.
/// </summary>
/// <remarks>
/// During the parsing stage, where the entire document hasn't been fully parsed,
/// this property will return <see langword="null"/>.
/// </remarks>
public CXParser? Parser
{
get
{
if (node is CXDocument document) return document.Parser;
return node.Document?.Parser;
}
}
/// <summary>
/// Gets the source text containing this AST node.
/// </summary>
/// <remarks>
/// During the parsing stage, where the entire document hasn't been fully parsed,
/// this property will return <see langword="null"/>.
/// </remarks>
public CXSourceText? Source => node.Parser?.Reader.Source;
/// <summary>
/// Gets the character offset of this node relative to the entire AST.
/// </summary>
public int Offset
{
get
{
/*
* Traverse the AST upwards until we hit the root node, on each step up, add up the left-most
* siblings widths
*/
var offset = 0;
var current = node;
while (current is not null)
{
if (current.Parent is null) return offset;
var index = current.GetParentSlotIndex();
// continue traversing if we are the left-most sibling
if (index is 0)
{
current = current.Parent;
continue;
}
// add up the widths of the left-most siblings
for (var i = 0; i < index; i++)
offset += current.Parent.Slots[i].Width;
current = current.Parent;
}
return offset;
}
}
/// <summary>
/// Gets the index of this node within its parents <see cref="ICXNode.Slots"/>.
/// </summary>
/// <returns>
/// The index of this node within its parent; <c>-1</c> if there is no parent or this node isn't in its parents'
/// slots.
/// </returns>
public int GetParentSlotIndex()
{
if (node.Parent is null) return -1;
for (var i = 0; i < node.Parent.Slots.Count; i++)
if (ReferenceEquals(node, node.Parent.Slots[i]))
return i;
return -1;
}
/// <summary>
/// Converts this <see cref="ICXNode"/> into its equivalent CX syntax including trivia.
/// </summary>
/// <returns>The CX syntax representing this <see cref="ICXNode"/>.</returns>
public string ToFullString() => node.ToString(true, true);
}
}