From b1484cb690d0000fcd53659616ae41214701541a Mon Sep 17 00:00:00 2001 From: Maurice Makaay Date: Thu, 27 Jun 2019 22:54:54 +0000 Subject: [PATCH] Slight improvement for the document-level error in case no key or table was found. --- parse/document.go | 4 +++- parse/{comment_test.go => document_test.go} | 11 ++++++++++- 2 files changed, 13 insertions(+), 2 deletions(-) rename parse/{comment_test.go => document_test.go} (68%) diff --git a/parse/document.go b/parse/document.go index 50504a4..22c577d 100644 --- a/parse/document.go +++ b/parse/document.go @@ -21,8 +21,10 @@ func (t *parser) startDocument(p *parse.API) { p.Handle(t.startTable) case p.Peek(detectKey): p.Handle(t.startKeyValuePair) + case p.Accept(a.EndOfFile): + p.Stop() default: - p.ExpectEndOfFile() + p.Expected("key/value pair, table or array of tables") return } if p.IsStoppedOrInError() { diff --git a/parse/comment_test.go b/parse/document_test.go similarity index 68% rename from parse/comment_test.go rename to parse/document_test.go index 3b6a4e6..5cd49a0 100644 --- a/parse/comment_test.go +++ b/parse/document_test.go @@ -6,7 +6,7 @@ import ( func TestComment(t *testing.T) { for _, test := range []parseTest{ - {``, `{}`, `unexpected end of file (expected comment) at start of file`}, + {``, `{}`, ``}, {`#`, `{}`, ``}, {`# `, `{}`, ``}, {`# with data`, `{}`, ``}, @@ -20,3 +20,12 @@ func TestComment(t *testing.T) { testParse(t, p, p.startDocument, test) } } + +func TestInvalidDocument(t *testing.T) { + for _, test := range []parseTest{ + {`!false!`, `{}`, `unexpected input (expected key/value pair, table or array of tables) at start of file`}, + } { + p := newParser() + testParse(t, p, p.startDocument, test) + } +}