Skip to content

Commit

Permalink
refactor(lexer): to use null-coalescing operator and add diagnostics …
Browse files Browse the repository at this point in the history
…collection method

Simplify initialization of _diagnostics list
Use null-coalescing operator for initializing _diagnostics
Add new private method AddDiagnostics for managing diagnostics
Improve readability by clearly separating concerns
  • Loading branch information
alirezanet committed Jun 22, 2024
1 parent 2f0e722 commit 106b993
Showing 1 changed file with 14 additions and 15 deletions.
29 changes: 14 additions & 15 deletions src/Gridify/Syntax/Lexer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 5,14 @@

namespace Gridify.Syntax;

internal ref struct Lexer
internal ref struct Lexer(string text, IEnumerable<IGridifyOperator> customOperators)
{
private readonly List<string> _diagnostics = new();
private readonly ReadOnlySpan<char> _text;
private readonly IEnumerable<IGridifyOperator> _customOperators;
private List<string>? _diagnostics = null;
private readonly ReadOnlySpan<char> _text = text.AsSpan();
private int _position;
private bool _waitingForValue;

public Lexer(string text, IEnumerable<IGridifyOperator> customOperators)
{
_text = text.AsSpan();
_customOperators = customOperators;
}

public IEnumerable<string> Diagnostics => _diagnostics;
public IEnumerable<string> Diagnostics => _diagnostics ?? Enumerable.Empty<string>();

private char Current => _position >= _text.Length ? '\0' : _text[_position];

Expand Down Expand Up @@ -116,9 109,9 @@ public SyntaxToken NextToken()
? new SyntaxToken(SyntaxKind.GreaterOrEqualThan, _position = 2, ">=")
: new SyntaxToken(SyntaxKind.GreaterThan, _position , ">");
}
case '#' when _customOperators.Any(): // Custom Operators
case '#' when customOperators.Any(): // Custom Operators
{
foreach (var cOp in _customOperators)
foreach (var cOp in customOperators)
{
var op = cOp.GetOperator();
var opSlice = op.AsSpan();
Expand Down Expand Up @@ -164,7 157,7 @@ public SyntaxToken NextToken()
return new SyntaxToken(SyntaxKind.FieldToken, start, text.ToString());
}

_diagnostics.Add($"bad character input: '{Current.ToString()}', at index {_position.ToString()}");
AddDiagnostics($"bad character input: '{Current.ToString()}', at index {_position.ToString()}");
return new SyntaxToken(SyntaxKind.BadToken, _position , string.Empty);
}

Expand All @@ -189,7 182,7 @@ private bool TryParseIndexer(char peek, out SyntaxToken nextToken)
}
}

_diagnostics.Add($"Indexer is not closed: '{peek.ToString()}' at {_position .ToString()}. expected ']' ");
AddDiagnostics($"Indexer is not closed: '{peek.ToString()}' at {_position .ToString()}. expected ']' ");
{
nextToken = new SyntaxToken(SyntaxKind.BadToken, _position, Current.ToString());
return true;
Expand Down Expand Up @@ -259,4 252,10 @@ private bool TryToReadTheValue(out SyntaxToken? valueToken)
valueToken = null;
return false;
}

private void AddDiagnostics(string message)
{
_diagnostics ??= [];
_diagnostics.Add(message);
}
}

0 comments on commit 106b993

Please sign in to comment.