DynamicExpresso icon indicating copy to clipboard operation
DynamicExpresso copied to clipboard

Lambda parsing can fail for generics and multi-parameter indexers

Open Pentiva opened this issue 1 year ago • 1 comments

Similar to #259, square and angle brackets are not currently used to decide when the body of a lambda stops.

var intList = new List<int> { 1, 2 };
target.SetVariable("intList", intList);
var results = target.Eval<IEnumerable<Dictionary<string, int>>>("intList.Select(a => new Dictionary<string, int>())");

DynamicExpresso.Exceptions.ParseException : '.' or '(' expected (at index 46).

var intMultiArray = new int[2, 3];
target.SetVariable("intMultiArray", intMultiArray);
var results = target.Eval<int[,]>("intMultiArray.MultiArrayTest(t => t[0, 0])")

public static int[,] MultiArrayTest(this int[,] source, Func<int[,], int> func) {
    func(source);
    return source;
}

DynamicExpresso.Exceptions.ParseException : ')' or ',' expected (at index 40).

This should be an easy fix for the multi-dimensional arrays (basically another extension of #260 ), but I'm struggling to figure out how it would be done for generics, as the left angle bracket could easily be used as less than instead meaning there wouldn't be a closing one.

.Where(a => a < variable)
.Select(a => new A < TypeName >()) // I wouldn't expect anyone to do this, but I hope it helps illustrate my point.

I discovered this on purpose, while trying to explicitly find bugs with the lambda parsing. As such it is not urgent, and you can also work around it by putting the whole expression after the Lambda Arrow in parentheses.

Pentiva avatar Jul 27 '23 13:07 Pentiva

Thanks for the report! It's indeed similar to #259. Good catch for the angle brackets.. I'll add this to my TODO list :)

metoule avatar Jul 27 '23 14:07 metoule