/// <summary> /// Checks syntax errors on input expression, /// as well as performing disambiguation for identifiers. /// </summary> /// <param name="tokenList"> /// A list of tokenized input. /// </param> private void CheckSyntaxError(List <Token> tokenList) { // Initialize, assuming preceded by OR TokenKind previous = TokenKind.Or; for (int i = 0; i < tokenList.Count; i++) { Token token = tokenList[i]; // Not allowed: ... AND/OR AND/OR ... // Allowed: ... AND/OR NOT/ID ... if (previous == TokenKind.Or || previous == TokenKind.And) { if ((token.Kind == TokenKind.Or) || (token.Kind == TokenKind.And)) { throw InterpreterError.NewInterpreterException(null, typeof(RuntimeException), null, "SyntaxErrorUnexpectedBinaryOperator", EnumExpressionEvaluatorStrings.SyntaxErrorUnexpectedBinaryOperator); } } // Not allowed: ... NOT AND/OR/NOT ... // Allowed: ... NOT ID ... else if (previous == TokenKind.Not) { if (token.Kind != TokenKind.Identifier) { throw InterpreterError.NewInterpreterException(null, typeof(RuntimeException), null, "SyntaxErrorIdentifierExpected", EnumExpressionEvaluatorStrings.SyntaxErrorIdentifierExpected); } } // Not allowed: ... ID NOT/ID ... // Allowed: ... ID AND/OR ... else if (previous == TokenKind.Identifier) { if ((token.Kind == TokenKind.Identifier) || (token.Kind == TokenKind.Not)) { throw InterpreterError.NewInterpreterException(null, typeof(RuntimeException), null, "SyntaxErrorBinaryOperatorExpected", EnumExpressionEvaluatorStrings.SyntaxErrorBinaryOperatorExpected); } } if (token.Kind == TokenKind.Identifier) { string text = token.Text; token.Text = EnumMinimumDisambiguation.EnumDisambiguate(text, typeof(T)); } previous = token.Kind; } }
private void CheckSyntaxError(List <Token> tokenList) { TokenKind or = TokenKind.Or; for (int i = 0; i < tokenList.Count; i++) { Token token = tokenList[i]; switch (or) { case TokenKind.Or: case TokenKind.And: if ((token.Kind == TokenKind.Or) || (token.Kind == TokenKind.And)) { throw InterpreterError.NewInterpreterException(null, typeof(RuntimeException), null, "SyntaxErrorUnexpectedBinaryOperator", EnumExpressionEvaluatorStrings.SyntaxErrorUnexpectedBinaryOperator, new object[0]); } break; default: if (or == TokenKind.Not) { if (token.Kind != TokenKind.Identifier) { throw InterpreterError.NewInterpreterException(null, typeof(RuntimeException), null, "SyntaxErrorIdentifierExpected", EnumExpressionEvaluatorStrings.SyntaxErrorIdentifierExpected, new object[0]); } } else if ((or == TokenKind.Identifier) && ((token.Kind == TokenKind.Identifier) || (token.Kind == TokenKind.Not))) { throw InterpreterError.NewInterpreterException(null, typeof(RuntimeException), null, "SyntaxErrorBinaryOperatorExpected", EnumExpressionEvaluatorStrings.SyntaxErrorBinaryOperatorExpected, new object[0]); } break; } if (token.Kind == TokenKind.Identifier) { string text = token.Text; CompareInfo.GetCompareInfo(CultureInfo.InvariantCulture.LCID); token.Text = EnumMinimumDisambiguation.EnumDisambiguate(text, typeof(T)); } or = token.Kind; } }
/// <summary> /// Given the start (offset) of the next token, traverse through /// the string to find the next token, stripping correctly /// enclosed quotes. /// </summary> /// <param name="input"> /// Input string /// </param> /// <param name="_offset"> /// Current offset position for the string parser. /// </param> /// <returns> /// The next token on the input string /// </returns> private Token GetNextToken(string input, ref int _offset) { StringBuilder sb = new StringBuilder(); //bool singleQuoted = false; //bool doubleQuoted = false; bool readingIdentifier = false; while (_offset < input.Length) { char cc = input[_offset++]; if ((cc == ',') || (cc == '+') || (cc == '!')) { if (!readingIdentifier) { sb.Append(cc); } else { _offset--; } break; } else { sb.Append(cc); readingIdentifier = true; } } string result = sb.ToString().Trim(); // If resulting identifier is enclosed in paired quotes, // remove the only the first pair of quotes from the string if (result.Length >= 2 && ((result[0] == '\'' && result[result.Length - 1] == '\'') || (result[0] == '\"' && result[result.Length - 1] == '\"'))) { result = result.Substring(1, result.Length - 2); } result = result.Trim(); // possible empty token because white spaces are enclosed in quotation marks. if (String.IsNullOrWhiteSpace(result)) { throw InterpreterError.NewInterpreterException(input, typeof(RuntimeException), null, "EmptyTokenString", EnumExpressionEvaluatorStrings.EmptyTokenString, EnumMinimumDisambiguation.EnumAllValues(typeof(T))); } else if (result[0] == '(') { int matchIndex = input.IndexOf(')', _offset); if (result[result.Length - 1] == ')' || matchIndex >= 0) { throw InterpreterError.NewInterpreterException(input, typeof(RuntimeException), null, "NoIdentifierGroupingAllowed", EnumExpressionEvaluatorStrings.NoIdentifierGroupingAllowed); } } if (result.Equals(",")) { return(new Token(TokenKind.Or)); } else if (result.Equals("+")) { return(new Token(TokenKind.And)); } else if (result.Equals("!")) { return(new Token(TokenKind.Not)); } else { return(new Token(result)); } }
private Token GetNextToken(string input, ref int _offset) { string str; StringBuilder builder = new StringBuilder(); for (bool flag = false; _offset < input.Length; flag = true) { char ch = input[_offset++]; switch (ch) { case ',': case '+': case '!': if (!flag) { builder.Append(ch); } else { _offset--; } goto Label_0054; } builder.Append(ch); } Label_0054: str = builder.ToString().Trim(); if ((str.Length >= 2) && (((str[0] == '\'') && (str[str.Length - 1] == '\'')) || ((str[0] == '"') && (str[str.Length - 1] == '"')))) { str = str.Substring(1, str.Length - 2); } str = str.Trim(); if (string.IsNullOrWhiteSpace(str)) { throw InterpreterError.NewInterpreterException(input, typeof(RuntimeException), null, "EmptyTokenString", EnumExpressionEvaluatorStrings.EmptyTokenString, new object[] { EnumMinimumDisambiguation.EnumAllValues(typeof(T)) }); } if (str[0] == '(') { int index = input.IndexOf(')', _offset); if ((str[str.Length - 1] == ')') || (index >= 0)) { throw InterpreterError.NewInterpreterException(input, typeof(RuntimeException), null, "NoIdentifierGroupingAllowed", EnumExpressionEvaluatorStrings.NoIdentifierGroupingAllowed, new object[0]); } } if (str.Equals(",")) { return(new Token(TokenKind.Or)); } if (str.Equals("+")) { return(new Token(TokenKind.And)); } if (str.Equals("!")) { return(new Token(TokenKind.Not)); } return(new Token(str)); }