- Refactored code
- Implemented basic JSON Path interpreter - Clean up
This commit is contained in:
10
DevDisciples.Parsing/Extensions/CastingExtensions.cs
Normal file
10
DevDisciples.Parsing/Extensions/CastingExtensions.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
namespace DevDisciples.Parsing.Extensions;
|
||||
|
||||
public static class CastingExtensions
|
||||
{
|
||||
public static T As<T>(this object @object)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(@object);
|
||||
return (T)@object;
|
||||
}
|
||||
}
|
||||
@@ -1,3 +0,0 @@
|
||||
namespace DevDisciples.Parsing;
|
||||
|
||||
public interface ISyntaxNode { }
|
||||
@@ -187,10 +187,8 @@ public abstract partial class Lexer<TToken> where TToken : Enum
|
||||
|
||||
ProcessDigits(src);
|
||||
|
||||
if (src.Peek() == '.' && IsDigit(src.Peek(1)))
|
||||
if (IsDigit(src.Peek(1)) && src.Match('.'))
|
||||
{
|
||||
// Consume the "."
|
||||
src.Advance();
|
||||
ProcessDigits(src);
|
||||
}
|
||||
|
||||
|
||||
@@ -6,11 +6,11 @@ public abstract class ParsableStream<T>
|
||||
|
||||
protected ReadOnlySpan<T> Tokens => _tokens.Span;
|
||||
|
||||
public int Position { get; set; }
|
||||
protected int Position { get; set; }
|
||||
|
||||
public T Current => Position < Tokens.Length ? Tokens[Position] : default!;
|
||||
|
||||
public ParsableStream(ReadOnlyMemory<T> tokens)
|
||||
protected ParsableStream(ReadOnlyMemory<T> tokens)
|
||||
{
|
||||
_tokens = tokens;
|
||||
}
|
||||
|
||||
@@ -2,9 +2,9 @@
|
||||
|
||||
public class ParserContext<TToken> : ParsableStream<Lexer<TToken>.Token> where TToken : Enum
|
||||
{
|
||||
protected readonly TToken _endOfSource;
|
||||
private readonly TToken _endOfSource;
|
||||
|
||||
public ParserContext(Memory<Lexer<TToken>.Token> tokens, TToken endOfSource) : base(tokens)
|
||||
protected ParserContext(Memory<Lexer<TToken>.Token> tokens, TToken endOfSource) : base(tokens)
|
||||
{
|
||||
_endOfSource = endOfSource;
|
||||
}
|
||||
@@ -98,9 +98,4 @@ public class ParserContext<TToken> : ParsableStream<Lexer<TToken>.Token> where T
|
||||
{
|
||||
return new ParsingException(Report.FormatMessage(token, message));
|
||||
}
|
||||
|
||||
public void Halt(Lexer<TToken>.Token token, string message)
|
||||
{
|
||||
throw new ParsingException(Report.FormatMessage(token, message));
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
public static class Report
|
||||
{
|
||||
public static Exception Error(ISourceLocation token, string message)
|
||||
public static ParsingException Error(ISourceLocation token, string message)
|
||||
{
|
||||
return new ParsingException(FormatMessage(token, message));
|
||||
}
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
namespace DevDisciples.Parsing;
|
||||
namespace DevDisciples.Parsing;
|
||||
|
||||
public static class Visitor
|
||||
{
|
||||
public delegate void Visit(object visitee, params object[] args);
|
||||
public delegate TOut Visit<TOut>(object visitee, params object[] args);
|
||||
public delegate TOut Visit<TIn, TOut>(TIn visitee, params object[] args);
|
||||
public delegate void Visit<TVisitee>(TVisitee visitee);
|
||||
|
||||
public delegate void Visit<TVisitee, TContext>(TVisitee visitee, TContext context);
|
||||
|
||||
public delegate TOut Visit<TVisitee, TContext, TOut>(TVisitee visitee, TContext context);
|
||||
}
|
||||
@@ -1,42 +1,40 @@
|
||||
namespace DevDisciples.Parsing;
|
||||
namespace DevDisciples.Parsing;
|
||||
|
||||
public class VisitorContainer
|
||||
public class VisitorContainer<TBase>
|
||||
{
|
||||
private Dictionary<Type, Visitor.Visit> Visitors { get; } = new();
|
||||
private Visitor.Visit Default { get; set; } = default!;
|
||||
private Dictionary<Type, Visitor.Visit<TBase>> Visitors { get; } = new();
|
||||
private Visitor.Visit<TBase> Default { get; set; } = default!;
|
||||
|
||||
public void Register<TVisitee>(Visitor.Visit visitor)
|
||||
public void Register<T>(Visitor.Visit<TBase> visitor)
|
||||
{
|
||||
Visitors[typeof(TVisitee)] = visitor;
|
||||
Visitors[typeof(T)] = visitor;
|
||||
}
|
||||
|
||||
public Visitor.Visit this[Type type] => Visitors.GetValueOrDefault(type, Default);
|
||||
public Visitor.Visit<TBase> this[Type type] => Visitors.GetValueOrDefault(type, Default);
|
||||
}
|
||||
|
||||
public class VisitorContainer<T>
|
||||
public class VisitorContainer<TBase, TContext>
|
||||
{
|
||||
protected Dictionary<Type, Visitor.Visit<T>> Visitors { get; } = new();
|
||||
public Visitor.Visit<T> Default { get; } = default!;
|
||||
private Dictionary<Type, Visitor.Visit<TBase, TContext>> Visitors { get; } = new();
|
||||
private Visitor.Visit<TBase, TContext> Default { get; set; } = default!;
|
||||
|
||||
|
||||
public VisitorContainer<T> Register<TVisitee>(Visitor.Visit<T> visitor)
|
||||
public void Register<T>(Visitor.Visit<TBase, TContext> visitor)
|
||||
{
|
||||
Visitors[typeof(TVisitee)] = visitor;
|
||||
return this;
|
||||
Visitors[typeof(T)] = visitor;
|
||||
}
|
||||
|
||||
public Visitor.Visit<T> this[Type type] => Visitors.ContainsKey(type) ? Visitors[type] : Default;
|
||||
public Visitor.Visit<TBase, TContext> this[Type type] => Visitors.GetValueOrDefault(type, Default);
|
||||
}
|
||||
|
||||
public class VisitorContainer<TIn, TOut>
|
||||
public class VisitorContainer<TBase, TContext, TResult>
|
||||
{
|
||||
protected Dictionary<Type, Visitor.Visit<TIn, TOut>> Visitors { get; } = new();
|
||||
public Visitor.Visit<TIn, TOut> Default { get; } = default!;
|
||||
private Dictionary<Type, Visitor.Visit<TBase, TContext, TResult>> Visitors { get; } = new();
|
||||
private Visitor.Visit<TBase, TContext, TResult> Default { get; set; } = default!;
|
||||
|
||||
public void Register<TVisitee>(Visitor.Visit<TIn, TOut> visitor)
|
||||
public void Register<T>(Visitor.Visit<TBase, TContext, TResult> visitor)
|
||||
{
|
||||
Visitors[typeof(TVisitee)] = visitor;
|
||||
Visitors[typeof(T)] = visitor;
|
||||
}
|
||||
|
||||
public Visitor.Visit<TIn, TOut> this[Type type] => Visitors.ContainsKey(type) ? Visitors[type] : Default;
|
||||
public Visitor.Visit<TBase, TContext, TResult> this[Type type] => Visitors.GetValueOrDefault(type, Default);
|
||||
}
|
||||
Reference in New Issue
Block a user