Skip to content

Commit

Permalink
fix(array test): Fail with missing separator
Browse files Browse the repository at this point in the history
The toml-test generated test for an array with missing separators was
failing since the parser wasn't noticing missing separators.

This change leverages the zero value of `ast.Reference` (an int). The
var is now declared outside the while block, and re-zeroed if the
token is not a non-trailing comma.

The parser checks if the previous value of `valueRef` is zero. The
only time this check would pass is when the previous byte in the array
was a whitespace, which would only be the case when an array is
missing a separator.

Resolves: part of pelletier#613
  • Loading branch information
jidicula committed Oct 8, 2021
1 parent 62acca2 commit 5875df6
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,7 @@ func (p *parser) parseValArray(b []byte) (ast.Reference, []byte, error) {

var lastChild ast.Reference

var valueRef ast.Reference
var err error
for len(b) > 0 {
b, err = p.parseOptionalWhitespaceCommentNewline(b)
Expand All @@ -439,14 +440,18 @@ func (p *parser) parseValArray(b []byte) (ast.Reference, []byte, error) {
if err != nil {
return parent, nil, err
}
} else {
valueRef = 0
}

// TOML allows trailing commas in arrays.
if len(b) > 0 && b[0] == ']' {
break
}

var valueRef ast.Reference
if !first && valueRef == 0 {
return parent, nil, newDecodeError(b[0:1], "array elements must be separated by commas")
}

valueRef, b, err = p.parseVal(b)
if err != nil {
Expand Down

0 comments on commit 5875df6

Please sign in to comment.