forked from UbiquityDotNET/Ubiquity.NET.Utils
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLocationExtensions.cs
More file actions
67 lines (58 loc) · 3.25 KB
/
LocationExtensions.cs
File metadata and controls
67 lines (58 loc) · 3.25 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
// Copyright (c) Ubiquity.NET Contributors. All rights reserved.
// Licensed under the Apache-2.0 WITH LLVM-exception license. See the LICENSE.md file in the project root for full license information.
namespace Ubiquity.NET.ANTLR.Utils
{
/// <summary>Utility class to provide extensions for translation of ANTLR location data into a <see cref="SourceRange"/></summary>
public static class LocationExtensions
{
/// <summary>Gets the <see cref="SourceRange"/> for a given <see cref="ParserRuleContext"/></summary>
/// <param name="ctx">Parser rule context to get the location from</param>
/// <returns><see cref="SourceRange"/> for the context</returns>
public static SourceRange GetSourceRange( this ParserRuleContext ctx )
{
ArgumentNullException.ThrowIfNull( ctx );
return new SourceRange( new(ctx.Start.Line, ctx.Start.Column, ctx.Start.StartIndex)
, new(ctx.Stop.Line, ctx.Stop.Column, ctx.Stop.StopIndex)
);
}
/// <summary>Attempts to retrieve the <see cref="SourceRange"/> from a <see cref="RuleContext"/></summary>
/// <param name="ctx">Context to get the span from</param>
/// <returns><see cref="SourceRange"/> for this input context or a default constructed one</returns>
/// <remarks>
/// Not all <see cref="RuleContext"/> derived types will support line+col location information. This
/// only tests for a <see cref="ParserRuleContext"/> and retrieves the location from that. The base
/// RuleContext and other derived types simply get a default constructed location as the location is
/// not known. (They only store location as an integral interval without the line+col information)
/// </remarks>
public static SourceRange GetSourceRange( this RuleContext ctx )
{
ArgumentNullException.ThrowIfNull( ctx );
if(ctx is ParserRuleContext ruleCtx)
{
ruleCtx.GetSourceRange();
}
// NOTE other RuleContext types may track position but a RuleContext itself
// only tracks the integral position as an Interval (no line+col info!)
return default;
}
/// <summary>Gets the source location information for a token an <see cref="ITerminalNode"/> represents</summary>
/// <param name="node">Terminal node</param>
/// <returns>Source span for the terminal's token</returns>
public static SourceRange GetSourceRange( this ITerminalNode node )
{
ArgumentNullException.ThrowIfNull( node );
return node.Symbol.GetSourceRange();
}
/// <summary>Gets the <see cref="SourceRange"/> from an <see cref="IToken"/></summary>
/// <param name="token">Token to get the location information for</param>
/// <returns>SourceLocation</returns>
public static SourceRange GetSourceRange( this IToken token )
{
ArgumentNullException.ThrowIfNull( token );
return new SourceRange(
new(token.Line, token.Column, token.StartIndex),
new(token.Line, token.Column + token.Text.Length, token.StopIndex)
);
}
}
}