forked from IronLanguages/dlr
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSourceLocation.cs
More file actions
176 lines (149 loc) · 7.17 KB
/
SourceLocation.cs
File metadata and controls
176 lines (149 loc) · 7.17 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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information.
#nullable enable
using System;
using System.Globalization;
namespace Microsoft.Scripting {
/// <summary>
/// Represents a location in source code.
/// </summary>
[Serializable]
public readonly struct SourceLocation : IEquatable<SourceLocation> {
/// <summary>
/// Creates a new source location.
/// </summary>
/// <param name="index">The index in the source stream the location represents (0-based).</param>
/// <param name="line">The line in the source stream the location represents (1-based).</param>
/// <param name="column">The column in the source stream the location represents (1-based).</param>
public SourceLocation(int index, int line, int column) {
ValidateLocation(index, line, column);
Index = index;
Line = line;
Column = column;
}
private static void ValidateLocation(int index, int line, int column) {
if (index < 0) {
throw ErrorOutOfRange("index", 0);
}
if (line < 1) {
throw ErrorOutOfRange("line", 1);
}
if (column < 1) {
throw ErrorOutOfRange("column", 1);
}
}
private static Exception ErrorOutOfRange(object p0, object p1) {
return new ArgumentOutOfRangeException($"{p0} must be greater than or equal to {p1}");
}
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters")]
private SourceLocation(int index, int line, int column, bool noChecks) {
Index = index;
Line = line;
Column = column;
}
/// <summary>
/// The index in the source stream the location represents (0-based).
/// </summary>
public int Index { get; } // TODO: remove index
/// <summary>
/// The line in the source stream the location represents (1-based).
/// </summary>
public int Line { get; }
/// <summary>
/// The column in the source stream the location represents (1-based).
/// </summary>
public int Column { get; }
/// <summary>
/// Compares two specified location values to see if they are equal.
/// </summary>
/// <param name="left">One location to compare.</param>
/// <param name="right">The other location to compare.</param>
/// <returns>True if the locations are the same, False otherwise.</returns>
public static bool operator ==(SourceLocation left, SourceLocation right) => left.Equals(right);
/// <summary>
/// Compares two specified location values to see if they are not equal.
/// </summary>
/// <param name="left">One location to compare.</param>
/// <param name="right">The other location to compare.</param>
/// <returns>True if the locations are not the same, False otherwise.</returns>
public static bool operator !=(SourceLocation left, SourceLocation right) => !left.Equals(right);
/// <summary>
/// Compares two specified location values to see if one is before the other.
/// </summary>
/// <param name="left">One location to compare.</param>
/// <param name="right">The other location to compare.</param>
/// <returns>True if the first location is before the other location, False otherwise.</returns>
public static bool operator <(SourceLocation left, SourceLocation right) {
return left.Index < right.Index;
}
/// <summary>
/// Compares two specified location values to see if one is after the other.
/// </summary>
/// <param name="left">One location to compare.</param>
/// <param name="right">The other location to compare.</param>
/// <returns>True if the first location is after the other location, False otherwise.</returns>
public static bool operator >(SourceLocation left, SourceLocation right) {
return left.Index > right.Index;
}
/// <summary>
/// Compares two specified location values to see if one is before or the same as the other.
/// </summary>
/// <param name="left">One location to compare.</param>
/// <param name="right">The other location to compare.</param>
/// <returns>True if the first location is before or the same as the other location, False otherwise.</returns>
public static bool operator <=(SourceLocation left, SourceLocation right) {
return left.Index <= right.Index;
}
/// <summary>
/// Compares two specified location values to see if one is after or the same as the other.
/// </summary>
/// <param name="left">One location to compare.</param>
/// <param name="right">The other location to compare.</param>
/// <returns>True if the first location is after or the same as the other location, False otherwise.</returns>
public static bool operator >=(SourceLocation left, SourceLocation right) {
return left.Index >= right.Index;
}
/// <summary>
/// Compares two specified location values.
/// </summary>
/// <param name="left">One location to compare.</param>
/// <param name="right">The other location to compare.</param>
/// <returns>0 if the locations are equal, -1 if the left one is less than the right one, 1 otherwise.</returns>
public static int Compare(SourceLocation left, SourceLocation right) {
if (left < right) return -1;
if (left > right) return 1;
return 0;
}
/// <summary>
/// A location that is valid but represents no location at all.
/// </summary>
public static readonly SourceLocation None = new(0, 0xfeefee, 0, true);
/// <summary>
/// An invalid location.
/// </summary>
public static readonly SourceLocation Invalid = new(0, 0, 0, true);
/// <summary>
/// A minimal valid location.
/// </summary>
public static readonly SourceLocation MinValue = new(0, 1, 1);
/// <summary>
/// Whether the location is a valid location.
/// </summary>
/// <returns>True if the location is valid, False otherwise.</returns>
public bool IsValid => Line != 0 && Column != 0;
public bool Equals(SourceLocation other) =>
other.Index == Index && other.Line == Line && other.Column == Column;
public override bool Equals(object? obj) =>
obj is SourceLocation other && Equals(other);
public override int GetHashCode() {
return (Line << 16) ^ Column;
}
public override string ToString() {
return "(" + Line + "," + Column + ")";
}
internal string ToDebugString() {
return String.Format(CultureInfo.CurrentCulture, "({0},{1},{2})", Index, Line, Column);
}
}
}