Skip to content

Commit 1ce55d6

Browse files
committed
Redesigned the tokenizer to use specialized methods for each state condition, greatly simplifying it.
1 parent 852caf3 commit 1ce55d6

4 files changed

Lines changed: 154 additions & 263 deletions

File tree

SqlServerSimulator/Parser/ParserContext.cs

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Collections.Frozen;
1+
using SqlServerSimulator.Parser.Tokens;
2+
using System.Collections.Frozen;
23
using System.Data.Common;
34

45
namespace SqlServerSimulator.Parser;
@@ -35,10 +36,29 @@ public ParserContext(SimulatedDbCommand command)
3536
#if DEBUG
3637
this.tokens = new TokenArrayEnumerator(command.CommandText);
3738
#else
38-
this.tokens = Tokenizer.Tokenize(command.CommandText).GetEnumerator();
39+
this.tokens = TokenizerIterator(command.CommandText).GetEnumerator();
3940
#endif
4041
}
4142

43+
/// <summary>
44+
/// Converts the redesigned tokenizer to a traditional enumerable for compatibility with the existing logic.
45+
/// This should be phased out eventually.
46+
/// </summary>
47+
static IEnumerable<Token> TokenizerIterator(string? commandText)
48+
{
49+
if (string.IsNullOrEmpty(commandText))
50+
throw new InvalidOperationException("ExecuteReader: CommandText property has not been initialized");
51+
52+
var index = -1;
53+
while (Tokenizer.NextToken(commandText, ref index) is Token token)
54+
{
55+
if (token is Whitespace)
56+
continue;
57+
58+
yield return token;
59+
}
60+
}
61+
4262
public Simulation Simulation => Command.simulation;
4363

4464
/// <summary>
@@ -167,10 +187,10 @@ public void Dispose()
167187
private sealed class TokenArrayEnumerator(string? command) : IEnumerator<Token>
168188
{
169189
/// <summary>
170-
/// Retains the full results of <see cref="Tokenizer.Tokenize(string?)"/>.
190+
/// Retains the full results of tokenization.
171191
/// This is less efficient than streaming the results, but enables this class's debugging-friendly <see cref="ToString"/>.
172192
/// </summary>
173-
private readonly Token[] source = [.. Tokenizer.Tokenize(command)];
193+
private readonly Token[] source = [.. TokenizerIterator(command)];
174194

175195
public int Index { get; private set; } = -1;
176196

0 commit comments

Comments
 (0)