using CanonSharp.Combinator.Abstractions; using CanonSharp.Pascal.Scanner; namespace CanonSharp.Pascal.Parser; public sealed class LexicalTokenReadState : IReadState { private readonly List _tokens; private readonly int _pos; public LexicalToken Current => _tokens[_pos]; public bool HasValue => _pos < _tokens.Count; public LexicalTokenReadState Next => new(_tokens, _pos + 1); private LexicalTokenReadState(List tokens, int pos) { _tokens = tokens; _pos = pos; } public LexicalTokenReadState(IEnumerable tokens) { _tokens = tokens.ToList(); _pos = 0; } public bool Equals(LexicalTokenReadState? other) { if (other is null) { return false; } if (_tokens.Count != other._tokens.Count) { return false; } foreach ((LexicalToken first, LexicalToken second) in _tokens.Zip(other._tokens)) { if (!first.Equals(second)) { return false; } } return _pos == other._pos; } public override string ToString() => HasValue ? Current.ToString() : "End of input stream."; }