Skip to content

Commit 886187a

Browse files
authored
Make TextParser public (#562)
1 parent 15f6e19 commit 886187a

4 files changed

Lines changed: 63 additions & 34 deletions

File tree

src/System.Linq.Dynamic.Core/Parser/ExpressionParser.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ Expression ParseOrOperator()
281281
Expression ParseAndOperator()
282282
{
283283
Expression left = ParseIn();
284-
while (_textParser.CurrentToken.Id == TokenId.DoubleAmphersand)
284+
while (_textParser.CurrentToken.Id == TokenId.DoubleAmpersand)
285285
{
286286
Token op = _textParser.CurrentToken;
287287
_textParser.NextToken();
@@ -379,7 +379,7 @@ Expression ParseIn()
379379
Expression ParseLogicalAndOrOperator()
380380
{
381381
Expression left = ParseComparisonOperator();
382-
while (_textParser.CurrentToken.Id == TokenId.Amphersand || _textParser.CurrentToken.Id == TokenId.Bar)
382+
while (_textParser.CurrentToken.Id == TokenId.Ampersand || _textParser.CurrentToken.Id == TokenId.Bar)
383383
{
384384
Token op = _textParser.CurrentToken;
385385
_textParser.NextToken();
@@ -397,7 +397,7 @@ Expression ParseLogicalAndOrOperator()
397397

398398
switch (op.Id)
399399
{
400-
case TokenId.Amphersand:
400+
case TokenId.Ampersand:
401401
int parseValue;
402402
if (left.Type == typeof(string) && left.NodeType == ExpressionType.Constant && int.TryParse((string)((ConstantExpression)left).Value, out parseValue) && TypeHelper.IsNumericType(right.Type))
403403
{

src/System.Linq.Dynamic.Core/Tokenizer/TextParser.cs

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44

55
namespace System.Linq.Dynamic.Core.Tokenizer
66
{
7-
internal class TextParser
7+
/// <summary>
8+
/// TextParser which can be used to parse a text into tokens.
9+
/// </summary>
10+
public class TextParser
811
{
912
private const char DefaultNumberDecimalSeparator = '.';
10-
11-
private static readonly char[] EscapeCharacters = new[] { '\\', 'a', 'b', 'f', 'n', 'r', 't', 'v' };
13+
private static readonly char[] EscapeCharacters = { '\\', 'a', 'b', 'f', 'n', 'r', 't', 'v' };
1214

1315
// These aliases are supposed to simply the where clause and make it more human readable
1416
private static readonly Dictionary<string, TokenId> PredefinedOperatorAliases = new Dictionary<string, TokenId>(StringComparer.OrdinalIgnoreCase)
@@ -26,8 +28,8 @@ internal class TextParser
2628
{ "GreaterThan", TokenId.GreaterThan },
2729
{ "ge", TokenId.GreaterThanEqual },
2830
{ "GreaterThanEqual", TokenId.GreaterThanEqual },
29-
{ "and", TokenId.DoubleAmphersand },
30-
{ "AndAlso", TokenId.DoubleAmphersand },
31+
{ "and", TokenId.DoubleAmpersand },
32+
{ "AndAlso", TokenId.DoubleAmpersand },
3133
{ "or", TokenId.DoubleBar },
3234
{ "OrElse", TokenId.DoubleBar },
3335
{ "not", TokenId.Exclamation },
@@ -41,8 +43,17 @@ internal class TextParser
4143

4244
private int _textPos;
4345
private char _ch;
46+
47+
/// <summary>
48+
/// The current parsed <see cref="Token"/>.
49+
/// </summary>
4450
public Token CurrentToken;
4551

52+
/// <summary>
53+
/// Constructor for TextParser
54+
/// </summary>
55+
/// <param name="config"></param>
56+
/// <param name="text"></param>
4657
public TextParser(ParsingConfig config, string text)
4758
{
4859
_config = config;
@@ -70,16 +81,18 @@ private void NextChar()
7081
_ch = _textPos < _textLen ? _text[_textPos] : '\0';
7182
}
7283

84+
/// <summary>
85+
/// Peek the next character.
86+
/// </summary>
87+
/// <returns>The next character, or \0 if end of string.</returns>
7388
public char PeekNextChar()
7489
{
75-
if (_textPos + 1 < _textLen)
76-
{
77-
return _text[_textPos + 1];
78-
}
79-
80-
return '\0';
90+
return _textPos + 1 < _textLen ? _text[_textPos + 1] : '\0';
8191
}
8292

93+
/// <summary>
94+
/// Go to the next token.
95+
/// </summary>
8396
public void NextToken()
8497
{
8598
while (char.IsWhiteSpace(_ch))
@@ -115,11 +128,11 @@ public void NextToken()
115128
if (_ch == '&')
116129
{
117130
NextChar();
118-
tokenId = TokenId.DoubleAmphersand;
131+
tokenId = TokenId.DoubleAmpersand;
119132
}
120133
else
121134
{
122-
tokenId = TokenId.Amphersand;
135+
tokenId = TokenId.Ampersand;
123136
}
124137
break;
125138

@@ -431,19 +444,16 @@ public void NextToken()
431444
CurrentToken.Id = GetAliasedTokenId(tokenId, CurrentToken.Text);
432445
}
433446

434-
public void ValidateToken(TokenId t, string errorMessage)
435-
{
436-
if (CurrentToken.Id != t)
437-
{
438-
throw ParseError(errorMessage);
439-
}
440-
}
441-
442-
public void ValidateToken(TokenId t)
447+
/// <summary>
448+
/// Check if the current token is the specified <see cref="TokenId"/>.
449+
/// </summary>
450+
/// <param name="tokenId">The tokenId to check.</param>
451+
/// <param name="errorMessage">The (optional) error message.</param>
452+
public void ValidateToken(TokenId tokenId, string errorMessage = null)
443453
{
444-
if (CurrentToken.Id != t)
454+
if (CurrentToken.Id != tokenId)
445455
{
446-
throw ParseError(Res.SyntaxError);
456+
throw ParseError(errorMessage ?? Res.SyntaxError);
447457
}
448458
}
449459

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,28 @@
11
namespace System.Linq.Dynamic.Core.Tokenizer
22
{
3-
internal struct Token
3+
/// <summary>
4+
/// Token
5+
/// </summary>
6+
public struct Token
47
{
8+
/// <summary>
9+
/// The TokenId.
10+
/// </summary>
511
public TokenId Id { get; set; }
612

13+
/// <summary>
14+
/// The Original TokenId.
15+
/// </summary>
716
public TokenId OriginalId { get; set; }
817

18+
/// <summary>
19+
/// The text.
20+
/// </summary>
921
public string Text { get; set; }
1022

23+
/// <summary>
24+
/// The position.
25+
/// </summary>
1126
public int Pos { get; set; }
1227
}
13-
}
28+
}

src/System.Linq.Dynamic.Core/Tokenizer/TokenId.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1-
namespace System.Linq.Dynamic.Core.Tokenizer
1+
#pragma warning disable CS1591
2+
namespace System.Linq.Dynamic.Core.Tokenizer
23
{
3-
internal enum TokenId
4+
/// <summary>
5+
/// TokenId which defines the text which is parsed.
6+
/// </summary>
7+
public enum TokenId
48
{
59
Unknown,
610
End,
@@ -10,7 +14,7 @@ internal enum TokenId
1014
RealLiteral,
1115
Exclamation,
1216
Percent,
13-
Amphersand,
17+
Ampersand,
1418
OpenParen,
1519
CloseParen,
1620
OpenCurlyParen,
@@ -30,7 +34,7 @@ internal enum TokenId
3034
CloseBracket,
3135
Bar,
3236
ExclamationEqual,
33-
DoubleAmphersand,
37+
DoubleAmpersand,
3438
LessThanEqual,
3539
LessGreater,
3640
DoubleEqual,
@@ -42,4 +46,4 @@ internal enum TokenId
4246
Lambda,
4347
NullPropagation
4448
}
45-
}
49+
}

0 commit comments

Comments
 (0)