Skip to content

Commit

Permalink
Consider curly brackets to decide when the body of a lambda stops. (#260
Browse files Browse the repository at this point in the history
)
  • Loading branch information
metoule authored Nov 5, 2022
1 parent 115ffa4 commit 6ed699e
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
6 changes: 4 additions & 2 deletions src/DynamicExpresso.Core/Parsing/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,16 +149,18 @@ private Expression ParseLambdaExpression()

var startExpr = _parsePosition;

// decide where the lambda expression body ends:
// it's the first unmatched parenthesis, curly bracket, of comma
var parenCount = 0;
var inLambdaBody = true;
while (inLambdaBody)
{
NextToken();
if (_token.id == TokenId.End)
inLambdaBody = false;
if (_token.id == TokenId.OpenParen)
if (_token.id == TokenId.OpenParen || _token.id == TokenId.OpenCurlyBracket)
parenCount++;
if (_token.id == TokenId.CloseParen)
if (_token.id == TokenId.CloseParen || _token.id == TokenId.CloseCurlyBracket)
parenCount--;

// lambda is a function parameter
Expand Down
22 changes: 22 additions & 0 deletions test/DynamicExpresso.UnitTest/GithubIssues.cs
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,28 @@ public class BonusMatrix
public int Grade { get; set; }
public double BonusFactor { get; set; }
}

[Test]
public void Lambda_Issue_259()
{
var options = InterpreterOptions.Default | InterpreterOptions.LambdaExpressions;
var interpreter = new Interpreter(options);
interpreter.SetVariable("courseList", new[] { new { PageName = "Test" } });
interpreter.Reference(typeof(PageType));

var results = interpreter.Eval<IEnumerable<PageType>>(@"courseList.Select(x => new PageType() { PageName = x.PageName, VisualCount = 5 })");
Assert.AreEqual(1, results.Count());

var result = results.Single();
Assert.AreEqual("Test", result.PageName);
Assert.AreEqual(5, result.VisualCount);
}

public class PageType
{
public string PageName { get; set; }
public int VisualCount { get; set; }
}
}

internal static class GithubIssuesTestExtensionsMethods
Expand Down
11 changes: 9 additions & 2 deletions test/DynamicExpresso.UnitTest/LambdaExpressionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -249,12 +249,19 @@ public void Lambda_with_multiple_params()
public void Two_lambda_parameters()
{
var target = new Interpreter(_options);
target.Reference(typeof(WithProp));

var list = new List<string> { "aaaaa", "bbbb", "ccc", "ddd" };
target.SetVariable("myList", list);
var results = target.Eval<Dictionary<string, int>>("myList.ToDictionary(str => str, str => str.Length)");
var results = target.Eval<Dictionary<string, int>>("myList.ToDictionary(str => new WithProp { MyStr = str }.MyStr, str => str.Length)");

Assert.AreEqual(4, results.Count);
Assert.AreEqual(list.ToDictionary(str => str, str => str.Length), results);
Assert.AreEqual(list.ToDictionary(str => new WithProp { MyStr = str }.MyStr, str => str.Length), results);
}

private class WithProp
{
public string MyStr { get; set; }
}

[Test]
Expand Down

0 comments on commit 6ed699e

Please sign in to comment.