Debian Codegraph @

internal/decoder/syntax_error_legacy.go

raw
1 //go:build !go1.27 || !goexperiment.jsonv2
2
3 package decoder
4
5 import (
6 "github.com/goccy/go-json/internal/errors"
7 )
8
9 // text returns the words of the message of a syntax error which tell where the byte is, as encoding/json before
10 // Go 1.27 has them.
11 func (w syntaxWhere) text() string {
12 switch w {
13 case whereKey:
14 return "looking for beginning of object key string"
15 case whereAfterKey:
16 return "after object key"
17 case whereAfterPair:
18 return "after object key:value pair"
19 case whereAfterElement:
20 return "after array element"
21 case whereAfterTop:
22 return "after top-level value"
23 case whereString:
24 return "in string literal"
25 case whereNumber:
26 return "in numeric literal"
27 case whereFraction:
28 return "after decimal point in numeric literal"
29 case whereExponent:
30 return "in exponent of numeric literal"
31 }
32 return "looking for beginning of value"
33 }
34
35 // invalidCharacterError returns the syntax error of the byte at cursor, which the offset is after.
36 func invalidCharacterError(buf []byte, cursor int64, what string) *errors.SyntaxError {
37 return errors.ErrSyntax("invalid character "+quoteChar(buf[cursor])+" "+what, cursor+1)
38 }
39
40 // scalarEndError returns the syntax error of the end of the input at cursor in a number, a literal or an escape,
41 // which starts at start: encoding/json before Go 1.27 reads a white space at the end of the input, which is the
42 // byte of the error.
43 func scalarEndError(_ []byte, cursor, _ int64, what string) error {
44 return errors.ErrSyntaxAtEnd("invalid character ' ' "+what, cursor)
45 }
46
47 // escapeSyntaxError returns the syntax error of the byte at cursor, in the escape which the backslash at backslash
48 // starts.
49 func escapeSyntaxError(buf []byte, backslash, cursor int64) error {
50 what := "in string escape code"
51 if cursor > backslash+1 {
52 what = `in \u hexadecimal character escape`
53 }
54 if isEnd(buf, cursor) {
55 return scalarEndError(buf, cursor, backslash, what)
56 }
57 return invalidCharacterError(buf, cursor, what)
58 }
59
60 // depthSyntaxError returns the syntax error of the object or the array at cursor, which is nested deeper than the
61 // limit.
62 func depthSyntaxError(buf []byte, cursor int64) error {
63 return invalidCharacterError(buf, cursor, "exceeded max depth")
64 }