| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package json |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
import "strconv" |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
func 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 |
|
| 35 |
|
| 36 |
|
| 37 |
func 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 |
|
| 44 |
|
| 45 |
|
| 46 |
case scanEndObject, scanEndArray: |
| 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 |
|
| 64 |
type SyntaxError struct { |
| 65 |
msg string |
| 66 |
Offset int64 |
| 67 |
} |
| 68 |
|
| 69 |
func (e *SyntaxError) Error() string { return e.msg } |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
type scanner struct { |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
step func(*scanner, byte) int |
| 89 |
|
| 90 |
|
| 91 |
endTop bool |
| 92 |
|
| 93 |
|
| 94 |
parseState []int |
| 95 |
|
| 96 |
|
| 97 |
err error |
| 98 |
|
| 99 |
|
| 100 |
redo bool |
| 101 |
redoCode int |
| 102 |
redoState func(*scanner, byte) int |
| 103 |
|
| 104 |
|
| 105 |
bytes int64 |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
const ( |
| 116 |
|
| 117 |
scanContinue = iota |
| 118 |
scanBeginLiteral |
| 119 |
scanBeginObject |
| 120 |
scanObjectKey |
| 121 |
scanObjectValue |
| 122 |
scanEndObject |
| 123 |
scanBeginArray |
| 124 |
scanArrayValue |
| 125 |
scanEndArray |
| 126 |
scanSkipSpace |
| 127 |
|
| 128 |
|
| 129 |
scanEnd |
| 130 |
scanError |
| 131 |
) |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
const ( |
| 138 |
parseObjectKey = iota |
| 139 |
parseObjectValue |
| 140 |
parseArrayValue |
| 141 |
) |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
func (s *scanner) reset() { |
| 146 |
s.step = stateBeginValue |
| 147 |
s.parseState = s.parseState[0:0] |
| 148 |
s.err = nil |
| 149 |
s.redo = false |
| 150 |
s.endTop = false |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
func (s *scanner) eof() int { |
| 156 |
if s.err != nil { |
| 157 |
return scanError |
| 158 |
} |
| 159 |
if s.endTop { |
| 160 |
return scanEnd |
| 161 |
} |
| 162 |
s.step(s, ' ') |
| 163 |
if s.endTop { |
| 164 |
return scanEnd |
| 165 |
} |
| 166 |
if s.err == nil { |
| 167 |
s.err = &SyntaxError{"unexpected end of JSON input", s.bytes} |
| 168 |
} |
| 169 |
return scanError |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
func (s *scanner) pushParseState(p int) { |
| 174 |
s.parseState = append(s.parseState, p) |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
func (s *scanner) popParseState() { |
| 180 |
n := len(s.parseState) - 1 |
| 181 |
s.parseState = s.parseState[0:n] |
| 182 |
s.redo = false |
| 183 |
if n == 0 { |
| 184 |
s.step = stateEndTop |
| 185 |
s.endTop = true |
| 186 |
} else { |
| 187 |
s.step = stateEndValue |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
func isSpace(c byte) bool { |
| 192 |
return c == ' ' || c == '\t' || c == '\r' || c == '\n' |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
func stateBeginValueOrEmpty(s *scanner, c byte) int { |
| 197 |
if c <= ' ' && isSpace(c) { |
| 198 |
return scanSkipSpace |
| 199 |
} |
| 200 |
if c == ']' { |
| 201 |
return stateEndValue(s, c) |
| 202 |
} |
| 203 |
return stateBeginValue(s, c) |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
func stateBeginValue(s *scanner, c byte) int { |
| 208 |
if c <= ' ' && isSpace(c) { |
| 209 |
return scanSkipSpace |
| 210 |
} |
| 211 |
switch c { |
| 212 |
case '{': |
| 213 |
s.step = stateBeginStringOrEmpty |
| 214 |
s.pushParseState(parseObjectKey) |
| 215 |
return scanBeginObject |
| 216 |
case '[': |
| 217 |
s.step = stateBeginValueOrEmpty |
| 218 |
s.pushParseState(parseArrayValue) |
| 219 |
return scanBeginArray |
| 220 |
case '"': |
| 221 |
s.step = stateInString |
| 222 |
return scanBeginLiteral |
| 223 |
case '-': |
| 224 |
s.step = stateNeg |
| 225 |
return scanBeginLiteral |
| 226 |
case '0': |
| 227 |
s.step = state0 |
| 228 |
return scanBeginLiteral |
| 229 |
case 't': |
| 230 |
s.step = stateT |
| 231 |
return scanBeginLiteral |
| 232 |
case 'f': |
| 233 |
s.step = stateF |
| 234 |
return scanBeginLiteral |
| 235 |
case 'n': |
| 236 |
s.step = stateN |
| 237 |
return scanBeginLiteral |
| 238 |
} |
| 239 |
if '1' <= c && c <= '9' { |
| 240 |
s.step = state1 |
| 241 |
return scanBeginLiteral |
| 242 |
} |
| 243 |
return s.error(c, "looking for beginning of value") |
| 244 |
} |
| 245 |
|
| 246 |
|
| 247 |
func stateBeginStringOrEmpty(s *scanner, c byte) int { |
| 248 |
if c <= ' ' && isSpace(c) { |
| 249 |
return scanSkipSpace |
| 250 |
} |
| 251 |
if c == '}' { |
| 252 |
n := len(s.parseState) |
| 253 |
s.parseState[n-1] = parseObjectValue |
| 254 |
return stateEndValue(s, c) |
| 255 |
} |
| 256 |
return stateBeginString(s, c) |
| 257 |
} |
| 258 |
|
| 259 |
|
| 260 |
func stateBeginString(s *scanner, c byte) int { |
| 261 |
if c <= ' ' && isSpace(c) { |
| 262 |
return scanSkipSpace |
| 263 |
} |
| 264 |
if c == '"' { |
| 265 |
s.step = stateInString |
| 266 |
return scanBeginLiteral |
| 267 |
} |
| 268 |
return s.error(c, "looking for beginning of object key string") |
| 269 |
} |
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
func stateEndValue(s *scanner, c byte) int { |
| 274 |
n := len(s.parseState) |
| 275 |
if n == 0 { |
| 276 |
|
| 277 |
s.step = stateEndTop |
| 278 |
s.endTop = true |
| 279 |
return stateEndTop(s, c) |
| 280 |
} |
| 281 |
if c <= ' ' && isSpace(c) { |
| 282 |
s.step = stateEndValue |
| 283 |
return scanSkipSpace |
| 284 |
} |
| 285 |
ps := s.parseState[n-1] |
| 286 |
switch ps { |
| 287 |
case parseObjectKey: |
| 288 |
if c == ':' { |
| 289 |
s.parseState[n-1] = parseObjectValue |
| 290 |
s.step = stateBeginValue |
| 291 |
return scanObjectKey |
| 292 |
} |
| 293 |
return s.error(c, "after object key") |
| 294 |
case parseObjectValue: |
| 295 |
if c == ',' { |
| 296 |
s.parseState[n-1] = parseObjectKey |
| 297 |
s.step = stateBeginString |
| 298 |
return scanObjectValue |
| 299 |
} |
| 300 |
if c == '}' { |
| 301 |
s.popParseState() |
| 302 |
return scanEndObject |
| 303 |
} |
| 304 |
return s.error(c, "after object key:value pair") |
| 305 |
case parseArrayValue: |
| 306 |
if c == ',' { |
| 307 |
s.step = stateBeginValue |
| 308 |
return scanArrayValue |
| 309 |
} |
| 310 |
if c == ']' { |
| 311 |
s.popParseState() |
| 312 |
return scanEndArray |
| 313 |
} |
| 314 |
return s.error(c, "after array element") |
| 315 |
} |
| 316 |
return s.error(c, "") |
| 317 |
} |
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
func stateEndTop(s *scanner, c byte) int { |
| 323 |
if c != ' ' && c != '\t' && c != '\r' && c != '\n' { |
| 324 |
|
| 325 |
s.error(c, "after top-level value") |
| 326 |
} |
| 327 |
return scanEnd |
| 328 |
} |
| 329 |
|
| 330 |
|
| 331 |
func stateInString(s *scanner, c byte) int { |
| 332 |
if c == '"' { |
| 333 |
s.step = stateEndValue |
| 334 |
return scanContinue |
| 335 |
} |
| 336 |
if c == '\\' { |
| 337 |
s.step = stateInStringEsc |
| 338 |
return scanContinue |
| 339 |
} |
| 340 |
if c < 0x20 { |
| 341 |
return s.error(c, "in string literal") |
| 342 |
} |
| 343 |
return scanContinue |
| 344 |
} |
| 345 |
|
| 346 |
|
| 347 |
func stateInStringEsc(s *scanner, c byte) int { |
| 348 |
switch c { |
| 349 |
case 'b', 'f', 'n', 'r', 't', '\\', '/', '"': |
| 350 |
s.step = stateInString |
| 351 |
return scanContinue |
| 352 |
case 'u': |
| 353 |
s.step = stateInStringEscU |
| 354 |
return scanContinue |
| 355 |
} |
| 356 |
return s.error(c, "in string escape code") |
| 357 |
} |
| 358 |
|
| 359 |
|
| 360 |
func stateInStringEscU(s *scanner, c byte) int { |
| 361 |
if '0' <= c && c <= '9' || 'a' <= c && c <= 'f' || 'A' <= c && c <= 'F' { |
| 362 |
s.step = stateInStringEscU1 |
| 363 |
return scanContinue |
| 364 |
} |
| 365 |
|
| 366 |
return s.error(c, "in \\u hexadecimal character escape") |
| 367 |
} |
| 368 |
|
| 369 |
|
| 370 |
func stateInStringEscU1(s *scanner, c byte) int { |
| 371 |
if '0' <= c && c <= '9' || 'a' <= c && c <= 'f' || 'A' <= c && c <= 'F' { |
| 372 |
s.step = stateInStringEscU12 |
| 373 |
return scanContinue |
| 374 |
} |
| 375 |
|
| 376 |
return s.error(c, "in \\u hexadecimal character escape") |
| 377 |
} |
| 378 |
|
| 379 |
|
| 380 |
func stateInStringEscU12(s *scanner, c byte) int { |
| 381 |
if '0' <= c && c <= '9' || 'a' <= c && c <= 'f' || 'A' <= c && c <= 'F' { |
| 382 |
s.step = stateInStringEscU123 |
| 383 |
return scanContinue |
| 384 |
} |
| 385 |
|
| 386 |
return s.error(c, "in \\u hexadecimal character escape") |
| 387 |
} |
| 388 |
|
| 389 |
|
| 390 |
func stateInStringEscU123(s *scanner, c byte) int { |
| 391 |
if '0' <= c && c <= '9' || 'a' <= c && c <= 'f' || 'A' <= c && c <= 'F' { |
| 392 |
s.step = stateInString |
| 393 |
return scanContinue |
| 394 |
} |
| 395 |
|
| 396 |
return s.error(c, "in \\u hexadecimal character escape") |
| 397 |
} |
| 398 |
|
| 399 |
|
| 400 |
func stateNeg(s *scanner, c byte) int { |
| 401 |
if c == '0' { |
| 402 |
s.step = state0 |
| 403 |
return scanContinue |
| 404 |
} |
| 405 |
if '1' <= c && c <= '9' { |
| 406 |
s.step = state1 |
| 407 |
return scanContinue |
| 408 |
} |
| 409 |
return s.error(c, "in numeric literal") |
| 410 |
} |
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
func state1(s *scanner, c byte) int { |
| 415 |
if '0' <= c && c <= '9' { |
| 416 |
s.step = state1 |
| 417 |
return scanContinue |
| 418 |
} |
| 419 |
return state0(s, c) |
| 420 |
} |
| 421 |
|
| 422 |
|
| 423 |
func state0(s *scanner, c byte) int { |
| 424 |
if c == '.' { |
| 425 |
s.step = stateDot |
| 426 |
return scanContinue |
| 427 |
} |
| 428 |
if c == 'e' || c == 'E' { |
| 429 |
s.step = stateE |
| 430 |
return scanContinue |
| 431 |
} |
| 432 |
return stateEndValue(s, c) |
| 433 |
} |
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
func stateDot(s *scanner, c byte) int { |
| 438 |
if '0' <= c && c <= '9' { |
| 439 |
s.step = stateDot0 |
| 440 |
return scanContinue |
| 441 |
} |
| 442 |
return s.error(c, "after decimal point in numeric literal") |
| 443 |
} |
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
func stateDot0(s *scanner, c byte) int { |
| 448 |
if '0' <= c && c <= '9' { |
| 449 |
return scanContinue |
| 450 |
} |
| 451 |
if c == 'e' || c == 'E' { |
| 452 |
s.step = stateE |
| 453 |
return scanContinue |
| 454 |
} |
| 455 |
return stateEndValue(s, c) |
| 456 |
} |
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
func stateE(s *scanner, c byte) int { |
| 461 |
if c == '+' || c == '-' { |
| 462 |
s.step = stateESign |
| 463 |
return scanContinue |
| 464 |
} |
| 465 |
return stateESign(s, c) |
| 466 |
} |
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
func stateESign(s *scanner, c byte) int { |
| 471 |
if '0' <= c && c <= '9' { |
| 472 |
s.step = stateE0 |
| 473 |
return scanContinue |
| 474 |
} |
| 475 |
return s.error(c, "in exponent of numeric literal") |
| 476 |
} |
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
func stateE0(s *scanner, c byte) int { |
| 482 |
if '0' <= c && c <= '9' { |
| 483 |
return scanContinue |
| 484 |
} |
| 485 |
return stateEndValue(s, c) |
| 486 |
} |
| 487 |
|
| 488 |
|
| 489 |
func stateT(s *scanner, c byte) int { |
| 490 |
if c == 'r' { |
| 491 |
s.step = stateTr |
| 492 |
return scanContinue |
| 493 |
} |
| 494 |
return s.error(c, "in literal true (expecting 'r')") |
| 495 |
} |
| 496 |
|
| 497 |
|
| 498 |
func stateTr(s *scanner, c byte) int { |
| 499 |
if c == 'u' { |
| 500 |
s.step = stateTru |
| 501 |
return scanContinue |
| 502 |
} |
| 503 |
return s.error(c, "in literal true (expecting 'u')") |
| 504 |
} |
| 505 |
|
| 506 |
|
| 507 |
func stateTru(s *scanner, c byte) int { |
| 508 |
if c == 'e' { |
| 509 |
s.step = stateEndValue |
| 510 |
return scanContinue |
| 511 |
} |
| 512 |
return s.error(c, "in literal true (expecting 'e')") |
| 513 |
} |
| 514 |
|
| 515 |
|
| 516 |
func stateF(s *scanner, c byte) int { |
| 517 |
if c == 'a' { |
| 518 |
s.step = stateFa |
| 519 |
return scanContinue |
| 520 |
} |
| 521 |
return s.error(c, "in literal false (expecting 'a')") |
| 522 |
} |
| 523 |
|
| 524 |
|
| 525 |
func stateFa(s *scanner, c byte) int { |
| 526 |
if c == 'l' { |
| 527 |
s.step = stateFal |
| 528 |
return scanContinue |
| 529 |
} |
| 530 |
return s.error(c, "in literal false (expecting 'l')") |
| 531 |
} |
| 532 |
|
| 533 |
|
| 534 |
func stateFal(s *scanner, c byte) int { |
| 535 |
if c == 's' { |
| 536 |
s.step = stateFals |
| 537 |
return scanContinue |
| 538 |
} |
| 539 |
return s.error(c, "in literal false (expecting 's')") |
| 540 |
} |
| 541 |
|
| 542 |
|
| 543 |
func stateFals(s *scanner, c byte) int { |
| 544 |
if c == 'e' { |
| 545 |
s.step = stateEndValue |
| 546 |
return scanContinue |
| 547 |
} |
| 548 |
return s.error(c, "in literal false (expecting 'e')") |
| 549 |
} |
| 550 |
|
| 551 |
|
| 552 |
func stateN(s *scanner, c byte) int { |
| 553 |
if c == 'u' { |
| 554 |
s.step = stateNu |
| 555 |
return scanContinue |
| 556 |
} |
| 557 |
return s.error(c, "in literal null (expecting 'u')") |
| 558 |
} |
| 559 |
|
| 560 |
|
| 561 |
func stateNu(s *scanner, c byte) int { |
| 562 |
if c == 'l' { |
| 563 |
s.step = stateNul |
| 564 |
return scanContinue |
| 565 |
} |
| 566 |
return s.error(c, "in literal null (expecting 'l')") |
| 567 |
} |
| 568 |
|
| 569 |
|
| 570 |
func stateNul(s *scanner, c byte) int { |
| 571 |
if c == 'l' { |
| 572 |
s.step = stateEndValue |
| 573 |
return scanContinue |
| 574 |
} |
| 575 |
return s.error(c, "in literal null (expecting 'l')") |
| 576 |
} |
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
func stateError(s *scanner, c byte) int { |
| 581 |
return scanError |
| 582 |
} |
| 583 |
|
| 584 |
|
| 585 |
func (s *scanner) error(c byte, context string) int { |
| 586 |
s.step = stateError |
| 587 |
s.err = &SyntaxError{"invalid character " + quoteChar(c) + " " + context, s.bytes} |
| 588 |
return scanError |
| 589 |
} |
| 590 |
|
| 591 |
|
| 592 |
func quoteChar(c byte) string { |
| 593 |
|
| 594 |
if c == '\'' { |
| 595 |
return `'\''` |
| 596 |
} |
| 597 |
if c == '"' { |
| 598 |
return `'"'` |
| 599 |
} |
| 600 |
|
| 601 |
|
| 602 |
s := strconv.Quote(string(c)) |
| 603 |
return "'" + s[1:len(s)-1] + "'" |
| 604 |
} |
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
func (s *scanner) undo(scanCode int) { |
| 609 |
if s.redo { |
| 610 |
panic("json: invalid use of scanner") |
| 611 |
} |
| 612 |
s.redoCode = scanCode |
| 613 |
s.redoState = s.step |
| 614 |
s.step = stateRedo |
| 615 |
s.redo = true |
| 616 |
} |
| 617 |
|
| 618 |
|
| 619 |
func stateRedo(s *scanner, c byte) int { |
| 620 |
s.redo = false |
| 621 |
s.step = s.redoState |
| 622 |
return s.redoCode |
| 623 |
} |