Recursive Expression Tree Parsing With Sprache

huangapple go评论67阅读模式
英文:

Recursive Expression Tree Parsing With Sprache

问题

I've translated the code you provided as requested. Here it is:

public static class FilterParser
{
    internal static Expression<Func<T, bool>> ParseFilter<T>(string input, IQueryKitConfiguration? config = null)
    {
        var parameter = Expression.Parameter(typeof(T), "x");
        Expression expr; 
        try
        {
            expr = ExprParser<T>(parameter, config).End().Parse(input);
        }
        catch (ParseException e)
        {
            throw new ParsingException(e);
        }
        return Expression.Lambda<Func<T, bool>>(expr, parameter);
    }

    private static readonly Parser<string> Identifier =
        from first in Parse.Letter.Once()
        from rest in Parse.LetterOrDigit.XOr(Parse.Char('_')).Many()
        select new string(first.Concat(rest).ToArray());
    
    // Other parser methods...

    private static Parser<Expression?>? CreateLeftExprParser(ParameterExpression parameter, IQueryKitConfiguration? config)
    {
        var leftIdentifierParser = Identifier.DelimitedBy(Parse.Char('.')).Token();

        return leftIdentifierParser?.Select(left =>
        {
            // Implementation of CreateLeftExprParser...
        });
    }

    // Other parser methods...

    private static Expression CreateRightExpr(Expression leftExpr, string right)
    {
        var targetType = leftExpr.Type;

        targetType = TransformTargetTypeIfNullable(targetType);

        if (TypeConversionFunctions.TryGetValue(targetType, out var conversionFunction))
        {
            // Implementation of CreateRightExpr...
        }

        throw new InvalidOperationException($"Unsupported value '{right}' for type '{targetType.Name}'");
    }

    // Rest of the code...

}

Please note that I've omitted some of the repetitive parser method implementations for brevity. If you need any specific part translated or have questions about a particular section, please let me know.

英文:

so i have a parser that uses use sprache to create an expression from a string. something like PhysicalAddress.State == &quot;GA&quot; has PhysicalAddress.State parsed as the left, == as the operator, and &quot;GA&quot; as the right expression. the parts can then be combined to an expression for use in a queryable like x =&gt; (x.PhysicalAddress.State == &quot;GA&quot;).

i want to expand it to be able to dynamically handle ienumerables when building expressions like in the test below. i started working on the below for checking if i have an ienumerable (in CreateLeftExprParser surrounded by WIP comment), but i'm stuck on how to get the inner expression for the any. My first thought was to recursively call ParseFiler() to get the inner expression, but that doesn't really fly since it needs the raw string to go on. maybe i could amke an overload that takes the comparison and right tokens?

regardless, open to thoughts and ideas. this is a tricky one.

    [Fact]
    public void simple_with_operator_for_string()
    {
        var input = &quot;&quot;&quot;Ingredients.Name != &quot;flour&quot; &quot;&quot;&quot;;
        var filterExpression = FilterParser.ParseFilter&lt;Recipe&gt;(input);
        filterExpression.ToString().Should()
            .Be(&quot;&quot;&quot;&quot;x =&gt; x.Ingredients.Any(y =&gt; (y.Name != \&quot;flour\&quot;))&quot;&quot;&quot;&quot;);
    }
public static class FilterParser
{
    internal static Expression&lt;Func&lt;T, bool&gt;&gt; ParseFilter&lt;T&gt;(string input, IQueryKitConfiguration? config = null)
    {
        var parameter = Expression.Parameter(typeof(T), &quot;x&quot;);
        Expression expr; 
        try
        {
            expr = ExprParser&lt;T&gt;(parameter, config).End().Parse(input);
        }
        catch (ParseException e)
        {
            throw new ParsingException(e);
        }
        return Expression.Lambda&lt;Func&lt;T, bool&gt;&gt;(expr, parameter);
    }

