...
1
16
17 package ast
18
19 import (
20 `encoding/json`
21
22 `github.com/bytedance/sonic/internal/native/types`
23 )
24
25
26
27
28
29
30
31
32
33
34
35
36 type Visitor interface {
37
38
39 OnNull() error
40
41
42 OnBool(v bool) error
43
44
45 OnString(v string) error
46
47
48 OnInt64(v int64, n json.Number) error
49
50
51 OnFloat64(v float64, n json.Number) error
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66 OnObjectBegin(capacity int) error
67
68
69 OnObjectKey(key string) error
70
71
72 OnObjectEnd() error
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87 OnArrayBegin(capacity int) error
88
89
90 OnArrayEnd() error
91 }
92
93
94
95 type VisitorOptions struct {
96
97
98
99 OnlyNumber bool
100 }
101
102 var defaultVisitorOptions = &VisitorOptions{}
103
104
105
106
107
108 func Preorder(str string, visitor Visitor, opts *VisitorOptions) error {
109 if opts == nil {
110 opts = defaultVisitorOptions
111 }
112
113
114 var (
115 optDecodeNumber = !opts.OnlyNumber
116 )
117
118 tv := &traverser{
119 parser: Parser{
120 s: str,
121 noLazy: true,
122 skipValue: false,
123 },
124 visitor: visitor,
125 }
126
127 if optDecodeNumber {
128 tv.parser.decodeNumber(true)
129 }
130
131 err := tv.decodeValue()
132
133 if optDecodeNumber {
134 tv.parser.decodeNumber(false)
135 }
136 return err
137 }
138
139 type traverser struct {
140 parser Parser
141 visitor Visitor
142 }
143
144
145 func (self *traverser) decodeValue() error {
146 switch val := self.parser.decodeValue(); val.Vt {
147 case types.V_EOF:
148 return types.ERR_EOF
149 case types.V_NULL:
150 return self.visitor.OnNull()
151 case types.V_TRUE:
152 return self.visitor.OnBool(true)
153 case types.V_FALSE:
154 return self.visitor.OnBool(false)
155 case types.V_STRING:
156 return self.decodeString(val.Iv, val.Ep)
157 case types.V_DOUBLE:
158 return self.visitor.OnFloat64(val.Dv,
159 json.Number(self.parser.s[val.Ep:self.parser.p]))
160 case types.V_INTEGER:
161 return self.visitor.OnInt64(val.Iv,
162 json.Number(self.parser.s[val.Ep:self.parser.p]))
163 case types.V_ARRAY:
164 return self.decodeArray()
165 case types.V_OBJECT:
166 return self.decodeObject()
167 default:
168 return types.ParsingError(-val.Vt)
169 }
170 }
171
172
173 func (self *traverser) decodeArray() error {
174 sp := self.parser.p
175 ns := len(self.parser.s)
176
177
178 self.parser.p = self.parser.lspace(sp)
179 if self.parser.p >= ns {
180 return types.ERR_EOF
181 }
182
183
184 if self.parser.s[self.parser.p] == ']' {
185 self.parser.p++
186 if err := self.visitor.OnArrayBegin(0); err != nil {
187 return err
188 }
189 return self.visitor.OnArrayEnd()
190 }
191
192
193 if err := self.visitor.OnArrayBegin(_DEFAULT_NODE_CAP); err != nil {
194 return err
195 }
196 for {
197
198 if err := self.decodeValue(); err != nil {
199 return err
200 }
201 self.parser.p = self.parser.lspace(self.parser.p)
202
203
204 if self.parser.p >= ns {
205 return types.ERR_EOF
206 }
207
208
209 switch self.parser.s[self.parser.p] {
210 case ',':
211 self.parser.p++
212 case ']':
213 self.parser.p++
214 return self.visitor.OnArrayEnd()
215 default:
216 return types.ERR_INVALID_CHAR
217 }
218 }
219 }
220
221
222 func (self *traverser) decodeObject() error {
223 sp := self.parser.p
224 ns := len(self.parser.s)
225
226
227 self.parser.p = self.parser.lspace(sp)
228 if self.parser.p >= ns {
229 return types.ERR_EOF
230 }
231
232
233 if self.parser.s[self.parser.p] == '}' {
234 self.parser.p++
235 if err := self.visitor.OnObjectBegin(0); err != nil {
236 return err
237 }
238 return self.visitor.OnObjectEnd()
239 }
240
241
242 if err := self.visitor.OnObjectBegin(_DEFAULT_NODE_CAP); err != nil {
243 return err
244 }
245 for {
246 var njs types.JsonState
247 var err types.ParsingError
248
249
250 if njs = self.parser.decodeValue(); njs.Vt != types.V_STRING {
251 return types.ERR_INVALID_CHAR
252 }
253
254
255 idx := self.parser.p - 1
256 key := self.parser.s[njs.Iv:idx]
257
258
259 if njs.Ep != -1 {
260 if key, err = unquote(key); err != 0 {
261 return err
262 }
263 }
264
265 if err := self.visitor.OnObjectKey(key); err != nil {
266 return err
267 }
268
269
270 if err = self.parser.delim(); err != 0 {
271 return err
272 }
273
274
275 if err := self.decodeValue(); err != nil {
276 return err
277 }
278
279 self.parser.p = self.parser.lspace(self.parser.p)
280
281
282 if self.parser.p >= ns {
283 return types.ERR_EOF
284 }
285
286
287 switch self.parser.s[self.parser.p] {
288 case ',':
289 self.parser.p++
290 case '}':
291 self.parser.p++
292 return self.visitor.OnObjectEnd()
293 default:
294 return types.ERR_INVALID_CHAR
295 }
296 }
297 }
298
299
300 func (self *traverser) decodeString(iv int64, ep int) error {
301 p := self.parser.p - 1
302 s := self.parser.s[iv:p]
303
304
305 if ep == -1 {
306 return self.visitor.OnString(s)
307 }
308
309
310 out, err := unquote(s)
311 if err != 0 {
312 return err
313 }
314 return self.visitor.OnString(out)
315 }
316
View as plain text