44
55namespace 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
0 commit comments