    private static readonly Parser&lt;string&gt; Identifier =
        from first in Parse.Letter.Once()
        from rest in Parse.LetterOrDigit.XOr(Parse.Char(&#39;_&#39;)).Many()
        select new string(first.Concat(rest).ToArray());
    
    private static Parser&lt;ComparisonOperator&gt; ComparisonOperatorParser
    {
        get
        {
            var parsers = Parse.String(ComparisonOperator.EqualsOperator().Operator()).Text()
                .Or(Parse.String(ComparisonOperator.NotEqualsOperator().Operator()).Text())
                .Or(Parse.String(ComparisonOperator.GreaterThanOrEqualOperator().Operator()).Text())
                .Or(Parse.String(ComparisonOperator.LessThanOrEqualOperator().Operator()).Text())
                .Or(Parse.String(ComparisonOperator.GreaterThanOperator().Operator()).Text())
                .Or(Parse.String(ComparisonOperator.LessThanOperator().Operator()).Text())
                .Or(Parse.String(ComparisonOperator.ContainsOperator().Operator()).Text())
                .Or(Parse.String(ComparisonOperator.StartsWithOperator().Operator()).Text())
                .Or(Parse.String(ComparisonOperator.EndsWithOperator().Operator()).Text())
                .Or(Parse.String(ComparisonOperator.NotContainsOperator().Operator()).Text())
                .Or(Parse.String(ComparisonOperator.NotStartsWithOperator().Operator()).Text())
                .Or(Parse.String(ComparisonOperator.NotEndsWithOperator().Operator()).Text())
                .Or(Parse.String(ComparisonOperator.InOperator().Operator()).Text())
                .SelectMany(op =&gt; Parse.Char(&#39;*&#39;).Optional(), (op, caseInsensitive) =&gt; new { op, caseInsensitive });
                
            var compOperator = parsers
                .Select(x =&gt; ComparisonOperator.GetByOperatorString(x.op, x.caseInsensitive.IsDefined));
            return compOperator;
        }
    }

    private static Parser&lt;Expression&gt; ComparisonExprParser&lt;T&gt;(ParameterExpression parameter, IQueryKitConfiguration? config)
    {
        var comparisonOperatorParser = ComparisonOperatorParser.Token();
        var rightSideValueParser = RightSideValueParser.Token();

        return CreateLeftExprParser(parameter, config)
            .SelectMany(leftExpr =&gt; comparisonOperatorParser, (leftExpr, op) =&gt; new { leftExpr, op })
            .SelectMany(temp =&gt; rightSideValueParser, (temp, right) =&gt; new { temp.leftExpr, temp.op, right })
            .Select(temp =&gt;
            {
                if (temp.leftExpr.NodeType == ExpressionType.Constant &amp;&amp; ((ConstantExpression)temp.leftExpr).Value!.Equals(true))
                {
                    return Expression.Equal(Expression.Constant(true), Expression.Constant(true));
                }

                var rightExpr = CreateRightExpr(temp.leftExpr, temp.right);
                return temp.op.GetExpression&lt;T&gt;(temp.leftExpr, rightExpr);
            });
    }

    private static Parser&lt;Expression?&gt;? CreateLeftExprParser(ParameterExpression parameter, IQueryKitConfiguration? config)
    {
        var leftIdentifierParser = Identifier.DelimitedBy(Parse.Char(&#39;.&#39;)).Token();

        return leftIdentifierParser?.Select(left =&gt;
        {
            // If only a single identifier is present
            var leftList = left.ToList();
            if (leftList.Count == 1)
            {
                var propName = leftList?.First();
                var fullPropPath = config?.GetPropertyPathByQueryName(propName) ?? propName;
                var propNames = fullPropPath?.Split(&#39;.&#39;);

                var propertyExpression = propNames?.Aggregate((Expression)parameter, (expr, pn) =&gt;
                {
                    var propertyInfo = GetPropertyInfo(expr.Type, pn);
                    var actualPropertyName = propertyInfo?.Name ?? pn;
                    try
                    {
                        return Expression.PropertyOrField(expr, actualPropertyName);
                    }
                    catch(ArgumentException)
                    {
                        throw new UnknownFilterPropertyException(actualPropertyName);
                    }
                });

                var propertyConfig = config?.PropertyMappings?.GetPropertyInfo(fullPropPath);
                if (propertyConfig != null &amp;&amp; !propertyConfig.CanFilter)
                {
                    return Expression.Constant(true, typeof(bool));
                }

                return propertyExpression;
            }

            // If the property is nested with a dot (&#39;.&#39;) separator
            var nestedPropertyExpression = leftList.Aggregate((Expression)parameter, (expr, propName) =&gt;
            {
                var propertyInfo = GetPropertyInfo(expr.Type, propName);

                // ------- WIP for IEnumerable Handling
                if (propertyInfo != null &amp;&amp; typeof(IEnumerable).IsAssignableFrom(propertyInfo.PropertyType) &amp;&amp; propertyInfo.PropertyType != typeof(string))
                {
                    // Create a parameter for the lambda in the Any method
                    var anyParameter = Expression.Parameter(propertyInfo.PropertyType.GetGenericArguments().First(), &quot;y&quot;);

                    // Get the rest of the property path
                    var subProperties = leftList.Skip(leftList.IndexOf(propName) + 1);

                    // Get the rest of the input for the recursive call
                    var restOfInput = string.Join(&quot;.&quot;, subProperties);
        
                    var anyLambda = ????

                    // Create the Any method call
                    var anyMethod = typeof(Enumerable).GetMethods().First(m =&gt; m.Name == &quot;Any&quot; &amp;&amp; m.GetParameters().Length == 2);
                    var genericAnyMethod = anyMethod.MakeGenericMethod(anyParameter.Type);

                    return Expression.Call(genericAnyMethod, expr, anyLambda);
                }
                // --------- End WIP

                var mappedPropertyInfo = config?.PropertyMappings?.GetPropertyInfoByQueryName(propName);
                var actualPropertyName = mappedPropertyInfo?.Name ?? propertyInfo?.Name ?? propName;
                try
                {
                    return Expression.PropertyOrField(expr, actualPropertyName);
                }
                catch(ArgumentException)
                {
                    throw new UnknownFilterPropertyException(actualPropertyName);
                }
            });

            var nestedPropertyConfig = config?.PropertyMappings?.GetPropertyInfo(leftList.Last());
            if (nestedPropertyConfig != null &amp;&amp; !nestedPropertyConfig.CanFilter)
            {
                return Expression.Constant(true, typeof(bool));
            }

            return nestedPropertyExpression;
        });
    }

    private static PropertyInfo? GetPropertyInfo(Type type, string propertyName)
    {
        return type.GetProperty(propertyName, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance);
    }

    public static Parser&lt;LogicalOperator&gt; LogicalOperatorParser =&gt;
        from leadingSpaces in Parse.WhiteSpace.Many()
        from op in Parse.String(LogicalOperator.AndOperator.Operator()).Text().Or(Parse.String(LogicalOperator.OrOperator.Operator()).Text())
        from trailingSpaces in Parse.WhiteSpace.Many()
        select LogicalOperator.GetByOperatorString(op);
    
    private static Parser&lt;string&gt; DoubleQuoteParser
        =&gt; Parse.Char(&#39;&quot;&#39;).Then(_ =&gt; Parse.AnyChar.Except(Parse.Char(&#39;&quot;&#39;)).Many().Text().Then(innerValue =&gt; Parse.Char(&#39;&quot;&#39;).Return(innerValue)));


    private static Parser&lt;string&gt; TimeFormatParser =&gt; Parse.Regex(@&quot;\d{2}:\d{2}:\d{2}&quot;).Text();
    private static Parser&lt;string&gt; DateTimeFormatParser =&gt; 
        from dateFormat in Parse.Regex(@&quot;\d{4}-\d{2}-\d{2}&quot;).Text()
        from timeFormat in Parse.Regex(@&quot;T\d{2}:\d{2}:\d{2}&quot;).Text().Optional().Select(x =&gt; x.GetOrElse(&quot;&quot;))
        from timeZone in Parse.Regex(@&quot;(Z|[+-]\d{2}(:\d{2})?)&quot;).Text().Optional().Select(x =&gt; x.GetOrElse(&quot;&quot;))
        from millis in Parse.Regex(@&quot;\.\d{3}&quot;).Text().Optional().Select(x =&gt; x.GetOrElse(&quot;&quot;))
        select dateFormat + timeFormat + timeZone + millis;

    private static Parser&lt;string&gt; GuidFormatParser =&gt; Parse.Regex(@&quot;[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}&quot;).Text();
    
    private static Parser&lt;string&gt; RawStringLiteralParser =&gt;
        from openingQuotes in Parse.Regex(&quot;\&quot;{3,}&quot;).Text()
        let count = openingQuotes.Length
        from content in Parse.AnyChar.Except(Parse.Char(&#39;&quot;&#39;).Repeat(count)).Many().Text()
        from closingQuotes in Parse.Char(&#39;&quot;&#39;).Repeat(count).Text()
        select content;

    private static Parser&lt;string&gt; RightSideValueParser =&gt;
        from atSign in Parse.Char(&#39;@&#39;).Optional()
        from leadingSpaces in Parse.WhiteSpace.Many()
        from value in Parse.String(&quot;null&quot;).Text()
            .Or(GuidFormatParser)
            .XOr(Identifier)
            .XOr(DateTimeFormatParser)
            .XOr(TimeFormatParser)
            .XOr(Parse.Decimal)
            .XOr(Parse.Number)
            .XOr(RawStringLiteralParser.Or(DoubleQuoteParser))
            .XOr(SquareBracketParser) 
        from trailingSpaces in Parse.WhiteSpace.Many()
        select atSign.IsDefined ? &quot;@&quot; + value : value;
    
    private static Parser&lt;string&gt; SquareBracketParser =&gt;
        from openingBracket in Parse.Char(&#39;[&#39;)
        from content in DoubleQuoteParser
            .Or(GuidFormatParser)
            .Or(Parse.Decimal)
            .Or(Parse.Number)
            .Or(Identifier)
            .DelimitedBy(Parse.Char(&#39;,&#39;).Token())
        from closingBracket in Parse.Char(&#39;]&#39;)
        select &quot;[&quot; + string.Join(&quot;,&quot;, content) + &quot;]&quot;;

    private static Parser&lt;Expression&gt; AtomicExprParser&lt;T&gt;(ParameterExpression parameter, IQueryKitConfiguration? config = null)
        =&gt; ComparisonExprParser&lt;T&gt;(parameter, config)
            .Or(Parse.Ref(() =&gt; ExprParser&lt;T&gt;(parameter, config)).Contained(Parse.Char(&#39;(&#39;), Parse.Char(&#39;)&#39;)));

    private static Parser&lt;Expression&gt; ExprParser&lt;T&gt;(ParameterExpression parameter, IQueryKitConfiguration? config = null)
        =&gt; OrExprParser&lt;T&gt;(parameter, config);

    private static readonly Dictionary&lt;Type, Func&lt;string, object&gt;&gt; TypeConversionFunctions = new()
    {
        { typeof(string), value =&gt; value },
        { typeof(bool), value =&gt; bool.Parse(value) },
        { typeof(Guid), value =&gt; Guid.Parse(value) },
        { typeof(char), value =&gt; char.Parse(value) },
        { typeof(int), value =&gt; int.Parse(value, CultureInfo.InvariantCulture) },
        { typeof(float), x =&gt; float.Parse(x, CultureInfo.InvariantCulture) },
        { typeof(double), x =&gt; double.Parse(x, CultureInfo.InvariantCulture) },
        { typeof(decimal), x =&gt; decimal.Parse(x, CultureInfo.InvariantCulture) },
        { typeof(long), value =&gt; long.Parse(value, CultureInfo.InvariantCulture) },
        { typeof(short), value =&gt; short.Parse(value, CultureInfo.InvariantCulture) },
        { typeof(byte), value =&gt; byte.Parse(value, CultureInfo.InvariantCulture) },
        { typeof(DateTime), value =&gt; DateTime.Parse(value, CultureInfo.InvariantCulture) },
        { typeof(DateTimeOffset), value =&gt; DateTimeOffset.Parse(value, CultureInfo.InvariantCulture) },
        { typeof(DateOnly), value =&gt; DateOnly.Parse(value, CultureInfo.InvariantCulture) },
        { typeof(TimeOnly), value =&gt; TimeOnly.Parse(value, CultureInfo.InvariantCulture) },
        { typeof(TimeSpan), value =&gt; TimeSpan.Parse(value, CultureInfo.InvariantCulture) },
        { typeof(uint), value =&gt; uint.Parse(value, CultureInfo.InvariantCulture) },
        { typeof(ulong), value =&gt; ulong.Parse(value, CultureInfo.InvariantCulture) },
        { typeof(ushort), value =&gt; ushort.Parse(value, CultureInfo.InvariantCulture) },
        { typeof(sbyte), value =&gt; sbyte.Parse(value, CultureInfo.InvariantCulture) },
        // { typeof(Enum), value =&gt; Enum.Parse(typeof(T), value) },
    };

    private static Expression CreateRightExpr(Expression leftExpr, string right)
    {
        var targetType = leftExpr.Type;

        targetType = TransformTargetTypeIfNullable(targetType);

        if (TypeConversionFunctions.TryGetValue(targetType, out var conversionFunction))
        {
            if (right == &quot;null&quot;)
            {
                return Expression.Constant(null, leftExpr.Type);
            }

            if (right.StartsWith(&quot;[&quot;) &amp;&amp; right.EndsWith(&quot;]&quot;))
            {
                var values = right.Trim(&#39;[&#39;, &#39;]&#39;).Split(&#39;,&#39;).Select(x =&gt; x.Trim()).ToList();
                var elementType = targetType.IsArray ? targetType.GetElementType() : targetType;
            
                var expressions = values.Select(x =&gt;
                {
                    if (elementType == typeof(string) &amp;&amp; x.StartsWith(&quot;\&quot;&quot;) &amp;&amp; x.EndsWith(&quot;\&quot;&quot;))
                    {
                        x = x.Trim(&#39;&quot;&#39;);
                    }
            
                    var convertedValue = TypeConversionFunctions[elementType](x);
                    return Expression.Constant(convertedValue, elementType);
                }).ToArray();
            
                var newArrayExpression = Expression.NewArrayInit(elementType, expressions);
                return newArrayExpression;
            }

            if (targetType == typeof(string))
            {
                right = right.Trim(&#39;&quot;&#39;);
            }
            
            var convertedValue = conversionFunction(right);

            if (targetType == typeof(Guid))
            {
                var guidParseMethod = typeof(Guid).GetMethod(&quot;Parse&quot;, new[] { typeof(string) });
                return Expression.Call(guidParseMethod, Expression.Constant(convertedValue.ToString(), typeof(string)));
            }

            return Expression.Constant(convertedValue, leftExpr.Type);
        }

        throw new InvalidOperationException($&quot;Unsupported value &#39;{right}&#39; for type &#39;{targetType.Name}&#39;&quot;);
    }

    private static Type TransformTargetTypeIfNullable(Type targetType)
    {
        if (targetType.IsGenericType &amp;&amp; targetType.GetGenericTypeDefinition() == typeof(Nullable&lt;&gt;))
        {
            targetType = Nullable.GetUnderlyingType(targetType);
        }

        return targetType;
    }

    private static Parser&lt;Expression&gt; AndExprParser&lt;T&gt;(ParameterExpression parameter, IQueryKitConfiguration? config = null)
        =&gt; Parse.ChainOperator(
            LogicalOperatorParser.Where(x =&gt; x.Name == LogicalOperator.AndOperator.Operator()),
            AtomicExprParser&lt;T&gt;(parameter, config),
            (op, left, right) =&gt; op.GetExpression&lt;T&gt;(left, right)
        );

    private static Parser&lt;Expression&gt; OrExprParser&lt;T&gt;(ParameterExpression parameter, IQueryKitConfiguration? config = null)
        =&gt; Parse.ChainOperator(
            LogicalOperatorParser.Where(x =&gt; x.Name == LogicalOperator.OrOperator.Operator()),
            AndExprParser&lt;T&gt;(parameter, config),
            (op, left, right) =&gt; op.GetExpression&lt;T&gt;(left, right)
        );
}

答案1

得分: 1

我的第一个想法是重新编写解析器,不直接解析为 Expression,而是使用中间的AST。然后,可以将此AST转换为 Expression。这将把检查可枚举项和构建Any表达式的问题移到了转换阶段,在此阶段,您可以根据树的结构自由访问子项/父项/兄弟项等,而不受Expression的限制,您可以创建与您想要解决的问题匹配的结构。

如果创建一个中间结构不是一个选项,那么我会返回一个存根Any表达式(例如,Any(y => true)),然后在您的ComparisonExprParser方法中检查是否存在这样的存根,然后在最终的Select(temp => ...)中构建正确的Any表达式,据我所知,您在这里拥有所有所需的信息。

英文:

My first thought would be to rewrite the parser to not parse directly to Expression, but have an intermediary AST. This AST could then be transformed into Expression. This will move the problem of checking for enumerables and constructing the Any-expression to this transform phase where you will have access to children/parents/siblings and so on based on how you structured your tree - and without the constraints of Expression you are free to make a structure that matches the problem, you would like to solve.

If making an intermediary is not an option, then I'd return a stub Any-expression (e.g. Any(y => true)) and then check for such stub in your ComparisonExprParser method - and then build the correct Any-expression in your final Select(temp =&gt; ...) where you - as far as I can tell - have all the needed information.

huangapple
  • 本文由 发表于 2023年5月21日 05:42:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76297467.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定