56 lines
1.7 KiB
C#
56 lines
1.7 KiB
C#
namespace NubLang.Diagnostics;
|
|
|
|
public readonly struct SourceSpan
|
|
{
|
|
private readonly int _startIndex;
|
|
private readonly int _endIndex;
|
|
|
|
public static SourceSpan Merge(params IEnumerable<SourceSpan> spans)
|
|
{
|
|
var spanArray = spans as SourceSpan[] ?? spans.ToArray();
|
|
if (spanArray.Length == 0)
|
|
{
|
|
return new SourceSpan(string.Empty, string.Empty, 0, 0, 0, 0, 0, 0);
|
|
}
|
|
|
|
var first = spanArray.MinBy(x => x._startIndex);
|
|
var last = spanArray.MaxBy(x => x._endIndex);
|
|
|
|
return new SourceSpan(first.SourcePath, first.Source, first._startIndex, last._endIndex, first.StartLine, last.EndLine, first.StartColumn, last.EndColumn);
|
|
}
|
|
|
|
public SourceSpan(string sourcePath, string source, int startIndex, int endIndex, int startLine, int startColumn, int endLine, int endColumn)
|
|
{
|
|
_startIndex = startIndex;
|
|
_endIndex = endIndex;
|
|
SourcePath = sourcePath;
|
|
Source = source;
|
|
StartLine = startLine;
|
|
StartColumn = startColumn;
|
|
EndLine = endLine;
|
|
EndColumn = endColumn;
|
|
}
|
|
|
|
public int StartLine { get; }
|
|
public int StartColumn { get; }
|
|
public int EndLine { get; }
|
|
public int EndColumn { get; }
|
|
|
|
public string SourcePath { get; }
|
|
public string Source { get; }
|
|
|
|
public override string ToString()
|
|
{
|
|
if (StartLine == EndLine && StartColumn == EndColumn)
|
|
{
|
|
return $"{SourcePath}:{StartColumn}:{StartColumn}";
|
|
}
|
|
|
|
if (StartLine == EndLine)
|
|
{
|
|
return $"{SourcePath}:{StartLine}:{StartColumn}-{EndColumn}";
|
|
}
|
|
|
|
return $"{SourcePath}:{StartLine}:{StartColumn}-{EndLine}:{EndColumn}";
|
|
}
|
|
} |