...
This commit is contained in:
41
compiler/NubLang/Code/SourceFile.cs
Normal file
41
compiler/NubLang/Code/SourceFile.cs
Normal file
@@ -0,0 +1,41 @@
|
||||
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; }
|
||||
}
|
||||
42
compiler/NubLang/Code/SourceLocation.cs
Normal file
42
compiler/NubLang/Code/SourceLocation.cs
Normal file
@@ -0,0 +1,42 @@
|
||||
namespace NubLang.Code;
|
||||
|
||||
public readonly struct SourceLocation : IEquatable<SourceLocation>
|
||||
{
|
||||
public static SourceLocation Zero => new(0, 0);
|
||||
|
||||
public SourceLocation(int line, int column)
|
||||
{
|
||||
Line = line;
|
||||
Column = column;
|
||||
}
|
||||
|
||||
public int Line { get; }
|
||||
public int Column { get; }
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return $"{Line}:{Column}";
|
||||
}
|
||||
|
||||
public override bool Equals(object? obj)
|
||||
{
|
||||
return obj is SourceLocation other && Equals(other);
|
||||
}
|
||||
|
||||
public bool Equals(SourceLocation other)
|
||||
{
|
||||
return Line == other.Line && Column == other.Column;
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return HashCode.Combine(typeof(SourceLocation), Line, Column);
|
||||
}
|
||||
|
||||
public static bool operator ==(SourceLocation left, SourceLocation right) => Equals(left, right);
|
||||
public static bool operator !=(SourceLocation left, SourceLocation right) => !Equals(left, right);
|
||||
public static bool operator <(SourceLocation left, SourceLocation right) => left.Line < right.Line || (left.Line == right.Line && left.Column < right.Column);
|
||||
public static bool operator >(SourceLocation left, SourceLocation right) => left.Line > right.Line || (left.Line == right.Line && left.Column > right.Column);
|
||||
public static bool operator <=(SourceLocation left, SourceLocation right) => left.Line <= right.Line || (left.Line == right.Line && left.Column <= right.Column);
|
||||
public static bool operator >=(SourceLocation left, SourceLocation right) => left.Line >= right.Line || (left.Line == right.Line && left.Column >= right.Column);
|
||||
}
|
||||
57
compiler/NubLang/Code/SourceSpan.cs
Normal file
57
compiler/NubLang/Code/SourceSpan.cs
Normal file
@@ -0,0 +1,57 @@
|
||||
namespace NubLang.Code;
|
||||
|
||||
public readonly struct SourceSpan : IEquatable<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;
|
||||
}
|
||||
|
||||
var minStart = spanArray.Min(s => s.Start);
|
||||
var maxEnd = spanArray.Max(s => s.End);
|
||||
|
||||
return new SourceSpan(minStart, maxEnd);
|
||||
}
|
||||
|
||||
public SourceSpan(SourceLocation start, SourceLocation end)
|
||||
{
|
||||
if (start > end)
|
||||
{
|
||||
throw new ArgumentException("Start location cannot be after end location");
|
||||
}
|
||||
|
||||
Start = start;
|
||||
End = end;
|
||||
}
|
||||
|
||||
public SourceLocation Start { get; }
|
||||
public SourceLocation End { get; }
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
if (Start == End)
|
||||
{
|
||||
return $"{Start}";
|
||||
}
|
||||
|
||||
if (Start.Line == End.Line)
|
||||
{
|
||||
return Start.Column == End.Column ? $"{Start}" : $"{Start.Line}:{Start.Column}-{End.Column}";
|
||||
}
|
||||
|
||||
return $"{Start}-{End}";
|
||||
}
|
||||
|
||||
public bool Equals(SourceSpan other) => Start == other.Start && End == other.End;
|
||||
public override bool Equals(object? obj) => obj is SourceSpan other && Equals(other);
|
||||
public override int GetHashCode() => HashCode.Combine(typeof(SourceSpan), Start, End);
|
||||
|
||||
public static bool operator ==(SourceSpan left, SourceSpan right) => Equals(left, right);
|
||||
public static bool operator !=(SourceSpan left, SourceSpan right) => !Equals(left, right);
|
||||
}
|
||||
Reference in New Issue
Block a user