From 022271e34333120cd87daea4521e6a25b8ad1486 Mon Sep 17 00:00:00 2001 From: Anton Medvedev Date: Fri, 15 May 2020 19:03:50 +0300 Subject: [PATCH] Fix parsing of pointers in nested closures --- expr_test.go | 11 +++++++++++ parser/parser.go | 8 ++++---- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/expr_test.go b/expr_test.go index 3394eb952..3f04d6c9a 100644 --- a/expr_test.go +++ b/expr_test.go @@ -1123,6 +1123,17 @@ func TestIssue105(t *testing.T) { require.Contains(t, err.Error(), "ambiguous identifier Field") } +func TestIssue_nested_closures(t *testing.T) { + code := `all(1..3, { all(1..3, { # > 0 }) and # > 0 })` + + program, err := expr.Compile(code) + require.NoError(t, err) + + output, err := expr.Run(program, nil) + require.NoError(t, err) + require.True(t, output.(bool)) +} + // // Mock types // diff --git a/parser/parser.go b/parser/parser.go index 7ddb4f9a5..5b640817e 100644 --- a/parser/parser.go +++ b/parser/parser.go @@ -77,7 +77,7 @@ type parser struct { current Token pos int err *file.Error - closure bool + depth int // closure call depth } type Tree struct { @@ -219,7 +219,7 @@ func (p *parser) parsePrimary() Node { return p.parsePostfixExpression(expr) } - if p.closure { + if p.depth > 0 { if token.Is(Operator, "#") || token.Is(Operator, ".") { if token.Is(Operator, "#") { p.next() @@ -377,9 +377,9 @@ func (p *parser) parseClosure() Node { token := p.current p.expect(Bracket, "{") - p.closure = true + p.depth++ node := p.parseExpression(0) - p.closure = false + p.depth-- p.expect(Bracket, "}") closure := &ClosureNode{