From a339e1f7089ced06bae1eaf374cdfb950e92d2e8 Mon Sep 17 00:00:00 2001 From: Martin Tournoij Date: Sun, 26 May 2024 20:31:03 +0100 Subject: [PATCH] Correct position on invalid files ending without value Previously it would be on the next line: % tomlv <(print 'a=') Error in '/proc/self/fd/11': toml: error: expected value but found '\n' instead At line 2, column 3: 1 | a= 2 | ^ --- error_test.go | 17 ++++++++++++++++- lex.go | 6 ++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/error_test.go b/error_test.go index 4fd7250c..d14283e3 100644 --- a/error_test.go +++ b/error_test.go @@ -51,8 +51,23 @@ At line 2, column 5-15: 1 | # Date cannot end with trailing T 2 | d = 2006-01-30T ^^^^^^^^^^^`}, - } + {"key/without-value-1.toml", ` +toml: error: expected '.' or '=', but got '\n' instead + +At line 1, column 4: + + 1 | key + ^`}, + // Position looks wonky, but test has trailing space, so it's correct. + {"key/without-value-2.toml", ` +toml: error: expected value but found '\n' instead + +At line 1, column 7: + + 1 | key = + ^`}, + } fsys := tomltest.EmbeddedTests() for _, tt := range tests { t.Run(tt.test, func(t *testing.T) { diff --git a/lex.go b/lex.go index a1016d98..6878d9d6 100644 --- a/lex.go +++ b/lex.go @@ -492,6 +492,9 @@ func lexKeyEnd(lx *lexer) stateFn { lx.emit(itemKeyEnd) return lexSkip(lx, lexValue) default: + if r == '\n' { + return lx.errorPrevLine(fmt.Errorf("expected '.' or '=', but got %q instead", r)) + } return lx.errorf("expected '.' or '=', but got %q instead", r) } } @@ -560,6 +563,9 @@ func lexValue(lx *lexer) stateFn { if r == eof { return lx.errorf("unexpected EOF; expected value") } + if r == '\n' { + return lx.errorPrevLine(fmt.Errorf("expected value but found %q instead", r)) + } return lx.errorf("expected value but found %q instead", r) }