Skip to content

Commit

Permalink
Consider brackets when deciding when the lambda body ends.
Browse files Browse the repository at this point in the history
  • Loading branch information
metoule committed Nov 5, 2023
1 parent 2696232 commit 49d00d4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/DynamicExpresso.Core/Parsing/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,9 @@ private Expression ParseLambdaExpression()
NextToken();
if (_token.id == TokenId.End)
inLambdaBody = false;
if (_token.id == TokenId.OpenParen || _token.id == TokenId.OpenCurlyBracket)
if (_token.id == TokenId.OpenParen || _token.id == TokenId.OpenCurlyBracket || _token.id == TokenId.OpenBracket)
parenCount++;
if (_token.id == TokenId.CloseParen || _token.id == TokenId.CloseCurlyBracket)
if (_token.id == TokenId.CloseParen || _token.id == TokenId.CloseCurlyBracket || _token.id == TokenId.CloseBracket)
parenCount--;

// lambda is a function parameter
Expand Down
18 changes: 18 additions & 0 deletions test/DynamicExpresso.UnitTest/GithubIssues.cs
Original file line number Diff line number Diff line change
Expand Up @@ -793,6 +793,19 @@ public void GitHub_Issue_295() {
var doesntWork = (string) evaluator.Eval("StringConcat(GlobalSettings.MyTestPath,\"test.txt\")");
Assert.That(doesntWork, Is.EqualTo("C:\\delme\\test.txt"));
}

[Test]
public void Lambda_Issue_294()
{
var options = InterpreterOptions.Default | InterpreterOptions.LambdaExpressions;
var target = new Interpreter(options);
target.Reference(typeof(GithubIssuesTestExtensionsMethods));

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

internal static class GithubIssuesTestExtensionsMethods
Expand All @@ -803,5 +816,10 @@ public static void ActionToAll<T>(this IEnumerable<T> source, Action<T> action)
foreach (var item in source)
action(item);
}

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

0 comments on commit 49d00d4

Please sign in to comment.