Skip to content

Commit

Permalink
ast/parser: Remove early MHS return in infix parsing. (open-policy-ag…
Browse files Browse the repository at this point in the history
…ent#4773)

This parser change allows the parser to properly fall through to the RHS
case where it was not doing so before, due to returning early during MHS
parsing in the parseTermIn() function.

Fixes open-policy-agent#4672.

Signed-off-by: Philip Conrad <philipaconrad@gmail.com>
  • Loading branch information
philipaconrad committed Jun 14, 2022
1 parent 7fa701c commit dbeb2d6
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 1 deletion.
1 change: 0 additions & 1 deletion ast/parser.go
Expand Up @@ -1106,7 +1106,6 @@ func (p *Parser) parseTermIn(lhs *Term, keyVal bool, offset int) *Term {
}
}
p.restore(s)
return nil
}
if op := p.parseTermOpName(Member.Ref(), tokens.In); op != nil {
if rhs := p.parseTermRelation(nil, p.s.loc.Offset); rhs != nil {
Expand Down
91 changes: 91 additions & 0 deletions ast/parser_test.go
Expand Up @@ -2400,6 +2400,97 @@ func TestNoMatchError(t *testing.T) {
}
}

func TestBraceBracketParenMatchingErrors(t *testing.T) {
// Checks to prevent regression on issue #4672.
// Error location is important here, which is why we check
// the error strings directly.
tests := []struct {
note string
err string
input string
}{
{
note: "Unmatched ')' case",
err: `1 error occurred: test.rego:4: rego_parse_error: unexpected , token: expected \n or ; or }
y := contains("a"), "b")
^`,
input: `package test
p {
x := 5
y := contains("a"), "b")
}`,
},
{
note: "Unmatched '}' case",
err: `1 error occurred: test.rego:4: rego_parse_error: unexpected , token: expected \n or ; or }
y := {"a", "b", "c"}, "a"}
^`,
input: `package test
p {
x := 5
y := {"a", "b", "c"}, "a"}
}`,
},
{
note: "Unmatched ']' case",
err: `1 error occurred: test.rego:4: rego_parse_error: unexpected , token: expected \n or ; or }
y := ["a", "b", "c"], "a"]
^`,
input: `package test
p {
x := 5
y := ["a", "b", "c"], "a"]
}`,
},
{
note: "Unmatched '(' case",
err: `1 error occurred: test.rego:5: rego_parse_error: unexpected } token: expected "," or ")"
}
^`,
input: `package test
p {
x := 5
y := contains("a", "b"
}`,
},
{
note: "Unmatched '{' case",

err: `1 error occurred: test.rego:5: rego_parse_error: unexpected eof token: expected \n or ; or }
}
^`,
input: `package test
p {
x := 5
y := {{"a", "b", "c"}, "a"
}`,
},
{
note: "Unmatched '[' case",
err: `1 error occurred: test.rego:5: rego_parse_error: unexpected } token: expected "," or "]"
}
^`,
input: `package test
p {
x := 5
y := [["a", "b", "c"], "a"
}`,
},
}

for _, tc := range tests {
t.Run(tc.note, func(t *testing.T) {
_, err := ParseModule("test.rego", tc.input)
if err == nil {
t.Fatal("Expected error")
}
if tc.err != "" && tc.err != err.Error() {
t.Fatalf("Expected error string %q but got: %q", tc.err, err.Error())
}
})
}
}

func TestParseErrorDetails(t *testing.T) {

tests := []struct {
Expand Down

0 comments on commit dbeb2d6

Please sign in to comment.