Skip to content

Commit

Permalink
perf(syntax): Refactor syntax nodes to use ISyntaxNode interface
Browse files Browse the repository at this point in the history
- Replace SyntaxNode with ISyntaxNode throughout the project
- Update ExpressionSyntax and derived classes to implement ISyntaxNode
- Modify constructors and methods to accommodate the new interface
- Ensure consistency in method signatures using ISyntaxNode
  • Loading branch information
alirezanet committed Jun 22, 2024
1 parent d3c1ed8 commit 82e55da
Show file tree
Hide file tree
Showing 14 changed files with 69 additions and 64 deletions.
4 changes: 2 additions & 2 deletions src/Gridify.Elasticsearch/ElasticsearchQueryBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 13,7 @@ internal class ElasticsearchQueryBuilder<T>(IGridifyMapper<T> mapper) : BaseQuer
private readonly IGridifyMapper<T> _mapper = mapper;

protected override Query BuildNestedQuery(
Expression body, IGMap<T> gMap, ValueExpressionSyntax value, SyntaxNode op)
Expression body, IGMap<T> gMap, ValueExpressionSyntax value, ISyntaxNode op)
{
throw new NotSupportedException();
}
Expand All @@ -40,7 40,7 @@ protected override object BuildQueryAccordingToValueType(
Expression body,
ParameterExpression parameter,
object? value,
SyntaxNode op,
ISyntaxNode op,
ValueExpressionSyntax valueExpression)
{
if (valueExpression.IsCaseInsensitive)
Expand Down
4 changes: 2 additions & 2 deletions src/Gridify/GridifyExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -197,9 197,9 @@ internal static IGridifyMapper<T> FixMapper<T>(this IGridifyMapper<T>? mapper, S
return mapper;
}

private static IEnumerable<SyntaxNode> Descendants(this SyntaxNode root)
private static IEnumerable<ISyntaxNode> Descendants(this ISyntaxNode root)
{
var nodes = new Stack<SyntaxNode>(new[] { root });
var nodes = new Stack<ISyntaxNode>(new[] { root });
while (nodes.Any())
{
var node = nodes.Pop();
Expand Down
6 changes: 3 additions & 3 deletions src/Gridify/QueryBuilders/BaseQueryBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 16,7 @@ internal TQuery Build(ExpressionSyntax expression)
}

protected abstract TQuery? BuildNestedQuery(
Expression body, IGMap<T> gMap, ValueExpressionSyntax value, SyntaxNode op);
Expression body, IGMap<T> gMap, ValueExpressionSyntax value, ISyntaxNode op);

protected abstract TQuery BuildAlwaysTrueQuery();

Expand All @@ -31,7 31,7 @@ internal TQuery Build(ExpressionSyntax expression)
Expression body,
ParameterExpression parameter,
object? value,
SyntaxNode op,
ISyntaxNode op,
ValueExpressionSyntax valueExpression);

