Skip to content

Commit a339e1f

Browse files
committed
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 | ^
1 parent 14f5b21 commit a339e1f

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

error_test.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,23 @@ At line 2, column 5-15:
5151
1 | # Date cannot end with trailing T
5252
2 | d = 2006-01-30T
5353
^^^^^^^^^^^`},
54-
}
5554

55+
{"key/without-value-1.toml", `
56+
toml: error: expected '.' or '=', but got '\n' instead
57+
58+
At line 1, column 4:
59+
60+
1 | key
61+
^`},
62+
// Position looks wonky, but test has trailing space, so it's correct.
63+
{"key/without-value-2.toml", `
64+
toml: error: expected value but found '\n' instead
65+
66+
At line 1, column 7:
67+
68+
1 | key =
69+
^`},
70+
}
5671
fsys := tomltest.EmbeddedTests()
5772
for _, tt := range tests {
5873
t.Run(tt.test, func(t *testing.T) {

lex.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,9 @@ func lexKeyEnd(lx *lexer) stateFn {
492492
lx.emit(itemKeyEnd)
493493
return lexSkip(lx, lexValue)
494494
default:
495+
if r == '\n' {
496+
return lx.errorPrevLine(fmt.Errorf("expected '.' or '=', but got %q instead", r))
497+
}
495498
return lx.errorf("expected '.' or '=', but got %q instead", r)
496499
}
497500
}
@@ -560,6 +563,9 @@ func lexValue(lx *lexer) stateFn {
560563
if r == eof {
561564
return lx.errorf("unexpected EOF; expected value")
562565
}
566+
if r == '\n' {
567+
return lx.errorPrevLine(fmt.Errorf("expected value but found %q instead", r))
568+
}
563569
return lx.errorf("expected value but found %q instead", r)
564570
}
565571

0 commit comments

Comments
 (0)