/
scanner.go
1136 lines (1067 loc) · 27.6 KB
/
scanner.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2010 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package ubjson
// JSON value parser state machine.
// Just about at the limit of what is reasonable to write by hand.
// Some parts are a bit tedious, but overall it nicely factors out the
// otherwise common code from the multiple scanning functions
// in this package (Compact, Indent, checkValid, nextValue, etc).
//
// This file starts with two simple examples using the scanner
// before diving into the scanner itself.
import (
"bytes"
"encoding/binary"
"fmt"
"strconv"
)
// checkValid verifies that data is valid JSON-encoded data.
// scan is passed in for use by checkValid to avoid an allocation.
func checkValid(data []byte, scan *scanner) error {
scan.reset()
for _, c := range data {
scan.bytes++
v := scan.step(scan, int(c))
if v == scanError {
return scan.err
}
}
if scan.eof() == scanError {
return scan.err
}
return nil
}
// nextValue splits data after the next whole JSON value,
// returning that value and the bytes that follow it as separate slices.
// scan is passed in for use by nextValue to avoid an allocation.
func nextValue(data []byte, scan *scanner) (value, rest []byte, err error) {
scan.reset()
for i, c := range data {
v := scan.step(scan, int(c))
if v >= scanEndObject {
switch v {
// probe the scanner with a space to determine whether we will
// get scanEnd on the next character. Otherwise, if the next character
// is not a space, scanEndTop allocates a needless error.
case scanEndObject, scanEndArray:
if len(scan.parseState) == 0 {
return data[:i+1], data[i+1:], nil
}
case scanError:
return nil, nil, scan.err
case scanEnd:
return data[0:i], data[i:], nil
}
}
}
if scan.eof() == scanError {
return nil, nil, scan.err
}
return data, nil, nil
}
// A SyntaxError is a description of a JSON syntax error.
type SyntaxError struct {
msg string // description of error
Offset int64 // error occurred after reading Offset bytes
}
func (e *SyntaxError) Error() string { return e.msg }
// A scanner is a JSON scanning state machine.
// Callers call scan.reset() and then pass bytes in one at a time
// by calling scan.step(&scan, c) for each byte.
// The return value, referred to as an opcode, tells the
// caller about significant parsing events like beginning
// and ending literals, objects, and arrays, so that the
// caller can follow along if it wishes.
// The return value scanEnd indicates that a single top-level
// JSON value has been completed, *before* the byte that
// just got passed in. (The indication must be delayed in order
// to recognize the end of numbers: is 123 a whole value or
// the beginning of 12345e+6?).
type scanner struct {
// The step is a func to be called to execute the next transition.
// Also tried using an integer constant and a single func
// with a switch, but using the func directly was 10% faster
// on a 64-bit Mac Mini, and it's nicer to read.
step func(*scanner, int) int
// Reached end of top-level value.
endTop bool
// Stack of what we're in the middle of - array values, object keys, object values.
parseState []parseStackFrame
// Error that happened, if any.
err error
// 1-byte redo (see undo method)
redo bool
redoCode int
redoState func(*scanner, int) int
// total bytes consumed, updated by decoder.Decode
bytes int64
// Number of bytes left until the end of value.
bytesLeft int
scanningBytes bool
afterBytes func(*scanner)
lenBytes []byte
}
type parseStackFrame struct {
container int
valueType byte
itemsLeft int
hasCount bool
}
var scanToName = map[int]string{
scanContinue: "continue",
scanBeginObject: "beginObject",
scanEndObject: "endObject",
scanBeginArray: "beginArray",
scanEndArray: "endArray",
scanSkipSpace: "skipSpace",
scanEnd: "end",
scanError: "error",
scanNull: "null",
scanTrue: "true",
scanFalse: "false",
scanInt8: "int8",
scanUint8: "uint8",
scanInt16: "int16",
scanInt32: "int32",
scanInt64: "int64",
scanBignum: "bignum",
scanString: "string",
scanFloat64: "float64",
scanFloat32: "float32",
scanChar: "char",
scanPayload: "payload",
scanEndPayload: "endPayload",
scanContainerType: "containerType",
scanContainerLen: "containerLen",
}
// These values are returned by the state transition functions
// assigned to scanner.state and the method scanner.eof.
// They give details about the current state of the scan that
// callers might be interested to know about.
// It is okay to ignore the return value of any particular
// call to scanner.state: if one call returns scanError,
// every subsequent call will return scanError too.
const (
// Continue.
scanContinue = iota // uninteresting byte
scanBeginObject // begin object
scanEndObject // end object (implies scanObjectValue if possible)
scanBeginArray // begin array
scanEndArray // end array (implies scanArrayValue if possible)
scanSkipSpace // space byte; can skip; known to be last "continue" result
scanNull
scanTrue
scanFalse
scanInt8
scanUint8
scanInt16
scanInt32
scanInt64
scanBignum
scanString
scanFloat64
scanFloat32
scanChar
scanPayload
scanEndPayload
scanContainerType
scanContainerLen
// Stop.
scanEnd // top-level value ended *before* this byte; known to be first "stop" result
scanError // hit an error, scanner.err.
)
// These values are stored in the parseState stack.
// They give the current state of a composite value
// being scanned. If the parser is inside a nested value
// the parseState describes the nested state, outermost at entry 0.
const (
parseObjectKey = iota // parsing object key (before colon)
parseObjectValue // parsing object value (after colon)
parseArrayValue // parsing array value
parseObject
)
func scanTypeFromByte(c byte) (int, error) {
switch c {
case 'Z':
return scanNull, nil
case 'T':
return scanTrue, nil
case 'F':
return scanFalse, nil
case 'i':
return scanInt8, nil
case 'U':
return scanUint8, nil
case 'I':
return scanInt16, nil
case 'l':
return scanInt32, nil
case 'L':
return scanInt64, nil
case 'd':
return scanFloat32, nil
case 'D':
return scanFloat64, nil
case 'H':
return scanBignum, nil
case 'C':
return scanChar, nil
case 'S':
return scanString, nil
case '[':
return scanBeginArray, nil
case '{':
return scanBeginObject, nil
default:
return -1, fmt.Errorf("unknown type tag: %#v", string([]byte{c}))
}
}
// reset prepares the scanner for use.
// It must be called before calling s.step.
func (s *scanner) reset() {
s.step = stateBeginValue
s.parseState = s.parseState[0:0]
s.err = nil
s.redo = false
s.endTop = false
// TODO(imax): update undo/redo().
s.bytesLeft = 0
s.scanningBytes = false
}
// eof tells the scanner that the end of input has been reached.
// It returns a scan status just as s.step does.
func (s *scanner) eof() int {
if s.err != nil {
return scanError
}
if s.endTop {
return scanEnd
}
if s.err == nil {
s.err = &SyntaxError{"unexpected end of JSON input", s.bytes}
}
return scanError
}
// pushParseState pushes a new parse state p onto the parse stack.
func (s *scanner) pushParseState(p int) {
s.parseState = append(s.parseState, parseStackFrame{container: p})
}
// popParseState pops a parse state (already obtained) off the stack
// and updates s.step accordingly.
func (s *scanner) popParseState() {
n := len(s.parseState) - 1
s.parseState = s.parseState[0:n]
s.redo = false
if n == 0 {
s.step = stateEndTop
s.endTop = true
} else {
s.step = stateEndValue
}
}
func endValue(s *scanner) {
s.step = stateEndValue
if len(s.parseState) == 0 {
// We're done with scanning one top-level value.
s.endTop = true
s.step = stateEndTop
}
for len(s.parseState) > 0 {
ps := s.parseState[len(s.parseState)-1]
if !ps.hasCount || ps.itemsLeft > 0 {
break
}
s.popParseState()
}
}
func isSpace(c rune) bool {
return c == 'N'
}
func isType(c rune) bool {
switch c {
case 'Z', 'T', 'F', 'i', 'U', 'I', 'l', 'L', 'd', 'D', 'H', 'C', 'S', '[', '{':
return true
default:
return false
}
}
// stateBeginValue is the state at the beginning of the input.
func stateBeginValue(s *scanner, c int) int {
if isSpace(rune(c)) {
return scanSkipSpace
}
switch c {
case '{':
s.step = stateBeginObject
s.pushParseState(parseObject)
return scanBeginObject
case '[':
s.step = stateBeginArray
s.pushParseState(parseArrayValue)
return scanBeginArray
case 'S':
s.step = stateWantStringLen
return scanString
case 'T':
endValue(s)
return scanTrue
case 'F':
endValue(s)
return scanFalse
case 'Z':
endValue(s)
return scanNull
case 'i':
s.step = stateScanBytes
s.scanningBytes = true
s.bytesLeft = 1
s.afterBytes = endValue
return scanInt8
case 'U':
s.step = stateScanBytes
s.scanningBytes = true
s.bytesLeft = 1
s.afterBytes = endValue
return scanUint8
case 'I':
s.step = stateScanBytes
s.scanningBytes = true
s.bytesLeft = 2
s.afterBytes = endValue
return scanInt16
case 'l':
s.step = stateScanBytes
s.scanningBytes = true
s.bytesLeft = 4
s.afterBytes = endValue
return scanInt32
case 'L':
s.step = stateScanBytes
s.scanningBytes = true
s.bytesLeft = 8
s.afterBytes = endValue
return scanInt64
case 'd':
s.step = stateScanBytes
s.scanningBytes = true
s.bytesLeft = 4
s.afterBytes = endValue
return scanFloat32
case 'D':
s.step = stateScanBytes
s.scanningBytes = true
s.bytesLeft = 8
s.afterBytes = endValue
return scanFloat64
case 'H':
// TODO(imax): parse as uint64 or big number.
s.step = stateWantStringLen
return scanBignum
case 'C':
s.step = stateScanBytes
s.scanningBytes = true
s.bytesLeft = 1
s.afterBytes = endValue
return scanChar
}
return s.error(c, "looking for beginning of value")
}
// stateEndValue is the state after completing a value,
// such as after reading `{}` or `true` or `["x"`.
func stateEndValue(s *scanner, c int) int {
n := len(s.parseState)
if n == 0 {
// Completed top-level before the current byte.
s.step = stateEndTop
s.endTop = true
return stateEndTop(s, c)
}
ps := s.parseState[n-1]
switch ps.container {
case parseObject:
switch {
case ps.hasCount:
if ps.itemsLeft <= 0 {
s.popParseState()
return scanEndObject
}
case c == '}':
s.popParseState()
return scanEndObject
}
return stateObjectKey(s, c)
case parseArrayValue:
switch {
case ps.valueType != 0: // typed array (count is mandatory)
if ps.itemsLeft > 0 {
return stateTypedArrayItems(s, c)
}
s.popParseState()
return scanEndArray
case ps.valueType == 0 && ps.hasCount: // untyped array with count
if ps.itemsLeft > 0 {
return stateCountedArrayItems(s, c)
}
s.popParseState()
return scanEndArray
case c == ']':
s.popParseState()
return scanEndArray
}
return stateBeginValue(s, c)
}
return s.error(c, "")
}
// stateEndTop is the state after finishing the top-level value,
// such as after reading `{}` or `[1,2,3]`.
// Only space characters should be seen now.
func stateEndTop(s *scanner, c int) int {
if c != 'N' {
// Complain about non-space byte on next call.
s.error(c, "after top-level value")
}
return scanEnd
}
func stateWantStringLen(s *scanner, c int) int {
var r int
switch c {
case 'i':
s.bytesLeft = 1
r = scanInt8
case 'U':
s.bytesLeft = 1
r = scanUint8
case 'I':
s.bytesLeft = 2
r = scanInt16
case 'l':
s.bytesLeft = 4
r = scanInt32
case 'L':
s.bytesLeft = 8
r = scanInt64
default:
return s.error(c, "when expecting integer length")
}
s.step = stateStringLen
s.scanningBytes = true
s.lenBytes = make([]byte, 0, s.bytesLeft+1)
s.lenBytes = append(s.lenBytes, byte(c))
return r
}
func stateStringLen(s *scanner, c int) int {
s.bytesLeft--
s.lenBytes = append(s.lenBytes, byte(c))
if s.bytesLeft <= 0 {
switch s.lenBytes[0] {
case 'i':
s.bytesLeft = int(s.lenBytes[1])
case 'U':
s.bytesLeft = int(s.lenBytes[1])
case 'I':
var v int16
if err := binary.Read(bytes.NewBuffer(s.lenBytes[1:]), binary.BigEndian, &v); err != nil {
return s.error(c, fmt.Sprintf("invalid length: %s", err))
}
s.bytesLeft = int(v)
case 'l':
var v int32
if err := binary.Read(bytes.NewBuffer(s.lenBytes[1:]), binary.BigEndian, &v); err != nil {
return s.error(c, fmt.Sprintf("invalid length: %s", err))
}
s.bytesLeft = int(v)
case 'L':
var v int64
if err := binary.Read(bytes.NewBuffer(s.lenBytes[1:]), binary.BigEndian, &v); err != nil {
return s.error(c, fmt.Sprintf("invalid length: %s", err))
}
s.bytesLeft = int(v)
default:
return s.error(c, "invalid len type")
}
s.step = stateScanBytes
s.afterBytes = endValue
if s.bytesLeft == 0 {
// Shortcut for zero-length values.
s.scanningBytes = false
endValue(s)
}
return scanEndPayload
}
return scanContinue
}
/*
// stateNeg is the state after reading `-` during a number.
func stateNeg(s *scanner, c int) int {
if c == '0' {
s.step = state0
return scanContinue
}
if '1' <= c && c <= '9' {
s.step = state1
return scanContinue
}
return s.error(c, "in numeric literal")
}
// state1 is the state after reading a non-zero integer during a number,
// such as after reading `1` or `100` but not `0`.
func state1(s *scanner, c int) int {
if '0' <= c && c <= '9' {
s.step = state1
return scanContinue
}
return state0(s, c)
}
// state0 is the state after reading `0` during a number.
func state0(s *scanner, c int) int {
if c == '.' {
s.step = stateDot
return scanContinue
}
if c == 'e' || c == 'E' {
s.step = stateE
return scanContinue
}
return stateEndValue(s, c)
}
// stateDot is the state after reading the integer and decimal point in a number,
// such as after reading `1.`.
func stateDot(s *scanner, c int) int {
if '0' <= c && c <= '9' {
s.step = stateDot0
return scanContinue
}
return s.error(c, "after decimal point in numeric literal")
}
// stateDot0 is the state after reading the integer, decimal point, and subsequent
// digits of a number, such as after reading `3.14`.
func stateDot0(s *scanner, c int) int {
if '0' <= c && c <= '9' {
s.step = stateDot0
return scanContinue
}
if c == 'e' || c == 'E' {
s.step = stateE
return scanContinue
}
return stateEndValue(s, c)
}
// stateE is the state after reading the mantissa and e in a number,
// such as after reading `314e` or `0.314e`.
func stateE(s *scanner, c int) int {
if c == '+' {
s.step = stateESign
return scanContinue
}
if c == '-' {
s.step = stateESign
return scanContinue
}
return stateESign(s, c)
}
// stateESign is the state after reading the mantissa, e, and sign in a number,
// such as after reading `314e-` or `0.314e+`.
func stateESign(s *scanner, c int) int {
if '0' <= c && c <= '9' {
s.step = stateE0
return scanContinue
}
return s.error(c, "in exponent of numeric literal")
}
// stateE0 is the state after reading the mantissa, e, optional sign,
// and at least one digit of the exponent in a number,
// such as after reading `314e-2` or `0.314e+1` or `3.14e0`.
func stateE0(s *scanner, c int) int {
if '0' <= c && c <= '9' {
s.step = stateE0
return scanContinue
}
return stateEndValue(s, c)
}
*/
func stateScanBytes(s *scanner, c int) int {
s.bytesLeft--
if s.bytesLeft <= 0 {
s.scanningBytes = false
s.afterBytes(s)
s.afterBytes = nil
return scanEndPayload
}
return scanContinue
}
func stateBeginArray(s *scanner, c int) int {
switch c {
case '$':
s.step = stateArrayType
s.parseState[len(s.parseState)-1].hasCount = true
return scanContainerType
case '#':
s.step = stateArrayLen
s.parseState[len(s.parseState)-1].hasCount = true
return scanContainerLen
case ']':
endValue(s)
s.popParseState()
return scanEndArray
default:
return stateBeginValue(s, c)
}
}
func stateArrayType(s *scanner, c int) int {
switch {
case isType(rune(c)):
s.parseState[len(s.parseState)-1].valueType = byte(c)
s.step = stateArrayHashAfterType
return scanEndPayload
default:
return s.error(c, "expected type tag")
}
}
func stateArrayHashAfterType(s *scanner, c int) int {
switch c {
case '#':
s.step = stateArrayLenAfterType
return scanContainerLen
default:
return s.error(c, "expected #")
}
}
func stateArrayLenAfterType(s *scanner, c int) int {
var r int
switch c {
case 'i':
s.bytesLeft = 1
r = scanInt8
case 'U':
s.bytesLeft = 1
r = scanUint8
case 'I':
s.bytesLeft = 2
r = scanInt16
case 'l':
s.bytesLeft = 4
r = scanInt32
case 'L':
s.bytesLeft = 8
r = scanInt64
default:
return s.error(c, "when expecting integer length")
}
s.step = stateArrayLenBytesAfterType
s.scanningBytes = true
s.lenBytes = make([]byte, 0, s.bytesLeft+1)
s.lenBytes = append(s.lenBytes, byte(c))
return r
}
func stateArrayLenBytesAfterType(s *scanner, c int) int {
s.bytesLeft--
s.lenBytes = append(s.lenBytes, byte(c))
if s.bytesLeft <= 0 {
s.scanningBytes = false
switch s.lenBytes[0] {
case 'i':
s.parseState[len(s.parseState)-1].itemsLeft = int(s.lenBytes[1])
case 'U':
s.parseState[len(s.parseState)-1].itemsLeft = int(s.lenBytes[1])
case 'I':
var v int16
if err := binary.Read(bytes.NewBuffer(s.lenBytes[1:]), binary.BigEndian, &v); err != nil {
return s.error(c, fmt.Sprintf("invalid length: %s", err))
}
s.parseState[len(s.parseState)-1].itemsLeft = int(v)
case 'l':
var v int32
if err := binary.Read(bytes.NewBuffer(s.lenBytes[1:]), binary.BigEndian, &v); err != nil {
return s.error(c, fmt.Sprintf("invalid length: %s", err))
}
s.parseState[len(s.parseState)-1].itemsLeft = int(v)
case 'L':
var v int64
if err := binary.Read(bytes.NewBuffer(s.lenBytes[1:]), binary.BigEndian, &v); err != nil {
return s.error(c, fmt.Sprintf("invalid length: %s", err))
}
s.parseState[len(s.parseState)-1].itemsLeft = int(v)
default:
return s.error(c, "invalid len type")
}
if s.parseState[len(s.parseState)-1].itemsLeft == 0 {
endValue(s)
} else {
s.step = stateTypedArrayItems
}
return scanEndPayload
}
return scanContinue
}
func stateTypedArrayItems(s *scanner, c int) int {
s.parseState[len(s.parseState)-1].itemsLeft--
stateBeginValue(s, int(s.parseState[len(s.parseState)-1].valueType))
return s.step(s, c)
}
func stateArrayLen(s *scanner, c int) int {
var r int
switch c {
case 'i':
s.bytesLeft = 1
r = scanInt8
case 'U':
s.bytesLeft = 1
r = scanUint8
case 'I':
s.bytesLeft = 2
r = scanInt16
case 'l':
s.bytesLeft = 4
r = scanInt32
case 'L':
s.bytesLeft = 8
r = scanInt64
default:
return s.error(c, "when expecting integer length")
}
s.step = stateArrayLenBytes
s.scanningBytes = true
s.lenBytes = make([]byte, 0, s.bytesLeft+1)
s.lenBytes = append(s.lenBytes, byte(c))
return r
}
func stateArrayLenBytes(s *scanner, c int) int {
s.bytesLeft--
s.lenBytes = append(s.lenBytes, byte(c))
if s.bytesLeft <= 0 {
s.scanningBytes = false
switch s.lenBytes[0] {
case 'i':
s.parseState[len(s.parseState)-1].itemsLeft = int(s.lenBytes[1])
case 'U':
s.parseState[len(s.parseState)-1].itemsLeft = int(s.lenBytes[1])
case 'I':
var v int16
if err := binary.Read(bytes.NewBuffer(s.lenBytes[1:]), binary.BigEndian, &v); err != nil {
return s.error(c, fmt.Sprintf("invalid length: %s", err))
}
s.parseState[len(s.parseState)-1].itemsLeft = int(v)
case 'l':
var v int32
if err := binary.Read(bytes.NewBuffer(s.lenBytes[1:]), binary.BigEndian, &v); err != nil {
return s.error(c, fmt.Sprintf("invalid length: %s", err))
}
s.parseState[len(s.parseState)-1].itemsLeft = int(v)
case 'L':
var v int64
if err := binary.Read(bytes.NewBuffer(s.lenBytes[1:]), binary.BigEndian, &v); err != nil {
return s.error(c, fmt.Sprintf("invalid length: %s", err))
}
s.parseState[len(s.parseState)-1].itemsLeft = int(v)
default:
return s.error(c, "invalid len type")
}
if s.parseState[len(s.parseState)-1].itemsLeft == 0 {
endValue(s)
} else {
s.step = stateCountedArrayItems
}
return scanEndPayload
}
return scanContinue
}
func stateCountedArrayItems(s *scanner, c int) int {
s.parseState[len(s.parseState)-1].itemsLeft--
return stateBeginValue(s, c)
}
func stateBeginObject(s *scanner, c int) int {
switch c {
case '$':
s.step = stateObjectType
s.parseState[len(s.parseState)-1].hasCount = true
return scanContainerType
case '#':
s.step = stateObjectLen
s.parseState[len(s.parseState)-1].hasCount = true
return scanContainerLen
case '}':
endValue(s)
s.popParseState()
return scanEndObject
default:
return stateObjectKey(s, c)
}
}
func stateObjectType(s *scanner, c int) int {
switch {
case isType(rune(c)):
s.parseState[len(s.parseState)-1].valueType = byte(c)
s.step = stateObjectHashAfterType
return scanEndPayload
default:
return s.error(c, "expected type tag")
}
}
func stateObjectHashAfterType(s *scanner, c int) int {
switch c {
case '#':
s.step = stateObjectLenAfterType
return scanContainerLen
default:
return s.error(c, "expected #")
}
}
func stateObjectLenAfterType(s *scanner, c int) int {
var r int
switch c {
case 'i':
s.bytesLeft = 1
r = scanInt8
case 'U':
s.bytesLeft = 1
r = scanUint8
case 'I':
s.bytesLeft = 2
r = scanInt16
case 'l':
s.bytesLeft = 4
r = scanInt32
case 'L':
s.bytesLeft = 8
r = scanInt64
default:
return s.error(c, "when expecting integer length")
}
s.step = stateObjectLenBytesAfterType
s.scanningBytes = true
s.lenBytes = make([]byte, 0, s.bytesLeft+1)
s.lenBytes = append(s.lenBytes, byte(c))
return r
}
func stateObjectLenBytesAfterType(s *scanner, c int) int {
s.bytesLeft--
s.lenBytes = append(s.lenBytes, byte(c))
if s.bytesLeft <= 0 {
s.scanningBytes = false
switch s.lenBytes[0] {
case 'i':
s.parseState[len(s.parseState)-1].itemsLeft = int(s.lenBytes[1])
case 'U':
s.parseState[len(s.parseState)-1].itemsLeft = int(s.lenBytes[1])
case 'I':
var v int16
if err := binary.Read(bytes.NewBuffer(s.lenBytes[1:]), binary.BigEndian, &v); err != nil {
return s.error(c, fmt.Sprintf("invalid length: %s", err))
}
s.parseState[len(s.parseState)-1].itemsLeft = int(v)
case 'l':
var v int32
if err := binary.Read(bytes.NewBuffer(s.lenBytes[1:]), binary.BigEndian, &v); err != nil {
return s.error(c, fmt.Sprintf("invalid length: %s", err))
}
s.parseState[len(s.parseState)-1].itemsLeft = int(v)
case 'L':
var v int64
if err := binary.Read(bytes.NewBuffer(s.lenBytes[1:]), binary.BigEndian, &v); err != nil {
return s.error(c, fmt.Sprintf("invalid length: %s", err))
}
s.parseState[len(s.parseState)-1].itemsLeft = int(v)
default:
return s.error(c, "invalid len type")
}
if s.parseState[len(s.parseState)-1].itemsLeft == 0 {
endValue(s)
} else {
s.step = stateObjectKey
}
return scanEndPayload
}
return scanContinue
}
func stateObjectLen(s *scanner, c int) int {
var r int
switch c {
case 'i':
s.bytesLeft = 1
r = scanInt8
case 'U':
s.bytesLeft = 1
r = scanUint8
case 'I':
s.bytesLeft = 2
r = scanInt16
case 'l':
s.bytesLeft = 4
r = scanInt32
case 'L':
s.bytesLeft = 8
r = scanInt64
default:
return s.error(c, "when expecting integer length")
}
s.step = stateObjectLenBytes
s.scanningBytes = true
s.lenBytes = make([]byte, 0, s.bytesLeft+1)
s.lenBytes = append(s.lenBytes, byte(c))
return r
}
func stateObjectLenBytes(s *scanner, c int) int {
s.bytesLeft--
s.lenBytes = append(s.lenBytes, byte(c))
if s.bytesLeft <= 0 {
s.scanningBytes = false
switch s.lenBytes[0] {
case 'i':
s.parseState[len(s.parseState)-1].itemsLeft = int(s.lenBytes[1])
case 'U':
s.parseState[len(s.parseState)-1].itemsLeft = int(s.lenBytes[1])
case 'I':
var v int16
if err := binary.Read(bytes.NewBuffer(s.lenBytes[1:]), binary.BigEndian, &v); err != nil {
return s.error(c, fmt.Sprintf("invalid length: %s", err))
}
s.parseState[len(s.parseState)-1].itemsLeft = int(v)
case 'l':
var v int32
if err := binary.Read(bytes.NewBuffer(s.lenBytes[1:]), binary.BigEndian, &v); err != nil {
return s.error(c, fmt.Sprintf("invalid length: %s", err))
}
s.parseState[len(s.parseState)-1].itemsLeft = int(v)
case 'L':
var v int64
if err := binary.Read(bytes.NewBuffer(s.lenBytes[1:]), binary.BigEndian, &v); err != nil {
return s.error(c, fmt.Sprintf("invalid length: %s", err))
}
s.parseState[len(s.parseState)-1].itemsLeft = int(v)
default:
return s.error(c, "invalid len type")
}
if s.parseState[len(s.parseState)-1].itemsLeft == 0 {
endValue(s)
} else {
s.step = stateObjectKey
}
return scanEndPayload
}