| 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 |
|
| 5 |
package json5 |
| 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 |
|
| 16 |
import "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. |
| 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 |
// 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. |
| 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 |
// 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: |
| 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. |
| 64 |
type SyntaxError struct { |
| 65 |
msg string // description of error |
| 66 |
Offset int64 // error occurred after reading Offset bytes |
| 67 |
} |
| 68 |
|
| 69 |
func (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?). |
| 83 |
type 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 |
// Comments are hidden from callers of the scanner, commentEndStep is used |
| 91 |
// to resume normal parsing when a comment ends. |
| 92 |
commentEndStep func(*scanner, byte) int |
| 93 |
|
| 94 |
// Reached end of top-level value. |
| 95 |
endTop bool |
| 96 |
|
| 97 |
// Stack of what we're in the middle of - array values, object keys, object values. |
| 98 |
parseState []int |
| 99 |
|
| 100 |
// Error that happened, if any. |
| 101 |
err error |
| 102 |
|
| 103 |
// 1-byte redo (see undo method) |
| 104 |
redo bool |
| 105 |
redoCode int |
| 106 |
redoState func(*scanner, byte) int |
| 107 |
|
| 108 |
// total bytes consumed, updated by decoder.Decode |
| 109 |
bytes int64 |
| 110 |
} |
| 111 |
|
| 112 |
// These values are returned by the state transition functions |
| 113 |
// assigned to scanner.state and the method scanner.eof. |
| 114 |
// They give details about the current state of the scan that |
| 115 |
// callers might be interested to know about. |
| 116 |
// It is okay to ignore the return value of any particular |
| 117 |
// call to scanner.state: if one call returns scanError, |
| 118 |
// every subsequent call will return scanError too. |
| 119 |
const ( |
| 120 |
// Continue. |
| 121 |
scanContinue = iota // uninteresting byte |
| 122 |
scanBeginLiteral // end implied by next result != scanContinue |
| 123 |
scanBeginObject // begin object |
| 124 |
scanObjectKey // just finished object key (string) |
| 125 |
scanObjectValue // just finished non-last object value |
| 126 |
scanEndObject // end object (implies scanObjectValue if possible) |
| 127 |
scanBeginArray // begin array |
| 128 |
scanArrayValue // just finished array value |
| 129 |
scanEndArray // end array (implies scanArrayValue if possible) |
| 130 |
scanSkipSpace // space byte; can skip; known to be last "continue" result |
| 131 |
|
| 132 |
// Stop. |
| 133 |
scanEnd // top-level value ended *before* this byte; known to be first "stop" result |
| 134 |
scanError // hit an error, scanner.err. |
| 135 |
) |
| 136 |
|
| 137 |
// These values are stored in the parseState stack. |
| 138 |
// They give the current state of a composite value |
| 139 |
// being scanned. If the parser is inside a nested value |
| 140 |
// the parseState describes the nested state, outermost at entry 0. |
| 141 |
const ( |
| 142 |
parseObjectKey = iota // parsing object key (before colon) |
| 143 |
parseObjectValue // parsing object value (after colon) |
| 144 |
parseArrayValue // parsing array value |
| 145 |
) |
| 146 |
|
| 147 |
// reset prepares the scanner for use. |
| 148 |
// It must be called before calling s.step. |
| 149 |
func (s *scanner) reset() { |
| 150 |
s.step = stateBeginValue |
| 151 |
s.parseState = s.parseState[0:0] |
| 152 |
s.err = nil |
| 153 |
s.redo = false |
| 154 |
s.endTop = false |
| 155 |
} |
| 156 |
|
| 157 |
// eof tells the scanner that the end of input has been reached. |
| 158 |
// It returns a scan status just as s.step does. |
| 159 |
func (s *scanner) eof() int { |
| 160 |
if s.err != nil { |
| 161 |
return scanError |
| 162 |
} |
| 163 |
if s.endTop { |
| 164 |
return scanEnd |
| 165 |
} |
| 166 |
s.step(s, ' ') |
| 167 |
if s.endTop { |
| 168 |
return scanEnd |
| 169 |
} |
| 170 |
if s.err == nil { |
| 171 |
s.err = &SyntaxError{"unexpected end of JSON input", s.bytes} |
| 172 |
} |
| 173 |
return scanError |
| 174 |
} |
| 175 |
|
| 176 |
// pushParseState pushes a new parse state p onto the parse stack. |
| 177 |
func (s *scanner) pushParseState(p int) { |
| 178 |
s.parseState = append(s.parseState, p) |
| 179 |
} |
| 180 |
|
| 181 |
// popParseState pops a parse state (already obtained) off the stack |
| 182 |
// and updates s.step accordingly. |
| 183 |
func (s *scanner) popParseState() { |
| 184 |
n := len(s.parseState) - 1 |
| 185 |
s.parseState = s.parseState[0:n] |
| 186 |
s.redo = false |
| 187 |
if n == 0 { |
| 188 |
s.step = stateEndTop |
| 189 |
s.endTop = true |
| 190 |
} else { |
| 191 |
s.step = stateEndValue |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
func isSpace(c byte) bool { |
| 196 |
return c == ' ' || c == '\t' || c == '\r' || c == '\n' || c == '\f' |
| 197 |
} |
| 198 |
|
| 199 |
// stateBeginValueOrEmpty is the state after reading `[`. |
| 200 |
func stateBeginValueOrEmpty(s *scanner, c byte) int { |
| 201 |
if c <= ' ' && isSpace(c) { |
| 202 |
return scanSkipSpace |
| 203 |
} |
| 204 |
if c == ']' { |
| 205 |
return stateEndValue(s, c) |
| 206 |
} |
| 207 |
if c == '/' { |
| 208 |
s.step = stateBeginComment |
| 209 |
s.commentEndStep = stateBeginValueOrEmpty |
| 210 |
return scanSkipSpace |
| 211 |
} |
| 212 |
return stateBeginValue(s, c) |
| 213 |
} |
| 214 |
|
| 215 |
// stateBeginValue is the state at the beginning of the input. |
| 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 = stateBeginObjectKeyOrEmpty |
| 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 = stateInStringDouble |
| 231 |
return scanBeginLiteral |
| 232 |
case '\'': |
| 233 |
s.step = stateInStringSingle |
| 234 |
return scanBeginLiteral |
| 235 |
case '-', '+': |
| 236 |
s.step = stateSign |
| 237 |
return scanBeginLiteral |
| 238 |
case '.': |
| 239 |
s.step = stateDot |
| 240 |
return scanBeginLiteral |
| 241 |
case '0': // beginning of 0.123 |
| 242 |
s.step = stateFirst0 |
| 243 |
return scanBeginLiteral |
| 244 |
case 't': // beginning of true |
| 245 |
s.step = stateT |
| 246 |
return scanBeginLiteral |
| 247 |
case 'f': // beginning of false |
| 248 |
s.step = stateF |
| 249 |
return scanBeginLiteral |
| 250 |
case 'n': // beginning of null |
| 251 |
s.step = stateN |
| 252 |
return scanBeginLiteral |
| 253 |
case 'I': // beginning of Infinity |
| 254 |
s.step = stateInfinity |
| 255 |
return scanBeginLiteral |
| 256 |
case 'N': // beginning of NaN |
| 257 |
s.step = stateNaN |
| 258 |
return scanBeginLiteral |
| 259 |
case '/': |
| 260 |
s.step = stateBeginComment |
| 261 |
s.commentEndStep = stateBeginValue |
| 262 |
return scanSkipSpace |
| 263 |
} |
| 264 |
if '1' <= c && c <= '9' { // beginning of 1234.5 |
| 265 |
s.step = state1 |
| 266 |
return scanBeginLiteral |
| 267 |
} |
| 268 |
return s.error(c, "looking for beginning of value") |
| 269 |
} |
| 270 |
|
| 271 |
func stateBeginComment(s *scanner, c byte) int { |
| 272 |
if c == '/' { |
| 273 |
s.step = stateInLineComment |
| 274 |
return scanSkipSpace |
| 275 |
} |
| 276 |
if c == '*' { |
| 277 |
s.step = stateInBlockComment |
| 278 |
return scanSkipSpace |
| 279 |
} |
| 280 |
return s.error(c, "potentially starting comment") |
| 281 |
} |
| 282 |
|
| 283 |
func stateInLineComment(s *scanner, c byte) int { |
| 284 |
if c == '\r' { |
| 285 |
s.step = stateInLineCommentCR |
| 286 |
} |
| 287 |
if c == '\n' { |
| 288 |
s.step = s.commentEndStep |
| 289 |
s.commentEndStep = nil |
| 290 |
} |
| 291 |
return scanSkipSpace |
| 292 |
} |
| 293 |
|
| 294 |
func stateInLineCommentCR(s *scanner, c byte) int { |
| 295 |
if c == '\n' { |
| 296 |
s.step = s.commentEndStep |
| 297 |
s.commentEndStep = nil |
| 298 |
return scanSkipSpace |
| 299 |
} |
| 300 |
return s.commentEndStep(s, c) |
| 301 |
} |
| 302 |
|
| 303 |
func stateInBlockComment(s *scanner, c byte) int { |
| 304 |
if c == '*' { |
| 305 |
s.step = stateMaybeEndBlockComment |
| 306 |
} |
| 307 |
return scanSkipSpace |
| 308 |
} |
| 309 |
|
| 310 |
func stateMaybeEndBlockComment(s *scanner, c byte) int { |
| 311 |
if c == '/' { |
| 312 |
s.step = s.commentEndStep |
| 313 |
s.commentEndStep = nil |
| 314 |
} |
| 315 |
return scanSkipSpace |
| 316 |
} |
| 317 |
|
| 318 |
// stateBeginObjectKeyOrEmpty is the state after reading `{`. |
| 319 |
func stateBeginObjectKeyOrEmpty(s *scanner, c byte) int { |
| 320 |
if c <= ' ' && isSpace(c) { |
| 321 |
return scanSkipSpace |
| 322 |
} |
| 323 |
if c == '}' { |
| 324 |
n := len(s.parseState) |
| 325 |
s.parseState[n-1] = parseObjectValue |
| 326 |
return stateEndValue(s, c) |
| 327 |
} |
| 328 |
if c == '/' { |
| 329 |
s.step = stateBeginComment |
| 330 |
s.commentEndStep = stateBeginObjectKeyOrEmpty |
| 331 |
return scanSkipSpace |
| 332 |
} |
| 333 |
return stateBeginObjectKey(s, c) |
| 334 |
} |
| 335 |
|
| 336 |
// stateBeginObjectKey is the state after reading `{"key": value,`. |
| 337 |
func stateBeginObjectKey(s *scanner, c byte) int { |
| 338 |
if c <= ' ' && isSpace(c) { |
| 339 |
return scanSkipSpace |
| 340 |
} |
| 341 |
if c == '"' { |
| 342 |
s.step = stateInStringDouble |
| 343 |
return scanBeginLiteral |
| 344 |
} |
| 345 |
if c == '\'' { |
| 346 |
s.step = stateInStringSingle |
| 347 |
return scanBeginLiteral |
| 348 |
} |
| 349 |
if isValidKeyLiteralFirstByte(c) { |
| 350 |
s.step = stateInKeyLiteral |
| 351 |
return scanBeginLiteral |
| 352 |
} |
| 353 |
if c == '/' { |
| 354 |
s.step = stateBeginComment |
| 355 |
s.commentEndStep = stateBeginObjectKey |
| 356 |
return scanSkipSpace |
| 357 |
} |
| 358 |
|
| 359 |
return s.error(c, "looking for beginning of object key") |
| 360 |
} |
| 361 |
|
| 362 |
// stateInKeyLiteral is the state when starting to read an object key with no quotes |
| 363 |
func stateInKeyLiteral(s *scanner, c byte) int { |
| 364 |
if c == ':' || isSpace(c) { |
| 365 |
return stateEndValue(s, c) |
| 366 |
} |
| 367 |
if !isValidKeyLiteralByte(c) { |
| 368 |
return s.error(c, "in key literal") |
| 369 |
} |
| 370 |
return scanContinue |
| 371 |
} |
| 372 |
|
| 373 |
func isValidKeyLiteralFirstByte(c byte) bool { |
| 374 |
return isValidKeyLiteralByte(c) && (c < '0' || c > '9') |
| 375 |
} |
| 376 |
|
| 377 |
func isValidKeyLiteralByte(c byte) bool { |
| 378 |
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '_' || c == '$' |
| 379 |
} |
| 380 |
|
| 381 |
// stateEndValue is the state after completing a value, |
| 382 |
// such as after reading `{}` or `true` or `["x"`. |
| 383 |
func stateEndValue(s *scanner, c byte) int { |
| 384 |
n := len(s.parseState) |
| 385 |
if n == 0 { |
| 386 |
// Completed top-level before the current byte. |
| 387 |
s.step = stateEndTop |
| 388 |
s.endTop = true |
| 389 |
return stateEndTop(s, c) |
| 390 |
} |
| 391 |
if c <= ' ' && isSpace(c) { |
| 392 |
s.step = stateEndValue |
| 393 |
return scanSkipSpace |
| 394 |
} |
| 395 |
if c == '/' { |
| 396 |
s.step = stateBeginComment |
| 397 |
s.commentEndStep = stateEndValue |
| 398 |
return scanSkipSpace |
| 399 |
} |
| 400 |
ps := s.parseState[n-1] |
| 401 |
switch ps { |
| 402 |
case parseObjectKey: |
| 403 |
if c == ':' { |
| 404 |
s.parseState[n-1] = parseObjectValue |
| 405 |
s.step = stateBeginValue |
| 406 |
return scanObjectKey |
| 407 |
} |
| 408 |
return s.error(c, "after object key") |
| 409 |
case parseObjectValue: |
| 410 |
if c == ',' { |
| 411 |
s.parseState[n-1] = parseObjectKey |
| 412 |
s.step = stateBeginObjectKeyOrEmpty |
| 413 |
return scanObjectValue |
| 414 |
} |
| 415 |
if c == '}' { |
| 416 |
s.popParseState() |
| 417 |
return scanEndObject |
| 418 |
} |
| 419 |
return s.error(c, "after object key:value pair") |
| 420 |
case parseArrayValue: |
| 421 |
if c == ',' { |
| 422 |
s.step = stateBeginValueOrEmpty |
| 423 |
return scanArrayValue |
| 424 |
} |
| 425 |
if c == ']' { |
| 426 |
s.popParseState() |
| 427 |
return scanEndArray |
| 428 |
} |
| 429 |
return s.error(c, "after array element") |
| 430 |
} |
| 431 |
return s.error(c, "") |
| 432 |
} |
| 433 |
|
| 434 |
// stateEndTop is the state after finishing the top-level value, |
| 435 |
// such as after reading `{}` or `[1,2,3]`. |
| 436 |
// Only space characters should be seen now. |
| 437 |
func stateEndTop(s *scanner, c byte) int { |
| 438 |
if c == '/' { |
| 439 |
s.step = stateBeginComment |
| 440 |
s.commentEndStep = stateEndTop |
| 441 |
return scanSkipSpace |
| 442 |
} |
| 443 |
if c != ' ' && c != '\t' && c != '\r' && c != '\n' { |
| 444 |
// Complain about non-space byte on next call. |
| 445 |
s.error(c, "after top-level value") |
| 446 |
} |
| 447 |
return scanEnd |
| 448 |
} |
| 449 |
|
| 450 |
// stateInStringDouble is the state after reading `"`. |
| 451 |
func stateInStringDouble(s *scanner, c byte) int { |
| 452 |
if c == '"' { |
| 453 |
s.step = stateEndValue |
| 454 |
return scanContinue |
| 455 |
} |
| 456 |
if c == '\\' { |
| 457 |
s.step = stateInStringEsc(stateInStringDouble) |
| 458 |
return scanContinue |
| 459 |
} |
| 460 |
if c < 0x20 { |
| 461 |
return s.error(c, "in string literal") |
| 462 |
} |
| 463 |
return scanContinue |
| 464 |
} |
| 465 |
|
| 466 |
// stateInStringSingle is the state after reading `"`. |
| 467 |
func stateInStringSingle(s *scanner, c byte) int { |
| 468 |
if c == '\'' { |
| 469 |
s.step = stateEndValue |
| 470 |
return scanContinue |
| 471 |
} |
| 472 |
if c == '\\' { |
| 473 |
s.step = stateInStringEsc(stateInStringSingle) |
| 474 |
return scanContinue |
| 475 |
} |
| 476 |
if c < 0x20 { |
| 477 |
return s.error(c, "in string literal") |
| 478 |
} |
| 479 |
return scanContinue |
| 480 |
} |
| 481 |
|
| 482 |
// stateInStringEsc is the state after reading `"\` during a quoted string. |
| 483 |
func stateInStringEsc(resume func(s *scanner, c byte) int) func(s *scanner, c byte) int { |
| 484 |
return func(s *scanner, c byte) int { |
| 485 |
switch c { |
| 486 |
case 'b', 'f', 'n', 'r', 't', '\\', '/', '"', '\'', '\n': |
| 487 |
s.step = resume |
| 488 |
return scanContinue |
| 489 |
case 'u': |
| 490 |
s.step = stateInStringEscU(resume) |
| 491 |
return scanContinue |
| 492 |
case 'x': |
| 493 |
s.step = stateInStringEscX(resume) |
| 494 |
return scanContinue |
| 495 |
case '\r': |
| 496 |
s.step = stateInStringEscCR(resume) |
| 497 |
return scanContinue |
| 498 |
} |
| 499 |
return s.error(c, "in string escape code") |
| 500 |
} |
| 501 |
} |
| 502 |
|
| 503 |
// stateInStringEscCR is the state after reading `"\\r` during a quoted string. |
| 504 |
func stateInStringEscCR(resume func(s *scanner, c byte) int) func(s *scanner, c byte) int { |
| 505 |
return func(s *scanner, c byte) int { |
| 506 |
s.step = resume |
| 507 |
if c == '\n' { |
| 508 |
return scanContinue |
| 509 |
} |
| 510 |
return resume(s, c) |
| 511 |
} |
| 512 |
} |
| 513 |
|
| 514 |
// stateInStringEscU is the state after reading `"\u` during a quoted string. |
| 515 |
func stateInStringEscU(resume func(s *scanner, c byte) int) func(s *scanner, c byte) int { |
| 516 |
return func(s *scanner, c byte) int { |
| 517 |
if '0' <= c && c <= '9' || 'a' <= c && c <= 'f' || 'A' <= c && c <= 'F' { |
| 518 |
s.step = stateInStringEscU1(resume) |
| 519 |
return scanContinue |
| 520 |
} |
| 521 |
// numbers |
| 522 |
return s.error(c, "in \\u hexadecimal character escape") |
| 523 |
} |
| 524 |
} |
| 525 |
|
| 526 |
// stateInStringEscU1 is the state after reading `"\u1` during a quoted string. |
| 527 |
func stateInStringEscU1(resume func(s *scanner, c byte) int) func(s *scanner, c byte) int { |
| 528 |
return func(s *scanner, c byte) int { |
| 529 |
if '0' <= c && c <= '9' || 'a' <= c && c <= 'f' || 'A' <= c && c <= 'F' { |
| 530 |
s.step = stateInStringEscU12(resume) |
| 531 |
return scanContinue |
| 532 |
} |
| 533 |
// numbers |
| 534 |
return s.error(c, "in \\u hexadecimal character escape") |
| 535 |
} |
| 536 |
} |
| 537 |
|
| 538 |
// stateInStringEscU12 is the state after reading `"\u12` during a quoted string. |
| 539 |
func stateInStringEscU12(resume func(s *scanner, c byte) int) func(s *scanner, c byte) int { |
| 540 |
return func(s *scanner, c byte) int { |
| 541 |
if '0' <= c && c <= '9' || 'a' <= c && c <= 'f' || 'A' <= c && c <= 'F' { |
| 542 |
s.step = stateInStringEscU123(resume) |
| 543 |
return scanContinue |
| 544 |
} |
| 545 |
// numbers |
| 546 |
return s.error(c, "in \\u hexadecimal character escape") |
| 547 |
} |
| 548 |
} |
| 549 |
|
| 550 |
// stateInStringEscU123 is the state after reading `"\u123` during a quoted string. |
| 551 |
func stateInStringEscU123(resume func(s *scanner, c byte) int) func(s *scanner, c byte) int { |
| 552 |
return func(s *scanner, c byte) int { |
| 553 |
if '0' <= c && c <= '9' || 'a' <= c && c <= 'f' || 'A' <= c && c <= 'F' { |
| 554 |
s.step = resume |
| 555 |
return scanContinue |
| 556 |
} |
| 557 |
// numbers |
| 558 |
return s.error(c, "in \\u hexadecimal character escape") |
| 559 |
} |
| 560 |
} |
| 561 |
|
| 562 |
// stateInStringEscX is the state after reading `"\x` during a quoted string. |
| 563 |
func stateInStringEscX(resume func(s *scanner, c byte) int) func(s *scanner, c byte) int { |
| 564 |
return func(s *scanner, c byte) int { |
| 565 |
if '0' <= c && c <= '9' || 'a' <= c && c <= 'f' || 'A' <= c && c <= 'F' { |
| 566 |
s.step = stateInStringEscX1(resume) |
| 567 |
return scanContinue |
| 568 |
} |
| 569 |
// numbers |
| 570 |
return s.error(c, "in \\x hexadecimal character escape") |
| 571 |
} |
| 572 |
} |
| 573 |
|
| 574 |
// stateInStringEscX1 is the state after reading `"\x1` during a quoted string. |
| 575 |
func stateInStringEscX1(resume func(s *scanner, c byte) int) func(s *scanner, c byte) int { |
| 576 |
return func(s *scanner, c byte) int { |
| 577 |
if '0' <= c && c <= '9' || 'a' <= c && c <= 'f' || 'A' <= c && c <= 'F' { |
| 578 |
s.step = resume |
| 579 |
return scanContinue |
| 580 |
} |
| 581 |
// numbers |
| 582 |
return s.error(c, "in \\x hexadecimal character escape") |
| 583 |
} |
| 584 |
} |
| 585 |
|
| 586 |
// stateSign is the state after reading `+` or `-` during a number. |
| 587 |
func stateSign(s *scanner, c byte) int { |
| 588 |
switch { |
| 589 |
case c == '0': |
| 590 |
s.step = stateFirst0 |
| 591 |
return scanContinue |
| 592 |
case '1' <= c && c <= '9': |
| 593 |
s.step = state1 |
| 594 |
return scanContinue |
| 595 |
case c == '.': |
| 596 |
s.step = stateSign |
| 597 |
return scanContinue |
| 598 |
case c == 'I': |
| 599 |
s.step = stateInfinity |
| 600 |
return scanContinue |
| 601 |
default: |
| 602 |
return s.error(c, "in numeric literal") |
| 603 |
} |
| 604 |
} |
| 605 |
|
| 606 |
// state1 is the state after reading a non-zero integer during a number, |
| 607 |
// such as after reading `1` or `100` but not `0`. |
| 608 |
func state1(s *scanner, c byte) int { |
| 609 |
if '0' <= c && c <= '9' { |
| 610 |
s.step = state1 |
| 611 |
return scanContinue |
| 612 |
} |
| 613 |
return state0(s, c) |
| 614 |
} |
| 615 |
|
| 616 |
// stateFirst0 is the state after the first integer in a number is `0` |
| 617 |
func stateFirst0(s *scanner, c byte) int { |
| 618 |
switch c { |
| 619 |
case '.': |
| 620 |
s.step = stateDot |
| 621 |
return scanContinue |
| 622 |
case 'e', 'E': |
| 623 |
s.step = stateE |
| 624 |
return scanContinue |
| 625 |
case 'x', 'X': |
| 626 |
s.step = stateFirstHex |
| 627 |
return scanContinue |
| 628 |
default: |
| 629 |
return stateEndValue(s, c) |
| 630 |
} |
| 631 |
} |
| 632 |
|
| 633 |
// stateFirstHex is the state after reading 0x in a number |
| 634 |
func stateFirstHex(s *scanner, c byte) int { |
| 635 |
if (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F') { |
| 636 |
s.step = stateHex |
| 637 |
return scanContinue |
| 638 |
} |
| 639 |
return s.error(c, "in hex number") |
| 640 |
} |
| 641 |
|
| 642 |
// stateHex is the state after reading the first hex digit in a number |
| 643 |
func stateHex(s *scanner, c byte) int { |
| 644 |
if (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F') { |
| 645 |
return scanContinue |
| 646 |
} |
| 647 |
return stateEndValue(s, c) |
| 648 |
} |
| 649 |
|
| 650 |
// state0 is the state after reading `0` during a number. |
| 651 |
func state0(s *scanner, c byte) int { |
| 652 |
if c == '.' { |
| 653 |
s.step = stateDot |
| 654 |
return scanContinue |
| 655 |
} |
| 656 |
if c == 'e' || c == 'E' { |
| 657 |
s.step = stateE |
| 658 |
return scanContinue |
| 659 |
} |
| 660 |
return stateEndValue(s, c) |
| 661 |
} |
| 662 |
|
| 663 |
// stateDot is the state after reading the integer and decimal point in a number, |
| 664 |
// such as after reading `1.`. |
| 665 |
func stateDot(s *scanner, c byte) int { |
| 666 |
if '0' <= c && c <= '9' { |
| 667 |
s.step = stateDot0 |
| 668 |
return scanContinue |
| 669 |
} |
| 670 |
if c == 'e' || c == 'E' { |
| 671 |
s.step = stateE |
| 672 |
return scanContinue |
| 673 |
} |
| 674 |
return stateEndValue(s, c) |
| 675 |
} |
| 676 |
|
| 677 |
// stateDot0 is the state after reading the integer, decimal point, and subsequent |
| 678 |
// digits of a number, such as after reading `3.14`. |
| 679 |
func stateDot0(s *scanner, c byte) int { |
| 680 |
if '0' <= c && c <= '9' { |
| 681 |
return scanContinue |
| 682 |
} |
| 683 |
if c == 'e' || c == 'E' { |
| 684 |
s.step = stateE |
| 685 |
return scanContinue |
| 686 |
} |
| 687 |
return stateEndValue(s, c) |
| 688 |
} |
| 689 |
|
| 690 |
// stateE is the state after reading the mantissa and e in a number, |
| 691 |
// such as after reading `314e` or `0.314e`. |
| 692 |
func stateE(s *scanner, c byte) int { |
| 693 |
if c == '+' || c == '-' { |
| 694 |
s.step = stateESign |
| 695 |
return scanContinue |
| 696 |
} |
| 697 |
return stateESign(s, c) |
| 698 |
} |
| 699 |
|
| 700 |
// stateESign is the state after reading the mantissa, e, and sign in a number, |
| 701 |
// such as after reading `314e-` or `0.314e+`. |
| 702 |
func stateESign(s *scanner, c byte) int { |
| 703 |
if '0' <= c && c <= '9' { |
| 704 |
s.step = stateE0 |
| 705 |
return scanContinue |
| 706 |
} |
| 707 |
return s.error(c, "in exponent of numeric literal") |
| 708 |
} |
| 709 |
|
| 710 |
// stateE0 is the state after reading the mantissa, e, optional sign, |
| 711 |
// and at least one digit of the exponent in a number, |
| 712 |
// such as after reading `314e-2` or `0.314e+1` or `3.14e0`. |
| 713 |
func stateE0(s *scanner, c byte) int { |
| 714 |
if '0' <= c && c <= '9' { |
| 715 |
return scanContinue |
| 716 |
} |
| 717 |
return stateEndValue(s, c) |
| 718 |
} |
| 719 |
|
| 720 |
// stateT is the state after reading `t`. |
| 721 |
func stateT(s *scanner, c byte) int { |
| 722 |
if c == 'r' { |
| 723 |
s.step = stateTr |
| 724 |
return scanContinue |
| 725 |
} |
| 726 |
return s.error(c, "in literal true (expecting 'r')") |
| 727 |
} |
| 728 |
|
| 729 |
// stateTr is the state after reading `tr`. |
| 730 |
func stateTr(s *scanner, c byte) int { |
| 731 |
if c == 'u' { |
| 732 |
s.step = stateTru |
| 733 |
return scanContinue |
| 734 |
} |
| 735 |
return s.error(c, "in literal true (expecting 'u')") |
| 736 |
} |
| 737 |
|
| 738 |
// stateTru is the state after reading `tru`. |
| 739 |
func stateTru(s *scanner, c byte) int { |
| 740 |
if c == 'e' { |
| 741 |
s.step = stateEndValue |
| 742 |
return scanContinue |
| 743 |
} |
| 744 |
return s.error(c, "in literal true (expecting 'e')") |
| 745 |
} |
| 746 |
|
| 747 |
// stateF is the state after reading `f`. |
| 748 |
func stateF(s *scanner, c byte) int { |
| 749 |
if c == 'a' { |
| 750 |
s.step = stateFa |
| 751 |
return scanContinue |
| 752 |
} |
| 753 |
return s.error(c, "in literal false (expecting 'a')") |
| 754 |
} |
| 755 |
|
| 756 |
// stateFa is the state after reading `fa`. |
| 757 |
func stateFa(s *scanner, c byte) int { |
| 758 |
if c == 'l' { |
| 759 |
s.step = stateFal |
| 760 |
return scanContinue |
| 761 |
} |
| 762 |
return s.error(c, "in literal false (expecting 'l')") |
| 763 |
} |
| 764 |
|
| 765 |
// stateFal is the state after reading `fal`. |
| 766 |
func stateFal(s *scanner, c byte) int { |
| 767 |
if c == 's' { |
| 768 |
s.step = stateFals |
| 769 |
return scanContinue |
| 770 |
} |
| 771 |
return s.error(c, "in literal false (expecting 's')") |
| 772 |
} |
| 773 |
|
| 774 |
// stateFals is the state after reading `fals`. |
| 775 |
func stateFals(s *scanner, c byte) int { |
| 776 |
if c == 'e' { |
| 777 |
s.step = stateEndValue |
| 778 |
return scanContinue |
| 779 |
} |
| 780 |
return s.error(c, "in literal false (expecting 'e')") |
| 781 |
} |
| 782 |
|
| 783 |
// stateN is the state after reading `n`. |
| 784 |
func stateN(s *scanner, c byte) int { |
| 785 |
if c == 'u' { |
| 786 |
s.step = stateNu |
| 787 |
return scanContinue |
| 788 |
} |
| 789 |
return s.error(c, "in literal null (expecting 'u')") |
| 790 |
} |
| 791 |
|
| 792 |
// stateNu is the state after reading `nu`. |
| 793 |
func stateNu(s *scanner, c byte) int { |
| 794 |
if c == 'l' { |
| 795 |
s.step = stateNul |
| 796 |
return scanContinue |
| 797 |
} |
| 798 |
return s.error(c, "in literal null (expecting 'l')") |
| 799 |
} |
| 800 |
|
| 801 |
// stateNul is the state after reading `nul`. |
| 802 |
func stateNul(s *scanner, c byte) int { |
| 803 |
if c == 'l' { |
| 804 |
s.step = stateEndValue |
| 805 |
return scanContinue |
| 806 |
} |
| 807 |
return s.error(c, "in literal null (expecting 'l')") |
| 808 |
} |
| 809 |
|
| 810 |
func stateInfinity(s *scanner, c byte) int { |
| 811 |
str := "nfinity" |
| 812 |
nextState := func(s *scanner, c byte) int { |
| 813 |
if c == str[0] { |
| 814 |
str = str[1:] |
| 815 |
if str == "" { |
| 816 |
s.step = stateEndValue |
| 817 |
} |
| 818 |
return scanContinue |
| 819 |
} |
| 820 |
return s.error(c, "in literal Infinity (expecting "+quoteChar(str[0])+")") |
| 821 |
} |
| 822 |
s.step = nextState |
| 823 |
return nextState(s, c) |
| 824 |
} |
| 825 |
|
| 826 |
func stateNaN(s *scanner, c byte) int { |
| 827 |
str := "aN" |
| 828 |
nextState := func(s *scanner, c byte) int { |
| 829 |
if c == str[0] { |
| 830 |
str = str[1:] |
| 831 |
if str == "" { |
| 832 |
s.step = stateEndValue |
| 833 |
} |
| 834 |
return scanContinue |
| 835 |
} |
| 836 |
return s.error(c, "in literal NaN (expecting "+quoteChar(str[0])+")") |
| 837 |
} |
| 838 |
s.step = nextState |
| 839 |
return nextState(s, c) |
| 840 |
} |
| 841 |
|
| 842 |
// stateError is the state after reaching a syntax error, |
| 843 |
// such as after reading `[1}` or `5.1.2`. |
| 844 |
func stateError(s *scanner, c byte) int { |
| 845 |
return scanError |
| 846 |
} |
| 847 |
|
| 848 |
// error records an error and switches to the error state. |
| 849 |
func (s *scanner) error(c byte, context string) int { |
| 850 |
s.step = stateError |
| 851 |
s.err = &SyntaxError{"invalid character " + quoteChar(c) + " " + context, s.bytes} |
| 852 |
return scanError |
| 853 |
} |
| 854 |
|
| 855 |
// quoteChar formats c as a quoted character literal |
| 856 |
func quoteChar(c byte) string { |
| 857 |
// special cases - different from quoted strings |
| 858 |
if c == '\'' { |
| 859 |
return `'\''` |
| 860 |
} |
| 861 |
if c == '"' { |
| 862 |
return `'"'` |
| 863 |
} |
| 864 |
|
| 865 |
// use quoted string with different quotation marks |
| 866 |
s := strconv.Quote(string(c)) |
| 867 |
return "'" + s[1:len(s)-1] + "'" |
| 868 |
} |
| 869 |
|
| 870 |
// undo causes the scanner to return scanCode from the next state transition. |
| 871 |
// This gives callers a simple 1-byte undo mechanism. |
| 872 |
func (s *scanner) undo(scanCode int) { |
| 873 |
if s.redo { |
| 874 |
panic("json: invalid use of scanner") |
| 875 |
} |
| 876 |
s.redoCode = scanCode |
| 877 |
s.redoState = s.step |
| 878 |
s.step = stateRedo |
| 879 |
s.redo = true |
| 880 |
} |
| 881 |
|
| 882 |
// stateRedo helps implement the scanner's 1-byte undo. |
| 883 |
func stateRedo(s *scanner, c byte) int { |
| 884 |
s.redo = false |
| 885 |
s.step = s.redoState |
| 886 |
return s.redoCode |
| 887 |
} |