diff --git a/decode_test.go b/decode_test.go index f51101f9..8d1f6248 100644 --- a/decode_test.go +++ b/decode_test.go @@ -1,7 +1,6 @@ package toml import ( - "errors" "fmt" "io/ioutil" "os" @@ -710,39 +709,6 @@ func TestDecodeDatetime(t *testing.T) { } } -func TestParseError(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 pErr ParseError - if !errors.As(err, &pErr) { - t.Fatalf("err is not a ParseError: %T %[1]v", err) - } - - want := ParseError{ - Line: 3, - LastKey: "c", - Message: `Invalid integer "001": cannot have leading zeroes`, - } - if !strings.Contains(pErr.Message, want.Message) || - pErr.Line != want.Line || - pErr.LastKey != want.LastKey { - t.Errorf("unexpected data\nhave: %#v\nwant: %#v", pErr, want) - } -} - // errorContains checks if the error message in have contains the text in // want. // diff --git a/error.go b/error.go new file mode 100644 index 00000000..fa9e3f7a --- /dev/null +++ b/error.go @@ -0,0 +1,179 @@ +package toml + +import ( + "fmt" + "strings" +) + +// ParseError is used when there is an error decoding TOML data. +// +// For example invalid TOML syntax, duplicate keys, etc. +type ParseError struct { + Message string // Short technical message. + Usage string // Longer message with usage guidance; may be blank. + Position Position // Position of the error + LastKey string // Last parsed key, may be blank. + + err error + input string +} + +// Position of an error. +type Position struct { + Line int // Line number, starting at 1. + Start int // Start of error, as byte offset starting at 0. + Len int // Lenght in bytes. +} + +func (pe ParseError) Error() string { + msg := pe.Message + if msg == "" { // Error from errorf() + msg = pe.err.Error() + } + + if pe.LastKey == "" { + return fmt.Sprintf("toml: line %d: %s", pe.Position.Line, msg) + } + return fmt.Sprintf("toml: line %d (last key %q): %s", + pe.Position.Line, pe.LastKey, msg) +} + +func (pe ParseError) ExtendedWithUsage() string { + m := pe.Extended() + if u, ok := pe.err.(interface{ Usage() string }); ok && u.Usage() != "" { + return m + "Error help:\n\n " + + strings.ReplaceAll(strings.TrimSpace(u.Usage()), "\n", "\n ") + + "\n" + } + return m +} + +func (pe ParseError) Extended() string { + if pe.input == "" { // Should never happen, but just in case. + return pe.Error() + } + + var ( + lines = strings.Split(pe.input, "\n") + col = pe.column(lines) + b = new(strings.Builder) + ) + + msg := pe.Message + if msg == "" { + msg = pe.err.Error() + } + + // TODO: don't show control characters as literals? This may not show up + // well everywhere. + + if pe.Position.Len == 1 { + fmt.Fprintf(b, "toml: error: %s\n\nAt line %d, column %d:\n\n", + msg, pe.Position.Line, col+1) + } else { + fmt.Fprintf(b, "toml: error: %s\n\nAt line %d, column %d-%d:\n\n", + msg, pe.Position.Line, col, col+pe.Position.Len) + } + if pe.Position.Line > 2 { + fmt.Fprintf(b, "% 7d | %s\n", pe.Position.Line-2, lines[pe.Position.Line-3]) + } + if pe.Position.Line > 1 { + fmt.Fprintf(b, "% 7d | %s\n", pe.Position.Line-1, lines[pe.Position.Line-2]) + } + fmt.Fprintf(b, "% 7d | %s\n", pe.Position.Line, lines[pe.Position.Line-1]) + fmt.Fprintf(b, "% 10s%s%s\n", "", strings.Repeat(" ", col), strings.Repeat("^", pe.Position.Len)) + return b.String() +} + +func (pe ParseError) column(lines []string) int { + var pos, col int + for i := range lines { + ll := len(lines[i]) + 1 // +1 for the removed newline + if pos+ll >= pe.Position.Start { + col = pe.Position.Start - pos + if col < 0 { // Should never happen, but just in case. + col = 0 + } + break + } + pos += ll + } + + return col +} + +type ( + errLexControl struct{ r rune } + errLexEscape struct{ r rune } + errLexUTF8 struct{ b byte } + errLexInvalidNum struct{ v string } + errLexInvalidDate struct{ v string } + errLexInlineTableNL struct{} + errLexStringNL struct{} +) + +func (e errLexControl) Error() string { + return fmt.Sprintf("TOML files cannot contain control characters: '0x%02x'", e.r) +} +func (e errLexControl) Usage() string { return "" } + +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 "" } +func (e errLexInlineTableNL) Error() string { return "newlines not allowed within inline tables" } +func (e errLexInlineTableNL) Usage() string { return usageInlineNewline } +func (e errLexStringNL) Error() string { return "strings cannot contain newlines" } +func (e errLexStringNL) Usage() string { return usageStringNewline } + +const usageEscape = ` +A '\' inside a "-delimited string is interpreted as an escape character. + +The following escape sequences are supported: +\b, \t, \n, \f, \r, \", \\, \uXXXX, and \UXXXXXXXX + +To prevent a '\' from being recognized as an escape character, use: + +- a ' or '''-delimited string; escape characters aren't processed in them; or +- two backslashes to get a single backslash: '\\'. + +If you're trying to add a Windows path (e.g. "C:\Users\martin") then using '/' +instead of '\' will usually also work: "C:/Users/martin". +` + +const usageInlineNewline = ` +Inline tables must always be on a single line: + + table = {key = 42, second = 43} + +It is invalid to split them over multiple lines like so: + + # INVALID + table = { + key = 42, + second = 43 + } + +Use regular for this: + + [table] + key = 42 + second = 43 +` + +const usageStringNewline = ` +Strings must always be on a single line, and cannot span more than one line: + + # INVALID + string = "Hello, + world!" + +Instead use """ or ''' to split strings over multiple lines: + + string = """Hello, + world!""" +` diff --git a/error_test.go b/error_test.go new file mode 100644 index 00000000..fa2c2760 --- /dev/null +++ b/error_test.go @@ -0,0 +1,117 @@ +//go:build go1.16 +// +build go1.16 + +package toml_test + +import ( + "errors" + "fmt" + "io/fs" + "strings" + "testing" + + "github.com/BurntSushi/toml" + tomltest "github.com/BurntSushi/toml/internal/toml-test" +) + +func TestErrorPosition(t *testing.T) { + // Note: take care to use leading spaces (not tabs). + tests := []struct { + test, err string + }{ + {"array/missing-separator.toml", ` +toml: error: expected a comma (',') or array terminator (']'), but got '2' + +At line 1, column 13: + + 1 | wrong = [ 1 2 3 ] + ^`}, + + {"array/no-close-2.toml", ` +toml: error: expected a comma (',') or array terminator (']'), but got end of file + +At line 1, column 10: + + 1 | x = [42 # + ^`}, + } + + fsys := tomltest.EmbeddedTests() + for _, tt := range tests { + t.Run(tt.test, func(t *testing.T) { + input, err := fs.ReadFile(fsys, "invalid/"+tt.test) + if err != nil { + t.Fatal(err) + } + + var x interface{} + _, err = toml.Decode(string(input), &x) + if err == nil { + t.Fatal("err is nil") + } + + var pErr toml.ParseError + if !errors.As(err, &pErr) { + t.Errorf("err is not a ParseError: %T %[1]v", err) + } + + tt.err = tt.err[1:] // Remove first newline. + want := pErr.ExtendedWithUsage() + + if !strings.Contains(want, tt.err) { + t.Fatalf("\nwant:\n%s\nhave:\n%s", tt.err, want) + } + }) + } +} + +// Useful to print all errors, to see if they look alright. +func TestParseError(t *testing.T) { + fsys := tomltest.EmbeddedTests() + err := fs.WalkDir(fsys, ".", func(path string, f fs.DirEntry, err error) error { + if err != nil { + return err + } + if !strings.HasSuffix(path, ".toml") { + return nil + } + if f.Name() != "datetime-no-secs.toml" { + //continue + } + + if f.Name() == "string-multiline-escape-space.toml" || f.Name() == "bad-utf8-at-end.toml" { + return nil + } + + input, err := fs.ReadFile(fsys, path) + if err != nil { + t.Fatal(err) + } + + var x interface{} + _, err = toml.Decode(string(input), &x) + if err == nil { + return nil + } + + var pErr toml.ParseError + if !errors.As(err, &pErr) { + t.Errorf("err is not a ParseError: %T %[1]v", err) + return nil + } + + fmt.Println() + fmt.Println("━━━", path, strings.Repeat("━", 65-len(path))) + fmt.Print(pErr.Error()) + fmt.Println() + fmt.Println(strings.Repeat("–", 70)) + fmt.Print(pErr.Extended()) + fmt.Println(strings.Repeat("–", 70)) + fmt.Print(pErr.ExtendedWithUsage()) + fmt.Println(strings.Repeat("━", 70)) + return nil + }) + if err != nil { + t.Fatal(err) + } +} diff --git a/go.sum b/go.sum deleted file mode 100644 index e69de29b..00000000 diff --git a/lex.go b/lex.go index adc4eb5d..c985abad 100644 --- a/lex.go +++ b/lex.go @@ -59,6 +59,10 @@ const ( type stateFn func(lx *lexer) stateFn +func (p Position) String() string { + return fmt.Sprintf("at line %d; start %d; length %d", p.Line, p.Start, p.Len) +} + type lexer struct { input string start int @@ -67,26 +71,26 @@ type lexer struct { state stateFn items chan item - // Allow for backing up up to four runes. - // This is necessary because TOML contains 3-rune tokens (""" and '''). + // Allow for backing up up to four runes. This is necessary because TOML + // contains 3-rune tokens (""" and '''). prevWidths [4]int - nprev int // how many of prevWidths are in use - // If we emit an eof, we can still back up, but it is not OK to call - // next again. - atEOF bool + nprev int // how many of prevWidths are in use + atEOF bool // If we emit an eof, we can still back up, but it is not OK to call next again. // A stack of state functions used to maintain context. - // The idea is to reuse parts of the state machine in various places. - // For example, values can appear at the top level or within arbitrarily - // nested arrays. The last state on the stack is used after a value has - // been lexed. Similarly for comments. + // + // The idea is to reuse parts of the state machine in various places. For + // example, values can appear at the top level or within arbitrarily nested + // arrays. The last state on the stack is used after a value has been lexed. + // Similarly for comments. stack []stateFn } type item struct { - typ itemType - val string - line int + typ itemType + val string + err error + pos Position } func (lx *lexer) nextItem() item { @@ -96,7 +100,7 @@ func (lx *lexer) nextItem() item { return item default: lx.state = lx.state(lx) - //fmt.Printf(" STATE %-24s current: %-10q stack: %s\n", lx.state, lx.current(), lx.stack) + //fmt.Printf(" STATE %-24s current: %-10q stack: %s\n", lx.state, lx.current(), lx.stack) } } } @@ -105,9 +109,9 @@ func lex(input string) *lexer { lx := &lexer{ input: input, state: lexTop, - line: 1, items: make(chan item, 10), stack: make([]stateFn, 0, 10), + line: 1, } return lx } @@ -129,13 +133,25 @@ func (lx *lexer) current() string { return lx.input[lx.start:lx.pos] } +func (lx lexer) getPos() Position { + p := Position{ + Line: lx.line, + Start: lx.start, + Len: lx.pos - lx.start, + } + if p.Len <= 0 { + p.Len = 1 + } + return p +} + func (lx *lexer) emit(typ itemType) { - lx.items <- item{typ, lx.current(), lx.line} + lx.items <- item{typ: typ, pos: lx.getPos(), val: lx.current()} lx.start = lx.pos } func (lx *lexer) emitTrim(typ itemType) { - lx.items <- item{typ, strings.TrimSpace(lx.current()), lx.line} + lx.items <- item{typ: typ, pos: lx.getPos(), val: strings.TrimSpace(lx.current())} lx.start = lx.pos } @@ -160,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 position %d (line %d): 0x%02x", lx.pos, lx.line, lx.input[lx.pos]) + lx.error(errLexUTF8{lx.input[lx.pos]}) return utf8.RuneError } @@ -188,6 +204,7 @@ func (lx *lexer) backup() { lx.prevWidths[1] = lx.prevWidths[2] lx.prevWidths[2] = lx.prevWidths[3] lx.nprev-- + lx.pos -= w if lx.pos < len(lx.input) && lx.input[lx.pos] == '\n' { lx.line-- @@ -223,18 +240,58 @@ func (lx *lexer) skip(pred func(rune) bool) { } } -// errorf stops all lexing by emitting an error and returning `nil`. +// error stops all lexing by emitting an error and returning `nil`. +// // Note that any value that is a character is escaped if it's a special // character (newlines, tabs, etc.). +func (lx *lexer) error(err error) stateFn { + if lx.atEOF { + return lx.errorPrevLine(err) + } + lx.items <- item{typ: itemError, pos: lx.getPos(), err: err} + return nil +} + +// errorfPrevline is like error(), but sets the position to the last column of +// the previous line. +// +// This is so that unexpected EOF or NL errors don't show on a new blank line. +func (lx *lexer) errorPrevLine(err error) stateFn { + pos := lx.getPos() + pos.Line-- + pos.Len = 1 + pos.Start = lx.pos - 1 + lx.items <- item{typ: itemError, pos: pos, err: err} + return nil +} + +// errorPos is like error(), but allows explicitly setting the position. +func (lx *lexer) errorPos(start, length int, err error) stateFn { + pos := lx.getPos() + pos.Start = start + pos.Len = length + lx.items <- item{typ: itemError, pos: pos, err: err} + return nil +} + +// errorf is like error, and creates a new error. func (lx *lexer) errorf(format string, values ...interface{}) stateFn { - lx.items <- item{ - itemError, - fmt.Sprintf(format, values...), - lx.line, + if lx.atEOF { + pos := lx.getPos() + pos.Line-- + pos.Len = 1 + pos.Start = lx.pos - 1 + lx.items <- item{typ: itemError, pos: pos, err: fmt.Errorf(format, values...)} + return nil } + lx.items <- item{typ: itemError, pos: lx.getPos(), err: fmt.Errorf(format, values...)} return nil } +func (lx *lexer) errorControlChar(cc rune) stateFn { + return lx.errorPos(lx.pos-1, 1, errLexControl{cc}) +} + // lexTop consumes elements at the top level of TOML data. func lexTop(lx *lexer) stateFn { r := lx.next() @@ -540,8 +597,7 @@ func lexArrayValue(lx *lexer) stateFn { // the next value (or the end of the array): it ignores whitespace and newlines // and expects either a ',' or a ']'. func lexArrayValueEnd(lx *lexer) stateFn { - r := lx.next() - switch { + switch r := lx.next(); { case isWhitespace(r) || isNL(r): return lexSkip(lx, lexArrayValueEnd) case r == commentStart: @@ -552,10 +608,11 @@ func lexArrayValueEnd(lx *lexer) stateFn { return lexArrayValue // move on to the next value case r == arrayEnd: return lexArrayEnd + default: + return lx.errorf( + "expected a comma (',') or array terminator (']'), but got %s", + runeOrEOF(r)) } - return lx.errorf( - "expected a comma or array terminator %q, but got %s instead", - arrayEnd, runeOrEOF(r)) } // lexArrayEnd finishes the lexing of an array. @@ -574,7 +631,7 @@ func lexInlineTableValue(lx *lexer) stateFn { case isWhitespace(r): return lexSkip(lx, lexInlineTableValue) case isNL(r): - return lx.errorf("newlines not allowed within inline tables") + return lx.errorPrevLine(errLexInlineTableNL{}) case r == commentStart: lx.push(lexInlineTableValue) return lexCommentStart @@ -596,7 +653,7 @@ func lexInlineTableValueEnd(lx *lexer) stateFn { case isWhitespace(r): return lexSkip(lx, lexInlineTableValueEnd) case isNL(r): - return lx.errorf("newlines not allowed within inline tables") + return lx.errorPrevLine(errLexInlineTableNL{}) case r == commentStart: lx.push(lexInlineTableValueEnd) return lexCommentStart @@ -639,9 +696,9 @@ func lexString(lx *lexer) stateFn { case r == eof: return lx.errorf(`unexpected EOF; expected '"'`) case isControl(r) || r == '\r': - return lx.errorf("control characters are not allowed inside strings: '0x%02x'", r) + return lx.errorControlChar(r) case isNL(r): - return lx.errorf("strings cannot contain newlines") + return lx.errorPrevLine(errLexStringNL{}) case r == '\\': lx.push(lexString) return lexStringEscape @@ -664,7 +721,7 @@ func lexMultilineString(lx *lexer) stateFn { return lx.errorf(`unexpected EOF; expected '"""'`) case '\r': if lx.peek() != '\n' { - return lx.errorf("control characters are not allowed inside strings: '0x%02x'", r) + return lx.errorControlChar(r) } return lexMultilineString case '\\': @@ -702,7 +759,7 @@ func lexMultilineString(lx *lexer) stateFn { } if isControl(r) { - return lx.errorf("control characters are not allowed inside strings: '0x%02x'", r) + return lx.errorControlChar(r) } return lexMultilineString } @@ -715,9 +772,9 @@ func lexRawString(lx *lexer) stateFn { case r == eof: return lx.errorf(`unexpected EOF; expected "'"`) case isControl(r) || r == '\r': - return lx.errorf("control characters are not allowed inside strings: '0x%02x'", r) + return lx.errorControlChar(r) case isNL(r): - return lx.errorf("strings cannot contain newlines") + return lx.errorPrevLine(errLexStringNL{}) case r == rawStringEnd: lx.backup() lx.emit(itemRawString) @@ -738,7 +795,7 @@ func lexMultilineRawString(lx *lexer) stateFn { return lx.errorf(`unexpected EOF; expected "'''"`) case '\r': if lx.peek() != '\n' { - return lx.errorf("control characters are not allowed inside strings: '0x%02x'", r) + return lx.errorControlChar(r) } return lexMultilineRawString case rawStringEnd: @@ -774,7 +831,7 @@ func lexMultilineRawString(lx *lexer) stateFn { } if isControl(r) { - return lx.errorf("control characters are not allowed inside strings: '0x%02x'", r) + return lx.errorControlChar(r) } return lexMultilineRawString } @@ -817,8 +874,7 @@ func lexStringEscape(lx *lexer) stateFn { case 'U': return lexLongUnicodeEscape } - return lx.errorf("invalid escape character %q; only the following escape characters are allowed: "+ - `\b, \t, \n, \f, \r, \", \\, \uXXXX, and \UXXXXXXXX`, r) + return lx.error(errLexEscape{r}) } func lexShortUnicodeEscape(lx *lexer) stateFn { @@ -1109,7 +1165,7 @@ func lexComment(lx *lexer) stateFn { lx.emit(itemText) return lx.pop() case isControl(r): - return lx.errorf("control characters are not allowed inside comments: '0x%02x'", r) + return lx.errorControlChar(r) default: return lexComment } diff --git a/parse.go b/parse.go index d9ae5db9..4fcb27dd 100644 --- a/parse.go +++ b/parse.go @@ -1,7 +1,6 @@ package toml import ( - "errors" "fmt" "strconv" "strings" @@ -19,28 +18,16 @@ type parser struct { ordered []Key // List of keys in the order that they appear in the TOML data. context Key // Full key for the current hash in scope. currentKey string // Base key name for everything except hashes. - approxLine int // Rough approximation of line number + pos Position // Position implicits map[string]bool // Record implied keys (e.g. 'key.group.names'). } -// ParseError is used when a file can't be parsed: for example invalid integer -// literals, duplicate keys, etc. -type ParseError struct { - Message string - Line int - LastKey string -} - -func (pe ParseError) Error() string { - return fmt.Sprintf("Near line %d (last key parsed '%s'): %s", - pe.Line, pe.LastKey, pe.Message) -} - func parse(data string) (p *parser, err error) { defer func() { if r := recover(); r != nil { - var ok bool - if err, ok = r.(ParseError); ok { + if pErr, ok := r.(ParseError); ok { + pErr.input = data + err = pErr return } panic(r) @@ -60,8 +47,12 @@ func parse(data string) (p *parser, err error) { if len(data) < 6 { ex = len(data) } - if strings.ContainsRune(data[:ex], 0) { - return nil, errors.New("files cannot contain NULL bytes; probably using UTF-16; TOML files must be UTF-8") + if i := strings.IndexRune(data[:ex], 0); i > -1 { + return nil, ParseError{ + Message: "files cannot contain NULL bytes; probably using UTF-16; TOML files must be UTF-8", + Position: Position{Line: 1, Start: i, Len: 1}, + input: data, + } } p = &parser{ @@ -82,12 +73,19 @@ func parse(data string) (p *parser, err error) { return p, nil } +func (p *parser) panicItemf(it item, format string, v ...interface{}) { + panic(ParseError{ + Message: fmt.Sprintf(format, v...), + Position: it.pos, + LastKey: p.current(), + }) +} + func (p *parser) panicf(format string, v ...interface{}) { - msg := fmt.Sprintf(format, v...) panic(ParseError{ - Message: msg, - Line: p.approxLine, - LastKey: p.current(), + Message: fmt.Sprintf(format, v...), + Position: p.pos, + LastKey: p.current(), }) } @@ -95,11 +93,25 @@ func (p *parser) next() item { it := p.lx.nextItem() //fmt.Printf("ITEM %-18s line %-3d │ %q\n", it.typ, it.line, it.val) if it.typ == itemError { - p.panicf("%s", it.val) + if it.err != nil { + panic(ParseError{ + Position: it.pos, + LastKey: p.current(), + err: it.err, + }) + } + + p.panicItemf(it, "%s", it.val) } return it } +func (p *parser) nextPos() item { + it := p.next() + p.pos = it.pos + return it +} + func (p *parser) bug(format string, v ...interface{}) { panic(fmt.Sprintf("BUG: "+format+"\n\n", v...)) } @@ -119,11 +131,9 @@ func (p *parser) assertEqual(expected, got itemType) { func (p *parser) topLevel(item item) { switch item.typ { case itemCommentStart: // # .. - p.approxLine = item.line p.expect(itemText) case itemTableStart: // [ .. ] - name := p.next() - p.approxLine = name.line + name := p.nextPos() var key Key for ; name.typ != itemTableEnd && name.typ != itemEOF; name = p.next() { @@ -135,8 +145,7 @@ func (p *parser) topLevel(item item) { p.setType("", tomlHash) p.ordered = append(p.ordered, key) case itemArrayTableStart: // [[ .. ]] - name := p.next() - p.approxLine = name.line + name := p.nextPos() var key Key for ; name.typ != itemArrayTableEnd && name.typ != itemEOF; name = p.next() { @@ -150,8 +159,7 @@ func (p *parser) topLevel(item item) { case itemKeyStart: // key = .. outerContext := p.context /// Read all the key parts (e.g. 'a' and 'b' in 'a.b') - k := p.next() - p.approxLine = k.line + k := p.nextPos() var key Key for ; k.typ != itemKeyEnd && k.typ != itemEOF; k = p.next() { key = append(key, p.keyString(k)) @@ -206,9 +214,9 @@ var datetimeRepl = strings.NewReplacer( func (p *parser) value(it item, parentIsArray bool) (interface{}, tomlType) { switch it.typ { case itemString: - return p.replaceEscapes(it.val), p.typeOfPrimitive(it) + return p.replaceEscapes(it, it.val), p.typeOfPrimitive(it) case itemMultilineString: - return p.replaceEscapes(stripFirstNewline(stripEscapedNewlines(it.val))), p.typeOfPrimitive(it) + return p.replaceEscapes(it, stripFirstNewline(stripEscapedNewlines(it.val))), p.typeOfPrimitive(it) case itemRawString: return it.val, p.typeOfPrimitive(it) case itemRawMultilineString: @@ -240,10 +248,10 @@ func (p *parser) value(it item, parentIsArray bool) (interface{}, tomlType) { func (p *parser) valueInteger(it item) (interface{}, tomlType) { if !numUnderscoresOK(it.val) { - p.panicf("Invalid integer %q: underscores must be surrounded by digits", it.val) + p.panicItemf(it, "Invalid integer %q: underscores must be surrounded by digits", it.val) } if numHasLeadingZero(it.val) { - p.panicf("Invalid integer %q: cannot have leading zeroes", it.val) + p.panicItemf(it, "Invalid integer %q: cannot have leading zeroes", it.val) } num, err := strconv.ParseInt(it.val, 0, 64) @@ -254,7 +262,7 @@ func (p *parser) valueInteger(it item) (interface{}, tomlType) { // So mark the former as a bug but the latter as a legitimate user // error. if e, ok := err.(*strconv.NumError); ok && e.Err == strconv.ErrRange { - p.panicf("Integer '%s' is out of the range of 64-bit signed integers.", it.val) + p.panicItemf(it, "Integer '%s' is out of the range of 64-bit signed integers.", it.val) } else { p.bug("Expected integer value, but got '%s'.", it.val) } @@ -272,18 +280,18 @@ func (p *parser) valueFloat(it item) (interface{}, tomlType) { }) for _, part := range parts { if !numUnderscoresOK(part) { - p.panicf("Invalid float %q: underscores must be surrounded by digits", it.val) + p.panicItemf(it, "Invalid float %q: underscores must be surrounded by digits", it.val) } } if len(parts) > 0 && numHasLeadingZero(parts[0]) { - p.panicf("Invalid float %q: cannot have leading zeroes", it.val) + p.panicItemf(it, "Invalid float %q: cannot have leading zeroes", it.val) } if !numPeriodsOK(it.val) { // As a special case, numbers like '123.' or '1.e2', // which are valid as far as Go/strconv are concerned, // must be rejected because TOML says that a fractional // part consists of '.' followed by 1+ digits. - p.panicf("Invalid float %q: '.' must be followed by one or more digits", it.val) + p.panicItemf(it, "Invalid float %q: '.' must be followed by one or more digits", it.val) } val := strings.Replace(it.val, "_", "", -1) if val == "+nan" || val == "-nan" { // Go doesn't support this, but TOML spec does. @@ -292,9 +300,9 @@ func (p *parser) valueFloat(it item) (interface{}, tomlType) { num, err := strconv.ParseFloat(val, 64) if err != nil { if e, ok := err.(*strconv.NumError); ok && e.Err == strconv.ErrRange { - p.panicf("Float '%s' is out of the range of 64-bit IEEE-754 floating-point numbers.", it.val) + p.panicItemf(it, "Float '%s' is out of the range of 64-bit IEEE-754 floating-point numbers.", it.val) } else { - p.panicf("Invalid float value: %q", it.val) + p.panicItemf(it, "Invalid float value: %q", it.val) } } return num, p.typeOfPrimitive(it) @@ -325,7 +333,7 @@ func (p *parser) valueDatetime(it item) (interface{}, tomlType) { } } if !ok { - p.panicf("Invalid TOML Datetime: %q.", it.val) + p.panicItemf(it, "Invalid TOML Datetime: %q.", it.val) } return t, p.typeOfPrimitive(it) } @@ -373,8 +381,7 @@ func (p *parser) valueInlineTable(it item, parentIsArray bool) (interface{}, tom } /// Read all key parts. - k := p.next() - p.approxLine = k.line + k := p.nextPos() var key Key for ; k.typ != itemKeyEnd && k.typ != itemEOF; k = p.next() { key = append(key, p.keyString(k)) @@ -503,7 +510,7 @@ func (p *parser) addContext(key Key, array bool) { if hash, ok := hashContext[k].([]map[string]interface{}); ok { hashContext[k] = append(hash, make(map[string]interface{})) } else { - p.panicf("Key '%s' was already created and cannot be used as an array.", keyContext) + p.panicf("Key '%s' was already created and cannot be used as an array.", key) } } else { p.setValue(key[len(key)-1], make(map[string]interface{})) @@ -662,7 +669,7 @@ func stripEscapedNewlines(s string) string { return strings.Join(split, "") } -func (p *parser) replaceEscapes(str string) string { +func (p *parser) replaceEscapes(it item, str string) string { var replaced []rune s := []byte(str) r := 0 @@ -683,7 +690,7 @@ func (p *parser) replaceEscapes(str string) string { p.bug("Expected valid escape code after \\, but got %q.", s[r]) return "" case ' ', '\t': - p.panicf("invalid escape: '\\%c'", s[r]) + p.panicItemf(it, "invalid escape: '\\%c'", s[r]) return "" case 'b': replaced = append(replaced, rune(0x0008)) @@ -710,14 +717,14 @@ func (p *parser) replaceEscapes(str string) string { // At this point, we know we have a Unicode escape of the form // `uXXXX` at [r, r+5). (Because the lexer guarantees this // for us.) - escaped := p.asciiEscapeToUnicode(s[r+1 : r+5]) + escaped := p.asciiEscapeToUnicode(it, s[r+1:r+5]) replaced = append(replaced, escaped) r += 5 case 'U': // At this point, we know we have a Unicode escape of the form // `uXXXX` at [r, r+9). (Because the lexer guarantees this // for us.) - escaped := p.asciiEscapeToUnicode(s[r+1 : r+9]) + escaped := p.asciiEscapeToUnicode(it, s[r+1:r+9]) replaced = append(replaced, escaped) r += 9 } @@ -725,15 +732,14 @@ func (p *parser) replaceEscapes(str string) string { return string(replaced) } -func (p *parser) asciiEscapeToUnicode(bs []byte) rune { +func (p *parser) asciiEscapeToUnicode(it item, bs []byte) rune { s := string(bs) hex, err := strconv.ParseUint(strings.ToLower(s), 16, 32) if err != nil { - p.bug("Could not parse '%s' as a hexadecimal number, but the "+ - "lexer claims it's OK: %s", s, err) + p.bug("Could not parse '%s' as a hexadecimal number, but the lexer claims it's OK: %s", s, err) } if !utf8.ValidRune(rune(hex)) { - p.panicf("Escaped character '\\u%s' is not valid UTF-8.", s) + p.panicItemf(it, "Escaped character '\\u%s' is not valid UTF-8.", s) } return rune(hex) }