Support implicit boolean expression nested in groups

This commit is contained in:
David Stotijn 2020-11-17 21:34:59 +01:00
parent 5bce912e89
commit 8ab65fb55f
2 changed files with 24 additions and 3 deletions

View file

@ -9,10 +9,10 @@ type precedence int
const (
_ precedence = iota
precLowest
precEq
precAnd
precOr
precNot
precEq
precLessGreater
precPrefix
precGroup
@ -218,8 +218,19 @@ func parseGroupedExpression(p *Parser) (Expression, error) {
return nil, fmt.Errorf("could not parse grouped expression: %v", err)
}
if err := p.expectPeek(TokParenClose); err != nil {
return nil, err
for p.nextToken(); !p.curTokenIs(TokParenClose); p.nextToken() {
if p.curTokenIs(TokEOF) {
return nil, fmt.Errorf("unexpected EOF: unmatched parentheses")
}
right, err := p.parseExpression(precLowest)
if err != nil {
return nil, fmt.Errorf("could not parse expression: %v", err)
}
expr = &InfixExpression{
Operator: TokOpAnd,
Left: expr,
Right: right,
}
}
return expr, nil

View file

@ -153,6 +153,16 @@ func TestParseQuery(t *testing.T) {
},
expectedError: nil,
},
{
name: "implicit boolean expression nested in group",
input: "(foo bar)",
expectedExpression: &InfixExpression{
Operator: TokOpAnd,
Left: &StringLiteral{Value: "foo"},
Right: &StringLiteral{Value: "bar"},
},
expectedError: nil,
},
{
name: "implicit and explicit boolean expression with string literal operands",
input: "foo bar OR baz yolo",