forked from ateleshev/go-json
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscan.go
829 lines (747 loc) · 17.6 KB
/
scan.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
// Copyright 2013 Gary Burd. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package json
import (
"io"
"strconv"
"unicode"
"unicode/utf16"
"unicode/utf8"
)
// Kind represents the kind a JSON document element.
type Kind int
const (
// Null represents a JSON null element.
Null Kind = iota
// Bool represents a JSON bool element.
Bool
// String represents a JSON string element.
String
// Number represents a JSON number element.
Number
// Array represents the start of a JSON array element.
Array
// Object represents the start of a JSON object element.
Object
// End represents the end of an object or array element.
End
)
func (k Kind) String() string {
switch k {
case Null:
return "null"
case Bool:
return "bool"
case Number:
return "number"
case Array:
return "array"
case Object:
return "object"
case End:
return "end"
default:
return "unknown"
}
}
// Scanner reads a JSON document from an io.Reader. Successive calls to the
// Scan method step through the elements of the document as follows:
//
// element = Null | Bool | Number | String | object | array
// array = Array element* End
// object = Object element* End
//
// Scanning stops unrecoverably at EOF, the first I/O error, or a syntax error.
// When a scan stops, the reader may have advanced arbitrarily far past the
// last token.
//
// When scanning strings, invalid UTF-8 or invalid UTF-16 surrogate pairs are
// not treated as an error. Instead, they are replaced by the Unicode
// replacement character U+FFFD.
type Scanner struct {
cook bool // if true, current name or value contains non-ASCII byte.
pos int // write position in buf.
buf []byte
states []stateFunc
isName bool // if true, then the current string is an boject member name.
err error
eofOK bool // if true, then EOF is expected in the input.
kind Kind // kind of the current element
boolValue bool
data [2]struct {
pos, end int // location in buf
cook bool // if true, data may contain escapes or invalid UTF-8.
}
rd io.Reader
}
const (
nameData = iota
valueData
)
type stateFunc func(*Scanner, byte) stateFunc
// NewScanner allocates and initializes a new scanner.
func NewScanner(rd io.Reader) *Scanner {
return &Scanner{
rd: rd,
buf: make([]byte, 0, 1024),
states: []stateFunc{(*Scanner).stateStart},
}
}
// Scan advances the Scanner to the next element, which will then be available
// through the Kind, Value, and BoolValue methods. Scan returns false if there
// are no more elements in the input or an error is encountered. The Err method
// returns the error if any.
func (s *Scanner) Scan() bool {
s.kind = -1
s.data[nameData].pos = -1
s.data[valueData].pos = -1
state := s.states[len(s.states)-1]
for {
for _, b := range s.buf[s.pos:] {
state = state(s, b)
s.pos += 1
if state == nil {
return s.kind >= 0
}
}
if s.err == nil {
s.fill()
if s.pos < len(s.buf) {
continue
}
}
if s.err != io.EOF {
return false
}
state = state(s, ' ')
s.pos = len(s.buf)
if state == nil && s.kind >= 0 {
return true
}
if !s.eofOK {
s.err = io.ErrUnexpectedEOF
}
return false
}
}
func (s *Scanner) fill() {
n := 0
for i := range s.data {
if pos := s.data[i].pos; pos >= 0 {
end := s.data[i].end
if end < 0 {
end = s.pos
}
n += end - pos
}
}
buf := s.buf[:cap(s.buf)]
const minRead = 512
if len(buf)-n < minRead {
buf = make([]byte, 2*len(buf)+minRead)
}
n = 0
for i := range s.data {
if pos := s.data[i].pos; pos >= 0 {
end := s.data[i].end
if end < 0 {
end = s.pos
} else {
s.data[i].end = n + end - pos
}
s.data[i].pos = n
n += copy(buf[n:], s.buf[pos:end])
}
}
var nn int
nn, s.err = s.rd.Read(buf[n:])
s.buf = buf[:n+nn]
s.pos = n
}
func (s *Scanner) stateStart(b byte) stateFunc {
s.top((*Scanner).stateEnd)
return s.stateValue(b)
}
func (s *Scanner) stateEnd(b byte) stateFunc {
switch {
case isWhiteSpace(b):
s.eofOK = true
return (*Scanner).stateEnd
default:
return s.syntaxError(b, expectWhitespace)
}
}
func (s *Scanner) stateValue(b byte) stateFunc {
switch {
case isWhiteSpace(b):
return (*Scanner).stateValue
case b == '"':
s.isName = false
s.cook = false
s.data[valueData].pos = s.pos + 1
s.data[valueData].end = -1
return (*Scanner).stateString
case b == '-':
s.data[valueData].pos = s.pos
s.data[valueData].end = -1
return (*Scanner).stateNumberNeg
case b == '0':
s.data[valueData].pos = s.pos
s.data[valueData].end = -1
return (*Scanner).stateNumberDotOrExp
case '1' <= b && b <= '9':
s.data[valueData].pos = s.pos
s.data[valueData].end = -1
return (*Scanner).stateNumberDigits
case b == 't':
return (*Scanner).stateTr
case b == 'f':
return (*Scanner).stateFa
case b == 'n':
return (*Scanner).stateNu
case b == '[':
s.push((*Scanner).stateArrayElementOrClose)
s.kind = Array
return nil
case b == '{':
s.push((*Scanner).stateObjectKeyOrClose)
s.kind = Object
return nil
default:
return s.syntaxError(b, expectValue)
}
}
func (s *Scanner) stateArrayElementOrClose(b byte) stateFunc {
switch {
case isWhiteSpace(b):
return (*Scanner).stateArrayElementOrClose
case b == ']':
s.pop()
s.kind = End
return nil
default:
s.top((*Scanner).stateArrayCommaOrClose)
return s.stateValue(b)
}
}
func (s *Scanner) stateArrayCommaOrClose(b byte) stateFunc {
switch {
case isWhiteSpace(b):
return (*Scanner).stateArrayCommaOrClose
case b == ',':
return (*Scanner).stateValue
case b == ']':
s.pop()
s.kind = End
return nil
default:
return s.syntaxError(b, expectArrayCommaOrClose)
}
}
func (s *Scanner) stateObjectKeyOrClose(b byte) stateFunc {
switch {
case isWhiteSpace(b):
return (*Scanner).stateObjectKeyOrClose
case b == '}':
s.pop()
s.kind = End
return nil
case b == '"':
s.top((*Scanner).stateObjectCommaOrClose)
s.cook = false
s.isName = true
s.data[nameData].pos = s.pos + 1
s.data[nameData].end = -1
return (*Scanner).stateString
default:
return s.syntaxError(b, expectObjectKeyOrClose)
}
}
func (s *Scanner) stateObjectColon(b byte) stateFunc {
switch {
case isWhiteSpace(b):
return (*Scanner).stateObjectColon
case b == ':':
return (*Scanner).stateValue
default:
return s.syntaxError(b, expectObjectColon)
}
}
func (s *Scanner) stateObjectCommaOrClose(b byte) stateFunc {
switch {
case isWhiteSpace(b):
return (*Scanner).stateObjectCommaOrClose
case b == ',':
return (*Scanner).stateObjectKey
case b == '}':
s.pop()
s.kind = End
return nil
default:
return s.syntaxError(b, expectObjectCommaOrClose)
}
}
func (s *Scanner) stateObjectKey(b byte) stateFunc {
switch {
case isWhiteSpace(b):
return (*Scanner).stateObjectKey
case b == '"':
s.cook = false
s.isName = true
s.data[nameData].pos = s.pos + 1
s.data[nameData].end = -1
return (*Scanner).stateString
default:
return s.syntaxError(b, expectObjectKey)
}
}
func (s *Scanner) stateNu(b byte) stateFunc {
switch {
case b == 'u':
return (*Scanner).stateNul
default:
return s.syntaxError(b, expectNu)
}
}
func (s *Scanner) stateNul(b byte) stateFunc {
switch {
case b == 'l':
return (*Scanner).stateNull
default:
return s.syntaxError(b, expectNul)
}
}
func (s *Scanner) stateNull(b byte) stateFunc {
switch {
case b == 'l':
s.kind = Null
return nil
default:
return s.syntaxError(b, expectNull)
}
}
func (s *Scanner) stateTr(b byte) stateFunc {
switch {
case b == 'r':
return (*Scanner).stateTru
default:
return s.syntaxError(b, expectTr)
}
}
func (s *Scanner) stateTru(b byte) stateFunc {
switch {
case b == 'u':
return (*Scanner).stateTrue
default:
return s.syntaxError(b, expectTru)
}
}
func (s *Scanner) stateTrue(b byte) stateFunc {
switch {
case b == 'e':
s.kind = Bool
s.boolValue = true
return nil
default:
return s.syntaxError(b, expectTrue)
}
}
func (s *Scanner) stateFa(b byte) stateFunc {
switch {
case b == 'a':
return (*Scanner).stateFal
default:
return s.syntaxError(b, expectFa)
}
}
func (s *Scanner) stateFal(b byte) stateFunc {
switch {
case b == 'l':
return (*Scanner).stateFals
default:
return s.syntaxError(b, expectFal)
}
}
func (s *Scanner) stateFals(b byte) stateFunc {
switch {
case b == 's':
return (*Scanner).stateFalse
default:
return s.syntaxError(b, expectFals)
}
}
func (s *Scanner) stateFalse(b byte) stateFunc {
switch {
case b == 'e':
s.kind = Bool
s.boolValue = false
return nil
default:
return s.syntaxError(b, expectFalse)
}
}
func (s *Scanner) stateString(b byte) stateFunc {
switch {
case b == '"':
if s.isName {
s.data[nameData].end = s.pos
s.data[nameData].cook = s.cook
return (*Scanner).stateObjectColon
}
s.data[valueData].end = s.pos
s.data[valueData].cook = s.cook
s.kind = String
return nil
case b == '\\':
s.cook = true
return (*Scanner).stateStringEscape
case b < ' ':
return s.syntaxError(b, expectStringNotControl)
case b < utf8.RuneSelf:
return (*Scanner).stateString
default:
s.cook = true
return (*Scanner).stateString
}
}
func (s *Scanner) stateStringEscape(b byte) stateFunc {
switch {
case b == '"' || b == '\\' || b == 'b' || b == 'f' || b == 'n' || b == 'r' || b == 't' || b == '/':
return (*Scanner).stateString
case b == 'u':
return (*Scanner).stateStringUnicodeEscape1
default:
return s.syntaxError(b, expectStringEscape)
}
}
func (s *Scanner) stateStringUnicodeEscape1(b byte) stateFunc {
switch {
case isHexDigit(b):
return (*Scanner).stateStringUnicodeEscape2
default:
return s.syntaxError(b, expectStringUnicodeEscape1)
}
}
func (s *Scanner) stateStringUnicodeEscape2(b byte) stateFunc {
switch {
case isHexDigit(b):
return (*Scanner).stateStringUnicodeEscape3
default:
return s.syntaxError(b, expectStringUnicodeEscape2)
}
}
func (s *Scanner) stateStringUnicodeEscape3(b byte) stateFunc {
switch {
case isHexDigit(b):
return (*Scanner).stateStringUnicodeEscape4
default:
return s.syntaxError(b, expectStringUnicodeEscape3)
}
}
func (s *Scanner) stateStringUnicodeEscape4(b byte) stateFunc {
switch {
case isHexDigit(b):
return (*Scanner).stateString
default:
return s.syntaxError(b, expectStringUnicodeEscape4)
}
}
func (s *Scanner) stateNumberNeg(b byte) stateFunc {
switch {
case b == '0':
return (*Scanner).stateNumberDotOrExp
case '1' <= b && b <= '9':
return (*Scanner).stateNumberDigits
default:
return s.syntaxError(b, expectNumberNeg)
}
}
func (s *Scanner) stateNumberDigits(b byte) stateFunc {
switch {
case isDecimalDigit(b):
return (*Scanner).stateNumberDigits
default:
return s.stateNumberDotOrExp(b)
}
}
func (s *Scanner) stateNumberDotOrExp(b byte) stateFunc {
switch {
case b == '.':
return (*Scanner).stateNumberFrac
case b == 'e' || b == 'E':
return (*Scanner).stateNumberExp
default:
return s.finishNumber()
}
}
func (s *Scanner) stateNumberFrac(b byte) stateFunc {
switch {
case isDecimalDigit(b):
return (*Scanner).stateNumberFracDigits
default:
return s.syntaxError(b, expectNumberFrac)
}
}
func (s *Scanner) stateNumberFracDigits(b byte) stateFunc {
switch {
case isDecimalDigit(b):
return (*Scanner).stateNumberFracDigits
case b == 'e' || b == 'E':
return (*Scanner).stateNumberExp
default:
return s.finishNumber()
}
}
func (s *Scanner) stateNumberExp(b byte) stateFunc {
switch {
case b == '+' || b == '-':
return (*Scanner).stateNumberExpDigit
case isDecimalDigit(b):
return (*Scanner).stateNumberExpDigits
default:
return s.syntaxError(b, expectNumberExp)
}
}
func (s *Scanner) stateNumberExpDigit(b byte) stateFunc {
switch {
case isDecimalDigit(b):
return (*Scanner).stateNumberExpDigits
default:
return s.syntaxError(b, expectNumberExpDigit)
}
}
func (s *Scanner) stateNumberExpDigits(b byte) stateFunc {
switch {
case isDecimalDigit(b):
return (*Scanner).stateNumberExpDigits
default:
return s.finishNumber()
}
}
func (s *Scanner) finishNumber() stateFunc {
s.kind = Number
s.data[valueData].end = s.pos
s.pos -= 1
return nil
}
func (s *Scanner) top(f stateFunc) {
s.states[len(s.states)-1] = f
}
func (s *Scanner) push(f stateFunc) {
s.states = append(s.states, f)
}
func (s *Scanner) pop() {
s.states = s.states[:len(s.states)-1]
}
// Kind returns the kind of the current value.
func (s *Scanner) Kind() Kind {
return s.kind
}
// Err returns the first non-EOF error that was encountered by the Scanner.
func (s *Scanner) Err() error {
err := s.err
if err == io.EOF {
return nil
}
return err
}
// Name returns the object member name of the current value. The underlying
// array may point to data that will be overwritten by a subsequent call to
// Scan.
func (s *Scanner) Name() []byte {
return s.cookedData(nameData)
}
// Value returns the bytes of the current string or number value. The
// underlying array may point to data that will be overwritten by a
// subsequent call to Scan.
func (s *Scanner) Value() []byte {
return s.cookedData(valueData)
}
func (s *Scanner) cookedData(dataIndex int) []byte {
data := &s.data[dataIndex]
if data.pos < 0 {
return nil
}
rbuf := s.buf[data.pos:data.end]
if !data.cook {
return rbuf
}
r := 0
w := 0
wbuf := rbuf
for r < len(rbuf) {
switch b := rbuf[r]; {
case b == '\\':
r++
b = rbuf[r]
if b != 'u' {
switch b {
case 'b':
b = '\b'
case 'f':
b = '\f'
case 'n':
b = '\n'
case 'r':
b = '\r'
case 't':
b = '\t'
}
wbuf[w] = b
r++
w++
} else {
c := parseHex(rbuf[r+1 : r+5])
r += 5
if utf16.IsSurrogate(c) {
if r+6 <= len(rbuf) && rbuf[r] == '\\' && rbuf[r+1] == 'u' {
c = utf16.DecodeRune(c, parseHex(rbuf[r+2:r+6]))
if c != unicode.ReplacementChar {
r += 6
}
} else {
c = unicode.ReplacementChar
}
}
w += utf8.EncodeRune(wbuf[w:], c)
}
case b < utf8.RuneSelf:
wbuf[w] = b
r++
w++
default:
c, n := utf8.DecodeRune(rbuf[r:])
r += n
if c == utf8.RuneError && n < 3 &&
((&wbuf[0] == &rbuf[0] && w+3 >= r) ||
(w+3 >= len(wbuf))) {
buf := make([]byte, w+3+(4*(len(rbuf)-r))/3)
copy(buf, wbuf[:w])
wbuf = buf
}
w += utf8.EncodeRune(wbuf[w:], c)
}
}
return wbuf[:w]
}
// BoolValue returns the value of the current boolean value.
func (s *Scanner) BoolValue() bool {
return s.boolValue
}
func (s *Scanner) syntaxError(b byte, expect string) stateFunc {
s.err = &SyntaxError{s.pos, b, expect}
return nil
}
type SyntaxError struct {
Pos int
b byte
expect string
}
func (e *SyntaxError) Error() string {
return "expected " + e.expect + ", found " + strconv.QuoteRune(rune(e.b))
}
const (
expectWhitespace = "whitespace"
expectValue = "start of JSON value"
expectArrayCommaOrClose = "',' or ']' in array"
expectObjectKeyOrClose = "key or '}' in object"
expectObjectColon = "':' in object member"
expectObjectCommaOrClose = "',' or '}' in object"
expectObjectKey = "string key in object"
expectNu = "'u' in null"
expectNul = "'l' in null"
expectNull = "'l' in null"
expectTr = "'r' in true"
expectTru = "'u' in true"
expectTrue = "'e' in true"
expectFa = "'a' in false"
expectFal = "'l' in false"
expectFals = "'s' in false"
expectFalse = "'e' in false"
expectStringNotControl = "non-control byte"
expectStringEscape = "string escape"
expectStringUnicodeEscape1 = "hex digit following \\u"
expectStringUnicodeEscape2 = "hex digit following \\u"
expectStringUnicodeEscape3 = "hex digit following \\u"
expectStringUnicodeEscape4 = "hex digit following \\u"
expectNumberNeg = "digit after '-'"
expectNumberFrac = "digit after '.'"
expectNumberExp = "exponent"
expectNumberExpDigit = "exponent digits"
)
func isWhiteSpace(b byte) bool {
return b == ' ' || b == '\n' || b == '\r' || b == '\t'
}
func isHexDigit(b byte) bool {
return ('0' <= b && b <= '9') ||
('a' <= b && b <= 'f') ||
('A' <= b && b <= 'F')
}
func isDecimalDigit(b byte) bool {
return '0' <= b && b <= '9'
}
func parseHex(p []byte) rune {
var r rune
for _, b := range p {
switch {
case '0' <= b && b <= '9':
r = r<<4 + rune(b-'0')
case 'a' <= b && b <= 'f':
r = r<<4 + rune(b-'a'+10)
case 'A' <= b && b <= 'F':
r = r<<4 + rune(b-'A'+10)
}
}
return r
}
func (s *Scanner) ArrayScanner() ArrayScanner {
return ArrayScanner{s: s, n: len(s.states)}
}
func (s *Scanner) ObjectScanner() ObjectScanner {
return ObjectScanner{s: s, n: len(s.states)}
}
// ArrayScanner is a helper for scanning arrays.
type ArrayScanner struct {
s *Scanner
n int
name string
}
// ObjectScanner is a helper for scanning objects.
type ObjectScanner struct {
s *Scanner
n int
name string
}
// Scan advances to the next element at the current nesting level, possibly
// skipping over nested elements. Scan returns false at the End element for the
// current level or if an error is encountered.
func (as *ArrayScanner) Scan() bool {
s := as.s
for len(s.states) > as.n {
if !s.Scan() {
return false
}
}
return s.Scan() && s.Kind() != End
}
// Scan advances to the next element at the current nesting level, possibly
// skipping over nested elements. Scan returns false at the End element for the
// current level or if an error is encountered.
func (os *ObjectScanner) Scan() bool {
s := os.s
for len(s.states) > os.n {
if !s.Scan() {
return false
}
}
v := s.Scan() && s.Kind() != End
if v {
os.name = string(s.Name())
}
return v
}
// Name returns the last member named scanned at the current nesting level.
func (os *ObjectScanner) Name() string {
return os.name
}