package decoderimport ()// The syntax errors are the ones of encoding/json of the Go version, whose messages and offsets differ between Go// 1.27, which is made of encoding/json/v2, and the earlier versions ( see syntax_error_legacy.go and// syntax_error_v2.go ).// syntaxWhere is where in the grammar a byte is which the grammar doesn't have there.type syntaxWhere uint8const ( whereValue syntaxWhere = iota whereKey whereAfterKey whereAfterPair whereAfterElement whereAfterTop whereString whereNumber whereFraction whereExponent)// isEnd reports whether cursor is the nul byte which ends buf, the input followed by it.func isEnd( []byte, int64) bool {return == int64(len())-1 && [] == nul}// endError returns the error of the end of the input at the total offset at.func endError( int64) error {returnerrors.ErrSyntaxAtEnd("unexpected end of JSON input", )}// syntaxErrorAt returns the syntax error of the byte at cursor, which the grammar doesn't have where it is: the end// of the input if it is the nul byte which ends the buffer. It is not inlined, so that its callers keep their size.////go:noinlinefunc syntaxErrorAt( []byte, int64, syntaxWhere) error {ifisEnd(, ) {returnendError() } := invalidCharacterError(, , .text())if == whereValue {// a stream of Go 1.27 tells a bracket there by the object or the array which Token opened = errors.WithValueStartAt(, ) }return}// numberSyntaxError returns the syntax error of the byte at cursor in the number which starts at start.////go:noinlinefunc numberSyntaxError( []byte, , int64, syntaxWhere) error {ifisEnd(, ) {returnscalarEndError(, , , .text()) }returninvalidCharacterError(, , .text())}// literalSyntaxError returns the syntax error of the literal at cursor, which is not lit: the first byte which// differs from lit.func literalSyntaxError( []byte, int64, string) error {for := 1; < len(); ++ { := + int64()if := []; != [] { := "in literal " + + " (expecting " + quoteChar([]) + ")"ifisEnd(, ) {returnscalarEndError(, , int64(len())-1, ) }returninvalidCharacterError(, , ) } }returnnil}// stringSyntaxError returns the syntax error of the string at cursor: a control character, an escape which is not// one, or the end of the input.func stringSyntaxError( []byte, int64) error {for := + 1; ; ++ {switch := []; {case == '"':returnnilcase < ' ':returnsyntaxErrorAt(, , whereString)case == '\\':switch [+1] {case'"', '\\', '/', 'b', 'f', 'n', 'r', 't': ++case'u':for := int64(2); <= 5; ++ {if !isHexDigit([+]) {returnescapeSyntaxError(, , +) } } += 5default:returnescapeSyntaxError(, , +1) } } }}func isHexDigit( byte) bool {return -'0' <= 9 || (|0x20)-'a' <= 5}
The pages are generated with Goldsv0.8.7. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds.