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

Fix lookaheadLambdaArg not to use parseExpr #221

Merged
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
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)
Loading