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