Debian Codegraph @

internal/decoder/stream_token_legacy.go

raw
1 //go:build !go1.27 || !goexperiment.jsonv2
2
3 package decoder
4
5 import (
6 "encoding/json"
7 "reflect"
8 "unsafe"
9
10 "github.com/goccy/go-json/internal/errors"
11 "github.com/goccy/go-json/internal/runtime"
12 )
13
14 // The tokens of a stream as encoding/json before Go 1.27 reads them: a comma or a colon which the grammar doesn't
15 // have is an error of Token, which is not kept, and Decode reads a value where Token would.
16
17 // streamPeek is what More peeked at, which encoding/json before Go 1.27 doesn't keep: More and Token move the
18 // offset over the white spaces.
19 type streamPeek struct{}
20
21 // reset does nothing.
22 func (streamPeek) reset() {}
23
24 // valueStartMismatch returns nil: encoding/json before Go 1.27 tells the bracket where a value is waited for as
25 // the grammar does.
26 func (*Stream) valueStartMismatch(int64) error {
27 return nil
28 }
29
30 // countsScanned is whether the offset of a syntax error of a value of Decode is counted from the bytes which the
31 // reads of the values read, as encoding/json before Go 1.27 counts it: the white spaces and the delimiters which
32 // Token or More read, and the comma or the colon before a value of Decode, are not counted.
33 const countsScanned = true
34
35 // prepareInTokens moves the cursor to the value which Decode reads between the tokens: after the comma or the
36 // colon which the state waits for. It is not inlined, so that Decode keeps the size it had.
37 //
38 //go:noinline
39 func (s *Stream) prepareInTokens() error {
40 switch s.tokenState {
41 case tokenFailed:
42 return s.err
43 case tokenArrayComma:
44 // the comma and the colon are looked for without an error kept, as encoding/json does
45 if !s.peek() {
46 return s.endError()
47 }
48 if s.buf[s.cursor] != ',' {
49 return errors.ErrSyntax("expected comma after array element", s.TotalOffset())
50 }
51 s.cursor++
52 s.tokenState = tokenArrayValue
53 // the value is read after the comma
54 s.markPrevEnd()
55 s.readStart = s.offset + s.cursor
56 case tokenObjectColon:
57 if !s.peek() {
58 return s.endError()
59 }
60 if s.buf[s.cursor] != ':' {
61 return errors.ErrSyntax("expected colon after object key", s.TotalOffset())
62 }
63 s.cursor++
64 s.tokenState = tokenObjectValue
65 // the value is read after the colon
66 s.markPrevEnd()
67 s.readStart = s.offset + s.cursor
68 default:
69 // the value is read from the cursor, white spaces before it too
70 s.readStart = s.offset + s.cursor
71 }
72 if !s.tokenValueAllowed() {
73 return errors.ErrSyntax("not at beginning of value", s.TotalOffset())
74 }
75 if !s.skipWhiteSpace() {
76 return s.fail(s.endError())
77 }
78 return nil
79 }
80
81 // Token returns the next token of the stream: a delimiter, a string, a number, a bool or nil. The commas and the
82 // colons are consumed where the grammar has them.
83 func (s *Stream) Token() (any, error) {
84 if s.tokenState == tokenFailed {
85 return nil, s.err
86 }
87 for {
88 if !s.peek() {
89 return nil, s.endError()
90 }
91 c := s.buf[s.cursor]
92 switch c {
93 case '[', '{':
94 if !s.tokenValueAllowed() {
95 return nil, s.tokenError(c)
96 }
97 s.cursor++
98 s.tokenStack = append(s.tokenStack, s.tokenState)
99 if c == '[' {
100 s.tokenState = tokenArrayStart
101 } else {
102 s.tokenState = tokenObjectStart
103 }
104 return json.Delim(c), nil
105 case ']', '}':
106 if c == ']' && s.tokenState != tokenArrayStart && s.tokenState != tokenArrayComma ||
107 c == '}' && s.tokenState != tokenObjectStart && s.tokenState != tokenObjectComma {
108 return nil, s.tokenError(c)
109 }
110 s.cursor++
111 s.tokenState = s.tokenStack[len(s.tokenStack)-1]
112 s.tokenStack = s.tokenStack[:len(s.tokenStack)-1]
113 s.tokenValueEnd()
114 return json.Delim(c), nil
115 case ':':
116 if s.tokenState != tokenObjectColon {
117 return nil, s.tokenError(c)
118 }
119 s.cursor++
120 s.tokenState = tokenObjectValue
121 continue
122 case ',':
123 switch s.tokenState {
124 case tokenArrayComma:
125 s.tokenState = tokenArrayValue
126 case tokenObjectComma:
127 s.tokenState = tokenObjectKey
128 default:
129 return nil, s.tokenError(c)
130 }
131 s.cursor++
132 continue
133 case '"':
134 if s.tokenState == tokenObjectStart || s.tokenState == tokenObjectKey {
135 key, err := s.tokenString()
136 if err != nil {
137 return nil, err
138 }
139 s.tokenState = tokenObjectColon
140 return key, nil
141 }
142 }
143 if !s.tokenValueAllowed() {
144 return nil, s.tokenError(c)
145 }
146 // a value is read as Decode reads it into an interface{}, as encoding/json before Go 1.27 reads it: its
147 // errors, and the bytes which the reads of the values read ( see countsScanned ), are the ones of Decode
148 dec, err := s.DecoderOf(anyPtrType)
149 if err != nil {
150 return nil, err
151 }
152 var v any
153 if err := s.Decode(dec, anyPtrType, unsafe.Pointer(&v)); err != nil {
154 return nil, err
155 }
156 return v, nil
157 }
158 }
159
160 // anyPtrType is the type of *interface{}, which Token decodes a value into.
161 var anyPtrType = runtime.TypePtr(reflect.TypeOf((*any)(nil)))
162
163 // tokenError returns the syntax error of the byte c at the cursor, which the grammar doesn't have in the state, as
164 // encoding/json has it.
165 func (s *Stream) tokenError(c byte) error {
166 var context string
167 switch s.tokenState {
168 case tokenTopValue, tokenArrayStart, tokenArrayValue, tokenObjectValue:
169 context = " looking for beginning of value"
170 case tokenArrayComma:
171 context = " after array element"
172 case tokenObjectKey:
173 context = " looking for beginning of object key string"
174 case tokenObjectColon:
175 context = " after object key"
176 case tokenObjectComma:
177 context = " after object key:value pair"
178 }
179 return errors.ErrSyntax("invalid character "+quoteChar(c)+context, s.TotalOffset())
180 }
181
182 // InputOffset returns the offset of the stream which Decoder.InputOffset reports: the number of the bytes
183 // consumed from the reader, or, after a read failed, the end of the value before it.
184 func (s *Stream) InputOffset() int64 {
185 if s.tokenState == tokenFailed {
186 return s.errOffset
187 }
188 return s.TotalOffset()
189 }
190
191 // More reports whether the current array or object has another element.
192 func (s *Stream) More() bool {
193 if !s.peek() {
194 return false
195 }
196 switch s.buf[s.cursor] {
197 case ']', '}':
198 return false
199 }
200 return true
201 }
202
203 // tokenValueAllowed reports whether a value may be read in the state.
204 func (s *Stream) tokenValueAllowed() bool {
205 switch s.tokenState {
206 case tokenTopValue, tokenArrayStart, tokenArrayValue, tokenObjectValue:
207 return true
208 }
209 return false
210 }