Debian Codegraph @

internal/decoder/syntax_error_v2.go

raw
1 //go:build go1.27 && goexperiment.jsonv2
2
3 package decoder
4
5 import (
6 "strconv"
7 "strings"
8 "unicode"
9 "unicode/utf8"
10
11 "github.com/goccy/go-json/internal/errors"
12 )
13
14 // text returns the words of the message of a syntax error which tell where the byte is, as encoding/json of Go 1.27
15 // has them: the ones of encoding/json/jsontext, which are made the ones of encoding/json.
16 func (w syntaxWhere) text() string {
17 switch w {
18 case whereKey:
19 return "looking for beginning of object key string"
20 case whereAfterKey:
21 return "after object key"
22 case whereAfterPair:
23 return "after object key:value pair"
24 case whereAfterElement:
25 return "after array element"
26 case whereAfterTop:
27 return "after top-level value"
28 case whereString:
29 return "in string"
30 case whereNumber, whereFraction, whereExponent:
31 return "in numeric literal"
32 }
33 return "looking for beginning of value"
34 }
35
36 // invalidCharacterError returns the syntax error of the character at cursor, which the offset is after: the
37 // character of the UTF-8 sequence at cursor, or its byte if it is not one.
38 func invalidCharacterError(buf []byte, cursor int64, what string) *errors.SyntaxError {
39 r, n := utf8.DecodeRune(buf[cursor : len(buf)-1])
40 quoted := strconv.QuoteRune(r)
41 if r == utf8.RuneError && n == 1 {
42 quoted = `'\x` + strconv.FormatUint(uint64(buf[cursor]), 16) + `'`
43 }
44 return errors.ErrSyntax("invalid character "+quoted+" "+what, cursor+int64(n))
45 }
46
47 // scalarEndError returns the syntax error of the end of the input at cursor in a number, a literal or an escape,
48 // which starts at start: encoding/json of Go 1.27 reports it at the start of the number or of the escape, and at
49 // the end of the input for a literal.
50 func scalarEndError(_ []byte, _, start int64, _ string) error {
51 return endError(start)
52 }
53
54 // escapeSyntaxError returns the syntax error of the byte at cursor, in the escape which the backslash at backslash
55 // starts: encoding/json of Go 1.27 reports the escape, which is 6 bytes for \u.
56 func escapeSyntaxError(buf []byte, backslash, cursor int64) error {
57 end := backslash + 2
58 if cursor > backslash+1 {
59 end = backslash + 6
60 }
61 input := buf[:len(buf)-1]
62 if end > int64(len(input)) {
63 // the escape is cut by the end of the input: the end of the input if what it has may start an escape,
64 // and else what it has
65 rest := input[backslash:]
66 prefix := len(rest) < 2 || rest[1] == 'u'
67 for i := 2; prefix && i < len(rest); i++ {
68 prefix = isHexDigit(rest[i])
69 }
70 if prefix {
71 return endError(backslash)
72 }
73 end = int64(len(input))
74 }
75 seq := string(input[backslash:end])
76 var quoted string
77 if strings.ContainsFunc(seq, func(r rune) bool {
78 return r == '`' || r == utf8.RuneError || unicode.IsSpace(r) || !unicode.IsPrint(r)
79 }) {
80 quoted = strconv.Quote(seq)
81 } else {
82 quoted = "`" + seq + "`"
83 }
84 return errors.ErrSyntax("invalid escape sequence "+quoted+" in string", end)
85 }
86
87 // depthSyntaxError returns the syntax error of the object or the array at cursor, which is nested deeper than the
88 // limit.
89 func depthSyntaxError(_ []byte, cursor int64) error {
90 return errors.ErrSyntax("exceeded max depth", cursor+1)
91 }