Skip to content

Commit 85aa1f2

Browse files
committed
Consolidated single-character tokens into the new Operator token.
1 parent c966c21 commit 85aa1f2

14 files changed

Lines changed: 42 additions & 69 deletions

SqlServerSimulator/Parser/Expression.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public static Expression Parse(ParserContext context)
6868
case Name name:
6969
expression = new Reference(name);
7070
break;
71-
case Plus:
71+
case Operator { Character: '+' }:
7272
if (expression is null)
7373
{
7474
context.MoveNextRequired();
@@ -87,7 +87,7 @@ public static Expression Parse(ParserContext context)
8787

8888
tokenWasRead = true;
8989
break;
90-
case Minus:
90+
case Operator { Character: '-' }:
9191
if (expression is null)
9292
{
9393
context.MoveNextRequired();
@@ -108,7 +108,7 @@ public static Expression Parse(ParserContext context)
108108
tokenWasRead = true;
109109
break;
110110

111-
case Period:
111+
case Operator { Character: '.' }:
112112
if (expression is null)
113113
throw new NotSupportedException("Simulated expression parser doesn't know how to handle '.' at the start of an expression.");
114114

@@ -119,12 +119,12 @@ public static Expression Parse(ParserContext context)
119119
reference.AddMultiPartComponent(context.GetNextRequired<Name>());
120120
}
121121
break;
122-
case Comma:
123-
case CloseParentheses:
122+
case Operator { Character: ',' }:
123+
case Operator { Character: ')' }:
124124
if (expression is null)
125125
throw SimulatedSqlException.SyntaxErrorNear(context.Token);
126126
return expression;
127-
case OpenParentheses:
127+
case Operator { Character: '(' }:
128128
{
129129
if (expression is not Reference reference)
130130
throw SimulatedSqlException.SyntaxErrorNear(context.Token);

SqlServerSimulator/Parser/ParserContext.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,9 @@ private bool MoveNext()
128128
if (token is Whitespace)
129129
continue;
130130

131+
#if DEBUG
132+
tokens.Add(token);
133+
#endif
131134
this.Token = token;
132135
return true;
133136
}
@@ -137,6 +140,11 @@ private bool MoveNext()
137140
}
138141

139142
#if DEBUG
143+
/// <summary>
144+
/// Contains all the non-whitespace tokens that have been read so far.
145+
/// </summary>
146+
private readonly List<Token> tokens = [];
147+
140148
/// <summary>
141149
/// Returns a string representation of the tokenized command.
142150
/// The <see cref="Token"/> token is wrapped by '»' and '«'.

SqlServerSimulator/Parser/Selection.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,14 @@ public static Selection Parse(ParserContext context, uint depth)
2929
{
3030
// SQL Server doesn't require outer parentheses.
3131
// When Expression.Parse supports them, the checks for them here should be removed.
32-
context.MoveNextRequired<OpenParentheses>();
32+
if (context.GetNextRequired<Operator>() is not { Character: '(' })
33+
throw SimulatedSqlException.SyntaxErrorNear(context.Token);
3334
context.MoveNextRequired();
3435

3536
var resolvedExpression = Expression.Parse(context).Run(name => throw SimulatedSqlException.ColumnReferenceNotAllowed(name));
3637
topCount = resolvedExpression is int unboxed ? unboxed : throw SimulatedSqlException.TopFetchRequiresInteger();
3738

38-
if (context.Token is not null and not CloseParentheses)
39+
if (context.Token is not null and not Operator { Character: ')' })
3940
throw SimulatedSqlException.SyntaxErrorNear(context.Token);
4041

4142
context.MoveNextRequired();
@@ -57,10 +58,10 @@ public static Selection Parse(ParserContext context, uint depth)
5758

5859
switch (context.Token)
5960
{
60-
case Comma:
61+
case Operator { Character: ',' }:
6162
continue;
6263

63-
case CloseParentheses:
64+
case Operator { Character: ')' }:
6465
if (depth == 0)
6566
throw SimulatedSqlException.SyntaxErrorNear(context.Token);
6667

@@ -104,7 +105,7 @@ public static Selection Parse(ParserContext context, uint depth)
104105
}))]))
105106
));
106107

107-
case OpenParentheses:
108+
case Operator { Character: '(' }:
108109
if (context.GetNextRequired() is not ReservedKeyword { Keyword: Keyword.Select })
109110
throw SimulatedSqlException.SyntaxErrorNear(context.Token);
110111

SqlServerSimulator/Parser/Tokenizer.cs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,7 @@ static class Tokenizer
2424
'@' => ParseAtOrDoubleAtPrefixedString(command, ref index),
2525
'-' => ParseMinusOrComment(command, ref index),
2626
'[' => ParseBracketDelimitedString(command, ref index),
27-
'+' => new Plus(command, index),
28-
'*' => new Asterisk(command, index),
29-
'(' => new OpenParentheses(command, index),
30-
')' => new CloseParentheses(command, index),
31-
',' => new Comma(command, index),
32-
'.' => new Period(command, index),
33-
';' => new StatementTerminator(command, index),
27+
'+' or '*' or '(' or ')' or ',' or '.' or ';' => new Operator(command, index),
3428
var c => throw SimulatedSqlException.SyntaxErrorNear(c) // Might throw on valid-but-unsupported syntax.
3529
};
3630

@@ -120,11 +114,11 @@ private static Token ParseMinusOrComment(string command, ref int index)
120114
{
121115
var start = index;
122116
if (++index == command.Length)
123-
return new Minus(command, --index);
117+
return new Operator(command, --index);
124118
if (command[index] != '-')
125119
{
126120
index--;
127-
return new Minus(command, index);
121+
return new Operator(command, index);
128122
}
129123

130124
while (++index < command.Length)

SqlServerSimulator/Parser/Tokens/Asterisk.cs

Lines changed: 0 additions & 5 deletions
This file was deleted.

SqlServerSimulator/Parser/Tokens/CloseParentheses.cs

Lines changed: 0 additions & 5 deletions
This file was deleted.

SqlServerSimulator/Parser/Tokens/Comma.cs

Lines changed: 0 additions & 5 deletions
This file was deleted.

SqlServerSimulator/Parser/Tokens/Minus.cs

Lines changed: 0 additions & 5 deletions
This file was deleted.

SqlServerSimulator/Parser/Tokens/OpenParentheses.cs

Lines changed: 0 additions & 5 deletions
This file was deleted.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace SqlServerSimulator.Parser.Tokens;
2+
3+
/// <summary>
4+
/// Represents any of the various single-character operators.
5+
/// </summary>
6+
/// <remarks>Multi-character operators use <see cref="ReservedKeyword"/>.</remarks>
7+
internal sealed class Operator(string command, int index) : Token(command, index, 1)
8+
{
9+
public char Character => base.Source[0];
10+
}

0 commit comments

Comments
 (0)