From ba9d6a88e043d5c6badb1b73f69eff94db32bfee Mon Sep 17 00:00:00 2001 From: tacosontitan Date: Fri, 29 Mar 2024 19:12:09 -0500 Subject: [PATCH] Updated lexer abstractions. --- src/Pasper/Parsing/ILexer.cs | 22 +++------------- src/Pasper/Parsing/Lexer.cs | 50 ++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 19 deletions(-) create mode 100644 src/Pasper/Parsing/Lexer.cs diff --git a/src/Pasper/Parsing/ILexer.cs b/src/Pasper/Parsing/ILexer.cs index a5ade2c..d12bb90 100644 --- a/src/Pasper/Parsing/ILexer.cs +++ b/src/Pasper/Parsing/ILexer.cs @@ -1,31 +1,15 @@ +using System.Collections; + namespace Pasper.Parsing; /// /// Represents a lexer for a serialization format. /// public interface ILexer + : IEnumerator { /// /// Gets the token at the previous position of the lexer. /// public IToken? Previous { get; } - - /// - /// Gets the token at the current position of the lexer. - /// - public IToken? Current { get; } - - /// - /// Advances the lexer to the next token. - /// - /// - /// if the lexer was successfully advanced to the next token; - /// otherwise, . - /// - public bool MoveNext(); - - /// - /// Resets the lexer to its initial position. - /// - public void Reset(); } \ No newline at end of file diff --git a/src/Pasper/Parsing/Lexer.cs b/src/Pasper/Parsing/Lexer.cs new file mode 100644 index 0000000..75b4717 --- /dev/null +++ b/src/Pasper/Parsing/Lexer.cs @@ -0,0 +1,50 @@ +using System.Collections; + +namespace Pasper.Parsing; + +/// +/// Represents a lexer for a serialization format. +/// +public abstract class Lexer + : ILexer +{ + private bool _disposed; + + /// + public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(this); + } + + /// + public abstract bool MoveNext(); + + /// + public abstract void Reset(); + + /// + public IToken? Current { get; protected set; } + + /// + object IEnumerator.Current => + Current!; + + /// + public IToken? Previous { get; protected set; } + + /// + /// Releases the unmanaged resources used by the and optionally releases the managed resources. + /// + /// Indicates whether the method was called from the method. + protected virtual void Dispose(bool disposing) + { + if (_disposed) + return; + + if (!disposing) + return; + + _disposed = true; + } +} \ No newline at end of file