Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consider curly brackets to decide when the body of a lambda stops #260

Merged
merged 1 commit into from
Nov 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
metoule marked this conversation as resolved.
Show resolved Hide resolved
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