System.Linq.Dynamic.Core icon indicating copy to clipboard operation
System.Linq.Dynamic.Core copied to clipboard

Parse exceptions with (nested) static classes

Open poostwoud opened this issue 1 year ago • 1 comments

1. Description

I'm trying to create an Excel-like function syntax using nested static classes. I'm experiencing difficulties with some class names and nested classes. Below is the Fiddle code:

using System;
using System.Collections.Generic;
using System.Linq.Dynamic.Core;
using System.Linq.Dynamic.Core.CustomTypeProviders;
using System.Linq.Expressions;

public class Program
{
    public static void Main()
    {
        var config = new ParsingConfig
        {
            CustomTypeProvider = new CustomTypeProvider(new[] { typeof(IST), typeof(IST.NOT) })
        };

        // Not working;
        var expression = "IS.NULL(null)";
        try
        {
            var result = DynamicExpressionParser.ParseLambda(
                config,
                false,
                new ParameterExpression[] {  },
                typeof(bool),
                expression
            );
            Console.WriteLine($"{expression} success");
        }
        catch (Exception ex)
        {
            Console.WriteLine(
                $"{expression} failed, because of {ex.GetType().FullName} with message {ex.Message}"
            );
        }

        // Working;
        expression = "IST.NULL(null)";
        try
        {
            var result = DynamicExpressionParser.ParseLambda(
                config,
                false,
                new ParameterExpression[] {  },
                typeof(bool),
                expression
            );
            Console.WriteLine($"{expression} success");
        }
        catch (Exception ex)
        {
            Console.WriteLine(
                $"{expression} failed, because of {ex.GetType().FullName} with message {ex.Message}"
            );
        }

        // Not working;
        expression = "IS.NOT.NULL(null)";
        try
        {
            var result = DynamicExpressionParser.ParseLambda(
                config,
                false,
                new ParameterExpression[] {  },
                typeof(bool),
                expression
            );
            Console.WriteLine($"{expression} success");
        }
        catch (Exception ex)
        {
            Console.WriteLine(
                $"{expression} failed, because of {ex.GetType().FullName} with message {ex.Message}"
            );
        }

        // Not working;
        expression = "IST.NOT.NULL(null)";
        try
        {
            var result = DynamicExpressionParser.ParseLambda(
                config,
                false,
                new ParameterExpression[] {  },
                typeof(bool),
                expression
            );
            Console.WriteLine($"{expression} success");
        }
        catch (Exception ex)
        {
            Console.WriteLine(
                $"{expression} failed, because of {ex.GetType().FullName} with message {ex.Message}"
            );
        }
    }

    public class CustomTypeProvider : DefaultDynamicLinqCustomTypeProvider
    {
        private readonly HashSet<Type> _types;

        public CustomTypeProvider(Type[] types)
        {
            _types = new HashSet<Type>(types);
        }

        public override HashSet<Type> GetCustomTypes()
        {
            return _types;
        }
    }

    public static class IS
    {
        public static bool NULL(object value) => value == null;

        public static class NOT
        {
            public static bool NULL(object value) => value != null;
        }
    }

    public static class IST
    {
        public static bool NULL(object value) => value == null;

        public static class NOT
        {
            public static bool NULL(object value) => value != null;
        }
    }
}

2. Exception

Fiddle output:

IS.NULL(null) failed, because of System.Linq.Dynamic.Core.Exceptions.ParseException with message '(' expected
IST.NULL(null) success
IS.NOT.NULL(null) failed, because of System.Linq.Dynamic.Core.Exceptions.ParseException with message '(' expected
IST.NOT.NULL(null) failed, because of System.Linq.Dynamic.Core.Exceptions.ParseException with message Identifier expected

3. Fiddle

https://dotnetfiddle.net/nRMI2q

poostwoud avatar Feb 06 '24 20:02 poostwoud

Hello @poostwoud;

This is indeed an error, related to #772 .

However IS is a reserved keyword. The casing issue for keywords is modified in https://github.com/zzzprojects/System.Linq.Dynamic.Core/pull/735, however this PR is not yet completed.

StefH avatar Feb 15 '24 10:02 StefH