blob: 970804388733d0ea92d6dab3bd7ec7f06c7864c8 [file] [log] [blame]
Serge Bazanskicc25bdf2018-10-25 14:02:58 +02001// Copyright 2010 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5package json
6
7// JSON value parser state machine.
8// Just about at the limit of what is reasonable to write by hand.
9// Some parts are a bit tedious, but overall it nicely factors out the
10// otherwise common code from the multiple scanning functions
11// in this package (Compact, Indent, checkValid, nextValue, etc).
12//
13// This file starts with two simple examples using the scanner
14// before diving into the scanner itself.
15
16import "strconv"
17
18// checkValid verifies that data is valid JSON-encoded data.
19// scan is passed in for use by checkValid to avoid an allocation.
20func checkValid(data []byte, scan *scanner) error {
21 scan.reset()
22 for _, c := range data {
23 scan.bytes++
24 if scan.step(scan, c) == scanError {
25 return scan.err
26 }
27 }
28 if scan.eof() == scanError {
29 return scan.err
30 }
31 return nil
32}
33
34// nextValue splits data after the next whole JSON value,
35// returning that value and the bytes that follow it as separate slices.
36// scan is passed in for use by nextValue to avoid an allocation.
37func nextValue(data []byte, scan *scanner) (value, rest []byte, err error) {
38 scan.reset()
39 for i, c := range data {
40 v := scan.step(scan, c)
41 if v >= scanEndObject {
42 switch v {
43 // probe the scanner with a space to determine whether we will
44 // get scanEnd on the next character. Otherwise, if the next character
45 // is not a space, scanEndTop allocates a needless error.
46 case scanEndObject, scanEndArray, scanEndParams:
47 if scan.step(scan, ' ') == scanEnd {
48 return data[:i+1], data[i+1:], nil
49 }
50 case scanError:
51 return nil, nil, scan.err
52 case scanEnd:
53 return data[:i], data[i:], nil
54 }
55 }
56 }
57 if scan.eof() == scanError {
58 return nil, nil, scan.err
59 }
60 return data, nil, nil
61}
62
63// A SyntaxError is a description of a JSON syntax error.
64type SyntaxError struct {
65 msg string // description of error
66 Offset int64 // error occurred after reading Offset bytes
67}
68
69func (e *SyntaxError) Error() string { return e.msg }
70
71// A scanner is a JSON scanning state machine.
72// Callers call scan.reset() and then pass bytes in one at a time
73// by calling scan.step(&scan, c) for each byte.
74// The return value, referred to as an opcode, tells the
75// caller about significant parsing events like beginning
76// and ending literals, objects, and arrays, so that the
77// caller can follow along if it wishes.
78// The return value scanEnd indicates that a single top-level
79// JSON value has been completed, *before* the byte that
80// just got passed in. (The indication must be delayed in order
81// to recognize the end of numbers: is 123 a whole value or
82// the beginning of 12345e+6?).
83type scanner struct {
84 // The step is a func to be called to execute the next transition.
85 // Also tried using an integer constant and a single func
86 // with a switch, but using the func directly was 10% faster
87 // on a 64-bit Mac Mini, and it's nicer to read.
88 step func(*scanner, byte) int
89
90 // Reached end of top-level value.
91 endTop bool
92
93 // Stack of what we're in the middle of - array values, object keys, object values.
94 parseState []int
95
96 // Error that happened, if any.
97 err error
98
99 // 1-byte redo (see undo method)
100 redo bool
101 redoCode int
102 redoState func(*scanner, byte) int
103
104 // total bytes consumed, updated by decoder.Decode
105 bytes int64
106}
107
108// These values are returned by the state transition functions
109// assigned to scanner.state and the method scanner.eof.
110// They give details about the current state of the scan that
111// callers might be interested to know about.
112// It is okay to ignore the return value of any particular
113// call to scanner.state: if one call returns scanError,
114// every subsequent call will return scanError too.
115const (
116 // Continue.
117 scanContinue = iota // uninteresting byte
118 scanBeginLiteral // end implied by next result != scanContinue
119 scanBeginObject // begin object
120 scanObjectKey // just finished object key (string)
121 scanObjectValue // just finished non-last object value
122 scanEndObject // end object (implies scanObjectValue if possible)
123 scanBeginArray // begin array
124 scanArrayValue // just finished array value
125 scanEndArray // end array (implies scanArrayValue if possible)
126 scanBeginName // begin function call
127 scanParam // begin function argument
128 scanEndParams // end function call
129 scanSkipSpace // space byte; can skip; known to be last "continue" result
130
131 // Stop.
132 scanEnd // top-level value ended *before* this byte; known to be first "stop" result
133 scanError // hit an error, scanner.err.
134)
135
136// These values are stored in the parseState stack.
137// They give the current state of a composite value
138// being scanned. If the parser is inside a nested value
139// the parseState describes the nested state, outermost at entry 0.
140const (
141 parseObjectKey = iota // parsing object key (before colon)
142 parseObjectValue // parsing object value (after colon)
143 parseArrayValue // parsing array value
144 parseName // parsing unquoted name
145 parseParam // parsing function argument value
146)
147
148// reset prepares the scanner for use.
149// It must be called before calling s.step.
150func (s *scanner) reset() {
151 s.step = stateBeginValue
152 s.parseState = s.parseState[0:0]
153 s.err = nil
154 s.redo = false
155 s.endTop = false
156}
157
158// eof tells the scanner that the end of input has been reached.
159// It returns a scan status just as s.step does.
160func (s *scanner) eof() int {
161 if s.err != nil {
162 return scanError
163 }
164 if s.endTop {
165 return scanEnd
166 }
167 s.step(s, ' ')
168 if s.endTop {
169 return scanEnd
170 }
171 if s.err == nil {
172 s.err = &SyntaxError{"unexpected end of JSON input", s.bytes}
173 }
174 return scanError
175}
176
177// pushParseState pushes a new parse state p onto the parse stack.
178func (s *scanner) pushParseState(p int) {
179 s.parseState = append(s.parseState, p)
180}
181
182// popParseState pops a parse state (already obtained) off the stack
183// and updates s.step accordingly.
184func (s *scanner) popParseState() {
185 n := len(s.parseState) - 1
186 s.parseState = s.parseState[0:n]
187 s.redo = false
188 if n == 0 {
189 s.step = stateEndTop
190 s.endTop = true
191 } else {
192 s.step = stateEndValue
193 }
194}
195
196func isSpace(c byte) bool {
197 return c == ' ' || c == '\t' || c == '\r' || c == '\n'
198}
199
200// stateBeginValueOrEmpty is the state after reading `[`.
201func stateBeginValueOrEmpty(s *scanner, c byte) int {
202 if c <= ' ' && isSpace(c) {
203 return scanSkipSpace
204 }
205 if c == ']' {
206 return stateEndValue(s, c)
207 }
208 return stateBeginValue(s, c)
209}
210
211// stateBeginValue is the state at the beginning of the input.
212func stateBeginValue(s *scanner, c byte) int {
213 if c <= ' ' && isSpace(c) {
214 return scanSkipSpace
215 }
216 switch c {
217 case '{':
218 s.step = stateBeginStringOrEmpty
219 s.pushParseState(parseObjectKey)
220 return scanBeginObject
221 case '[':
222 s.step = stateBeginValueOrEmpty
223 s.pushParseState(parseArrayValue)
224 return scanBeginArray
225 case '"':
226 s.step = stateInString
227 return scanBeginLiteral
228 case '-':
229 s.step = stateNeg
230 return scanBeginLiteral
231 case '0': // beginning of 0.123
232 s.step = state0
233 return scanBeginLiteral
234 case 'n':
235 s.step = stateNew0
236 return scanBeginName
237 }
238 if '1' <= c && c <= '9' { // beginning of 1234.5
239 s.step = state1
240 return scanBeginLiteral
241 }
242 if isName(c) {
243 s.step = stateName
244 return scanBeginName
245 }
246 return s.error(c, "looking for beginning of value")
247}
248
249func isName(c byte) bool {
250 return c == '$' || c == '_' || 'a' <= c && c <= 'z' || 'A' <= c && c <= 'Z' || '0' <= c && c <= '9'
251}
252
253// stateBeginStringOrEmpty is the state after reading `{`.
254func stateBeginStringOrEmpty(s *scanner, c byte) int {
255 if c <= ' ' && isSpace(c) {
256 return scanSkipSpace
257 }
258 if c == '}' {
259 n := len(s.parseState)
260 s.parseState[n-1] = parseObjectValue
261 return stateEndValue(s, c)
262 }
263 return stateBeginString(s, c)
264}
265
266// stateBeginString is the state after reading `{"key": value,`.
267func stateBeginString(s *scanner, c byte) int {
268 if c <= ' ' && isSpace(c) {
269 return scanSkipSpace
270 }
271 if c == '"' {
272 s.step = stateInString
273 return scanBeginLiteral
274 }
275 if isName(c) {
276 s.step = stateName
277 return scanBeginName
278 }
279 return s.error(c, "looking for beginning of object key string")
280}
281
282// stateEndValue is the state after completing a value,
283// such as after reading `{}` or `true` or `["x"`.
284func stateEndValue(s *scanner, c byte) int {
285 n := len(s.parseState)
286 if n == 0 {
287 // Completed top-level before the current byte.
288 s.step = stateEndTop
289 s.endTop = true
290 return stateEndTop(s, c)
291 }
292 if c <= ' ' && isSpace(c) {
293 s.step = stateEndValue
294 return scanSkipSpace
295 }
296 ps := s.parseState[n-1]
297 switch ps {
298 case parseObjectKey:
299 if c == ':' {
300 s.parseState[n-1] = parseObjectValue
301 s.step = stateBeginValue
302 return scanObjectKey
303 }
304 return s.error(c, "after object key")
305 case parseObjectValue:
306 if c == ',' {
307 s.parseState[n-1] = parseObjectKey
308 s.step = stateBeginStringOrEmpty
309 return scanObjectValue
310 }
311 if c == '}' {
312 s.popParseState()
313 return scanEndObject
314 }
315 return s.error(c, "after object key:value pair")
316 case parseArrayValue:
317 if c == ',' {
318 s.step = stateBeginValueOrEmpty
319 return scanArrayValue
320 }
321 if c == ']' {
322 s.popParseState()
323 return scanEndArray
324 }
325 return s.error(c, "after array element")
326 case parseParam:
327 if c == ',' {
328 s.step = stateBeginValue
329 return scanParam
330 }
331 if c == ')' {
332 s.popParseState()
333 return scanEndParams
334 }
335 return s.error(c, "after array element")
336 }
337 return s.error(c, "")
338}
339
340// stateEndTop is the state after finishing the top-level value,
341// such as after reading `{}` or `[1,2,3]`.
342// Only space characters should be seen now.
343func stateEndTop(s *scanner, c byte) int {
344 if c != ' ' && c != '\t' && c != '\r' && c != '\n' {
345 // Complain about non-space byte on next call.
346 s.error(c, "after top-level value")
347 }
348 return scanEnd
349}
350
351// stateInString is the state after reading `"`.
352func stateInString(s *scanner, c byte) int {
353 if c == '"' {
354 s.step = stateEndValue
355 return scanContinue
356 }
357 if c == '\\' {
358 s.step = stateInStringEsc
359 return scanContinue
360 }
361 if c < 0x20 {
362 return s.error(c, "in string literal")
363 }
364 return scanContinue
365}
366
367// stateInStringEsc is the state after reading `"\` during a quoted string.
368func stateInStringEsc(s *scanner, c byte) int {
369 switch c {
370 case 'b', 'f', 'n', 'r', 't', '\\', '/', '"':
371 s.step = stateInString
372 return scanContinue
373 case 'u':
374 s.step = stateInStringEscU
375 return scanContinue
376 }
377 return s.error(c, "in string escape code")
378}
379
380// stateInStringEscU is the state after reading `"\u` during a quoted string.
381func stateInStringEscU(s *scanner, c byte) int {
382 if '0' <= c && c <= '9' || 'a' <= c && c <= 'f' || 'A' <= c && c <= 'F' {
383 s.step = stateInStringEscU1
384 return scanContinue
385 }
386 // numbers
387 return s.error(c, "in \\u hexadecimal character escape")
388}
389
390// stateInStringEscU1 is the state after reading `"\u1` during a quoted string.
391func stateInStringEscU1(s *scanner, c byte) int {
392 if '0' <= c && c <= '9' || 'a' <= c && c <= 'f' || 'A' <= c && c <= 'F' {
393 s.step = stateInStringEscU12
394 return scanContinue
395 }
396 // numbers
397 return s.error(c, "in \\u hexadecimal character escape")
398}
399
400// stateInStringEscU12 is the state after reading `"\u12` during a quoted string.
401func stateInStringEscU12(s *scanner, c byte) int {
402 if '0' <= c && c <= '9' || 'a' <= c && c <= 'f' || 'A' <= c && c <= 'F' {
403 s.step = stateInStringEscU123
404 return scanContinue
405 }
406 // numbers
407 return s.error(c, "in \\u hexadecimal character escape")
408}
409
410// stateInStringEscU123 is the state after reading `"\u123` during a quoted string.
411func stateInStringEscU123(s *scanner, c byte) int {
412 if '0' <= c && c <= '9' || 'a' <= c && c <= 'f' || 'A' <= c && c <= 'F' {
413 s.step = stateInString
414 return scanContinue
415 }
416 // numbers
417 return s.error(c, "in \\u hexadecimal character escape")
418}
419
420// stateNeg is the state after reading `-` during a number.
421func stateNeg(s *scanner, c byte) int {
422 if c == '0' {
423 s.step = state0
424 return scanContinue
425 }
426 if '1' <= c && c <= '9' {
427 s.step = state1
428 return scanContinue
429 }
430 return s.error(c, "in numeric literal")
431}
432
433// state1 is the state after reading a non-zero integer during a number,
434// such as after reading `1` or `100` but not `0`.
435func state1(s *scanner, c byte) int {
436 if '0' <= c && c <= '9' {
437 s.step = state1
438 return scanContinue
439 }
440 return state0(s, c)
441}
442
443// state0 is the state after reading `0` during a number.
444func state0(s *scanner, c byte) int {
445 if c == '.' {
446 s.step = stateDot
447 return scanContinue
448 }
449 if c == 'e' || c == 'E' {
450 s.step = stateE
451 return scanContinue
452 }
453 return stateEndValue(s, c)
454}
455
456// stateDot is the state after reading the integer and decimal point in a number,
457// such as after reading `1.`.
458func stateDot(s *scanner, c byte) int {
459 if '0' <= c && c <= '9' {
460 s.step = stateDot0
461 return scanContinue
462 }
463 return s.error(c, "after decimal point in numeric literal")
464}
465
466// stateDot0 is the state after reading the integer, decimal point, and subsequent
467// digits of a number, such as after reading `3.14`.
468func stateDot0(s *scanner, c byte) int {
469 if '0' <= c && c <= '9' {
470 return scanContinue
471 }
472 if c == 'e' || c == 'E' {
473 s.step = stateE
474 return scanContinue
475 }
476 return stateEndValue(s, c)
477}
478
479// stateE is the state after reading the mantissa and e in a number,
480// such as after reading `314e` or `0.314e`.
481func stateE(s *scanner, c byte) int {
482 if c == '+' || c == '-' {
483 s.step = stateESign
484 return scanContinue
485 }
486 return stateESign(s, c)
487}
488
489// stateESign is the state after reading the mantissa, e, and sign in a number,
490// such as after reading `314e-` or `0.314e+`.
491func stateESign(s *scanner, c byte) int {
492 if '0' <= c && c <= '9' {
493 s.step = stateE0
494 return scanContinue
495 }
496 return s.error(c, "in exponent of numeric literal")
497}
498
499// stateE0 is the state after reading the mantissa, e, optional sign,
500// and at least one digit of the exponent in a number,
501// such as after reading `314e-2` or `0.314e+1` or `3.14e0`.
502func stateE0(s *scanner, c byte) int {
503 if '0' <= c && c <= '9' {
504 return scanContinue
505 }
506 return stateEndValue(s, c)
507}
508
509// stateNew0 is the state after reading `n`.
510func stateNew0(s *scanner, c byte) int {
511 if c == 'e' {
512 s.step = stateNew1
513 return scanContinue
514 }
515 s.step = stateName
516 return stateName(s, c)
517}
518
519// stateNew1 is the state after reading `ne`.
520func stateNew1(s *scanner, c byte) int {
521 if c == 'w' {
522 s.step = stateNew2
523 return scanContinue
524 }
525 s.step = stateName
526 return stateName(s, c)
527}
528
529// stateNew2 is the state after reading `new`.
530func stateNew2(s *scanner, c byte) int {
531 s.step = stateName
532 if c == ' ' {
533 return scanContinue
534 }
535 return stateName(s, c)
536}
537
538// stateName is the state while reading an unquoted function name.
539func stateName(s *scanner, c byte) int {
540 if isName(c) {
541 return scanContinue
542 }
543 if c == '(' {
544 s.step = stateParamOrEmpty
545 s.pushParseState(parseParam)
546 return scanParam
547 }
548 return stateEndValue(s, c)
549}
550
551// stateParamOrEmpty is the state after reading `(`.
552func stateParamOrEmpty(s *scanner, c byte) int {
553 if c <= ' ' && isSpace(c) {
554 return scanSkipSpace
555 }
556 if c == ')' {
557 return stateEndValue(s, c)
558 }
559 return stateBeginValue(s, c)
560}
561
562// stateT is the state after reading `t`.
563func stateT(s *scanner, c byte) int {
564 if c == 'r' {
565 s.step = stateTr
566 return scanContinue
567 }
568 return s.error(c, "in literal true (expecting 'r')")
569}
570
571// stateTr is the state after reading `tr`.
572func stateTr(s *scanner, c byte) int {
573 if c == 'u' {
574 s.step = stateTru
575 return scanContinue
576 }
577 return s.error(c, "in literal true (expecting 'u')")
578}
579
580// stateTru is the state after reading `tru`.
581func stateTru(s *scanner, c byte) int {
582 if c == 'e' {
583 s.step = stateEndValue
584 return scanContinue
585 }
586 return s.error(c, "in literal true (expecting 'e')")
587}
588
589// stateF is the state after reading `f`.
590func stateF(s *scanner, c byte) int {
591 if c == 'a' {
592 s.step = stateFa
593 return scanContinue
594 }
595 return s.error(c, "in literal false (expecting 'a')")
596}
597
598// stateFa is the state after reading `fa`.
599func stateFa(s *scanner, c byte) int {
600 if c == 'l' {
601 s.step = stateFal
602 return scanContinue
603 }
604 return s.error(c, "in literal false (expecting 'l')")
605}
606
607// stateFal is the state after reading `fal`.
608func stateFal(s *scanner, c byte) int {
609 if c == 's' {
610 s.step = stateFals
611 return scanContinue
612 }
613 return s.error(c, "in literal false (expecting 's')")
614}
615
616// stateFals is the state after reading `fals`.
617func stateFals(s *scanner, c byte) int {
618 if c == 'e' {
619 s.step = stateEndValue
620 return scanContinue
621 }
622 return s.error(c, "in literal false (expecting 'e')")
623}
624
625// stateN is the state after reading `n`.
626func stateN(s *scanner, c byte) int {
627 if c == 'u' {
628 s.step = stateNu
629 return scanContinue
630 }
631 return s.error(c, "in literal null (expecting 'u')")
632}
633
634// stateNu is the state after reading `nu`.
635func stateNu(s *scanner, c byte) int {
636 if c == 'l' {
637 s.step = stateNul
638 return scanContinue
639 }
640 return s.error(c, "in literal null (expecting 'l')")
641}
642
643// stateNul is the state after reading `nul`.
644func stateNul(s *scanner, c byte) int {
645 if c == 'l' {
646 s.step = stateEndValue
647 return scanContinue
648 }
649 return s.error(c, "in literal null (expecting 'l')")
650}
651
652// stateError is the state after reaching a syntax error,
653// such as after reading `[1}` or `5.1.2`.
654func stateError(s *scanner, c byte) int {
655 return scanError
656}
657
658// error records an error and switches to the error state.
659func (s *scanner) error(c byte, context string) int {
660 s.step = stateError
661 s.err = &SyntaxError{"invalid character " + quoteChar(c) + " " + context, s.bytes}
662 return scanError
663}
664
665// quoteChar formats c as a quoted character literal
666func quoteChar(c byte) string {
667 // special cases - different from quoted strings
668 if c == '\'' {
669 return `'\''`
670 }
671 if c == '"' {
672 return `'"'`
673 }
674
675 // use quoted string with different quotation marks
676 s := strconv.Quote(string(c))
677 return "'" + s[1:len(s)-1] + "'"
678}
679
680// undo causes the scanner to return scanCode from the next state transition.
681// This gives callers a simple 1-byte undo mechanism.
682func (s *scanner) undo(scanCode int) {
683 if s.redo {
684 panic("json: invalid use of scanner")
685 }
686 s.redoCode = scanCode
687 s.redoState = s.step
688 s.step = stateRedo
689 s.redo = true
690}
691
692// stateRedo helps implement the scanner's 1-byte undo.
693func stateRedo(s *scanner, c byte) int {
694 s.redo = false
695 s.step = s.redoState
696 return s.redoCode
697}