Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
arp242 committed Jul 4, 2021
1 parent 17bcf8c commit 7d7f51a
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 42 deletions.
17 changes: 13 additions & 4 deletions error.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type Position struct {

func (pe ParseError) Error() string {
msg := pe.Message
if msg == "" {
if msg == "" { // TODO: temporary
msg = pe.err.Error()
}

Expand Down Expand Up @@ -95,11 +95,20 @@ func (pe ParseError) column(lines []string) int {
}

type (
errLexEscape struct{ r rune }
errLexEscape struct{ r rune }
errLexUTF8 struct{ b byte }
errLexInvalidNum struct{ v string }
errLexInvalidDate struct{ v string }
)

func (e errLexEscape) Error() string { return fmt.Sprintf(`invalid escape in string '\%c'`, e.r) }
func (e errLexEscape) Usage() string { return usageEscape }
func (e errLexEscape) Error() string { return fmt.Sprintf(`invalid escape in string '\%c'`, e.r) }
func (e errLexEscape) Usage() string { return usageEscape }
func (e errLexUTF8) Error() string { return fmt.Sprintf("invalid UTF-8 byte: 0x%02x", e.b) }
func (e errLexUTF8) Usage() string { return "" }
func (e errLexInvalidNum) Error() string { return fmt.Sprintf("invalid number: %q", e.v) }
func (e errLexInvalidNum) Usage() string { return "" }
func (e errLexInvalidDate) Error() string { return fmt.Sprintf("invalid date: %q", e.v) }
func (e errLexInvalidDate) Usage() string { return "" }

const usageEscape = `
A '\' inside a "-delimited string is interpreted as an escape character.
Expand Down
36 changes: 1 addition & 35 deletions error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,42 +11,8 @@ import (
tomltest "github.com/BurntSushi/toml-test"
)

/*
func TestDecodeError(t *testing.T) {
file :=
`a = "a"
b = "b"
c = 001 # invalid
`
var s struct {
A, B string
C int
}
_, err := Decode(file, &s)
if err == nil {
t.Fatal("err is nil")
}
var dErr DecodeError
if !errors.As(err, &dErr) {
t.Fatalf("err is not a DecodeError: %T %[1]v", err)
}
want := DecodeError{
Line: 3,
Pos: 17,
LastKey: "c",
Message: `Invalid integer "001": cannot have leading zeroes`,
}
if !reflect.DeepEqual(dErr, want) {
t.Errorf("unexpected data\nhave: %#v\nwant: %#v", dErr, want)
}
}
*/

func TestParseError(t *testing.T) {
return
fsys := tomltest.EmbeddedTests()
ls, err := fs.ReadDir(fsys, "invalid")
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions lex.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ func (lx *lexer) next() (r rune) {

r, w := utf8.DecodeRuneInString(lx.input[lx.pos:])
if r == utf8.RuneError {
lx.errorf("invalid UTF-8 byte at: 0x%02x", lx.input[lx.pos])
lx.errorferr(errLexUTF8{lx.input[lx.pos]})
return utf8.RuneError
}

Expand Down Expand Up @@ -240,7 +240,7 @@ func (lx *lexer) skip(pred func(rune) bool) {
}
}

func (lx *lexer) err(err error) stateFn {
func (lx *lexer) errorferr(err error) stateFn {
//if lx.atEOF {
// return lx.errorfPrevline(format, values...)
//}
Expand Down Expand Up @@ -866,7 +866,7 @@ func lexStringEscape(lx *lexer) stateFn {
case 'U':
return lexLongUnicodeEscape
}
return lx.err(errLexEscape{r})
return lx.errorferr(errLexEscape{r})
}

func lexShortUnicodeEscape(lx *lexer) stateFn {
Expand Down

0 comments on commit 7d7f51a

Please sign in to comment.