private ExpressionCompilationResult Compile() { _operations = null; var result = new ExpressionCompilationResult { Success = true }; if (!string.IsNullOrEmpty(_statement)) { try { var tokens = ExpressionLexer.Tokenize(_statement); _operations = ExpressionParser.Parse(tokens); } catch (ExpressionTokenizeException exception) { result.Success = false; result.Message = string.Format(_expressionTokenizeError, _statement, exception.Location, exception.Message); } catch (ExpressionParseException exception) { result.Success = false; result.Message = string.Format(_expressionParseError, _statement, exception.Location, exception.Message); } } return(result); }
private static List <VariableToken> Parse(string variable) { var parsed = new List <VariableToken>(); var tokens = ExpressionLexer.Tokenize(variable); var state = ExpressionTokenType.Identifier; foreach (var token in tokens) { var text = variable.Substring(token.Start, token.End - token.Start); if (!CheckState(state, token)) { return(new List <VariableToken>()); } switch (token.Type) { case ExpressionTokenType.Identifier: { parsed.Add(new VariableToken { Type = state == ExpressionTokenType.Command ? VariableTokenType.Type : VariableTokenType.Name, Text = text }); state = ExpressionTokenType.Operator; break; } case ExpressionTokenType.Int: { parsed.Add(new VariableToken { Type = VariableTokenType.Number, Text = text }); state = ExpressionTokenType.EndLookup; break; } case ExpressionTokenType.Operator: { if (text == Cast) { state = ExpressionTokenType.Command; } else if (text.Length == 1 && text[0] == Separator) { state = ExpressionTokenType.Identifier; } else { return(new List <VariableToken>()); } break; } case ExpressionTokenType.StartLookup: { state = ExpressionTokenType.Int; break; } case ExpressionTokenType.EndLookup: { state = ExpressionTokenType.Operator; break; } default: { return(new List <VariableToken>()); } } } return(state == ExpressionTokenType.Operator ? parsed : new List <VariableToken>()); }