dynamicexpresso / DynamicExpresso

C# expressions interpreter
http://dynamic-expresso.azurewebsites.net/
MIT License
1.91k stars 364 forks source link

Lambda parsing can fail for generics and multi-parameter indexers #294

Open Pentiva opened 11 months ago

Pentiva commented 11 months ago

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.

metoule commented 11 months ago

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