Skip to content

Commit

Permalink
Merge pull request #216 from MasonM/fix-boolean-precedence
Browse files Browse the repository at this point in the history
Fix boolean operator precedence
  • Loading branch information
ysugimoto authored Nov 23, 2023
2 parents 1bffa50 + 5177fe2 commit 2b5a7bc
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 2 deletions.
3 changes: 2 additions & 1 deletion parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ import (
"github.com/ysugimoto/falco/token"
)

// Reference: https://developer.fastly.com/reference/vcl/operators/
const (
LOWEST int = iota + 1
AND
OR
AND
REGEX
EQUALS
LESS_GREATER
Expand Down
64 changes: 63 additions & 1 deletion parser/statement_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,68 @@ sub vcl_recv {
assert(t, vcl, expect)
})

t.Run("combination of boolean conditions", func(t *testing.T) {
input := `// Subroutine
sub vcl_recv {
// Leading comment
if (true || false && false) {
restart;
}
}`
expect := &ast.VCL{
Statements: []ast.Statement{
&ast.SubroutineDeclaration{
Meta: ast.New(T, 0, comments("// Subroutine")),
Name: &ast.Ident{
Meta: ast.New(T, 0),
Value: "vcl_recv",
},
Block: &ast.BlockStatement{
Meta: ast.New(T, 1),
Statements: []ast.Statement{
&ast.IfStatement{
Meta: ast.New(T, 1, comments("// Leading comment")),
Condition: &ast.InfixExpression{
Meta: ast.New(T, 1),
Operator: "||",
Left: &ast.Boolean{
Meta: ast.New(T, 1),
Value: true,
},
Right: &ast.InfixExpression{
Meta: ast.New(T, 1),
Operator: "&&",
Left: &ast.Boolean{
Meta: ast.New(T, 1),
Value: false,
},
Right: &ast.Boolean{
Meta: ast.New(T, 1),
Value: false,
},
},
},
Consequence: &ast.BlockStatement{
Meta: ast.New(T, 2),
Statements: []ast.Statement{
&ast.RestartStatement{
Meta: ast.New(T, 2),
},
},
},
},
},
},
},
},
}
vcl, err := New(lexer.NewFromString(input)).ParseVCL()
if err != nil {
t.Errorf("%+v", err)
}
assert(t, vcl, expect)
})

t.Run("if-else", func(t *testing.T) {
input := `// Subroutine
sub vcl_recv {
Expand Down Expand Up @@ -1185,7 +1247,7 @@ sub vcl_recv {
Meta: ast.New(T, 1),
Operator: "+",
Right: &ast.String{
Meta: ast.New(T, 1),
Meta: ast.New(T, 1),
Value: " timestamp:",
},
Left: &ast.InfixExpression{
Expand Down

0 comments on commit 2b5a7bc

Please sign in to comment.