1
16
17 package ast
18
19 import (
20 `fmt`
21
22 `github.com/bytedance/sonic/internal/native/types`
23 `github.com/bytedance/sonic/internal/rt`
24 )
25
26 const (
27 _DEFAULT_NODE_CAP int = 8
28 _APPEND_GROW_SHIFT = 1
29 )
30
31 const (
32 _ERR_NOT_FOUND types.ParsingError = 33
33 _ERR_UNSUPPORT_TYPE types.ParsingError = 34
34 )
35
36 var (
37
38 ErrNotExist error = newError(_ERR_NOT_FOUND, "value not exists")
39
40
41 ErrUnsupportType error = newError(_ERR_UNSUPPORT_TYPE, "unsupported type")
42 )
43
44 type Parser struct {
45 p int
46 s string
47 noLazy bool
48 skipValue bool
49 dbuf *byte
50 }
51
52
53
54 func (self *Parser) delim() types.ParsingError {
55 n := len(self.s)
56 p := self.lspace(self.p)
57
58
59 if p >= n {
60 return types.ERR_EOF
61 }
62
63
64 if self.s[p] != ':' {
65 return types.ERR_INVALID_CHAR
66 }
67
68
69 self.p = p + 1
70 return 0
71 }
72
73 func (self *Parser) object() types.ParsingError {
74 n := len(self.s)
75 p := self.lspace(self.p)
76
77
78 if p >= n {
79 return types.ERR_EOF
80 }
81
82
83 if self.s[p] != '{' {
84 return types.ERR_INVALID_CHAR
85 }
86
87
88 self.p = p + 1
89 return 0
90 }
91
92 func (self *Parser) array() types.ParsingError {
93 n := len(self.s)
94 p := self.lspace(self.p)
95
96
97 if p >= n {
98 return types.ERR_EOF
99 }
100
101
102 if self.s[p] != '[' {
103 return types.ERR_INVALID_CHAR
104 }
105
106
107 self.p = p + 1
108 return 0
109 }
110
111 func (self *Parser) lspace(sp int) int {
112 ns := len(self.s)
113 for ; sp<ns && isSpace(self.s[sp]); sp+=1 {}
114
115 return sp
116 }
117
118 func (self *Parser) decodeArray(ret *linkedNodes) (Node, types.ParsingError) {
119 sp := self.p
120 ns := len(self.s)
121
122
123 if self.p = self.lspace(sp); self.p >= ns {
124 return Node{}, types.ERR_EOF
125 }
126
127
128 if self.s[self.p] == ']' {
129 self.p++
130 return Node{t: types.V_ARRAY}, 0
131 }
132
133
134 for {
135 var val Node
136 var err types.ParsingError
137
138 if self.skipValue {
139
140 var start int
141 if start, err = self.skipFast(); err != 0 {
142 return Node{}, err
143 }
144 if self.p > ns {
145 return Node{}, types.ERR_EOF
146 }
147 t := switchRawType(self.s[start])
148 if t == _V_NONE {
149 return Node{}, types.ERR_INVALID_CHAR
150 }
151 val = newRawNode(self.s[start:self.p], t)
152 }else{
153
154 if val, err = self.Parse(); err != 0 {
155 return Node{}, err
156 }
157 }
158
159
160 ret.Push(val)
161 self.p = self.lspace(self.p)
162
163
164 if self.p >= ns {
165 return Node{}, types.ERR_EOF
166 }
167
168
169 switch self.s[self.p] {
170 case ',' : self.p++
171 case ']' : self.p++; return newArray(ret), 0
172 default:
173
174
175
176 return Node{}, types.ERR_INVALID_CHAR
177 }
178 }
179 }
180
181 func (self *Parser) decodeObject(ret *linkedPairs) (Node, types.ParsingError) {
182 sp := self.p
183 ns := len(self.s)
184
185
186 if self.p = self.lspace(sp); self.p >= ns {
187 return Node{}, types.ERR_EOF
188 }
189
190
191 if self.s[self.p] == '}' {
192 self.p++
193 return Node{t: types.V_OBJECT}, 0
194 }
195
196
197 for {
198 var val Node
199 var njs types.JsonState
200 var err types.ParsingError
201
202
203 if njs = self.decodeValue(); njs.Vt != types.V_STRING {
204 return Node{}, types.ERR_INVALID_CHAR
205 }
206
207
208 idx := self.p - 1
209 key := self.s[njs.Iv:idx]
210
211
212 if njs.Ep != -1 {
213 if key, err = unquote(key); err != 0 {
214 return Node{}, err
215 }
216 }
217
218
219 if err = self.delim(); err != 0 {
220 return Node{}, err
221 }
222
223
224 if self.skipValue {
225
226 var start int
227 if start, err = self.skipFast(); err != 0 {
228 return Node{}, err
229 }
230 if self.p > ns {
231 return Node{}, types.ERR_EOF
232 }
233 t := switchRawType(self.s[start])
234 if t == _V_NONE {
235 return Node{}, types.ERR_INVALID_CHAR
236 }
237 val = newRawNode(self.s[start:self.p], t)
238 } else {
239
240 if val, err = self.Parse(); err != 0 {
241 return Node{}, err
242 }
243 }
244
245
246
247 ret.Push(Pair{Key: key, Value: val})
248 self.p = self.lspace(self.p)
249
250
251 if self.p >= ns {
252 return Node{}, types.ERR_EOF
253 }
254
255
256 switch self.s[self.p] {
257 case ',' : self.p++
258 case '}' : self.p++; return newObject(ret), 0
259 default:
260
261
262
263 return Node{}, types.ERR_INVALID_CHAR
264 }
265 }
266 }
267
268 func (self *Parser) decodeString(iv int64, ep int) (Node, types.ParsingError) {
269 p := self.p - 1
270 s := self.s[iv:p]
271
272
273 if ep == -1 {
274 return NewString(s), 0
275 }
276
277
278 out, err := unquote(s)
279
280
281 if err != 0 {
282 return Node{}, err
283 } else {
284 return newBytes(rt.Str2Mem(out)), 0
285 }
286 }
287
288
289
290 func (self *Parser) Pos() int {
291 return self.p
292 }
293
294 func (self *Parser) Parse() (Node, types.ParsingError) {
295 switch val := self.decodeValue(); val.Vt {
296 case types.V_EOF : return Node{}, types.ERR_EOF
297 case types.V_NULL : return nullNode, 0
298 case types.V_TRUE : return trueNode, 0
299 case types.V_FALSE : return falseNode, 0
300 case types.V_STRING : return self.decodeString(val.Iv, val.Ep)
301 case types.V_ARRAY:
302 if p := skipBlank(self.s, self.p); p >= self.p && self.s[p] == ']' {
303 self.p = p + 1
304 return Node{t: types.V_ARRAY}, 0
305 }
306 if self.noLazy {
307 return self.decodeArray(new(linkedNodes))
308 }
309 return newLazyArray(self), 0
310 case types.V_OBJECT:
311 if p := skipBlank(self.s, self.p); p >= self.p && self.s[p] == '}' {
312 self.p = p + 1
313 return Node{t: types.V_OBJECT}, 0
314 }
315 if self.noLazy {
316 return self.decodeObject(new(linkedPairs))
317 }
318 return newLazyObject(self), 0
319 case types.V_DOUBLE : return NewNumber(self.s[val.Ep:self.p]), 0
320 case types.V_INTEGER : return NewNumber(self.s[val.Ep:self.p]), 0
321 default : return Node{}, types.ParsingError(-val.Vt)
322 }
323 }
324
325 func (self *Parser) searchKey(match string) types.ParsingError {
326 ns := len(self.s)
327 if err := self.object(); err != 0 {
328 return err
329 }
330
331
332 if self.p = self.lspace(self.p); self.p >= ns {
333 return types.ERR_EOF
334 }
335
336
337 if self.s[self.p] == '}' {
338 self.p++
339 return _ERR_NOT_FOUND
340 }
341
342 var njs types.JsonState
343 var err types.ParsingError
344
345 for {
346
347
348 if njs = self.decodeValue(); njs.Vt != types.V_STRING {
349 return types.ERR_INVALID_CHAR
350 }
351
352
353 idx := self.p - 1
354 key := self.s[njs.Iv:idx]
355
356
357 if njs.Ep != -1 {
358 if key, err = unquote(key); err != 0 {
359 return err
360 }
361 }
362
363
364 if err = self.delim(); err != 0 {
365 return err
366 }
367
368
369 if key != match {
370 if _, err = self.skipFast(); err != 0 {
371 return err
372 }
373 } else {
374 return 0
375 }
376
377
378 self.p = self.lspace(self.p)
379 if self.p >= ns {
380 return types.ERR_EOF
381 }
382
383
384 switch self.s[self.p] {
385 case ',':
386 self.p++
387 case '}':
388 self.p++
389 return _ERR_NOT_FOUND
390 default:
391 return types.ERR_INVALID_CHAR
392 }
393 }
394 }
395
396 func (self *Parser) searchIndex(idx int) types.ParsingError {
397 ns := len(self.s)
398 if err := self.array(); err != 0 {
399 return err
400 }
401
402
403 if self.p = self.lspace(self.p); self.p >= ns {
404 return types.ERR_EOF
405 }
406
407
408 if self.s[self.p] == ']' {
409 self.p++
410 return _ERR_NOT_FOUND
411 }
412
413 var err types.ParsingError
414
415 for i := 0; i < idx; i++ {
416
417
418 if _, err = self.skipFast(); err != 0 {
419 return err
420 }
421
422
423 self.p = self.lspace(self.p)
424 if self.p >= ns {
425 return types.ERR_EOF
426 }
427
428
429 switch self.s[self.p] {
430 case ',':
431 self.p++
432 case ']':
433 self.p++
434 return _ERR_NOT_FOUND
435 default:
436 return types.ERR_INVALID_CHAR
437 }
438 }
439
440 return 0
441 }
442
443 func (self *Node) skipNextNode() *Node {
444 if !self.isLazy() {
445 return nil
446 }
447
448 parser, stack := self.getParserAndArrayStack()
449 ret := &stack.v
450 sp := parser.p
451 ns := len(parser.s)
452
453
454 if parser.p = parser.lspace(sp); parser.p >= ns {
455 return newSyntaxError(parser.syntaxError(types.ERR_EOF))
456 }
457
458
459 if parser.s[parser.p] == ']' {
460 parser.p++
461 self.setArray(ret)
462 return nil
463 }
464
465 var val Node
466
467 if start, err := parser.skipFast(); err != 0 {
468 return newSyntaxError(parser.syntaxError(err))
469 } else {
470 t := switchRawType(parser.s[start])
471 if t == _V_NONE {
472 return newSyntaxError(parser.syntaxError(types.ERR_INVALID_CHAR))
473 }
474 val = newRawNode(parser.s[start:parser.p], t)
475 }
476
477
478 ret.Push(val)
479 self.l++
480 parser.p = parser.lspace(parser.p)
481
482
483 if parser.p >= ns {
484 return newSyntaxError(parser.syntaxError(types.ERR_EOF))
485 }
486
487
488 switch parser.s[parser.p] {
489 case ',':
490 parser.p++
491 return ret.At(ret.Len()-1)
492 case ']':
493 parser.p++
494 self.setArray(ret)
495 return ret.At(ret.Len()-1)
496 default:
497 return newSyntaxError(parser.syntaxError(types.ERR_INVALID_CHAR))
498 }
499 }
500
501 func (self *Node) skipNextPair() (*Pair) {
502 if !self.isLazy() {
503 return nil
504 }
505
506 parser, stack := self.getParserAndObjectStack()
507 ret := &stack.v
508 sp := parser.p
509 ns := len(parser.s)
510
511
512 if parser.p = parser.lspace(sp); parser.p >= ns {
513 return &Pair{"", *newSyntaxError(parser.syntaxError(types.ERR_EOF))}
514 }
515
516
517 if parser.s[parser.p] == '}' {
518 parser.p++
519 self.setObject(ret)
520 return nil
521 }
522
523
524 var val Node
525 var njs types.JsonState
526 var err types.ParsingError
527
528
529 if njs = parser.decodeValue(); njs.Vt != types.V_STRING {
530 return &Pair{"", *newSyntaxError(parser.syntaxError(types.ERR_INVALID_CHAR))}
531 }
532
533
534 idx := parser.p - 1
535 key := parser.s[njs.Iv:idx]
536
537
538 if njs.Ep != -1 {
539 if key, err = unquote(key); err != 0 {
540 return &Pair{key, *newSyntaxError(parser.syntaxError(err))}
541 }
542 }
543
544
545 if err = parser.delim(); err != 0 {
546 return &Pair{key, *newSyntaxError(parser.syntaxError(err))}
547 }
548
549
550 if start, err := parser.skipFast(); err != 0 {
551 return &Pair{key, *newSyntaxError(parser.syntaxError(err))}
552 } else {
553 t := switchRawType(parser.s[start])
554 if t == _V_NONE {
555 return &Pair{key, *newSyntaxError(parser.syntaxError(types.ERR_INVALID_CHAR))}
556 }
557 val = newRawNode(parser.s[start:parser.p], t)
558 }
559
560
561 ret.Push(Pair{Key: key, Value: val})
562 self.l++
563 parser.p = parser.lspace(parser.p)
564
565
566 if parser.p >= ns {
567 return &Pair{key, *newSyntaxError(parser.syntaxError(types.ERR_EOF))}
568 }
569
570
571 switch parser.s[parser.p] {
572 case ',':
573 parser.p++
574 return ret.At(ret.Len()-1)
575 case '}':
576 parser.p++
577 self.setObject(ret)
578 return ret.At(ret.Len()-1)
579 default:
580 return &Pair{key, *newSyntaxError(parser.syntaxError(types.ERR_INVALID_CHAR))}
581 }
582 }
583
584
585
586
587
588 func Loads(src string) (int, interface{}, error) {
589 ps := &Parser{s: src}
590 np, err := ps.Parse()
591
592
593 if err != 0 {
594 return 0, nil, ps.ExportError(err)
595 } else {
596 x, err := np.Interface()
597 if err != nil {
598 return 0, nil, err
599 }
600 return ps.Pos(), x, nil
601 }
602 }
603
604
605 func LoadsUseNumber(src string) (int, interface{}, error) {
606 ps := &Parser{s: src}
607 np, err := ps.Parse()
608
609
610 if err != 0 {
611 return 0, nil, err
612 } else {
613 x, err := np.InterfaceUseNumber()
614 if err != nil {
615 return 0, nil, err
616 }
617 return ps.Pos(), x, nil
618 }
619 }
620
621
622 func NewParser(src string) *Parser {
623 return &Parser{s: src}
624 }
625
626
627 func NewParserObj(src string) Parser {
628 return Parser{s: src}
629 }
630
631
632
633
634 func (self *Parser) decodeNumber(decode bool) {
635 if !decode && self.dbuf != nil {
636 types.FreeDbuf(self.dbuf)
637 self.dbuf = nil
638 return
639 }
640 if decode && self.dbuf == nil {
641 self.dbuf = types.NewDbuf()
642 }
643 }
644
645
646 func (self *Parser) ExportError(err types.ParsingError) error {
647 if err == _ERR_NOT_FOUND {
648 return ErrNotExist
649 }
650 return fmt.Errorf("%q", SyntaxError{
651 Pos : self.p,
652 Src : self.s,
653 Code: err,
654 }.Description())
655 }
656
View as plain text