From 5875df6602db2fdb504bf92981523262bfb28cb3 Mon Sep 17 00:00:00 2001 From: jidicula Date: Mon, 4 Oct 2021 20:21:48 -0400 Subject: [PATCH] fix(array test): Fail with missing separator 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/go-toml#613 --- parser.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/parser.go b/parser.go index b86904ae..00fca146 100644 --- a/parser.go +++ b/parser.go @@ -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) @@ -439,6 +440,8 @@ 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. @@ -446,7 +449,9 @@ func (p *parser) parseValArray(b []byte) (ast.Reference, []byte, error) { 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 {