Debian Codegraph @

internal/decoder/syntax_error.go

raw
1 package decoder
2
3 import (
4 "github.com/goccy/go-json/internal/errors"
5 )
6
7 // The syntax errors are the ones of encoding/json of the Go version, whose messages and offsets differ between Go
8 // 1.27, which is made of encoding/json/v2, and the earlier versions ( see syntax_error_legacy.go and
9 // syntax_error_v2.go ).
10
11 // syntaxWhere is where in the grammar a byte is which the grammar doesn't have there.
12 type syntaxWhere uint8
13
14 const (
15 whereValue syntaxWhere = iota
16 whereKey
17 whereAfterKey
18 whereAfterPair
19 whereAfterElement
20 whereAfterTop
21 whereString
22 whereNumber
23 whereFraction
24 whereExponent
25 )
26
27 // isEnd reports whether cursor is the nul byte which ends buf, the input followed by it.
28 func isEnd(buf []byte, cursor int64) bool {
29 return cursor == int64(len(buf))-1 && buf[cursor] == nul
30 }
31
32 // endError returns the error of the end of the input at the total offset at.
33 func endError(at int64) error {
34 return errors.ErrSyntaxAtEnd("unexpected end of JSON input", at)
35 }
36
37 // syntaxErrorAt returns the syntax error of the byte at cursor, which the grammar doesn't have where it is: the end
38 // of the input if it is the nul byte which ends the buffer. It is not inlined, so that its callers keep their size.
39 //
40 //go:noinline
41 func syntaxErrorAt(buf []byte, cursor int64, where syntaxWhere) error {
42 if isEnd(buf, cursor) {
43 return endError(cursor)
44 }
45 err := invalidCharacterError(buf, cursor, where.text())
46 if where == whereValue {
47 // a stream of Go 1.27 tells a bracket there by the object or the array which Token opened
48 err = errors.WithValueStartAt(err, cursor)
49 }
50 return err
51 }
52
53 // numberSyntaxError returns the syntax error of the byte at cursor in the number which starts at start.
54 //
55 //go:noinline
56 func numberSyntaxError(buf []byte, cursor, start int64, where syntaxWhere) error {
57 if isEnd(buf, cursor) {
58 return scalarEndError(buf, cursor, start, where.text())
59 }
60 return invalidCharacterError(buf, cursor, where.text())
61 }
62
63 // literalSyntaxError returns the syntax error of the literal at cursor, which is not lit: the first byte which
64 // differs from lit.
65 func literalSyntaxError(buf []byte, cursor int64, lit string) error {
66 for i := 1; i < len(lit); i++ {
67 at := cursor + int64(i)
68 if c := buf[at]; c != lit[i] {
69 what := "in literal " + lit + " (expecting " + quoteChar(lit[i]) + ")"
70 if isEnd(buf, at) {
71 return scalarEndError(buf, at, int64(len(buf))-1, what)
72 }
73 return invalidCharacterError(buf, at, what)
74 }
75 }
76 return nil
77 }
78
79 // stringSyntaxError returns the syntax error of the string at cursor: a control character, an escape which is not
80 // one, or the end of the input.
81 func stringSyntaxError(buf []byte, cursor int64) error {
82 for c := cursor + 1; ; c++ {
83 switch b := buf[c]; {
84 case b == '"':
85 return nil
86 case b < ' ':
87 return syntaxErrorAt(buf, c, whereString)
88 case b == '\\':
89 switch buf[c+1] {
90 case '"', '\\', '/', 'b', 'f', 'n', 'r', 't':
91 c++
92 case 'u':
93 for i := int64(2); i <= 5; i++ {
94 if !isHexDigit(buf[c+i]) {
95 return escapeSyntaxError(buf, c, c+i)
96 }
97 }
98 c += 5
99 default:
100 return escapeSyntaxError(buf, c, c+1)
101 }
102 }
103 }
104 }
105
106 func isHexDigit(c byte) bool {
107 return c-'0' <= 9 || (c|0x20)-'a' <= 5
108 }