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
|
// Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information.
namespace WixToolset.Data
{
using System;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using SimpleJson;
/// <summary>
/// Represents file name and line number for source file
/// </summary>
public sealed class SourceLineNumber
{
/// <summary>
/// Constructor for a source with no line information.
/// </summary>
/// <param name="fileName">File name of the source.</param>
public SourceLineNumber(string fileName)
{
this.FileName = fileName;
}
/// <summary>
/// Constructor for a source with line information.
/// </summary>
/// <param name="fileName">File name of the source.</param>
/// <param name="lineNumber">Line number of the source.</param>
public SourceLineNumber(string fileName, int lineNumber)
{
this.FileName = fileName;
this.LineNumber = lineNumber;
}
/// <summary>
/// Constructor for a source with a parent and no line information.
/// </summary>
/// <param name="fileName">File name of the source.</param>
/// <param name="parent">Parent of this source line number</param>
public SourceLineNumber(string fileName, SourceLineNumber parent)
{
this.FileName = fileName;
this.Parent = parent;
}
/// <summary>
/// Constructor for a source with a parent and line information.
/// </summary>
/// <param name="fileName">File name of the source.</param>
/// <param name="parent">Parent of this source line number</param>
/// <param name="lineNumber">Line number of the source.</param>
public SourceLineNumber(string fileName, SourceLineNumber parent, int lineNumber)
{
this.FileName = fileName;
this.Parent = parent;
this.LineNumber = lineNumber;
}
/// <summary>
/// Gets the file name of the source.
/// </summary>
/// <value>File name for the source.</value>
public string FileName { get; }
/// <summary>
/// Gets or sets the line number of the source.
/// </summary>
/// <value>Line number of the source.</value>
public int? LineNumber { get; set; }
/// <summary>
/// Gets or sets the parent source line number that included this source line number.
/// </summary>
public SourceLineNumber Parent { get; private set; }
/// <summary>
/// Gets the file name and line information.
/// </summary>
/// <value>File name and line information.</value>
public string QualifiedFileName => this.LineNumber.HasValue ? String.Concat(this.FileName, "*", this.LineNumber) : this.FileName;
internal static SourceLineNumber Deserialize(JsonObject jsonObject)
{
var fileName = jsonObject.GetValueOrDefault<string>("file");
var lineNumber = jsonObject.GetValueOrDefault("line", null);
var parentJson = jsonObject.GetValueOrDefault<JsonObject>("parent");
var parent = (parentJson == null) ? null : SourceLineNumber.Deserialize(parentJson);
return new SourceLineNumber(fileName)
{
LineNumber = lineNumber,
Parent = parent
};
}
internal JsonObject Serialize()
{
var jsonObject = new JsonObject
{
{ "file", this.FileName },
{ "line", this.LineNumber }
};
if (this.Parent != null)
{
var parentJson = this.Parent.Serialize();
jsonObject.Add("parent", parentJson);
}
return jsonObject;
}
/// <summary>
/// Creates a source line number from an encoded string.
/// </summary>
/// <param name="encodedSourceLineNumbers">Encoded string to parse.</param>
public static SourceLineNumber CreateFromEncoded(string encodedSourceLineNumbers)
{
var linesSplitIndex = encodedSourceLineNumbers.IndexOf('|');
// The most common case is that there is a single encoded line,
// so optimize for that case.
if (linesSplitIndex < 0)
{
return DecodeSourceLineNumber(encodedSourceLineNumbers, 0, -1);
}
else // decode the multiple lines.
{
var startLine = 0;
SourceLineNumber first = null;
SourceLineNumber parent = null;
while (startLine < encodedSourceLineNumbers.Length)
{
var source = DecodeSourceLineNumber(encodedSourceLineNumbers, startLine, linesSplitIndex - 1);
if (null != parent)
{
parent.Parent = source;
}
parent = source;
if (null == first)
{
first = parent;
}
if (linesSplitIndex < 0)
{
break;
}
startLine = linesSplitIndex + 1;
linesSplitIndex = encodedSourceLineNumbers.IndexOf('|', startLine);
}
return first;
}
}
/// <summary>
/// Creates a source line number from a URI.
/// </summary>
/// <param name="uri">Uri to convert into source line number</param>
public static SourceLineNumber CreateFromUri(string uri)
{
if (String.IsNullOrEmpty(uri))
{
return null;
}
// make the local path look like a normal local path
var localPath = new Uri(uri).LocalPath;
localPath = localPath.TrimStart(Path.AltDirectorySeparatorChar).Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar);
return new SourceLineNumber(localPath);
}
/// <summary>
/// Creates a source line number from an XObject.
/// </summary>
/// <param name="node">XML node to create source line number from.</param>
/// <param name="offset">Optional line number offset into XML file not already included in the line information.</param>
public static SourceLineNumber CreateFromXObject(XObject node, int offset = 0)
{
var result = CreateFromUri(node.BaseUri);
if (null != result && node is IXmlLineInfo lineInfo)
{
result.LineNumber = lineInfo.LineNumber + offset;
}
return result;
}
/// <summary>
/// Get the source line information for the current element. Typically this information
/// is set by the precompiler for each element that it encounters.
/// </summary>
/// <param name="node">Element to get source line information for.</param>
/// <returns>
/// The source line number used to author the element being processed or
/// null if the preprocessor did not process the element or the node is
/// not an element.
/// </returns>
public static SourceLineNumber GetFromXAnnotation(XObject node)
{
return node.Annotation<SourceLineNumber>();
}
/// <summary>
/// Returns the SourceLineNumber and parents encoded as a string.
/// </summary>
public string GetEncoded()
{
var sb = new StringBuilder(this.QualifiedFileName);
for (var parent = this.Parent; null != parent; parent = parent.Parent)
{
sb.Append("|");
sb.Append(parent.QualifiedFileName);
}
return sb.ToString();
}
/// <summary>
/// Determines if two SourceLineNumbers are equivalent.
/// </summary>
/// <param name="obj">Object to compare.</param>
/// <returns>True if SourceLineNumbers are equivalent.</returns>
public override bool Equals(object obj)
{
return obj is SourceLineNumber other &&
this.LineNumber.HasValue == other.LineNumber.HasValue &&
(!this.LineNumber.HasValue || this.LineNumber == other.LineNumber) &&
this.FileName.Equals(other.FileName, StringComparison.OrdinalIgnoreCase) &&
(null == this.Parent && null == other.Parent || this.Parent.Equals(other.Parent));
}
/// <summary>
/// Serves as a hash code for a particular type.
/// </summary>
/// <returns>The hash code.</returns>
public override int GetHashCode()
{
return this.GetEncoded().GetHashCode();
}
/// <summary>
/// Shows a string representation of a source line number.
/// </summary>
/// <returns>String representation of a source line number.</returns>
public override string ToString()
{
return this.LineNumber.HasValue && !String.IsNullOrEmpty(this.FileName) ? String.Concat(this.FileName, "(", this.LineNumber, ")") : this.FileName ?? String.Empty;
}
private static SourceLineNumber DecodeSourceLineNumber(string encoded, int startIndex, int endIndex)
{
if (endIndex < 0)
{
endIndex = encoded.Length - 1;
}
var count = endIndex - startIndex;
var filenameSplitIndex = encoded.LastIndexOf('*', endIndex - 1, count);
return (filenameSplitIndex < 0) ? new SourceLineNumber(encoded) :
new SourceLineNumber(encoded.Substring(startIndex, filenameSplitIndex - startIndex),
Convert.ToInt32(encoded.Substring(filenameSplitIndex + 1, endIndex - filenameSplitIndex)));
}
}
}
|