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

ast/parser: fresh wildcards for chained function heads #5426

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 6 additions & 3 deletions ast/parser.go
Expand Up @@ -660,9 +660,7 @@ func (p *Parser) parseRules() []*Rule {

rule.Location.Text = p.s.Text(rule.Location.Offset, p.s.lastEnd)

var rules []*Rule

rules = append(rules, &rule)
rules := []*Rule{&rule}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a small, but good refactoring. 👍


for p.s.tok == tokens.LBrace {

Expand All @@ -688,6 +686,11 @@ func (p *Parser) parseRules() []*Rule {
// rule's head AST but have their location
// set to the rule body.
next.Head = rule.Head.Copy()
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())
}
}
setLocRecursive(next.Head, loc)

rules = append(rules, &next)
Expand Down
20 changes: 20 additions & 0 deletions ast/parser_test.go
Expand Up @@ -1658,6 +1658,26 @@ func TestRule(t *testing.T) {
Body: MustParseBody(`true`),
},
})

name := Var("f")
ref := Ref{VarTerm("f")}
tr := BooleanTerm(true)
head := func(v string) *Head { return &Head{Name: name, Reference: ref, Value: tr, Args: []*Term{VarTerm(v)}} }
assertParseModule(t, "wildcard in chained function heads", `package test
f(_) { true } { true }
`, &Module{
Package: MustParsePackage(`package test`),
Rules: []*Rule{
{
Head: head("$0"),
Body: MustParseBody("true"),
},
{
Head: head("$1"),
Body: MustParseBody("true"),
},
},
})
}

func TestRuleContains(t *testing.T) {
Expand Down