You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1031 lines
26 KiB

  1. // 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. package fastjson
  5. // JSON value parser state machine.
  6. // Just about at the limit of what is reasonable to write by hand.
  7. // Some parts are a bit tedious, but overall it nicely factors out the
  8. // otherwise common code from the multiple scanning functions
  9. // in this package (Compact, Indent, checkValid, nextValue, etc).
  10. //
  11. // This file starts with two simple examples using the scanner
  12. // before diving into the scanner itself.
  13. import (
  14. "bytes"
  15. "strconv"
  16. )
  17. // checkValid verifies that data is valid JSON-encoded data.
  18. // scan is passed in for use by checkValid to avoid an allocation.
  19. func checkValid(data []byte, scan *scanner) error {
  20. scan.length_data = len(data)
  21. scan.reset()
  22. scan.endTop = true
  23. stream := streamByte{data: &data, pos: 0}
  24. op := scan.parseValue(&stream)
  25. if op == scanError {
  26. return scan.err
  27. }
  28. return nil
  29. }
  30. // A SyntaxError is a description of a JSON syntax error.
  31. type SyntaxError struct {
  32. msg string // description of error
  33. Offset int64 // error occurred after reading Offset bytes
  34. }
  35. type Record struct {
  36. state int
  37. pos int
  38. }
  39. func (e *SyntaxError) Error() string { return e.msg }
  40. // A scanner is a JSON scanning state machine.
  41. // Callers call scan.reset() and then pass bytes in one at a time
  42. // by calling scan.step(&scan, c) for each byte.
  43. // The return value, referred to as an opcode, tells the
  44. // caller about significant parsing events like beginning
  45. // and ending literals, objects, and arrays, so that the
  46. // caller can follow along if it wishes.
  47. // The return value scanEnd indicates that a single top-level
  48. // JSON value has been completed, *before* the byte that
  49. // just got passed in. (The indication must be delayed in order
  50. // to recognize the end of numbers: is 123 a whole value or
  51. // the beginning of 12345e+6?).
  52. type scanner struct {
  53. // The step is a func to be called to execute the next transition.
  54. // Also tried using an integer constant and a single func
  55. // with a switch, but using the func directly was 10% faster
  56. // on a 64-bit Mac Mini, and it's nicer to read.
  57. step func(*scanner, byte) int
  58. // Reached end of top-level value.
  59. endTop bool
  60. // Stack of what we're in the middle of - array values, object keys, object values.
  61. parseState []int
  62. // Error that happened, if any.
  63. err error
  64. stateRecord []Record//array of records of labels(position in array and state on this position)
  65. cacheRecord Record
  66. cached bool
  67. readPos int //position in array stateRecord during filling
  68. length_data int //length of data to read, initialized in unmarshal. Helps to set correct capacity of stateRecord
  69. inNumber bool // flag of parsing figure
  70. endLiteral bool //flag of finishing literal
  71. bytes int64 // total bytes consumed, updated by decoder.Decode
  72. }
  73. // These values are returned by the state transition functions
  74. // assigned to scanner.state and the method scanner.eof.
  75. // They give details about the current state of the scan that
  76. // callers might be interested to know about.
  77. // It is okay to ignore the return value of any particular
  78. // call to scanner.state: if one call returns scanError,
  79. // every subsequent call will return scanError too.
  80. const (
  81. scanBeginLiteral = iota // end implied by next result != scanContinue
  82. scanEndLiteral // not returned by scanner, but clearer for state recording
  83. scanBeginObject // begin object
  84. scanEndObject // end object (implies scanObjectValue if possible)
  85. scanBeginArray // begin array
  86. scanEndArray // end array (implies scanArrayValue if possible)
  87. scanObjectKey // just finished object key (string)
  88. scanObjectValue // just finished non-last object value
  89. scanContinue // uninteresting byte
  90. scanArrayValue // just finished array value
  91. scanSkipSpace // space byte; can skip; known to be last "continue" result
  92. // Stop.
  93. scanEnd // top-level value ended *before* this byte; known to be first "stop" result
  94. scanError // hit an error, scanner.err.
  95. )
  96. // These values are stored in the parseState stack.
  97. // They give the current state of a composite value
  98. // being scanned. If the parser is inside a nested value
  99. // the parseState describes the nested state, outermost at entry 0.
  100. const (
  101. parseObjectKey = iota // parsing object key (before colon)
  102. parseObjectValue // parsing object value (after colon)
  103. parseArrayValue // parsing array value
  104. )
  105. type streamByte struct {
  106. data *[]byte
  107. pos int
  108. }
  109. func (s *streamByte) isEnd() bool {
  110. return s.pos >= len(*s.data)
  111. }
  112. func (s *streamByte) Take() byte {
  113. result := s.Peek()
  114. s.pos++
  115. return result
  116. }
  117. func (s *streamByte) Peek() byte {
  118. if !s.isEnd() {
  119. return (*s.data)[s.pos]
  120. } else {
  121. return 0//I have to leave this case, because I can call Peek, when the stream is over. I won't use value it returns, but it should be protected
  122. }
  123. }
  124. func (sb *streamByte) skipSpaces() {
  125. for c := sb.Peek(); c <= ' ' && isSpace(c); {
  126. sb.pos++
  127. c = sb.Peek()
  128. }
  129. }
  130. const AVERAGE_LENGTH = 10000
  131. // reset prepares the scanner for use.
  132. // It must be called before calling s.step.
  133. func (s *scanner) reset() {
  134. s.step = stateBeginValue
  135. s.parseState = s.parseState[0:0]
  136. s.err = nil
  137. if s.isRecordEmpty() {
  138. if s.length_data >= AVERAGE_LENGTH {
  139. s.stateRecord = make([]Record, 0, s.length_data/4) //capacity doesn't depends on the length whole value, but on the length of nested values. But predictively the large values have large nested values.
  140. } else {
  141. s.stateRecord = make([]Record, 0, s.length_data/2)
  142. }
  143. }
  144. s.inNumber = false
  145. s.endLiteral = false
  146. s.cached = false
  147. s.readPos = 0
  148. s.endTop = false
  149. }
  150. // eof tells the scanner that the end of input has been reached.
  151. // It returns a scan status just as s.step does.
  152. func (s *scanner) eof() int {
  153. if s.err != nil {
  154. return scanError
  155. }
  156. if s.endTop {
  157. return scanEnd
  158. }
  159. s.step(s, ' ')
  160. if s.endTop {
  161. return scanEnd
  162. }
  163. if s.err == nil {
  164. s.err = &SyntaxError{"unexpected end of JSON input", s.bytes}
  165. }
  166. return scanError
  167. }
  168. // pushParseState pushes a new parse state p onto the parse stack.
  169. func (s *scanner) pushParseState(p int) {
  170. s.parseState = append(s.parseState, p)
  171. }
  172. // popParseState pops a parse state (already obtained) off the stack
  173. // and updates s.step accordingly.
  174. func (s *scanner) popParseState() {
  175. n := len(s.parseState) - 1
  176. s.parseState = s.parseState[0:n]
  177. if n == 0 {
  178. s.step = stateEndTop
  179. s.endTop = true
  180. } else {
  181. s.step = stateEndValue
  182. }
  183. }
  184. //checks if array of records is empty
  185. func (s *scanner) isRecordEmpty() bool {
  186. return len(s.stateRecord) == 0
  187. }
  188. //pushes Record into array
  189. func (s *scanner) pushRecord(state, pos int) {
  190. s.stateRecord = append(s.stateRecord, Record{state:state, pos:pos}) //state are at even positions, pos are at odd positions in stateRecord array
  191. }
  192. //peeks current state for filling object. Doesn't change position. Returns state, pos
  193. func (s *scanner) peekPos() int {
  194. if s.readPos >= len(s.stateRecord){
  195. return s.cacheRecord.pos// peek can be called when the array is over , only if unmarshal error occured, so return last read position
  196. }
  197. if !s.cached {
  198. s.cached = true
  199. s.cacheRecord = s.stateRecord[s.readPos]
  200. }
  201. return s.cacheRecord.pos
  202. }
  203. func (s *scanner) peekState() int {
  204. if s.readPos >= len(s.stateRecord) {
  205. return s.cacheRecord.state // the same as Peek
  206. }
  207. if !s.cached {
  208. s.cached = true
  209. s.cacheRecord = s.stateRecord[s.readPos]
  210. }
  211. return s.cacheRecord.state
  212. }
  213. //takes current state and increments reading position.
  214. func (s *scanner) takeState() int {
  215. if s.cached {
  216. s.cached = false
  217. }else{
  218. s.peekState()
  219. }
  220. s.readPos += 1
  221. return s.cacheRecord.state
  222. }
  223. func (s *scanner) takePos() int {
  224. if s.cached {
  225. s.cached = false
  226. }else{
  227. s.peekState()
  228. }
  229. s.readPos += 1
  230. return s.cacheRecord.pos
  231. }
  232. func (s *scanner) skipRecord() {
  233. s.readPos += 1
  234. s.cached = false
  235. }
  236. //checks if we need this state to be recorded
  237. func (s *scanner) isNeededState(state int) bool {
  238. if s.endLiteral {
  239. return true
  240. }
  241. if state > scanEndArray || state < scanBeginLiteral {
  242. return false
  243. }
  244. return true
  245. }
  246. func (s *scanner) fillRecord(pos, state int) {
  247. if s.isNeededState(state) {
  248. if s.inNumber && s.endLiteral { // in case 2] , 2} or 2,
  249. s.inNumber = false
  250. s.endLiteral = false
  251. s.pushRecord(scanEndLiteral, pos-1)
  252. if s.isNeededState(state) { // in case 2] or 2}
  253. s.pushRecord(state, pos)
  254. }
  255. return
  256. }
  257. if s.endLiteral {
  258. s.endLiteral = false
  259. state = scanEndLiteral
  260. }
  261. s.pushRecord(state, pos)
  262. }
  263. }
  264. func isSpace(c byte) bool {
  265. return c == ' ' || c == '\t' || c == '\r' || c == '\n'
  266. }
  267. // stateBeginValueOrEmpty is the state after reading `[`.
  268. func stateBeginValueOrEmpty(s *scanner, c byte) int {
  269. if c <= ' ' && isSpace(c) {
  270. return scanSkipSpace
  271. }
  272. if c == ']' {
  273. return stateEndValue(s, c)
  274. }
  275. return stateBeginValue(s, c)
  276. }
  277. // stateBeginValue is the state at the beginning of the input.
  278. func stateBeginValue(s *scanner, c byte) int {
  279. if c <= ' ' && isSpace(c) {
  280. return scanSkipSpace
  281. }
  282. switch c {
  283. case '{':
  284. s.step = stateBeginStringOrEmpty
  285. s.pushParseState(parseObjectKey)
  286. return scanBeginObject
  287. case '[':
  288. s.step = stateBeginValueOrEmpty
  289. s.pushParseState(parseArrayValue)
  290. return scanBeginArray
  291. case '"':
  292. s.step = stateInString
  293. return scanBeginLiteral
  294. case '-':
  295. s.step = stateNeg
  296. s.inNumber = true
  297. return scanBeginLiteral
  298. case '0': // beginning of 0.123
  299. s.step = state0
  300. s.inNumber = true
  301. return scanBeginLiteral
  302. case 't': // beginning of true
  303. s.step = stateT
  304. return scanBeginLiteral
  305. case 'f': // beginning of false
  306. s.step = stateF
  307. return scanBeginLiteral
  308. case 'n': // beginning of null
  309. s.step = stateN
  310. return scanBeginLiteral
  311. }
  312. if '1' <= c && c <= '9' { // beginning of 1234.5
  313. s.step = state1
  314. s.inNumber = true
  315. return scanBeginLiteral
  316. }
  317. return s.error(c, "looking for beginning of value")
  318. }
  319. // stateBeginStringOrEmpty is the state after reading `{`.
  320. func stateBeginStringOrEmpty(s *scanner, c byte) int {
  321. if c <= ' ' && isSpace(c) {
  322. return scanSkipSpace
  323. }
  324. if c == '}' {
  325. n := len(s.parseState)
  326. s.parseState[n-1] = parseObjectValue
  327. return stateEndValue(s, c)
  328. }
  329. return stateBeginString(s, c)
  330. }
  331. // stateBeginString is the state after reading `{"key": value,`.
  332. func stateBeginString(s *scanner, c byte) int {
  333. if c <= ' ' && isSpace(c) {
  334. return scanSkipSpace
  335. }
  336. if c == '"' {
  337. s.step = stateInString
  338. return scanBeginLiteral
  339. }
  340. return s.error(c, "looking for beginning of object key string")
  341. }
  342. // stateEndValue is the state after completing a value,
  343. // such as after reading `{}` or `true` or `["x"`.
  344. func stateEndValue(s *scanner, c byte) int {
  345. n := len(s.parseState)
  346. if n == 0 {
  347. // Completed top-level before the current byte.
  348. s.step = stateEndTop
  349. s.endTop = true
  350. return stateEndTop(s, c)
  351. }
  352. if c <= ' ' && isSpace(c) {
  353. s.step = stateEndValue
  354. return scanSkipSpace
  355. }
  356. ps := s.parseState[n-1]
  357. switch ps {
  358. case parseObjectKey:
  359. if c == ':' {
  360. s.parseState[n-1] = parseObjectValue
  361. s.step = stateBeginValue
  362. return scanObjectKey
  363. }
  364. return s.error(c, "after object key")
  365. case parseObjectValue:
  366. if c == ',' {
  367. s.parseState[n-1] = parseObjectKey
  368. s.step = stateBeginString
  369. return scanObjectValue
  370. }
  371. if c == '}' {
  372. s.popParseState()
  373. return scanEndObject
  374. }
  375. return s.error(c, "after object key:value pair")
  376. case parseArrayValue:
  377. if c == ',' {
  378. s.step = stateBeginValue
  379. return scanArrayValue
  380. }
  381. if c == ']' {
  382. s.popParseState()
  383. return scanEndArray
  384. }
  385. return s.error(c, "after array element")
  386. }
  387. return s.error(c, "")
  388. }
  389. // stateEndTop is the state after finishing the top-level value,
  390. // such as after reading `{}` or `[1,2,3]`.
  391. // Only space characters should be seen now.
  392. func stateEndTop(s *scanner, c byte) int {
  393. if c != ' ' && c != '\t' && c != '\r' && c != '\n' {
  394. // Complain about non-space byte on next call.
  395. s.error(c, "after top-level value")
  396. }
  397. return scanEnd
  398. }
  399. // stateInString is the state after reading `"`.
  400. func stateInString(s *scanner, c byte) int {
  401. if c == '"' {
  402. s.step = stateEndValue
  403. s.endLiteral = true
  404. return scanContinue
  405. }
  406. if c == '\\' {
  407. s.step = stateInStringEsc
  408. return scanContinue
  409. }
  410. if c < 0x20 {
  411. return s.error(c, "in string literal")
  412. }
  413. return scanContinue
  414. }
  415. // stateInStringEsc is the state after reading `"\` during a quoted string.
  416. func stateInStringEsc(s *scanner, c byte) int {
  417. switch c {
  418. case 'b', 'f', 'n', 'r', 't', '\\', '/', '"':
  419. s.step = stateInString
  420. return scanContinue
  421. case 'u':
  422. s.step = stateInStringEscU
  423. return scanContinue
  424. }
  425. return s.error(c, "in string escape code")
  426. }
  427. // stateInStringEscU is the state after reading `"\u` during a quoted string.
  428. func stateInStringEscU(s *scanner, c byte) int {
  429. if '0' <= c && c <= '9' || 'a' <= c && c <= 'f' || 'A' <= c && c <= 'F' {
  430. s.step = stateInStringEscU1
  431. return scanContinue
  432. }
  433. // numbers
  434. return s.error(c, "in \\u hexadecimal character escape")
  435. }
  436. // stateInStringEscU1 is the state after reading `"\u1` during a quoted string.
  437. func stateInStringEscU1(s *scanner, c byte) int {
  438. if '0' <= c && c <= '9' || 'a' <= c && c <= 'f' || 'A' <= c && c <= 'F' {
  439. s.step = stateInStringEscU12
  440. return scanContinue
  441. }
  442. // numbers
  443. return s.error(c, "in \\u hexadecimal character escape")
  444. }
  445. // stateInStringEscU12 is the state after reading `"\u12` during a quoted string.
  446. func stateInStringEscU12(s *scanner, c byte) int {
  447. if '0' <= c && c <= '9' || 'a' <= c && c <= 'f' || 'A' <= c && c <= 'F' {
  448. s.step = stateInStringEscU123
  449. return scanContinue
  450. }
  451. // numbers
  452. return s.error(c, "in \\u hexadecimal character escape")
  453. }
  454. // stateInStringEscU123 is the state after reading `"\u123` during a quoted string.
  455. func stateInStringEscU123(s *scanner, c byte) int {
  456. if '0' <= c && c <= '9' || 'a' <= c && c <= 'f' || 'A' <= c && c <= 'F' {
  457. s.step = stateInString
  458. return scanContinue
  459. }
  460. // numbers
  461. return s.error(c, "in \\u hexadecimal character escape")
  462. }
  463. // stateNeg is the state after reading `-` during a number.
  464. func stateNeg(s *scanner, c byte) int {
  465. if c == '0' {
  466. s.step = state0
  467. return scanContinue
  468. }
  469. if '1' <= c && c <= '9' {
  470. s.step = state1
  471. return scanContinue
  472. }
  473. return s.error(c, "in numeric literal")
  474. }
  475. // state1 is the state after reading a non-zero integer during a number,
  476. // such as after reading `1` or `100` but not `0`.
  477. func state1(s *scanner, c byte) int {
  478. if '0' <= c && c <= '9' {
  479. s.step = state1
  480. return scanContinue
  481. }
  482. return state0(s, c)
  483. }
  484. // state0 is the state after reading `0` during a number.
  485. func state0(s *scanner, c byte) int {
  486. if c == '.' {
  487. s.step = stateDot
  488. return scanContinue
  489. }
  490. if c == 'e' || c == 'E' {
  491. s.step = stateE
  492. return scanContinue
  493. }
  494. s.endLiteral = true
  495. return stateEndValue(s, c)
  496. }
  497. // stateDot is the state after reading the integer and decimal point in a number,
  498. // such as after reading `1.`.
  499. func stateDot(s *scanner, c byte) int {
  500. if '0' <= c && c <= '9' {
  501. s.step = stateDot0
  502. return scanContinue
  503. }
  504. return s.error(c, "after decimal point in numeric literal")
  505. }
  506. // stateDot0 is the state after reading the integer, decimal point, and subsequent
  507. // digits of a number, such as after reading `3.14`.
  508. func stateDot0(s *scanner, c byte) int {
  509. if '0' <= c && c <= '9' {
  510. return scanContinue
  511. }
  512. if c == 'e' || c == 'E' {
  513. s.step = stateE
  514. return scanContinue
  515. }
  516. s.endLiteral = true
  517. return stateEndValue(s, c)
  518. }
  519. // stateE is the state after reading the mantissa and e in a number,
  520. // such as after reading `314e` or `0.314e`.
  521. func stateE(s *scanner, c byte) int {
  522. if c == '+' || c == '-' {
  523. s.step = stateESign
  524. return scanContinue
  525. }
  526. return stateESign(s, c)
  527. }
  528. // stateESign is the state after reading the mantissa, e, and sign in a number,
  529. // such as after reading `314e-` or `0.314e+`.
  530. func stateESign(s *scanner, c byte) int {
  531. if '0' <= c && c <= '9' {
  532. s.step = stateE0
  533. return scanContinue
  534. }
  535. return s.error(c, "in exponent of numeric literal")
  536. }
  537. // stateE0 is the state after reading the mantissa, e, optional sign,
  538. // and at least one digit of the exponent in a number,
  539. // such as after reading `314e-2` or `0.314e+1` or `3.14e0`.
  540. func stateE0(s *scanner, c byte) int {
  541. if '0' <= c && c <= '9' {
  542. return scanContinue
  543. }
  544. s.endLiteral = true
  545. return stateEndValue(s, c)
  546. }
  547. // stateT is the state after reading `t`.
  548. func stateT(s *scanner, c byte) int {
  549. if c == 'r' {
  550. s.step = stateTr
  551. return scanContinue
  552. }
  553. return s.error(c, "in literal true (expecting 'r')")
  554. }
  555. // stateTr is the state after reading `tr`.
  556. func stateTr(s *scanner, c byte) int {
  557. if c == 'u' {
  558. s.step = stateTru
  559. return scanContinue
  560. }
  561. return s.error(c, "in literal true (expecting 'u')")
  562. }
  563. // stateTru is the state after reading `tru`.
  564. func stateTru(s *scanner, c byte) int {
  565. if c == 'e' {
  566. s.step = stateEndValue
  567. s.endLiteral = true
  568. return scanContinue
  569. }
  570. return s.error(c, "in literal true (expecting 'e')")
  571. }
  572. // stateF is the state after reading `f`.
  573. func stateF(s *scanner, c byte) int {
  574. if c == 'a' {
  575. s.step = stateFa
  576. return scanContinue
  577. }
  578. return s.error(c, "in literal false (expecting 'a')")
  579. }
  580. // stateFa is the state after reading `fa`.
  581. func stateFa(s *scanner, c byte) int {
  582. if c == 'l' {
  583. s.step = stateFal
  584. return scanContinue
  585. }
  586. return s.error(c, "in literal false (expecting 'l')")
  587. }
  588. // stateFal is the state after reading `fal`.
  589. func stateFal(s *scanner, c byte) int {
  590. if c == 's' {
  591. s.step = stateFals
  592. return scanContinue
  593. }
  594. return s.error(c, "in literal false (expecting 's')")
  595. }
  596. // stateFals is the state after reading `fals`.
  597. func stateFals(s *scanner, c byte) int {
  598. if c == 'e' {
  599. s.step = stateEndValue
  600. s.endLiteral = true
  601. return scanContinue
  602. }
  603. return s.error(c, "in literal false (expecting 'e')")
  604. }
  605. // stateN is the state after reading `n`.
  606. func stateN(s *scanner, c byte) int {
  607. if c == 'u' {
  608. s.step = stateNu
  609. return scanContinue
  610. }
  611. return s.error(c, "in literal null (expecting 'u')")
  612. }
  613. // stateNu is the state after reading `nu`.
  614. func stateNu(s *scanner, c byte) int {
  615. if c == 'l' {
  616. s.step = stateNul
  617. return scanContinue
  618. }
  619. return s.error(c, "in literal null (expecting 'l')")
  620. }
  621. // stateNul is the state after reading `nul`.
  622. func stateNul(s *scanner, c byte) int {
  623. if c == 'l' {
  624. s.step = stateEndValue
  625. s.endLiteral = true
  626. return scanContinue
  627. }
  628. return s.error(c, "in literal null (expecting 'l')")
  629. }
  630. // stateError is the state after reaching a syntax error,
  631. // such as after reading `[1}` or `5.1.2`.
  632. func stateError(s *scanner, c byte) int {
  633. return scanError
  634. }
  635. // error records an error and switches to the error state.
  636. func (s *scanner) error(c byte, context string) int {
  637. s.step = stateError
  638. s.err = &SyntaxError{"invalid character " + quoteChar(c) + " " + context, s.bytes}
  639. return scanError
  640. }
  641. // quoteChar formats c as a quoted character literal
  642. func quoteChar(c byte) string {
  643. // special cases - different from quoted strings
  644. if c == '\'' {
  645. return `'\''`
  646. }
  647. if c == '"' {
  648. return `'"'`
  649. }
  650. // use quoted string with different quotation marks
  651. s := strconv.Quote(string(c))
  652. return "'" + s[1:len(s)-1] + "'"
  653. }
  654. func (sb *streamByte) error(s *scanner, context string) int {
  655. s.err = &SyntaxError{"invalid character " + quoteChar(sb.Peek()) + " " + context, int64(sb.pos + 1)}
  656. return scanError
  657. }
  658. func (s *scanner) parseSimpleLiteral(sb *streamByte, length int) int {
  659. if len(*sb.data) < sb.pos+length {
  660. s.err = &SyntaxError{"unexpected end of JSON input", int64(len(*sb.data))}
  661. return scanError
  662. }
  663. s.pushRecord(scanBeginLiteral, sb.pos)
  664. sb.Take()
  665. s.bytes = int64(sb.pos)
  666. for i := 0; i < length-1; i++ {
  667. s.bytes++
  668. op := s.step(s, sb.Take())
  669. if op == scanError {
  670. return op
  671. }
  672. }
  673. s.pushRecord(scanEndLiteral, sb.pos-1)
  674. return scanContinue
  675. }
  676. func (s *scanner) parseValue(sb *streamByte) int {
  677. sb.skipSpaces()
  678. topValue := s.endTop
  679. if len(*sb.data) <= sb.pos {
  680. s.err = &SyntaxError{"unexpected end of JSON input", int64(sb.pos)}
  681. return scanError
  682. }
  683. cur := sb.Peek()
  684. s.endTop = false
  685. op := scanContinue
  686. switch cur {
  687. case '"':
  688. op = s.parseString(sb)
  689. case '{':
  690. op = s.parseObject(sb)
  691. case '[':
  692. op = s.parseArray(sb)
  693. case '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
  694. op = s.parseNumber(sb)
  695. case 't':
  696. s.step = stateT
  697. op = s.parseSimpleLiteral(sb, 4)
  698. case 'f':
  699. s.step = stateF
  700. op = s.parseSimpleLiteral(sb, 5)
  701. case 'n':
  702. s.step = stateN
  703. op = s.parseSimpleLiteral(sb, 4)
  704. default:
  705. return sb.error(s, "looking for beginning of value")
  706. }
  707. if topValue && op != scanError {
  708. sb.skipSpaces()
  709. if !sb.isEnd() {
  710. return sb.error(s, "after top-level value")
  711. }
  712. }
  713. return op
  714. }
  715. func (s *scanner) parseString(sb *streamByte) int {
  716. s.pushRecord(scanBeginLiteral, sb.pos)
  717. sb.pos++ //skip "
  718. quotePos := bytes.IndexByte((*sb.data)[sb.pos:], '"')
  719. if quotePos < 0 {
  720. s.err = &SyntaxError{"unexpected end of JSON input", int64(len(*sb.data))}
  721. return scanError
  722. }
  723. // in case without escape symbol \". Errors inside string will be handled during object filling, with function unquote
  724. // it's done in sake of speed
  725. sb.pos += quotePos - 1
  726. for sb.Peek() == '\\' { //pos on the symbol before "
  727. //it may escape symbol "
  728. sb.pos--
  729. sum := 1
  730. //checking multiple symbols \, kind of "...\\"..."
  731. for sb.Peek() == '\\' {
  732. sum++
  733. sb.pos--
  734. }
  735. if sum%2 == 0 { //even number of \, last of them doesn't escape "; it means that current qoute pos is end of string
  736. sb.pos += sum
  737. break
  738. }
  739. //otherwise odd number of \ escapes ". Looking for the next "
  740. sb.pos += sum + 1 // pos on "
  741. n := bytes.IndexByte((*sb.data)[sb.pos+1:], '"')
  742. if n < 0 {
  743. s.err = &SyntaxError{"unexpected end of JSON input", int64(len(*sb.data))}
  744. return scanError
  745. }
  746. sb.pos += n
  747. }
  748. //here pos is on the symbol before "
  749. sb.pos += 2
  750. s.pushRecord(scanEndLiteral, sb.pos-1)
  751. return scanEndLiteral
  752. }
  753. func (s *scanner) parseNumber(sb *streamByte) int {
  754. s.pushRecord(scanBeginLiteral, sb.pos)
  755. cur := sb.Take()
  756. if cur == '-' {
  757. if sb.isEnd() {
  758. s.err = &SyntaxError{"unexpected end of JSON input", int64(sb.pos)}
  759. return scanError
  760. }
  761. cur = sb.Take()
  762. }
  763. if sb.isEnd() {
  764. if '0' <= cur && cur <= '9' {
  765. s.pushRecord(scanEndLiteral, sb.pos-1)
  766. return scanEndLiteral
  767. } else {
  768. sb.pos--
  769. return sb.error(s, "in numeric literal")
  770. }
  771. }
  772. if !sb.isEnd() && '1' <= cur && cur <= '9' {
  773. sb.parseFigures()
  774. } else {
  775. if cur != '0' {
  776. sb.pos--
  777. return sb.error(s, "in numeric literal")
  778. }
  779. }
  780. cur = sb.Take() //pos on the next after cur
  781. if cur == '.' {
  782. if op := sb.Peek(); op > '9' || op < '0' {
  783. if sb.isEnd() {
  784. s.err = &SyntaxError{"unexpected end of JSON input", int64(sb.pos)}
  785. return scanError
  786. }
  787. return sb.error(s, "after decimal point in numeric literal")
  788. }
  789. sb.parseFigures()
  790. cur = sb.Take()
  791. }
  792. if cur == 'e' || cur == 'E' {
  793. op := sb.Peek()
  794. if op != '+' && op != '-' && (op < '0' || op > '9') {
  795. if sb.isEnd() {
  796. s.err = &SyntaxError{"unexpected end of JSON input", int64(sb.pos)}
  797. return scanError
  798. }
  799. return sb.error(s, "in exponent of numeric literal")
  800. }
  801. op = sb.Take()
  802. if op == '-' || op == '+' {
  803. op = sb.Peek()
  804. if op < '0' || op > '9' {
  805. if sb.isEnd() {
  806. s.err = &SyntaxError{"unexpected end of JSON input", int64(sb.pos)}
  807. return scanError
  808. }
  809. return sb.error(s, "in exponent of numeric literal")
  810. }
  811. }
  812. sb.parseFigures()
  813. } else { //pos on the second after unknown symbol. like 123ua. pos now at a
  814. sb.pos--
  815. }
  816. s.pushRecord(scanEndLiteral, sb.pos-1)
  817. return scanEndLiteral
  818. }
  819. func (sb *streamByte) parseFigures() {
  820. c := sb.Take()
  821. for '0' <= c && c <= '9' {
  822. c = sb.Take()
  823. }
  824. sb.pos--
  825. }
  826. func (s *scanner) parseObject(sb *streamByte) int {
  827. s.pushRecord(scanBeginObject, sb.pos)
  828. sb.pos++ // skip {
  829. sb.skipSpaces()
  830. cur := sb.Peek()
  831. if sb.isEnd() {
  832. s.err = &SyntaxError{"unexpected end of JSON input", int64(sb.pos)}
  833. return scanError
  834. }
  835. if cur != '"' && cur != '}' {
  836. return sb.error(s, "looking for beginning of object key string")
  837. }
  838. for !sb.isEnd() {
  839. sb.skipSpaces()
  840. switch cur {
  841. case '}':
  842. s.pushRecord(scanEndObject, sb.pos)
  843. sb.pos++
  844. return scanEndObject
  845. case '"':
  846. op := s.parseString(sb)
  847. if op == scanError {
  848. return op
  849. }
  850. sb.skipSpaces()
  851. if sb.isEnd() {
  852. s.err = &SyntaxError{"unexpected end of JSON input", int64(sb.pos)}
  853. return scanError
  854. }
  855. cur = sb.Peek()
  856. if cur == ':' {
  857. sb.pos++
  858. } else {
  859. return sb.error(s, "after object key")
  860. }
  861. op = s.parseValue(sb)
  862. if op == scanError {
  863. return op
  864. }
  865. sb.skipSpaces()
  866. if sb.isEnd() {
  867. s.err = &SyntaxError{"unexpected end of JSON input", int64(sb.pos)}
  868. return scanError
  869. }
  870. cur = sb.Peek()
  871. if cur == ',' {
  872. sb.pos++
  873. if sb.isEnd() {
  874. s.err = &SyntaxError{"unexpected end of JSON input", int64(sb.pos)}
  875. return scanError
  876. }
  877. sb.skipSpaces()
  878. cur = sb.Peek()
  879. }
  880. default:
  881. return sb.error(s, "after object key:value pair")
  882. }
  883. }
  884. s.err = &SyntaxError{"unexpected end of JSON input", int64(sb.pos)}
  885. return scanError
  886. }
  887. func (s *scanner) parseArray(sb *streamByte) int {
  888. s.pushRecord(scanBeginArray, sb.pos)
  889. sb.pos++
  890. sb.skipSpaces()
  891. if sb.isEnd() {
  892. s.err = &SyntaxError{"unexpected end of JSON input", int64(sb.pos)}
  893. return scanError
  894. }
  895. cur := sb.Peek()
  896. if cur == ']' {
  897. s.pushRecord(scanEndArray, sb.pos)
  898. sb.pos++
  899. return scanEndArray
  900. }
  901. op := s.parseValue(sb)
  902. if op == scanError {
  903. return op
  904. }
  905. sb.skipSpaces()
  906. cur = sb.Peek()
  907. for !sb.isEnd() {
  908. switch cur {
  909. case ']':
  910. s.pushRecord(scanEndArray, sb.pos)
  911. sb.pos++
  912. return scanEndArray
  913. case ',':
  914. sb.pos++
  915. sb.skipSpaces()
  916. default:
  917. return sb.error(s, "after array element")
  918. }
  919. op = s.parseValue(sb)
  920. if op == scanError {
  921. return op
  922. }
  923. sb.skipSpaces()
  924. cur = sb.Peek()
  925. }
  926. //here is incomplete array
  927. s.err = &SyntaxError{"unexpected end of JSON input", int64(sb.pos)}
  928. return scanError
  929. }