protected abstract TQuery CombineWithAndOperator(TQuery left, TQuery right);
Expand Down Expand Up @@ -157,7 157,7 @@ private static object AddIndexerNullCheck(IGMap<T> gMap, object query)
Expression body,
ParameterExpression parameter,
ValueExpressionSyntax valueExpression,
SyntaxNode op,
ISyntaxNode op,
Func<string, object>? convertor)
{
// Remove the boxing for value types
Expand Down
8 changes: 4 additions & 4 deletions src/Gridify/QueryBuilders/LinqQueryBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 10,7 @@ namespace Gridify.QueryBuilders;
internal class LinqQueryBuilder<T>(IGridifyMapper<T> mapper) : BaseQueryBuilder<Expression<Func<T, bool>>, T>(mapper)
{
protected override Expression<Func<T, bool>>? BuildNestedQuery(
Expression body, IGMap<T> gMap, ValueExpressionSyntax value, SyntaxNode op)
Expression body, IGMap<T> gMap, ValueExpressionSyntax value, ISyntaxNode op)
{
while (true)
switch (body)
Expand Down Expand Up @@ -105,7 105,7 @@ protected override Expression<Func<T, bool>> BuildAlwaysFalseQuery(ParameterExpr
Expression body,
ParameterExpression parameter,
object? value,
SyntaxNode op,
ISyntaxNode op,
ValueExpressionSyntax valueExpression)
{
Expression be;
Expand Down Expand Up @@ -229,7 229,7 @@ protected override Expression<Func<T, bool>> BuildAlwaysFalseQuery(ParameterExpr

break;
case SyntaxKind.CustomOperator:
var token = op as SyntaxToken;
var token = (SyntaxToken)op;
var customOperator = GridifyGlobalConfiguration.CustomOperators.Operators.First(q => q.GetOperator() == token!.Text);
var customExp = customOperator.OperatorHandler();

Expand Down Expand Up @@ -257,7 257,7 @@ protected override Expression<Func<T, bool>> CombineWithOrOperator(Expression<Fu
return left.Or(right);
}

private Expression<Func<T, bool>>? GenerateNestedExpression(Expression body, IGMap<T> gMap, ValueExpressionSyntax value, SyntaxNode op)
private Expression<Func<T, bool>>? GenerateNestedExpression(Expression body, IGMap<T> gMap, ValueExpressionSyntax value, ISyntaxNode op)
{
while (true)
switch (body)
Expand Down
6 changes: 3 additions & 3 deletions src/Gridify/Syntax/BinaryExpressionSyntax.cs
Original file line number Diff line number Diff line change
@@ -1,4 1,4 @@
using System.Collections.Generic;
using System.Collections.Generic;

namespace Gridify.Syntax;

Expand All @@ -17,10 17,10 @@ public BinaryExpressionSyntax(ExpressionSyntax left, SyntaxToken operatorToken,

public override SyntaxKind Kind => SyntaxKind.BinaryExpression;

public override IEnumerable<SyntaxNode> GetChildren()
public override IEnumerable<ISyntaxNode> GetChildren()
{
yield return Left;
yield return OperatorToken;
yield return Right;
}
}
}
10 changes: 7 additions & 3 deletions src/Gridify/Syntax/ExpressionSyntax.cs
Original file line number Diff line number Diff line change
@@ -1,5 1,9 @@
namespace Gridify.Syntax;
using System.Collections.Generic;

public abstract class ExpressionSyntax : SyntaxNode
namespace Gridify.Syntax;

public abstract class ExpressionSyntax : ISyntaxNode
{
}
public abstract SyntaxKind Kind { get; }
public abstract IEnumerable<ISyntaxNode> GetChildren();
}
2 changes: 1 addition & 1 deletion src/Gridify/Syntax/FieldExpressionSyntax.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 12,7 @@ internal FieldExpressionSyntax(SyntaxToken fieldToken, string? indexer = default

public override SyntaxKind Kind => SyntaxKind.FieldExpression;

public override IEnumerable<SyntaxNode> GetChildren()
public override IEnumerable<ISyntaxNode> GetChildren()
{
yield return FieldToken;
}
Expand Down
9 changes: 9 additions & 0 deletions src/Gridify/Syntax/ISyntaxNode.cs
Original file line number Diff line number Diff line change
@@ -0,0 1,9 @@
using System.Collections.Generic;

namespace Gridify.Syntax;

public interface ISyntaxNode
{
public SyntaxKind Kind { get; }
public IEnumerable<ISyntaxNode> GetChildren();
}
4 changes: 2 additions & 2 deletions src/Gridify/Syntax/Lexer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 192,7 @@ private bool TryParseIndexer(char peek, out SyntaxToken nextToken)
return false;
}

private bool TryToReadTheValue(out SyntaxToken? valueToken)
private bool TryToReadTheValue(out SyntaxToken valueToken)
{
if (_waitingForValue)
{
Expand Down Expand Up @@ -249,7 249,7 @@ private bool TryToReadTheValue(out SyntaxToken? valueToken)
}
}

valueToken = null;
valueToken = new SyntaxToken();
return false;
}

Expand Down
10 changes: 5 additions & 5 deletions src/Gridify/Syntax/ParenthesizedExpressionSyntax.cs
Original file line number Diff line number Diff line change
@@ -1,4 1,4 @@
using System.Collections.Generic;
using System.Collections.Generic;

namespace Gridify.Syntax;

Expand All @@ -7,18 7,18 @@ internal sealed class ParenthesizedExpressionSyntax : ExpressionSyntax
public SyntaxToken OpenParenthesisToken { get; }
public ExpressionSyntax Expression { get; }
public SyntaxToken CloseParenthesisToken { get; }
public override SyntaxKind Kind => SyntaxKind.ParenthesizedExpression;
public override SyntaxKind Kind => SyntaxKind.ParenthesizedExpression;

public ParenthesizedExpressionSyntax(SyntaxToken openParenthesisToken,ExpressionSyntax expression,SyntaxToken closeParenthesisToken)
public ParenthesizedExpressionSyntax(SyntaxToken openParenthesisToken, ExpressionSyntax expression, SyntaxToken closeParenthesisToken)
{
OpenParenthesisToken = openParenthesisToken;
Expression = expression;
CloseParenthesisToken = closeParenthesisToken;
}
public override IEnumerable<SyntaxNode> GetChildren()
public override IEnumerable<ISyntaxNode> GetChildren()
{
yield return OpenParenthesisToken;
yield return Expression;
yield return CloseParenthesisToken;
}
}
}
31 changes: 20 additions & 11 deletions src/Gridify/Syntax/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 3,10 @@

namespace Gridify.Syntax;

