-
Notifications
You must be signed in to change notification settings - Fork 10
/
LuaExtensions.cs
280 lines (246 loc) · 13.2 KB
/
LuaExtensions.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
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
using System.Diagnostics.CodeAnalysis;
using Loretta.CodeAnalysis.Lua;
using Loretta.CodeAnalysis.Syntax;
namespace Loretta.CodeAnalysis
{
/// <summary>
/// Extension methods for lua specific data.
/// </summary>
public static class LuaExtensions
{
/// <summary>
/// Determines if <see cref="SyntaxToken"/> is of a specified kind.
/// </summary>
/// <param name="token">The source token.</param>
/// <param name="kind">The syntax kind to test for.</param>
/// <returns><see langword="true"/> if the token is of the specified kind; otherwise, <see langword="false"/>.</returns>
public static bool IsKind(this SyntaxToken token, SyntaxKind kind) => token.RawKind == (int) kind;
/// <summary>
/// Determines if <see cref="SyntaxTrivia"/> is of a specified kind.
/// </summary>
/// <param name="trivia">The source trivia.</param>
/// <param name="kind">The syntax kind to test for.</param>
/// <returns><see langword="true"/> if the trivia is of the specified kind; otherwise, <see langword="false"/>.</returns>
public static bool IsKind(this SyntaxTrivia trivia, SyntaxKind kind) => trivia.RawKind == (int) kind;
/// <summary>
/// Determines if <see cref="SyntaxNode"/> is of a specified kind.
/// </summary>
/// <param name="node">The source node.</param>
/// <param name="kind">The syntax kind to test for.</param>
/// <returns><see langword="true"/> if the node is of the specified kind; otherwise, <see langword="false"/>.</returns>
public static bool IsKind([NotNullWhen(true)] this SyntaxNode? node, SyntaxKind kind) => node?.RawKind == (int) kind;
/// <summary>
/// Determines if <see cref="SyntaxNodeOrToken"/> is of a specified kind.
/// </summary>
/// <param name="nodeOrToken">The source node or token.</param>
/// <param name="kind">The syntax kind to test for.</param>
/// <returns><see langword="true"/> if the node or token is of the specified kind; otherwise, <see langword="false"/>.</returns>
public static bool IsKind(this SyntaxNodeOrToken nodeOrToken, SyntaxKind kind) => nodeOrToken.RawKind == (int) kind;
internal static SyntaxKind ContextualKind(this SyntaxToken token)
=> (object) token.Language == LanguageNames.Lua ? (SyntaxKind) token.RawContextualKind : SyntaxKind.None;
/// <summary>
/// Returns the index of the first node of a specified kind in the node list.
/// </summary>
/// <param name="list">Node list.</param>
/// <param name="kind">The <see cref="SyntaxKind"/> to find.</param>
/// <returns>Returns non-negative index if the list contains a node which matches <paramref name="kind"/>, -1 otherwise.</returns>
public static int IndexOf<TNode>(this SyntaxList<TNode> list, SyntaxKind kind)
where TNode : SyntaxNode => list.IndexOf((int) kind);
/// <summary>
/// True if the list has at least one node of the specified kind.
/// </summary>
public static bool Any<TNode>(this SyntaxList<TNode> list, SyntaxKind kind)
where TNode : SyntaxNode => list.IndexOf(kind) >= 0;
/// <summary>
/// Returns the index of the first node of a specified kind in the node list.
/// </summary>
/// <param name="list">Node list.</param>
/// <param name="kind">The <see cref="SyntaxKind"/> to find.</param>
/// <returns>Returns non-negative index if the list contains a node which matches <paramref name="kind"/>, -1 otherwise.</returns>
public static int IndexOf<TNode>(this SeparatedSyntaxList<TNode> list, SyntaxKind kind)
where TNode : SyntaxNode => list.IndexOf((int) kind);
/// <summary>
/// True if the list has at least one node of the specified kind.
/// </summary>
public static bool Any<TNode>(this SeparatedSyntaxList<TNode> list, SyntaxKind kind)
where TNode : SyntaxNode => list.IndexOf(kind) >= 0;
/// <summary>
/// Returns the index of the first trivia of a specified kind in the trivia list.
/// </summary>
/// <param name="list">Trivia list.</param>
/// <param name="kind">The <see cref="SyntaxKind"/> to find.</param>
/// <returns>Returns non-negative index if the list contains a trivia which matches <paramref name="kind"/>, -1 otherwise.</returns>
public static int IndexOf(this SyntaxTriviaList list, SyntaxKind kind) => list.IndexOf((int) kind);
/// <summary>
/// True if the list has at least one trivia of the specified kind.
/// </summary>
public static bool Any(this SyntaxTriviaList list, SyntaxKind kind) => list.IndexOf(kind) >= 0;
/// <summary>
/// Returns the index of the first token of a specified kind in the token list.
/// </summary>
/// <param name="list">Token list.</param>
/// <param name="kind">The <see cref="SyntaxKind"/> to find.</param>
/// <returns>Returns non-negative index if the list contains a token which matches <paramref name="kind"/>, -1 otherwise.</returns>
public static int IndexOf(this SyntaxTokenList list, SyntaxKind kind) => list.IndexOf((int) kind);
/// <summary>
/// Tests whether a list contains a token of a particular kind.
/// </summary>
/// <param name="list"></param>
/// <param name="kind">The <see cref="SyntaxKind"/> to test for.</param>
/// <returns>Returns true if the list contains a token which matches <paramref name="kind"/></returns>
public static bool Any(this SyntaxTokenList list, SyntaxKind kind) => list.IndexOf(kind) >= 0;
internal static SyntaxToken FirstOrDefault(this SyntaxTokenList list, SyntaxKind kind)
{
var index = list.IndexOf(kind);
return (index >= 0) ? list[index] : default;
}
}
}
namespace Loretta.CodeAnalysis.Lua
{
/// <summary>
/// Extension methods for lua specific data.
/// </summary>
public static class LuaExtensions
{
/// <summary>
/// Determines if the given raw kind value belongs to the C# <see cref="SyntaxKind"/> enumeration.
/// </summary>
/// <param name="rawKind">The raw value to test.</param>
/// <returns><see langword="true"/> when the raw value belongs to the C# syntax kind; otherwise, <see langword="false"/>.</returns>
internal static bool IsLuaKind(int rawKind)
{
const int FirstLuaKind = (int) SyntaxKind.ShebangTrivia;
const int LastLuaKind = (int) SyntaxKind.CompilationUnit;
// in the range [FirstLuaKind, LastLuaKind]
return unchecked((uint) (rawKind - FirstLuaKind)) <= (LastLuaKind - FirstLuaKind);
}
/// <summary>
/// Returns <see cref="SyntaxKind"/> for <see cref="SyntaxToken"/> from <see cref="SyntaxToken.RawKind"/> property.
/// </summary>
public static SyntaxKind Kind(this SyntaxToken token)
{
var rawKind = token.RawKind;
return IsLuaKind(rawKind) ? (SyntaxKind) rawKind : SyntaxKind.None;
}
/// <summary>
/// Returns <see cref="SyntaxKind"/> for <see cref="SyntaxTrivia"/> from <see cref="SyntaxTrivia.RawKind"/> property.
/// </summary>
public static SyntaxKind Kind(this SyntaxTrivia trivia)
{
var rawKind = trivia.RawKind;
return IsLuaKind(rawKind) ? (SyntaxKind) rawKind : SyntaxKind.None;
}
/// <summary>
/// Returns <see cref="SyntaxKind"/> for <see cref="SyntaxNode"/> from <see cref="SyntaxNode.RawKind"/> property.
/// </summary>
public static SyntaxKind Kind(this SyntaxNode node)
{
var rawKind = node.RawKind;
return IsLuaKind(rawKind) ? (SyntaxKind) rawKind : SyntaxKind.None;
}
/// <summary>
/// Returns <see cref="SyntaxKind"/> for <see cref="SyntaxNode"/> from <see cref="SyntaxNodeOrToken.RawKind"/> property.
/// </summary>
public static SyntaxKind Kind(this SyntaxNodeOrToken nodeOrToken)
{
var rawKind = nodeOrToken.RawKind;
return IsLuaKind(rawKind) ? (SyntaxKind) rawKind : SyntaxKind.None;
}
/// <summary>
/// Returns whether the provided <see cref="SyntaxToken"/> is a Lua keyword.
/// </summary>
/// <param name="token"></param>
/// <returns></returns>
public static bool IsKeyword(this SyntaxToken token) => SyntaxFacts.IsKeyword(token.Kind());
/// <summary>
/// Returns whether a <see cref="SyntaxToken"/> is a verbatim string literal.
/// </summary>
/// <param name="token"></param>
/// <returns></returns>
public static bool IsVerbatimStringLiteral(this SyntaxToken token)
=> token.IsKind(SyntaxKind.StringLiteralToken) && token.Text.StartsWith("[", StringComparison.Ordinal);
/// <summary>
/// Insert one or more tokens in the list at the specified index.
/// </summary>
/// <returns>A new list with the tokens inserted.</returns>
public static SyntaxTokenList Insert(this SyntaxTokenList list, int index, params SyntaxToken[] items)
{
if (index < 0 || index > list.Count)
{
throw new ArgumentOutOfRangeException(nameof(index));
}
if (items is null) throw new ArgumentNullException(nameof(items));
if (list.Count == 0)
{
return SyntaxFactory.TokenList(items);
}
else
{
var builder = new SyntaxTokenListBuilder(list.Count + items.Length);
if (index > 0)
{
builder.Add(list, 0, index);
}
builder.Add(items);
if (index < list.Count)
{
builder.Add(list, index, list.Count - index);
}
return builder.ToList();
}
}
/// <summary>
/// Creates a new token with the specified old trivia replaced with computed new trivia.
/// </summary>
/// <param name="token"></param>
/// <param name="trivia">The trivia to be replaced; descendants of the root token.</param>
/// <param name="computeReplacementTrivia">A function that computes a replacement trivia for
/// the argument trivia. The first argument is the original trivia. The second argument is
/// the same trivia rewritten with replaced structure.</param>
public static SyntaxToken ReplaceTrivia(this SyntaxToken token, IEnumerable<SyntaxTrivia> trivia, Func<SyntaxTrivia, SyntaxTrivia, SyntaxTrivia> computeReplacementTrivia)
=> Syntax.SyntaxReplacer.Replace(token, trivia: trivia, computeReplacementTrivia: computeReplacementTrivia);
/// <summary>
/// Creates a new token with the specified old trivia replaced with a new trivia. The old trivia may appear in
/// the token's leading or trailing trivia.
/// </summary>
/// <param name="token"></param>
/// <param name="oldTrivia">The trivia to be replaced.</param>
/// <param name="newTrivia">The new trivia to use in the new tree in place of the old
/// trivia.</param>
public static SyntaxToken ReplaceTrivia(this SyntaxToken token, SyntaxTrivia oldTrivia, SyntaxTrivia newTrivia)
=> Syntax.SyntaxReplacer.Replace(token, trivia: new[] { oldTrivia }, computeReplacementTrivia: (o, r) => newTrivia);
/// <summary>
/// Returns this list as a <see cref="Loretta.CodeAnalysis.SeparatedSyntaxList<TNode>"/>.
/// </summary>
/// <typeparam name="TOther">The type of the list elements in the separated list.</typeparam>
/// <returns></returns>
internal static SeparatedSyntaxList<TOther> AsSeparatedList<TOther>(this SyntaxNodeOrTokenList list) where TOther : SyntaxNode
{
var builder = SeparatedSyntaxListBuilder<TOther>.Create();
foreach (var i in list)
{
var node = i.AsNode();
if (node != null)
{
builder.Add((TOther) node);
}
else
{
builder.AddSeparator(i.AsToken());
}
}
return builder.ToList();
}
#region SyntaxTree
/// <summary>
/// Obtains the root as a <see cref="Syntax.CompilationUnitSyntax"/>.
/// </summary>
/// <param name="tree"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public static Syntax.CompilationUnitSyntax GetCompilationUnitRoot(this SyntaxTree tree, CancellationToken cancellationToken = default)
=> (Syntax.CompilationUnitSyntax) tree.GetRoot(cancellationToken);
#endregion SyntaxTree
}
}