42 lines
1.3 KiB
C#
42 lines
1.3 KiB
C#
namespace Qrakhen.Qamp.Core.Tokenization;
|
|
|
|
public readonly record struct Token(
|
|
TokenType Type,
|
|
string? Value = "",
|
|
TokenPosition Position = default,
|
|
StreamSpan Span = default
|
|
)
|
|
{
|
|
public static Token Void = new(TokenType.Void);
|
|
public static Token Error(string? message = null) => new Token(TokenType.Error, message);
|
|
public static Token Eof(string? message = null) => new Token(TokenType.Eof, message);
|
|
|
|
public override string ToString()
|
|
{
|
|
return $"[@{Position}] <{Type}> ({Value ?? "?"})";
|
|
}
|
|
|
|
public bool IsCompilationIrrelevant => Type == TokenType.NewLine || Type == TokenType.Comment || Type == TokenType.Whitespace;
|
|
}
|
|
|
|
public readonly record struct StreamSpan(long Start = -1, long Length = 0);
|
|
|
|
public readonly record struct TokenPosition(int Line = -1, int Column = -1)
|
|
{
|
|
public static readonly TokenPosition None = new(-1, -1);
|
|
|
|
public bool IsNone => this == None;
|
|
|
|
public override string ToString()
|
|
{
|
|
return $"{Line}:{Column}";
|
|
}
|
|
|
|
public TokenPosition Previous => new(Line, Column - 1);
|
|
|
|
public static TokenPosition operator +(TokenPosition a, TokenPosition b) =>
|
|
new(a.Line + b.Line, a.Column + b.Column);
|
|
|
|
public static TokenPosition operator -(TokenPosition a, TokenPosition b) =>
|
|
new(a.Line - b.Line, a.Column - b.Column);
|
|
} |