static ExpressionToken() { ExpressionToken token = new ExpressionToken { Text = "gt", Kind = ExpressionTokenKind.Identifier, Position = 0 }; GreaterThan = token; ExpressionToken token2 = new ExpressionToken { Text = "eq", Kind = ExpressionTokenKind.Identifier, Position = 0 }; EqualsTo = token2; ExpressionToken token3 = new ExpressionToken { Text = "lt", Kind = ExpressionTokenKind.Identifier, Position = 0 }; LessThan = token3; }
internal bool TryPeekNextToken(out ExpressionToken resultToken, out Exception error) { int textPos = this.textPos; char ch = this.ch; ExpressionToken token = this.token; resultToken = this.NextTokenImplementation(out error); this.textPos = textPos; this.ch = ch; this.token = token; return (error == null); }
/// <summary>Attempts to parse key values from the specified text.</summary> /// <param name='text'>Text to parse (not null).</param> /// <param name="allowNamedValues">Set to true if the parser should accept named values /// so syntax like Name='value'. If this is false, the parsing will fail on such constructs.</param> /// <param name="allowNull">Set to true if the parser should accept null values. /// If set to false, the parser will fail on null values.</param> /// <param name='instance'>After invocation, the parsed key instance.</param> /// <returns> /// true if the key instance was parsed; false if there was a /// syntactic error. /// </returns> /// <remarks> /// The returned instance contains only string values. To get typed values, a call to /// TryConvertValues is necessary. /// </remarks> private static bool TryParseFromUri(string text, bool allowNamedValues, bool allowNull, out SegmentArgumentParser instance) { Debug.Assert(text != null, "text != null"); Dictionary <string, string> namedValues = null; List <string> positionalValues = null; ExpressionLexer lexer = new ExpressionLexer(text, true, false); ExpressionToken currentToken = lexer.CurrentToken; if (currentToken.Kind == ExpressionTokenKind.End) { instance = Empty; return(true); } instance = null; do { if (currentToken.Kind == ExpressionTokenKind.Identifier && allowNamedValues) { // Name-value pair. if (positionalValues != null) { // We cannot mix named and non-named values. return(false); } string identifier = lexer.CurrentToken.GetIdentifier(); lexer.NextToken(); if (lexer.CurrentToken.Kind != ExpressionTokenKind.Equal) { return(false); } lexer.NextToken(); if (!lexer.CurrentToken.IsKeyValueToken) { return(false); } string namedValue = lexer.CurrentToken.Text; CreateIfNull(ref namedValues); if (namedValues.ContainsKey(identifier)) { // Duplicate name. return(false); } namedValues.Add(identifier, namedValue); } else if (currentToken.IsKeyValueToken || (allowNull && currentToken.Kind == ExpressionTokenKind.NullLiteral)) { // Positional value. if (namedValues != null) { // We cannot mix named and non-named values. return(false); } CreateIfNull(ref positionalValues); positionalValues.Add(lexer.CurrentToken.Text); } else { return(false); } // Read the next token. We should be at the end, or find // we have a comma followed by something. lexer.NextToken(); currentToken = lexer.CurrentToken; if (currentToken.Kind == ExpressionTokenKind.Comma) { lexer.NextToken(); currentToken = lexer.CurrentToken; if (currentToken.Kind == ExpressionTokenKind.End) { // Trailing comma. return(false); } } }while (currentToken.Kind != ExpressionTokenKind.End); instance = new SegmentArgumentParser(namedValues, positionalValues, false); return(true); }
/// <summary> /// Determines if the next token can be processed without error without advancing the token. /// </summary> /// <param name="resultToken">The next ExpressionToken. This value is undefined if error is defined.</param> /// <param name="error">Exception generated from trying to process the next token.</param> /// <returns>True if the next token can be processed, false otherwise.</returns> internal bool TryPeekNextToken(out ExpressionToken resultToken, out Exception error) { DebugUtils.CheckNoExternalCallers(); int savedTextPos = this.textPos; char savedChar = this.ch; ExpressionToken savedToken = this.token; resultToken = this.NextTokenImplementation(out error); this.textPos = savedTextPos; this.ch = savedChar; this.token = savedToken; return error == null; }
/// <summary>Attempts to parse key values from the specified text.</summary> /// <param name='text'>Text to parse (not null).</param> /// <returns> /// Enumeration of key values or null if there was a syntax error. /// </returns> /// <remarks> /// The returned instance contains only string values. /// </remarks> private static IEnumerable <NamedValue> ParseKeyValuesFromUri(string text) { Debug.Assert(text != null, "text != null"); //// This is a modified copy of KeyInstance.TryParseFromUri ExpressionLexer lexer = new ExpressionLexer(text, true /*moveToFirstToken*/); ExpressionToken currentToken = lexer.CurrentToken; if (currentToken.Kind == ExpressionTokenKind.End) { return(EmptyKeyValues); } List <NamedValue> keyValuesList = new List <NamedValue>(); do { if (currentToken.Kind == ExpressionTokenKind.Identifier) { // Name-value pair. string identifier = lexer.CurrentToken.GetIdentifier(); lexer.NextToken(); if (lexer.CurrentToken.Kind != ExpressionTokenKind.Equal) { return(null); } lexer.NextToken(); if (!lexer.CurrentToken.IsKeyValueToken) { return(null); } keyValuesList.Add(new NamedValue(identifier, ParseKeyValueLiteral(lexer))); } else if (currentToken.IsKeyValueToken) { // Unnamed value. keyValuesList.Add(new NamedValue(null, ParseKeyValueLiteral(lexer))); } else { return(null); } // Read the next token. We should be at the end, or find // we have a comma followed by something. currentToken = lexer.NextToken(); if (currentToken.Kind == ExpressionTokenKind.Comma) { currentToken = lexer.NextToken(); if (currentToken.Kind == ExpressionTokenKind.End) { // Trailing comma. return(null); } } }while (currentToken.Kind != ExpressionTokenKind.End); return(keyValuesList); }