jtr/DevDisciples.Parsing/ParsableStream.cs
2024-09-15 17:23:27 +02:00

32 lines
781 B
C#

namespace DevDisciples.Parsing;
public abstract class ParsableStream<T>
{
private readonly ReadOnlyMemory<T> _tokens;
protected ReadOnlySpan<T> Tokens => _tokens.Span;
public int Position { get; set; }
public T Current => Position < Tokens.Length ? Tokens[Position] : default!;
public ParsableStream(ReadOnlyMemory<T> tokens)
{
_tokens = tokens;
}
public virtual T Advance()
{
return Position + 1 <= Tokens.Length ? Tokens[++Position - 1] : default!;
}
public virtual T Peek(int offset = 0)
{
return Position + offset < Tokens.Length ? Tokens[Position + offset] : default!;
}
public virtual bool Ended()
{
return Position >= Tokens.Length;
}
}