-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLocation.cs
More file actions
148 lines (128 loc) · 5.03 KB
/
Location.cs
File metadata and controls
148 lines (128 loc) · 5.03 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
using System;
namespace GameDialog.Lang
{
/// <summary>
/// Represents a position range in the source code.
/// </summary>
internal readonly struct Location
{
/// <summary>
/// The source code input.
/// </summary>
public Source Source { get; }
/// <summary>
/// Line number (1-based).
/// </summary>
public int Line { get; }
/// <summary>
/// Start column (1-based).
/// </summary>
public int Initial { get; }
/// <summary>
/// End column (1-based, exclusive).
/// </summary>
public int Final { get; }
/// <summary>
/// Initializes a new instance of the <see cref="Location"/> struct.
/// </summary>
public Location(Source source, int line, int position) : this(source, line, position, position + 1)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="Location"/> struct.
/// </summary>
public Location(Source source, int line, int initial, int final)
{
if (final <= initial)
{
throw new ArgumentException("Final position must be greater than initial position.");
}
Source = source;
Line = line;
Initial = initial;
Final = final;
}
/// <summary>
/// Combines two locations into a single range spanning from the start of the first to the end of the second.
/// </summary>
/// <exception cref="InvalidOperationException">Thrown when the sources or lines of the two locations differ.</exception>
public static Location operator +(Location left, Location right)
{
if (left.Source != right.Source)
{
throw new ArgumentException($"Mismatched sources. Left source: {left.Source}, Right source: {right.Source}");
}
if (left.Line != right.Line)
{
throw new ArgumentException($"Mismatched lines. Left line: {left.Line}, Right line: {right.Line}");
}
return new Location(left.Source, left.Line, left.Initial, right.Final);
}
/// <summary>
/// Extends the location to a new final position based on the current position of the reader.
/// </summary>
/// <param name="location">The base location.</param>
/// <param name="reader">The reader providing the current position.</param>
/// <returns>
/// A new location starting at <c>Initial</c> and ending at the reader's current
/// column on the same line and source.
/// </returns>
public static Location operator |(Location location, Reader reader)
{
if (reader.Source != location.Source)
{
throw new ArgumentException($"Mismatched sources, location: {location.Source}, reader: {reader.Source}");
}
if (reader.Line != location.Line)
{
throw new ArgumentException($"Mismatched lines, location: {location.Line}, reader: {reader.Line}");
}
if (reader.Column < location.Initial)
{
throw new ArgumentException($"Final position too small, ({location}) | ({reader.Column})");
}
if (location.Final >= reader.Column)
{
return location;
}
return new Location(location.Source, location.Line, location.Initial, reader.Column);
}
/// <summary>
/// Extends the location to a new final position.
/// </summary>
/// <param name="location">The base location.</param>
/// <param name="final">The new final position.</param>
/// <returns>
/// A new location starting at <c>Initial</c> and ending at the specified final position
/// on the same line and source.
/// </returns>
public static Location operator |(Location location, int final)
{
if (final < location.Initial)
{
throw new ArgumentException($"Final position too small, ({location}) | ({final})");
}
if (location.Final >= final)
{
return location;
}
return new Location(location.Source, location.Line, location.Initial, final);
}
/// <summary>
/// Returns a string representation of the location.
/// </summary>
public override string ToString()
{
var source = Source.Type switch
{
SourceType.Inline => "",
SourceType.File => $"{Source}, ",
SourceType.None => "",
_ => throw new NotSupportedException($"Unknown source type: {Source.Type}"),
};
return Initial == Final - 1
? source + $"Line {Line}, Col {Initial}"
: source + $"Line {Line}, Col {Initial}-{Final - 1}";
}
}
}