Skip to content

Commit

Permalink
Correct position on invalid files ending without value
Browse files Browse the repository at this point in the history
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 |
                ^
  • Loading branch information
arp242 committed May 26, 2024
1 parent 14f5b21 commit a339e1f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
17 changes: 16 additions & 1 deletion error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
6 changes: 6 additions & 0 deletions lex.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down Expand Up @@ -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)
}

Expand Down

0 comments on commit a339e1f

Please sign in to comment.