Skip to content

Commit

Permalink
Fix lookaheadLambdaArg not to use parseExpr (#221)
Browse files Browse the repository at this point in the history
* Fix lookaheadLambdaArg

* Remove unreachable code
  • Loading branch information
apstndb authored Dec 3, 2024
1 parent 77fc9f8 commit 82ce77a
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 5 deletions.
31 changes: 26 additions & 5 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -1596,13 +1596,34 @@ func (p *Parser) lookaheadLambdaArg() bool {
p.Lexer = lexer
}()

if p.Token.Kind != "(" && p.Token.Kind != token.TokenIdent {
switch p.Token.Kind {
case "(":
p.nextToken()
if p.Token.Kind != token.TokenIdent {
return false
}
p.nextToken()

for p.Token.Kind != ")" {
if p.Token.Kind != "," {
return false
}
p.nextToken()

if p.Token.Kind != token.TokenIdent {
return false
}
p.nextToken()
}
p.nextToken()

return p.Token.Kind == "->"
case token.TokenIdent:
p.nextToken()
return p.Token.Kind == "->"
default:
return false
}

// Note: all lambda patterns can be parsed as expr -> expr.
p.parseExpr()
return p.Token.Kind == "->"
}

func (p *Parser) parseLambdaArg() *ast.LambdaArg {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ARRAY_FILTER([1 ,2, 3], (e) -> e > 1)
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
--- array_functions_array_filter_one_arg_with_paren_lambda.sql
ARRAY_FILTER([1 ,2, 3], (e) -> e > 1)
--- AST
&ast.CallExpr{
Rparen: 36,
Func: &ast.Ident{
NamePos: 0,
NameEnd: 12,
Name: "ARRAY_FILTER",
},
Distinct: false,
Args: []ast.Arg{
&ast.ExprArg{
Expr: &ast.ArrayLiteral{
Array: -1,
Lbrack: 13,
Rbrack: 21,
Type: nil,
Values: []ast.Expr{
&ast.IntLiteral{
ValuePos: 14,
ValueEnd: 15,
Base: 10,
Value: "1",
},
&ast.IntLiteral{
ValuePos: 17,
ValueEnd: 18,
Base: 10,
Value: "2",
},
&ast.IntLiteral{
ValuePos: 20,
ValueEnd: 21,
Base: 10,
Value: "3",
},
},
},
},
&ast.LambdaArg{
Lparen: 24,
Args: []*ast.Ident{
&ast.Ident{
NamePos: 25,
NameEnd: 26,
Name: "e",
},
},
Expr: &ast.BinaryExpr{
Op: ">",
Left: &ast.Ident{
NamePos: 31,
NameEnd: 32,
Name: "e",
},
Right: &ast.IntLiteral{
ValuePos: 35,
ValueEnd: 36,
Base: 10,
Value: "1",
},
},
},
},
NamedArgs: []*ast.NamedArg(nil),
NullHandling: nil,
Having: nil,
}

--- SQL
ARRAY_FILTER([1, 2, 3], (e) -> e > 1)

0 comments on commit 82ce77a

Please sign in to comment.