internal class Parser
internal struct Parser
{
private readonly List<string> _diagnostics = new();
private readonly SyntaxToken[] _tokens;
private List<string>? _diagnostics = null;
private readonly List<SyntaxToken> _tokens = [];
private int _position;

private static bool IsOperator(SyntaxKind kind)
Expand All @@ -28,32 28,31 @@ SyntaxKind.NotEndsWith or

public Parser(string text, IEnumerable<IGridifyOperator> customOperators)
{
var tokens = new List<SyntaxToken>();
var lexer = new Lexer(text, customOperators);
SyntaxToken token;
do
{
token = lexer.NextToken();
if (token.Kind != SyntaxKind.WhiteSpace) tokens.Add(token);
if (token.Kind != SyntaxKind.WhiteSpace) _tokens.Add(token);
} while (token.Kind != SyntaxKind.End);

_tokens = tokens.ToArray();
_diagnostics.AddRange(lexer.Diagnostics);
if (lexer.Diagnostics.Any())
AddDiagnostics(lexer.Diagnostics);
}

private SyntaxToken Current => Peek(0);

private SyntaxToken Peek(int offset)
{
var index = _position offset;
return index >= _tokens.Length ? _tokens[_tokens.Length - 1] : _tokens[index];
return index >= _tokens.Count ? _tokens[_tokens.Count - 1] : _tokens[index];
}

public SyntaxTree Parse()
{
var expression = ParseTerm();
var end = Match(SyntaxKind.End, GetExpectation(expression.Kind));
return new SyntaxTree(_diagnostics, expression, end);
return new SyntaxTree(_diagnostics ?? Enumerable.Empty<string>(), expression, end);
}

private SyntaxKind GetExpectation(SyntaxKind kind)
Expand Down Expand Up @@ -141,8 140,8 @@ private SyntaxToken Match(SyntaxKind kind, SyntaxKind? expectation = null)

expectation ??= kind;

if (!_diagnostics.Any(q => q.StartsWith("Unexpected token")))
_diagnostics.Add($"Unexpected token <{Current.Kind}> at index {Current.Position}, expected <{expectation}>");
if (_diagnostics != null && !_diagnostics.Any(q => q.StartsWith("Unexpected token")))
AddDiagnostics($"Unexpected token <{Current.Kind}> at index {Current.Position}, expected <{expectation}>");

return new SyntaxToken(kind, Current.Position, Current.Text);
}
Expand All @@ -165,4 164,14 @@ private ExpressionSyntax ParseFieldExpression()
? new FieldExpressionSyntax(fieldToken, fieldIndexer.Text)
: new FieldExpressionSyntax(fieldToken);
}
private void AddDiagnostics(string message)
{
_diagnostics ??= [];
_diagnostics.Add(message);
}
private void AddDiagnostics(IEnumerable<string> messages)
{
_diagnostics ??= [];
_diagnostics.AddRange(messages);
}
}
9 changes: 0 additions & 9 deletions src/Gridify/Syntax/SyntaxNode.cs

This file was deleted.

22 changes: 7 additions & 15 deletions src/Gridify/Syntax/SyntaxToken.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 3,18 @@

namespace Gridify.Syntax;

public class SyntaxToken : SyntaxNode
public struct SyntaxToken(SyntaxKind kind, int position, string text) : ISyntaxNode
{
public override SyntaxKind Kind { get; }
public int Position { get; }
public string Text { get; }
public int Position { get; } = position;
public string Text { get; } = text;
public SyntaxKind Kind { get; } = kind;

public override IEnumerable<SyntaxNode> GetChildren()
public IEnumerable<ISyntaxNode> GetChildren()
{
return Enumerable.Empty<SyntaxNode>();
return Enumerable.Empty<ISyntaxNode>();
}

public SyntaxToken(SyntaxKind kind, int position, string text)
public SyntaxToken() : this(SyntaxKind.End, 0, string.Empty)
{
Kind = kind;
Position = position;
Text = text;
}

public SyntaxToken()
{
Text = string.Empty;
}
}
8 changes: 4 additions & 4 deletions src/Gridify/Syntax/ValueExpressionSyntax.cs
Original file line number Diff line number Diff line change
@@ -1,10 1,10 @@
using System.Collections.Generic;
using System.Collections.Generic;

namespace Gridify.Syntax;

internal sealed class ValueExpressionSyntax : ExpressionSyntax
{
public ValueExpressionSyntax(SyntaxToken valueToken,bool isCaseInsensitive, bool isNullOrDefault)
public ValueExpressionSyntax(SyntaxToken valueToken, bool isCaseInsensitive, bool isNullOrDefault)
{
ValueToken = valueToken;
IsCaseInsensitive = isCaseInsensitive;
Expand All @@ -13,12 13,12 @@ public ValueExpressionSyntax(SyntaxToken valueToken,bool isCaseInsensitive, bool

public override SyntaxKind Kind => SyntaxKind.ValueExpression;

public override IEnumerable<SyntaxNode> GetChildren()
public override IEnumerable<ISyntaxNode> GetChildren()
{
yield return ValueToken;
}

public SyntaxToken ValueToken { get; }
public bool IsCaseInsensitive { get; }
public bool IsNullOrDefault { get; }
}
}

0 comments on commit 82e55da

Please sign in to comment.