/// <summary>Parse the passed string into a condition and resolve against the saved GameData.</summary> /// <param name="script">The condition script to parse.</param> /// <returns> /// True if the script resolves to true with the current data, else false. /// Null or empty scripts return true as there is no condition. /// </returns> public bool Evaluate(string script) { if (string.IsNullOrWhiteSpace(script)) { return(true); } var tokens = ConditionTokenizer.Tokenize(script); var ast = new ConditionTree(tokens); var value = Parse(ast.root); return(value); }
public void ConditionTokenizer_ParseCondition() { string condition = "(1==2) OR (3<2 OR 1>=1)"; ConditionTokenizer tokenizer = new ConditionTokenizer(new StringBuffer(condition)); ConditionToken[] tokens = tokenizer.Tokenize(); Assert.AreEqual(15, tokens.Length); Assert.AreEqual(ConditionTokenType.OpeningParenthesis, tokens[0].TokenType); Assert.AreEqual(ConditionTokenType.Number, tokens[1].TokenType); Assert.AreEqual(ConditionTokenType.EqualTo, tokens[2].TokenType); Assert.AreEqual(ConditionTokenType.Number, tokens[3].TokenType); Assert.AreEqual(ConditionTokenType.ClosingParenthesis, tokens[4].TokenType); Assert.AreEqual(ConditionTokenType.Or, tokens[5].TokenType); Assert.AreEqual(ConditionTokenType.OpeningParenthesis, tokens[6].TokenType); Assert.AreEqual(ConditionTokenType.Number, tokens[7].TokenType); Assert.AreEqual(ConditionTokenType.LessThan, tokens[8].TokenType); Assert.AreEqual(ConditionTokenType.Number, tokens[9].TokenType); Assert.AreEqual(ConditionTokenType.Or, tokens[10].TokenType); Assert.AreEqual(ConditionTokenType.Number, tokens[11].TokenType); Assert.AreEqual(ConditionTokenType.GreaterThanOrEqualTo, tokens[12].TokenType); Assert.AreEqual(ConditionTokenType.Number, tokens[13].TokenType); Assert.AreEqual(ConditionTokenType.ClosingParenthesis, tokens[14].TokenType); }
public static TriggerState GetConditionTriggerState( string expression, char typedChar, out int triggerLength, out ExpressionNode triggerExpression, out IReadOnlyList <ExpressionNode> comparandValues ) { triggerLength = 0; triggerExpression = null; comparandValues = null; if (expression.Length == 0 || (expression.Length == 0 && expression[0] == '\'')) { triggerExpression = new ExpressionText(0, "", true); return(TriggerState.Value); } if (expression.Length == 1) { triggerExpression = new ExpressionText(0, expression, true); triggerLength = 1; return(TriggerState.Value); } var tokens = new List <Token> (); var tokenizer = new ConditionTokenizer(); tokenizer.Tokenize(expression); int lastExpressionStart = 0; while (tokenizer.Token.Type != TokenType.EOF) { switch (tokenizer.Token.Type) { case TokenType.And: case TokenType.Or: lastExpressionStart = tokenizer.Token.Position + tokenizer.Token.Value.Length; break; } tokens.Add(tokenizer.Token); tokenizer.GetNextToken(); } int last = tokens.Count - 1; if (last >= 2 && TokenIsCondition(tokens[last - 1].Type)) { var lt = tokens[last]; if (lt.Type == TokenType.Apostrophe || (lt.Type == TokenType.String && (expression[lt.Position + lt.Value.Length] != '\''))) { lastExpressionStart = lt.Position; comparandValues = ReadPrecedingComparandVariables(tokens, last - 2); } else { triggerLength = 0; triggerExpression = null; return(TriggerState.None); } } var subexpr = expression.Substring(lastExpressionStart); return(GetTriggerState(subexpr, typedChar, out triggerLength, out triggerExpression, out _)); }
ConditionParser (string condition) { tokenizer = new ConditionTokenizer (); tokenizer.Tokenize (condition); conditionStr = condition; }
public void ConditionTokenizer_ParseConditionWithUnterminatedStringLiteral() { string condition = "'aa'=='aa"; ConditionTokenizer tokenizer = new ConditionTokenizer(new StringBuffer(condition)); ConditionToken[] tokens = tokenizer.Tokenize(); }
public void ConditionTokenizer_ParseConditionWithInvalidToken() { string condition = "(1==2) OR (3$2 OR 1>=1)"; ConditionTokenizer tokenizer = new ConditionTokenizer(new StringBuffer(condition)); ConditionToken[] tokens = tokenizer.Tokenize(); }