From 26e58d2e5c7f599cd534ca12cfb79fd7e3a5df7b Mon Sep 17 00:00:00 2001 From: John Colvin Date: Fri, 16 Aug 2019 11:35:28 -0500 Subject: [PATCH] added CoreExpressionEvaluator.try_create/validate methods (for pnl rules, and potential use down the road) --- .../Expression/CoreExpressionEvaluator.cs | 38 ++++++++++++----- .../core/Expression/CoreExpressionParser.cs | 42 +++++++++++++------ 2 files changed, 58 insertions(+), 22 deletions(-) diff --git a/src/client/spark/core/Expression/CoreExpressionEvaluator.cs b/src/client/spark/core/Expression/CoreExpressionEvaluator.cs index d0283abc8..66a70832a 100644 --- a/src/client/spark/core/Expression/CoreExpressionEvaluator.cs +++ b/src/client/spark/core/Expression/CoreExpressionEvaluator.cs @@ -1,15 +1,8 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Linq.Expressions; -using System.Text; -using System.Threading.Tasks; -namespace mbts.expression -{ +namespace mbts.expression { // Implements the language used in bts_core/Config - public class CoreExpressionEvaluator : IExpressionEvaluator - { + public class CoreExpressionEvaluator : IExpressionEvaluator { private Func _apply; public CoreExpressionEvaluator(string expression) { @@ -29,10 +22,35 @@ namespace mbts.expression } } + CoreExpressionEvaluator(Func apply, string expression, string parsed_expression) { + _apply = apply; + Expression = expression; + ParsedExpression = parsed_expression; + } + + public static bool try_create(string expression, out CoreExpressionEvaluator result) { + try { + var parameter = System.Linq.Expressions.Expression.Parameter(typeof(T)); + var parser = new CoreExpressionParser(); + if (parser.try_parse(expression, out var ast)) { + var body = ast.create_expression(parameter); + var parsed = body.ToString(); + var lambda = System.Linq.Expressions.Expression.Lambda>(body, parameter); + result = new CoreExpressionEvaluator(lambda.Compile(), expression, parsed); + return true; + } + } catch { } + result = null; + return false; + } + + public static bool validate(string expression) => + try_create(expression, out var _); + public string Expression { get; private set; } public string ParsedExpression { get; private set; } public bool apply(T arg) { return _apply(arg); } } -} +} \ No newline at end of file diff --git a/src/client/spark/core/Expression/CoreExpressionParser.cs b/src/client/spark/core/Expression/CoreExpressionParser.cs index 87868b32b..16ed2447b 100644 --- a/src/client/spark/core/Expression/CoreExpressionParser.cs +++ b/src/client/spark/core/Expression/CoreExpressionParser.cs @@ -4,17 +4,28 @@ using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Text; -using System.Threading.Tasks; -namespace mbts.expression -{ +namespace mbts.expression { // This is a re-implementation of the bts_core Expression parser in c#. // It compiles down to LINQ expression trees which means the code is JITed at runtime for very fast execution - class CoreExpressionParser - { + class CoreExpressionParser { // A cached stringbuilder StringBuilder result = new StringBuilder(); + internal bool try_parse(string str, out Token result) { + if (string.IsNullOrWhiteSpace(str)) { + result = null; + } else { + var l = lex(str); + try { + result = tree(l, false); + } catch { + result = null; + } + } + return result != null; + } + internal Token parse(string str) { if (string.IsNullOrWhiteSpace(str)) return null; var l = lex(str); @@ -158,22 +169,22 @@ namespace mbts.expression } return new Number(result.ToString()); } -#endregion + #endregion #region Recurively convert the Token list to an expression tree - private Token tree(IEnumerable tokens) { + private Token tree(IEnumerable tokens, bool assert_valid = true) { var t = tokens.ToList(); // Handle groups by recursion for (int x = 0; x < t.Count; ++x) { if (t[x] is GroupToken) { - t[x] = tree(((GroupToken)t[x]).Items); + t[x] = tree(((GroupToken)t[x]).Items, assert_valid); } } // This has to be a separate pass for "True == !False" - for (int x = t.Count-1; x >= 0; --x) { + for (int x = t.Count - 1; x >= 0; --x) { if (t[x] is NotOperator) { NotOperator no = (NotOperator)t[x]; no.Child = t[x + 1]; @@ -187,7 +198,11 @@ namespace mbts.expression if (t[x] is CompareOperator) { CompareOperator b = (CompareOperator)t[x]; if (b.Left != null) { - Log.assert(b.Right != null); + if (b.Right == null) { + if (assert_valid) { + Log.assert(false); + } else throw new InvalidOperationException("CompareOperator right side operand is null."); + } continue; } b.Left = t[x - 1]; @@ -213,8 +228,11 @@ namespace mbts.expression x--; } } - - Log.assert(t.Count == 1); + if (t.Count != 1) { + if (assert_valid) { + Log.assert(false); + } else throw new InvalidOperationException("Expression tree was not reduced to a single token."); + } return t.First(); } #endregion