Skip to content

Commit

Permalink
Fix blanket "unexpected assign token" error message / usability issue
Browse files Browse the repository at this point in the history
This one has been among my top annoyances, and thanks to @johanfylling,
it was easy to finally track down. Had to update a few tests, but not too
many.

Fixes open-policy-agent#6563

Signed-off-by: Anders Eknert <anders@styra.com>
  • Loading branch information
anderseknert committed May 31, 2024
1 parent f2ffbd6 commit 87d373e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
4 changes: 2 additions & 2 deletions ast/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -943,12 +943,12 @@ func (p *Parser) parseHead(defaultRule bool) (*Head, bool) {
p.illegal("expected rule value term (e.g., %s[%s] = <VALUE> { ... })", name, head.Key)
}
case tokens.Assign:
s := p.save()
//s := p.save()
p.scan()
head.Assign = true
head.Value = p.parseTermInfixCall()
if head.Value == nil {
p.restore(s)
//p.restore(s)
switch {
case len(head.Args) > 0:
p.illegal("expected function value term (e.g., %s(...) := <VALUE> { ... })", name)
Expand Down
34 changes: 29 additions & 5 deletions ast/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2191,13 +2191,37 @@ func TestRule(t *testing.T) {
// TODO: expect expressions instead?
assertParseErrorContains(t, "empty body", `f(_) = y {}`, "rego_parse_error: found empty body")
assertParseErrorContains(t, "empty rule body", "p {}", "rego_parse_error: found empty body")
assertParseErrorContains(t, "unmatched braces", `f(x) = y { trim(x, ".", y) `, `rego_parse_error: unexpected eof token: expected \n or ; or }`)
assertParseErrorContains(t, "unmatched braces", `f(x) = y { trim(x, ".", y) `, `rego_parse_error: unexpected eof token: expected \n or ; or }
f(x) = y { trim(x, ".", y)
^`)

assertParseErrorContains(t, "no output", `f(_) = { "foo" = "bar" }`, "rego_parse_error: unexpected eq token: expected rule value term")
assertParseErrorContains(t, "no output", `f(_) := { "foo" = "bar" }`, "rego_parse_error: unexpected assign token: expected function value term")
assertParseErrorContains(t, "no output", `f := { "foo" = "bar" }`, "rego_parse_error: unexpected assign token: expected rule value term")
assertParseErrorContains(t, "no output", `f[_] := { "foo" = "bar" }`, "rego_parse_error: unexpected assign token: expected rule value term")
assertParseErrorContains(t, "no output", `default f :=`, "rego_parse_error: unexpected assign token: expected default rule value term")

assertParseErrorContains(t, "no output", `f(_) := { "foo" = "bar" }`, `rego_parse_error: unexpected eq token: non-terminated set
f(_) := { "foo" = "bar" }
^
1:17: rego_parse_error: unexpected eq token: expected function value term (e.g., f(...) := <VALUE> { ... })
f(_) := { "foo" = "bar" }
^`)

assertParseErrorContains(t, "no output", `f := { "foo" = "bar" }`, `rego_parse_error: unexpected eq token: non-terminated set
f := { "foo" = "bar" }
^
1:14: rego_parse_error: unexpected eq token: expected rule value term (e.g., f := <VALUE> { ... })
f := { "foo" = "bar" }
^`)
assertParseErrorContains(t, "no output", `f[_] := { "foo" = "bar" }`, `rego_parse_error: unexpected eq token: non-terminated set
f[_] := { "foo" = "bar" }
^
1:17: rego_parse_error: unexpected eq token: expected rule value term (e.g., f[_] := <VALUE> { ... })
f[_] := { "foo" = "bar" }
^`)
assertParseErrorContains(t, "no output", `default f :=`, `rego_parse_error: unexpected eof token
default f :=
^
1:12: rego_parse_error: unexpected eof token: expected default rule value term (e.g., default f := <VALUE>)
default f :=
^`)

// TODO(tsandall): improve error checking here. This is a common mistake
// and the current error message is not very good. Need to investigate if the
Expand Down

0 comments on commit 87d373e

Please sign in to comment.