Skip to content

Commit

Permalink
rego.v1: Improving support for rules with chained bodies (#6374)
Browse files Browse the repository at this point in the history
Fixes: #6370
Signed-off-by: Johan Fylling <[email protected]>
  • Loading branch information
johanfylling authored Nov 3, 2023
1 parent eade10a commit 89855df
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 3 deletions.
4 changes: 1 addition & 3 deletions ast/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -724,14 +724,12 @@ func (p *Parser) parseRules() []*Rule {
// rule's head AST but have their location
// set to the rule body.
next.Head = rule.Head.Copy()
next.Head.keywords = rule.Head.keywords
for i := range next.Head.Args {
if v, ok := next.Head.Args[i].Value.(Var); ok && v.IsWildcard() {
next.Head.Args[i].Value = Var(p.genwildcard())
}
}
if hasIf {
next.Head.keywords = append(next.Head.keywords, tokens.If)
}
setLocRecursive(next.Head, loc)

rules = append(rules, &next)
Expand Down
107 changes: 107 additions & 0 deletions ast/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1787,6 +1787,113 @@ f(x) := x {
}`,
expectedErrors: []string{"rego_parse_error: `if` keyword is required before rule body"},
},
{
note: "rule with chained bodies, no `if`",
module: `package test
import rego.v1
p {
input.x == 1
} {
input.x == 2
} {
input.x == 3
}`,
expectedErrors: []string{"rego_parse_error: `if` keyword is required before rule body"},
},
{
note: "rule with chained bodies, `if` on first body",
module: `package test
import rego.v1
p if {
input.x == 1
} {
input.x == 2
} {
input.x == 3
}`,
},
{
note: "rule with chained bodies, `if` on second body",
module: `package test
import rego.v1
p if {
input.x == 1
} if {
input.x == 2
} {
input.x == 3
}`,
expectedErrors: []string{`5:3: rego_parse_error: unexpected if keyword
} if {
^`},
},
{
note: "rule with chained bodies, `if` on third/last body",
module: `package test
import rego.v1
p if {
input.x == 1
} {
input.x == 2
} if {
input.x == 3
}`,
expectedErrors: []string{`7:3: rego_parse_error: unexpected if keyword
} if {
^`},
},
{
note: "rule with chained bodies, `if` and `contains` on first body",
module: `package test
import rego.v1
p contains x if {
x == 1
} {
x == 2
} {
x == 3
}`,
},
{
note: "function with chained bodies, no `if`",
module: `package test
import rego.v1
f(x) {
x == 1
} {
x == 2
} {
x == 3
}`,
expectedErrors: []string{"rego_parse_error: `if` keyword is required before rule body"},
},
{
note: "function with chained bodies, `if` on first body",
module: `package test
import rego.v1
f(x) if {
x == 1
} {
x == 2
} {
x == 3
}`,
},
{
note: "function with chained bodies, `if` on other than first body",
module: `package test
import rego.v1
f(x) if {
x == 1
} if {
x == 2
} {
x == 3
}`,
expectedErrors: []string{`5:3: rego_parse_error: unexpected if keyword
} if {
^`},
},
}

for _, tc := range tests {
Expand Down

0 comments on commit 89855df

Please sign in to comment.