Perf improvements in tokenizer
This commit is contained in:
@@ -1,41 +0,0 @@
|
||||
namespace NubLang.Code;
|
||||
|
||||
public class SourceFile
|
||||
{
|
||||
private string? _content;
|
||||
|
||||
public SourceFile(string path)
|
||||
{
|
||||
Path = path ?? throw new ArgumentNullException(nameof(path));
|
||||
}
|
||||
|
||||
public string Path { get; }
|
||||
|
||||
public string GetText() => _content ??= File.ReadAllText(Path);
|
||||
public override string ToString() => Path;
|
||||
|
||||
public override bool Equals(object? obj)
|
||||
{
|
||||
return obj is SourceFile other && other.Path == Path;
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return HashCode.Combine(typeof(SourceFile), Path);
|
||||
}
|
||||
|
||||
public static bool operator ==(SourceFile? left, SourceFile? right) => Equals(left, right);
|
||||
public static bool operator !=(SourceFile? left, SourceFile? right) => !Equals(left, right);
|
||||
}
|
||||
|
||||
public class SourceFileSpan
|
||||
{
|
||||
public SourceFileSpan(SourceFile sourceFile, SourceSpan span)
|
||||
{
|
||||
SourceFile = sourceFile;
|
||||
Span = span;
|
||||
}
|
||||
|
||||
public SourceFile SourceFile { get; }
|
||||
public SourceSpan Span { get; }
|
||||
}
|
||||
@@ -2,8 +2,6 @@ namespace NubLang.Code;
|
||||
|
||||
public readonly struct SourceLocation : IEquatable<SourceLocation>, IComparable<SourceLocation>
|
||||
{
|
||||
public static SourceLocation Zero => new(0, 0);
|
||||
|
||||
public SourceLocation(int line, int column)
|
||||
{
|
||||
Line = line;
|
||||
|
||||
@@ -2,34 +2,33 @@ namespace NubLang.Code;
|
||||
|
||||
public readonly struct SourceSpan : IEquatable<SourceSpan>, IComparable<SourceSpan>
|
||||
{
|
||||
public static SourceSpan Zero => new(SourceLocation.Zero, SourceLocation.Zero);
|
||||
|
||||
public static SourceSpan Merge(params IEnumerable<SourceSpan> spans)
|
||||
{
|
||||
var spanArray = spans as SourceSpan[] ?? spans.ToArray();
|
||||
|
||||
if (spanArray.Length == 0)
|
||||
{
|
||||
return Zero;
|
||||
return new SourceSpan(string.Empty, new SourceLocation(0, 0), new SourceLocation(0, 0));
|
||||
}
|
||||
|
||||
var minStart = spanArray.Min(s => s.Start);
|
||||
var maxEnd = spanArray.Max(s => s.End);
|
||||
|
||||
return new SourceSpan(minStart, maxEnd);
|
||||
return new SourceSpan(spanArray[0].FilePath, minStart, maxEnd);
|
||||
}
|
||||
|
||||
public SourceSpan(SourceLocation start, SourceLocation end)
|
||||
public SourceSpan(string filePath, SourceLocation start, SourceLocation end)
|
||||
{
|
||||
if (start > end)
|
||||
{
|
||||
throw new ArgumentException("Start location cannot be after end location");
|
||||
}
|
||||
|
||||
FilePath = filePath;
|
||||
Start = start;
|
||||
End = end;
|
||||
}
|
||||
|
||||
public string FilePath { get; }
|
||||
public SourceLocation Start { get; }
|
||||
public SourceLocation End { get; }
|
||||
|
||||
@@ -37,15 +36,15 @@ public readonly struct SourceSpan : IEquatable<SourceSpan>, IComparable<SourceSp
|
||||
{
|
||||
if (Start == End)
|
||||
{
|
||||
return $"{Start}";
|
||||
return $"{FilePath}:{Start}";
|
||||
}
|
||||
|
||||
if (Start.Line == End.Line)
|
||||
{
|
||||
return Start.Column == End.Column ? $"{Start}" : $"{Start.Line}:{Start.Column}-{End.Column}";
|
||||
return Start.Column == End.Column ? $"{FilePath}:{Start}" : $"{FilePath}:{Start.Line}:{Start.Column}-{End.Column}";
|
||||
}
|
||||
|
||||
return $"{Start}-{End}";
|
||||
return $"{FilePath}:{Start}-{End}";
|
||||
}
|
||||
|
||||
public bool Equals(SourceSpan other) => Start == other.Start && End == other.End;
|
||||
@@ -54,7 +53,7 @@ public readonly struct SourceSpan : IEquatable<SourceSpan>, IComparable<SourceSp
|
||||
|
||||
public static bool operator ==(SourceSpan left, SourceSpan right) => Equals(left, right);
|
||||
public static bool operator !=(SourceSpan left, SourceSpan right) => !Equals(left, right);
|
||||
|
||||
|
||||
public static bool operator <(SourceSpan left, SourceSpan right) => left.CompareTo(right) < 0;
|
||||
public static bool operator <=(SourceSpan left, SourceSpan right) => left.CompareTo(right) <= 0;
|
||||
public static bool operator >(SourceSpan left, SourceSpan right) => left.CompareTo(right) > 0;
|
||||
|
||||
Reference in New Issue
Block a user