| ... | ... | @@ -84,11 +84,11 @@ pub const StreamingParser = struct { |
| 84 | 84 | } |
| 85 | 85 | |
| 86 | 86 | pub fn reset(p: *StreamingParser) void { |
| 87 | | p.state = State.TopLevelBegin; |
| 87 | p.state = .TopLevelBegin; |
| 88 | 88 | p.count = 0; |
| 89 | 89 | // Set before ever read in main transition function |
| 90 | 90 | p.after_string_state = undefined; |
| 91 | | p.after_value_state = State.ValueEnd; // handle end of values normally |
| 91 | p.after_value_state = .ValueEnd; // handle end of values normally |
| 92 | 92 | p.stack = 0; |
| 93 | 93 | p.stack_used = 0; |
| 94 | 94 | p.complete = false; |
| ... | ... | @@ -187,14 +187,14 @@ pub const StreamingParser = struct { |
| 187 | 187 | // Perform a single transition on the state machine and return any possible token. |
| 188 | 188 | fn transition(p: *StreamingParser, c: u8, token: *?Token) Error!bool { |
| 189 | 189 | switch (p.state) { |
| 190 | | State.TopLevelBegin => switch (c) { |
| 190 | .TopLevelBegin => switch (c) { |
| 191 | 191 | '{' => { |
| 192 | 192 | p.stack <<= 1; |
| 193 | 193 | p.stack |= object_bit; |
| 194 | 194 | p.stack_used += 1; |
| 195 | 195 | |
| 196 | | p.state = State.ValueBegin; |
| 197 | | p.after_string_state = State.ObjectSeparator; |
| 196 | p.state = .ValueBegin; |
| 197 | p.after_string_state = .ObjectSeparator; |
| 198 | 198 | |
| 199 | 199 | token.* = Token.ObjectBegin; |
| 200 | 200 | }, |
| ... | ... | @@ -203,50 +203,50 @@ pub const StreamingParser = struct { |
| 203 | 203 | p.stack |= array_bit; |
| 204 | 204 | p.stack_used += 1; |
| 205 | 205 | |
| 206 | | p.state = State.ValueBegin; |
| 207 | | p.after_string_state = State.ValueEnd; |
| 206 | p.state = .ValueBegin; |
| 207 | p.after_string_state = .ValueEnd; |
| 208 | 208 | |
| 209 | 209 | token.* = Token.ArrayBegin; |
| 210 | 210 | }, |
| 211 | 211 | '-' => { |
| 212 | 212 | p.number_is_integer = true; |
| 213 | | p.state = State.Number; |
| 214 | | p.after_value_state = State.TopLevelEnd; |
| 213 | p.state = .Number; |
| 214 | p.after_value_state = .TopLevelEnd; |
| 215 | 215 | p.count = 0; |
| 216 | 216 | }, |
| 217 | 217 | '0' => { |
| 218 | 218 | p.number_is_integer = true; |
| 219 | | p.state = State.NumberMaybeDotOrExponent; |
| 220 | | p.after_value_state = State.TopLevelEnd; |
| 219 | p.state = .NumberMaybeDotOrExponent; |
| 220 | p.after_value_state = .TopLevelEnd; |
| 221 | 221 | p.count = 0; |
| 222 | 222 | }, |
| 223 | 223 | '1'...'9' => { |
| 224 | 224 | p.number_is_integer = true; |
| 225 | | p.state = State.NumberMaybeDigitOrDotOrExponent; |
| 226 | | p.after_value_state = State.TopLevelEnd; |
| 225 | p.state = .NumberMaybeDigitOrDotOrExponent; |
| 226 | p.after_value_state = .TopLevelEnd; |
| 227 | 227 | p.count = 0; |
| 228 | 228 | }, |
| 229 | 229 | '"' => { |
| 230 | | p.state = State.String; |
| 231 | | p.after_value_state = State.TopLevelEnd; |
| 230 | p.state = .String; |
| 231 | p.after_value_state = .TopLevelEnd; |
| 232 | 232 | // We don't actually need the following since after_value_state should override. |
| 233 | | p.after_string_state = State.ValueEnd; |
| 233 | p.after_string_state = .ValueEnd; |
| 234 | 234 | p.string_has_escape = false; |
| 235 | 235 | p.count = 0; |
| 236 | 236 | }, |
| 237 | 237 | 't' => { |
| 238 | | p.state = State.TrueLiteral1; |
| 239 | | p.after_value_state = State.TopLevelEnd; |
| 238 | p.state = .TrueLiteral1; |
| 239 | p.after_value_state = .TopLevelEnd; |
| 240 | 240 | p.count = 0; |
| 241 | 241 | }, |
| 242 | 242 | 'f' => { |
| 243 | | p.state = State.FalseLiteral1; |
| 244 | | p.after_value_state = State.TopLevelEnd; |
| 243 | p.state = .FalseLiteral1; |
| 244 | p.after_value_state = .TopLevelEnd; |
| 245 | 245 | p.count = 0; |
| 246 | 246 | }, |
| 247 | 247 | 'n' => { |
| 248 | | p.state = State.NullLiteral1; |
| 249 | | p.after_value_state = State.TopLevelEnd; |
| 248 | p.state = .NullLiteral1; |
| 249 | p.after_value_state = .TopLevelEnd; |
| 250 | 250 | p.count = 0; |
| 251 | 251 | }, |
| 252 | 252 | 0x09, 0x0A, 0x0D, 0x20 => { |
| ... | ... | @@ -257,7 +257,7 @@ pub const StreamingParser = struct { |
| 257 | 257 | }, |
| 258 | 258 | }, |
| 259 | 259 | |
| 260 | | State.TopLevelEnd => switch (c) { |
| 260 | .TopLevelEnd => switch (c) { |
| 261 | 261 | 0x09, 0x0A, 0x0D, 0x20 => { |
| 262 | 262 | // whitespace |
| 263 | 263 | }, |
| ... | ... | @@ -266,7 +266,7 @@ pub const StreamingParser = struct { |
| 266 | 266 | }, |
| 267 | 267 | }, |
| 268 | 268 | |
| 269 | | State.ValueBegin => switch (c) { |
| 269 | .ValueBegin => switch (c) { |
| 270 | 270 | // NOTE: These are shared in ValueEnd as well, think we can reorder states to |
| 271 | 271 | // be a bit clearer and avoid this duplication. |
| 272 | 272 | '}' => { |
| ... | ... | @@ -278,7 +278,7 @@ pub const StreamingParser = struct { |
| 278 | 278 | return error.TooManyClosingItems; |
| 279 | 279 | } |
| 280 | 280 | |
| 281 | | p.state = State.ValueBegin; |
| 281 | p.state = .ValueBegin; |
| 282 | 282 | p.after_string_state = State.fromInt(p.stack & 1); |
| 283 | 283 | |
| 284 | 284 | p.stack >>= 1; |
| ... | ... | @@ -287,10 +287,10 @@ pub const StreamingParser = struct { |
| 287 | 287 | switch (p.stack_used) { |
| 288 | 288 | 0 => { |
| 289 | 289 | p.complete = true; |
| 290 | | p.state = State.TopLevelEnd; |
| 290 | p.state = .TopLevelEnd; |
| 291 | 291 | }, |
| 292 | 292 | else => { |
| 293 | | p.state = State.ValueEnd; |
| 293 | p.state = .ValueEnd; |
| 294 | 294 | }, |
| 295 | 295 | } |
| 296 | 296 | |
| ... | ... | @@ -304,7 +304,7 @@ pub const StreamingParser = struct { |
| 304 | 304 | return error.TooManyClosingItems; |
| 305 | 305 | } |
| 306 | 306 | |
| 307 | | p.state = State.ValueBegin; |
| 307 | p.state = .ValueBegin; |
| 308 | 308 | p.after_string_state = State.fromInt(p.stack & 1); |
| 309 | 309 | |
| 310 | 310 | p.stack >>= 1; |
| ... | ... | @@ -313,10 +313,10 @@ pub const StreamingParser = struct { |
| 313 | 313 | switch (p.stack_used) { |
| 314 | 314 | 0 => { |
| 315 | 315 | p.complete = true; |
| 316 | | p.state = State.TopLevelEnd; |
| 316 | p.state = .TopLevelEnd; |
| 317 | 317 | }, |
| 318 | 318 | else => { |
| 319 | | p.state = State.ValueEnd; |
| 319 | p.state = .ValueEnd; |
| 320 | 320 | }, |
| 321 | 321 | } |
| 322 | 322 | |
| ... | ... | @@ -331,8 +331,8 @@ pub const StreamingParser = struct { |
| 331 | 331 | p.stack |= object_bit; |
| 332 | 332 | p.stack_used += 1; |
| 333 | 333 | |
| 334 | | p.state = State.ValueBegin; |
| 335 | | p.after_string_state = State.ObjectSeparator; |
| 334 | p.state = .ValueBegin; |
| 335 | p.after_string_state = .ObjectSeparator; |
| 336 | 336 | |
| 337 | 337 | token.* = Token.ObjectBegin; |
| 338 | 338 | }, |
| ... | ... | @@ -345,40 +345,40 @@ pub const StreamingParser = struct { |
| 345 | 345 | p.stack |= array_bit; |
| 346 | 346 | p.stack_used += 1; |
| 347 | 347 | |
| 348 | | p.state = State.ValueBegin; |
| 349 | | p.after_string_state = State.ValueEnd; |
| 348 | p.state = .ValueBegin; |
| 349 | p.after_string_state = .ValueEnd; |
| 350 | 350 | |
| 351 | 351 | token.* = Token.ArrayBegin; |
| 352 | 352 | }, |
| 353 | 353 | '-' => { |
| 354 | 354 | p.number_is_integer = true; |
| 355 | | p.state = State.Number; |
| 355 | p.state = .Number; |
| 356 | 356 | p.count = 0; |
| 357 | 357 | }, |
| 358 | 358 | '0' => { |
| 359 | 359 | p.number_is_integer = true; |
| 360 | | p.state = State.NumberMaybeDotOrExponent; |
| 360 | p.state = .NumberMaybeDotOrExponent; |
| 361 | 361 | p.count = 0; |
| 362 | 362 | }, |
| 363 | 363 | '1'...'9' => { |
| 364 | 364 | p.number_is_integer = true; |
| 365 | | p.state = State.NumberMaybeDigitOrDotOrExponent; |
| 365 | p.state = .NumberMaybeDigitOrDotOrExponent; |
| 366 | 366 | p.count = 0; |
| 367 | 367 | }, |
| 368 | 368 | '"' => { |
| 369 | | p.state = State.String; |
| 369 | p.state = .String; |
| 370 | 370 | p.count = 0; |
| 371 | 371 | }, |
| 372 | 372 | 't' => { |
| 373 | | p.state = State.TrueLiteral1; |
| 373 | p.state = .TrueLiteral1; |
| 374 | 374 | p.count = 0; |
| 375 | 375 | }, |
| 376 | 376 | 'f' => { |
| 377 | | p.state = State.FalseLiteral1; |
| 377 | p.state = .FalseLiteral1; |
| 378 | 378 | p.count = 0; |
| 379 | 379 | }, |
| 380 | 380 | 'n' => { |
| 381 | | p.state = State.NullLiteral1; |
| 381 | p.state = .NullLiteral1; |
| 382 | 382 | p.count = 0; |
| 383 | 383 | }, |
| 384 | 384 | 0x09, 0x0A, 0x0D, 0x20 => { |
| ... | ... | @@ -390,7 +390,7 @@ pub const StreamingParser = struct { |
| 390 | 390 | }, |
| 391 | 391 | |
| 392 | 392 | // TODO: A bit of duplication here and in the following state, redo. |
| 393 | | State.ValueBeginNoClosing => switch (c) { |
| 393 | .ValueBeginNoClosing => switch (c) { |
| 394 | 394 | '{' => { |
| 395 | 395 | if (p.stack_used == max_stack_size) { |
| 396 | 396 | return error.TooManyNestedItems; |
| ... | ... | @@ -400,8 +400,8 @@ pub const StreamingParser = struct { |
| 400 | 400 | p.stack |= object_bit; |
| 401 | 401 | p.stack_used += 1; |
| 402 | 402 | |
| 403 | | p.state = State.ValueBegin; |
| 404 | | p.after_string_state = State.ObjectSeparator; |
| 403 | p.state = .ValueBegin; |
| 404 | p.after_string_state = .ObjectSeparator; |
| 405 | 405 | |
| 406 | 406 | token.* = Token.ObjectBegin; |
| 407 | 407 | }, |
| ... | ... | @@ -414,40 +414,40 @@ pub const StreamingParser = struct { |
| 414 | 414 | p.stack |= array_bit; |
| 415 | 415 | p.stack_used += 1; |
| 416 | 416 | |
| 417 | | p.state = State.ValueBegin; |
| 418 | | p.after_string_state = State.ValueEnd; |
| 417 | p.state = .ValueBegin; |
| 418 | p.after_string_state = .ValueEnd; |
| 419 | 419 | |
| 420 | 420 | token.* = Token.ArrayBegin; |
| 421 | 421 | }, |
| 422 | 422 | '-' => { |
| 423 | 423 | p.number_is_integer = true; |
| 424 | | p.state = State.Number; |
| 424 | p.state = .Number; |
| 425 | 425 | p.count = 0; |
| 426 | 426 | }, |
| 427 | 427 | '0' => { |
| 428 | 428 | p.number_is_integer = true; |
| 429 | | p.state = State.NumberMaybeDotOrExponent; |
| 429 | p.state = .NumberMaybeDotOrExponent; |
| 430 | 430 | p.count = 0; |
| 431 | 431 | }, |
| 432 | 432 | '1'...'9' => { |
| 433 | 433 | p.number_is_integer = true; |
| 434 | | p.state = State.NumberMaybeDigitOrDotOrExponent; |
| 434 | p.state = .NumberMaybeDigitOrDotOrExponent; |
| 435 | 435 | p.count = 0; |
| 436 | 436 | }, |
| 437 | 437 | '"' => { |
| 438 | | p.state = State.String; |
| 438 | p.state = .String; |
| 439 | 439 | p.count = 0; |
| 440 | 440 | }, |
| 441 | 441 | 't' => { |
| 442 | | p.state = State.TrueLiteral1; |
| 442 | p.state = .TrueLiteral1; |
| 443 | 443 | p.count = 0; |
| 444 | 444 | }, |
| 445 | 445 | 'f' => { |
| 446 | | p.state = State.FalseLiteral1; |
| 446 | p.state = .FalseLiteral1; |
| 447 | 447 | p.count = 0; |
| 448 | 448 | }, |
| 449 | 449 | 'n' => { |
| 450 | | p.state = State.NullLiteral1; |
| 450 | p.state = .NullLiteral1; |
| 451 | 451 | p.count = 0; |
| 452 | 452 | }, |
| 453 | 453 | 0x09, 0x0A, 0x0D, 0x20 => { |
| ... | ... | @@ -458,17 +458,17 @@ pub const StreamingParser = struct { |
| 458 | 458 | }, |
| 459 | 459 | }, |
| 460 | 460 | |
| 461 | | State.ValueEnd => switch (c) { |
| 461 | .ValueEnd => switch (c) { |
| 462 | 462 | ',' => { |
| 463 | 463 | p.after_string_state = State.fromInt(p.stack & 1); |
| 464 | | p.state = State.ValueBeginNoClosing; |
| 464 | p.state = .ValueBeginNoClosing; |
| 465 | 465 | }, |
| 466 | 466 | ']' => { |
| 467 | 467 | if (p.stack_used == 0) { |
| 468 | 468 | return error.UnbalancedBrackets; |
| 469 | 469 | } |
| 470 | 470 | |
| 471 | | p.state = State.ValueEnd; |
| 471 | p.state = .ValueEnd; |
| 472 | 472 | p.after_string_state = State.fromInt(p.stack & 1); |
| 473 | 473 | |
| 474 | 474 | p.stack >>= 1; |
| ... | ... | @@ -476,7 +476,7 @@ pub const StreamingParser = struct { |
| 476 | 476 | |
| 477 | 477 | if (p.stack_used == 0) { |
| 478 | 478 | p.complete = true; |
| 479 | | p.state = State.TopLevelEnd; |
| 479 | p.state = .TopLevelEnd; |
| 480 | 480 | } |
| 481 | 481 | |
| 482 | 482 | token.* = Token.ArrayEnd; |
| ... | ... | @@ -486,7 +486,7 @@ pub const StreamingParser = struct { |
| 486 | 486 | return error.UnbalancedBraces; |
| 487 | 487 | } |
| 488 | 488 | |
| 489 | | p.state = State.ValueEnd; |
| 489 | p.state = .ValueEnd; |
| 490 | 490 | p.after_string_state = State.fromInt(p.stack & 1); |
| 491 | 491 | |
| 492 | 492 | p.stack >>= 1; |
| ... | ... | @@ -494,7 +494,7 @@ pub const StreamingParser = struct { |
| 494 | 494 | |
| 495 | 495 | if (p.stack_used == 0) { |
| 496 | 496 | p.complete = true; |
| 497 | | p.state = State.TopLevelEnd; |
| 497 | p.state = .TopLevelEnd; |
| 498 | 498 | } |
| 499 | 499 | |
| 500 | 500 | token.* = Token.ObjectEnd; |
| ... | ... | @@ -507,10 +507,10 @@ pub const StreamingParser = struct { |
| 507 | 507 | }, |
| 508 | 508 | }, |
| 509 | 509 | |
| 510 | | State.ObjectSeparator => switch (c) { |
| 510 | .ObjectSeparator => switch (c) { |
| 511 | 511 | ':' => { |
| 512 | | p.state = State.ValueBegin; |
| 513 | | p.after_string_state = State.ValueEnd; |
| 512 | p.state = .ValueBegin; |
| 513 | p.after_string_state = .ValueEnd; |
| 514 | 514 | }, |
| 515 | 515 | 0x09, 0x0A, 0x0D, 0x20 => { |
| 516 | 516 | // whitespace |
| ... | ... | @@ -520,14 +520,14 @@ pub const StreamingParser = struct { |
| 520 | 520 | }, |
| 521 | 521 | }, |
| 522 | 522 | |
| 523 | | State.String => switch (c) { |
| 523 | .String => switch (c) { |
| 524 | 524 | 0x00...0x1F => { |
| 525 | 525 | return error.InvalidControlCharacter; |
| 526 | 526 | }, |
| 527 | 527 | '"' => { |
| 528 | 528 | p.state = p.after_string_state; |
| 529 | | if (p.after_value_state == State.TopLevelEnd) { |
| 530 | | p.state = State.TopLevelEnd; |
| 529 | if (p.after_value_state == .TopLevelEnd) { |
| 530 | p.state = .TopLevelEnd; |
| 531 | 531 | p.complete = true; |
| 532 | 532 | } |
| 533 | 533 | |
| ... | ... | @@ -539,41 +539,41 @@ pub const StreamingParser = struct { |
| 539 | 539 | }; |
| 540 | 540 | }, |
| 541 | 541 | '\\' => { |
| 542 | | p.state = State.StringEscapeCharacter; |
| 542 | p.state = .StringEscapeCharacter; |
| 543 | 543 | }, |
| 544 | 544 | 0x20, 0x21, 0x23...0x5B, 0x5D...0x7F => { |
| 545 | 545 | // non-control ascii |
| 546 | 546 | }, |
| 547 | 547 | 0xC0...0xDF => { |
| 548 | | p.state = State.StringUtf8Byte1; |
| 548 | p.state = .StringUtf8Byte1; |
| 549 | 549 | }, |
| 550 | 550 | 0xE0...0xEF => { |
| 551 | | p.state = State.StringUtf8Byte2; |
| 551 | p.state = .StringUtf8Byte2; |
| 552 | 552 | }, |
| 553 | 553 | 0xF0...0xFF => { |
| 554 | | p.state = State.StringUtf8Byte3; |
| 554 | p.state = .StringUtf8Byte3; |
| 555 | 555 | }, |
| 556 | 556 | else => { |
| 557 | 557 | return error.InvalidUtf8Byte; |
| 558 | 558 | }, |
| 559 | 559 | }, |
| 560 | 560 | |
| 561 | | State.StringUtf8Byte3 => switch (c >> 6) { |
| 562 | | 0b10 => p.state = State.StringUtf8Byte2, |
| 561 | .StringUtf8Byte3 => switch (c >> 6) { |
| 562 | 0b10 => p.state = .StringUtf8Byte2, |
| 563 | 563 | else => return error.InvalidUtf8Byte, |
| 564 | 564 | }, |
| 565 | 565 | |
| 566 | | State.StringUtf8Byte2 => switch (c >> 6) { |
| 567 | | 0b10 => p.state = State.StringUtf8Byte1, |
| 566 | .StringUtf8Byte2 => switch (c >> 6) { |
| 567 | 0b10 => p.state = .StringUtf8Byte1, |
| 568 | 568 | else => return error.InvalidUtf8Byte, |
| 569 | 569 | }, |
| 570 | 570 | |
| 571 | | State.StringUtf8Byte1 => switch (c >> 6) { |
| 572 | | 0b10 => p.state = State.String, |
| 571 | .StringUtf8Byte1 => switch (c >> 6) { |
| 572 | 0b10 => p.state = .String, |
| 573 | 573 | else => return error.InvalidUtf8Byte, |
| 574 | 574 | }, |
| 575 | 575 | |
| 576 | | State.StringEscapeCharacter => switch (c) { |
| 576 | .StringEscapeCharacter => switch (c) { |
| 577 | 577 | // NOTE: '/' is allowed as an escaped character but it also is allowed |
| 578 | 578 | // as unescaped according to the RFC. There is a reported errata which suggests |
| 579 | 579 | // removing the non-escaped variant but it makes more sense to simply disallow |
| ... | ... | @@ -584,53 +584,53 @@ pub const StreamingParser = struct { |
| 584 | 584 | // is further clarified. |
| 585 | 585 | '"', '\\', '/', 'b', 'f', 'n', 'r', 't' => { |
| 586 | 586 | p.string_has_escape = true; |
| 587 | | p.state = State.String; |
| 587 | p.state = .String; |
| 588 | 588 | }, |
| 589 | 589 | 'u' => { |
| 590 | 590 | p.string_has_escape = true; |
| 591 | | p.state = State.StringEscapeHexUnicode4; |
| 591 | p.state = .StringEscapeHexUnicode4; |
| 592 | 592 | }, |
| 593 | 593 | else => { |
| 594 | 594 | return error.InvalidEscapeCharacter; |
| 595 | 595 | }, |
| 596 | 596 | }, |
| 597 | 597 | |
| 598 | | State.StringEscapeHexUnicode4 => switch (c) { |
| 598 | .StringEscapeHexUnicode4 => switch (c) { |
| 599 | 599 | '0'...'9', 'A'...'F', 'a'...'f' => { |
| 600 | | p.state = State.StringEscapeHexUnicode3; |
| 600 | p.state = .StringEscapeHexUnicode3; |
| 601 | 601 | }, |
| 602 | 602 | else => return error.InvalidUnicodeHexSymbol, |
| 603 | 603 | }, |
| 604 | 604 | |
| 605 | | State.StringEscapeHexUnicode3 => switch (c) { |
| 605 | .StringEscapeHexUnicode3 => switch (c) { |
| 606 | 606 | '0'...'9', 'A'...'F', 'a'...'f' => { |
| 607 | | p.state = State.StringEscapeHexUnicode2; |
| 607 | p.state = .StringEscapeHexUnicode2; |
| 608 | 608 | }, |
| 609 | 609 | else => return error.InvalidUnicodeHexSymbol, |
| 610 | 610 | }, |
| 611 | 611 | |
| 612 | | State.StringEscapeHexUnicode2 => switch (c) { |
| 612 | .StringEscapeHexUnicode2 => switch (c) { |
| 613 | 613 | '0'...'9', 'A'...'F', 'a'...'f' => { |
| 614 | | p.state = State.StringEscapeHexUnicode1; |
| 614 | p.state = .StringEscapeHexUnicode1; |
| 615 | 615 | }, |
| 616 | 616 | else => return error.InvalidUnicodeHexSymbol, |
| 617 | 617 | }, |
| 618 | 618 | |
| 619 | | State.StringEscapeHexUnicode1 => switch (c) { |
| 619 | .StringEscapeHexUnicode1 => switch (c) { |
| 620 | 620 | '0'...'9', 'A'...'F', 'a'...'f' => { |
| 621 | | p.state = State.String; |
| 621 | p.state = .String; |
| 622 | 622 | }, |
| 623 | 623 | else => return error.InvalidUnicodeHexSymbol, |
| 624 | 624 | }, |
| 625 | 625 | |
| 626 | | State.Number => { |
| 627 | | p.complete = p.after_value_state == State.TopLevelEnd; |
| 626 | .Number => { |
| 627 | p.complete = p.after_value_state == .TopLevelEnd; |
| 628 | 628 | switch (c) { |
| 629 | 629 | '0' => { |
| 630 | | p.state = State.NumberMaybeDotOrExponent; |
| 630 | p.state = .NumberMaybeDotOrExponent; |
| 631 | 631 | }, |
| 632 | 632 | '1'...'9' => { |
| 633 | | p.state = State.NumberMaybeDigitOrDotOrExponent; |
| 633 | p.state = .NumberMaybeDigitOrDotOrExponent; |
| 634 | 634 | }, |
| 635 | 635 | else => { |
| 636 | 636 | return error.InvalidNumber; |
| ... | ... | @@ -638,16 +638,16 @@ pub const StreamingParser = struct { |
| 638 | 638 | } |
| 639 | 639 | }, |
| 640 | 640 | |
| 641 | | State.NumberMaybeDotOrExponent => { |
| 642 | | p.complete = p.after_value_state == State.TopLevelEnd; |
| 641 | .NumberMaybeDotOrExponent => { |
| 642 | p.complete = p.after_value_state == .TopLevelEnd; |
| 643 | 643 | switch (c) { |
| 644 | 644 | '.' => { |
| 645 | 645 | p.number_is_integer = false; |
| 646 | | p.state = State.NumberFractionalRequired; |
| 646 | p.state = .NumberFractionalRequired; |
| 647 | 647 | }, |
| 648 | 648 | 'e', 'E' => { |
| 649 | 649 | p.number_is_integer = false; |
| 650 | | p.state = State.NumberExponent; |
| 650 | p.state = .NumberExponent; |
| 651 | 651 | }, |
| 652 | 652 | else => { |
| 653 | 653 | p.state = p.after_value_state; |
| ... | ... | @@ -662,16 +662,16 @@ pub const StreamingParser = struct { |
| 662 | 662 | } |
| 663 | 663 | }, |
| 664 | 664 | |
| 665 | | State.NumberMaybeDigitOrDotOrExponent => { |
| 666 | | p.complete = p.after_value_state == State.TopLevelEnd; |
| 665 | .NumberMaybeDigitOrDotOrExponent => { |
| 666 | p.complete = p.after_value_state == .TopLevelEnd; |
| 667 | 667 | switch (c) { |
| 668 | 668 | '.' => { |
| 669 | 669 | p.number_is_integer = false; |
| 670 | | p.state = State.NumberFractionalRequired; |
| 670 | p.state = .NumberFractionalRequired; |
| 671 | 671 | }, |
| 672 | 672 | 'e', 'E' => { |
| 673 | 673 | p.number_is_integer = false; |
| 674 | | p.state = State.NumberExponent; |
| 674 | p.state = .NumberExponent; |
| 675 | 675 | }, |
| 676 | 676 | '0'...'9' => { |
| 677 | 677 | // another digit |
| ... | ... | @@ -689,11 +689,11 @@ pub const StreamingParser = struct { |
| 689 | 689 | } |
| 690 | 690 | }, |
| 691 | 691 | |
| 692 | | State.NumberFractionalRequired => { |
| 693 | | p.complete = p.after_value_state == State.TopLevelEnd; |
| 692 | .NumberFractionalRequired => { |
| 693 | p.complete = p.after_value_state == .TopLevelEnd; |
| 694 | 694 | switch (c) { |
| 695 | 695 | '0'...'9' => { |
| 696 | | p.state = State.NumberFractional; |
| 696 | p.state = .NumberFractional; |
| 697 | 697 | }, |
| 698 | 698 | else => { |
| 699 | 699 | return error.InvalidNumber; |
| ... | ... | @@ -701,15 +701,15 @@ pub const StreamingParser = struct { |
| 701 | 701 | } |
| 702 | 702 | }, |
| 703 | 703 | |
| 704 | | State.NumberFractional => { |
| 705 | | p.complete = p.after_value_state == State.TopLevelEnd; |
| 704 | .NumberFractional => { |
| 705 | p.complete = p.after_value_state == .TopLevelEnd; |
| 706 | 706 | switch (c) { |
| 707 | 707 | '0'...'9' => { |
| 708 | 708 | // another digit |
| 709 | 709 | }, |
| 710 | 710 | 'e', 'E' => { |
| 711 | 711 | p.number_is_integer = false; |
| 712 | | p.state = State.NumberExponent; |
| 712 | p.state = .NumberExponent; |
| 713 | 713 | }, |
| 714 | 714 | else => { |
| 715 | 715 | p.state = p.after_value_state; |
| ... | ... | @@ -724,12 +724,12 @@ pub const StreamingParser = struct { |
| 724 | 724 | } |
| 725 | 725 | }, |
| 726 | 726 | |
| 727 | | State.NumberMaybeExponent => { |
| 728 | | p.complete = p.after_value_state == State.TopLevelEnd; |
| 727 | .NumberMaybeExponent => { |
| 728 | p.complete = p.after_value_state == .TopLevelEnd; |
| 729 | 729 | switch (c) { |
| 730 | 730 | 'e', 'E' => { |
| 731 | 731 | p.number_is_integer = false; |
| 732 | | p.state = State.NumberExponent; |
| 732 | p.state = .NumberExponent; |
| 733 | 733 | }, |
| 734 | 734 | else => { |
| 735 | 735 | p.state = p.after_value_state; |
| ... | ... | @@ -744,32 +744,32 @@ pub const StreamingParser = struct { |
| 744 | 744 | } |
| 745 | 745 | }, |
| 746 | 746 | |
| 747 | | State.NumberExponent => switch (c) { |
| 747 | .NumberExponent => switch (c) { |
| 748 | 748 | '-', '+' => { |
| 749 | 749 | p.complete = false; |
| 750 | | p.state = State.NumberExponentDigitsRequired; |
| 750 | p.state = .NumberExponentDigitsRequired; |
| 751 | 751 | }, |
| 752 | 752 | '0'...'9' => { |
| 753 | | p.complete = p.after_value_state == State.TopLevelEnd; |
| 754 | | p.state = State.NumberExponentDigits; |
| 753 | p.complete = p.after_value_state == .TopLevelEnd; |
| 754 | p.state = .NumberExponentDigits; |
| 755 | 755 | }, |
| 756 | 756 | else => { |
| 757 | 757 | return error.InvalidNumber; |
| 758 | 758 | }, |
| 759 | 759 | }, |
| 760 | 760 | |
| 761 | | State.NumberExponentDigitsRequired => switch (c) { |
| 761 | .NumberExponentDigitsRequired => switch (c) { |
| 762 | 762 | '0'...'9' => { |
| 763 | | p.complete = p.after_value_state == State.TopLevelEnd; |
| 764 | | p.state = State.NumberExponentDigits; |
| 763 | p.complete = p.after_value_state == .TopLevelEnd; |
| 764 | p.state = .NumberExponentDigits; |
| 765 | 765 | }, |
| 766 | 766 | else => { |
| 767 | 767 | return error.InvalidNumber; |
| 768 | 768 | }, |
| 769 | 769 | }, |
| 770 | 770 | |
| 771 | | State.NumberExponentDigits => { |
| 772 | | p.complete = p.after_value_state == State.TopLevelEnd; |
| 771 | .NumberExponentDigits => { |
| 772 | p.complete = p.after_value_state == .TopLevelEnd; |
| 773 | 773 | switch (c) { |
| 774 | 774 | '0'...'9' => { |
| 775 | 775 | // another digit |
| ... | ... | @@ -787,20 +787,20 @@ pub const StreamingParser = struct { |
| 787 | 787 | } |
| 788 | 788 | }, |
| 789 | 789 | |
| 790 | | State.TrueLiteral1 => switch (c) { |
| 791 | | 'r' => p.state = State.TrueLiteral2, |
| 790 | .TrueLiteral1 => switch (c) { |
| 791 | 'r' => p.state = .TrueLiteral2, |
| 792 | 792 | else => return error.InvalidLiteral, |
| 793 | 793 | }, |
| 794 | 794 | |
| 795 | | State.TrueLiteral2 => switch (c) { |
| 796 | | 'u' => p.state = State.TrueLiteral3, |
| 795 | .TrueLiteral2 => switch (c) { |
| 796 | 'u' => p.state = .TrueLiteral3, |
| 797 | 797 | else => return error.InvalidLiteral, |
| 798 | 798 | }, |
| 799 | 799 | |
| 800 | | State.TrueLiteral3 => switch (c) { |
| 800 | .TrueLiteral3 => switch (c) { |
| 801 | 801 | 'e' => { |
| 802 | 802 | p.state = p.after_value_state; |
| 803 | | p.complete = p.state == State.TopLevelEnd; |
| 803 | p.complete = p.state == .TopLevelEnd; |
| 804 | 804 | token.* = Token.True; |
| 805 | 805 | }, |
| 806 | 806 | else => { |
| ... | ... | @@ -808,25 +808,25 @@ pub const StreamingParser = struct { |
| 808 | 808 | }, |
| 809 | 809 | }, |
| 810 | 810 | |
| 811 | | State.FalseLiteral1 => switch (c) { |
| 812 | | 'a' => p.state = State.FalseLiteral2, |
| 811 | .FalseLiteral1 => switch (c) { |
| 812 | 'a' => p.state = .FalseLiteral2, |
| 813 | 813 | else => return error.InvalidLiteral, |
| 814 | 814 | }, |
| 815 | 815 | |
| 816 | | State.FalseLiteral2 => switch (c) { |
| 817 | | 'l' => p.state = State.FalseLiteral3, |
| 816 | .FalseLiteral2 => switch (c) { |
| 817 | 'l' => p.state = .FalseLiteral3, |
| 818 | 818 | else => return error.InvalidLiteral, |
| 819 | 819 | }, |
| 820 | 820 | |
| 821 | | State.FalseLiteral3 => switch (c) { |
| 822 | | 's' => p.state = State.FalseLiteral4, |
| 821 | .FalseLiteral3 => switch (c) { |
| 822 | 's' => p.state = .FalseLiteral4, |
| 823 | 823 | else => return error.InvalidLiteral, |
| 824 | 824 | }, |
| 825 | 825 | |
| 826 | | State.FalseLiteral4 => switch (c) { |
| 826 | .FalseLiteral4 => switch (c) { |
| 827 | 827 | 'e' => { |
| 828 | 828 | p.state = p.after_value_state; |
| 829 | | p.complete = p.state == State.TopLevelEnd; |
| 829 | p.complete = p.state == .TopLevelEnd; |
| 830 | 830 | token.* = Token.False; |
| 831 | 831 | }, |
| 832 | 832 | else => { |
| ... | ... | @@ -834,20 +834,20 @@ pub const StreamingParser = struct { |
| 834 | 834 | }, |
| 835 | 835 | }, |
| 836 | 836 | |
| 837 | | State.NullLiteral1 => switch (c) { |
| 838 | | 'u' => p.state = State.NullLiteral2, |
| 837 | .NullLiteral1 => switch (c) { |
| 838 | 'u' => p.state = .NullLiteral2, |
| 839 | 839 | else => return error.InvalidLiteral, |
| 840 | 840 | }, |
| 841 | 841 | |
| 842 | | State.NullLiteral2 => switch (c) { |
| 843 | | 'l' => p.state = State.NullLiteral3, |
| 842 | .NullLiteral2 => switch (c) { |
| 843 | 'l' => p.state = .NullLiteral3, |
| 844 | 844 | else => return error.InvalidLiteral, |
| 845 | 845 | }, |
| 846 | 846 | |
| 847 | | State.NullLiteral3 => switch (c) { |
| 847 | .NullLiteral3 => switch (c) { |
| 848 | 848 | 'l' => { |
| 849 | 849 | p.state = p.after_value_state; |
| 850 | | p.complete = p.state == State.TopLevelEnd; |
| 850 | p.complete = p.state == .TopLevelEnd; |
| 851 | 851 | token.* = Token.Null; |
| 852 | 852 | }, |
| 853 | 853 | else => { |
| ... | ... | @@ -1075,7 +1075,7 @@ pub const Parser = struct { |
| 1075 | 1075 | pub fn init(allocator: *Allocator, copy_strings: bool) Parser { |
| 1076 | 1076 | return Parser{ |
| 1077 | 1077 | .allocator = allocator, |
| 1078 | | .state = State.Simple, |
| 1078 | .state = .Simple, |
| 1079 | 1079 | .copy_strings = copy_strings, |
| 1080 | 1080 | .stack = Array.init(allocator), |
| 1081 | 1081 | }; |
| ... | ... | @@ -1086,7 +1086,7 @@ pub const Parser = struct { |
| 1086 | 1086 | } |
| 1087 | 1087 | |
| 1088 | 1088 | pub fn reset(p: *Parser) void { |
| 1089 | | p.state = State.Simple; |
| 1089 | p.state = .Simple; |
| 1090 | 1090 | p.stack.shrink(0); |
| 1091 | 1091 | } |
| 1092 | 1092 | |
| ... | ... | @@ -1112,7 +1112,7 @@ pub const Parser = struct { |
| 1112 | 1112 | // can be cleaned up on error correctly during a `parse` on call. |
| 1113 | 1113 | fn transition(p: *Parser, allocator: *Allocator, input: []const u8, i: usize, token: Token) !void { |
| 1114 | 1114 | switch (p.state) { |
| 1115 | | State.ObjectKey => switch (token) { |
| 1115 | .ObjectKey => switch (token) { |
| 1116 | 1116 | .ObjectEnd => { |
| 1117 | 1117 | if (p.stack.len == 1) { |
| 1118 | 1118 | return; |
| ... | ... | @@ -1123,7 +1123,7 @@ pub const Parser = struct { |
| 1123 | 1123 | }, |
| 1124 | 1124 | .String => |s| { |
| 1125 | 1125 | try p.stack.append(try p.parseString(allocator, s, input, i)); |
| 1126 | | p.state = State.ObjectValue; |
| 1126 | p.state = .ObjectValue; |
| 1127 | 1127 | }, |
| 1128 | 1128 | else => { |
| 1129 | 1129 | // The streaming parser would return an error eventually. |
| ... | ... | @@ -1132,50 +1132,50 @@ pub const Parser = struct { |
| 1132 | 1132 | return error.InvalidLiteral; |
| 1133 | 1133 | }, |
| 1134 | 1134 | }, |
| 1135 | | State.ObjectValue => { |
| 1135 | .ObjectValue => { |
| 1136 | 1136 | var object = &p.stack.items[p.stack.len - 2].Object; |
| 1137 | 1137 | var key = p.stack.items[p.stack.len - 1].String; |
| 1138 | 1138 | |
| 1139 | 1139 | switch (token) { |
| 1140 | 1140 | .ObjectBegin => { |
| 1141 | 1141 | try p.stack.append(Value{ .Object = ObjectMap.init(allocator) }); |
| 1142 | | p.state = State.ObjectKey; |
| 1142 | p.state = .ObjectKey; |
| 1143 | 1143 | }, |
| 1144 | 1144 | .ArrayBegin => { |
| 1145 | 1145 | try p.stack.append(Value{ .Array = Array.init(allocator) }); |
| 1146 | | p.state = State.ArrayValue; |
| 1146 | p.state = .ArrayValue; |
| 1147 | 1147 | }, |
| 1148 | 1148 | .String => |s| { |
| 1149 | 1149 | _ = try object.put(key, try p.parseString(allocator, s, input, i)); |
| 1150 | 1150 | _ = p.stack.pop(); |
| 1151 | | p.state = State.ObjectKey; |
| 1151 | p.state = .ObjectKey; |
| 1152 | 1152 | }, |
| 1153 | 1153 | .Number => |n| { |
| 1154 | 1154 | _ = try object.put(key, try p.parseNumber(n, input, i)); |
| 1155 | 1155 | _ = p.stack.pop(); |
| 1156 | | p.state = State.ObjectKey; |
| 1156 | p.state = .ObjectKey; |
| 1157 | 1157 | }, |
| 1158 | 1158 | .True => { |
| 1159 | 1159 | _ = try object.put(key, Value{ .Bool = true }); |
| 1160 | 1160 | _ = p.stack.pop(); |
| 1161 | | p.state = State.ObjectKey; |
| 1161 | p.state = .ObjectKey; |
| 1162 | 1162 | }, |
| 1163 | 1163 | .False => { |
| 1164 | 1164 | _ = try object.put(key, Value{ .Bool = false }); |
| 1165 | 1165 | _ = p.stack.pop(); |
| 1166 | | p.state = State.ObjectKey; |
| 1166 | p.state = .ObjectKey; |
| 1167 | 1167 | }, |
| 1168 | 1168 | .Null => { |
| 1169 | 1169 | _ = try object.put(key, Value.Null); |
| 1170 | 1170 | _ = p.stack.pop(); |
| 1171 | | p.state = State.ObjectKey; |
| 1171 | p.state = .ObjectKey; |
| 1172 | 1172 | }, |
| 1173 | 1173 | .ObjectEnd, .ArrayEnd => { |
| 1174 | 1174 | unreachable; |
| 1175 | 1175 | }, |
| 1176 | 1176 | } |
| 1177 | 1177 | }, |
| 1178 | | State.ArrayValue => { |
| 1178 | .ArrayValue => { |
| 1179 | 1179 | var array = &p.stack.items[p.stack.len - 1].Array; |
| 1180 | 1180 | |
| 1181 | 1181 | switch (token) { |
| ... | ... | @@ -1189,11 +1189,11 @@ pub const Parser = struct { |
| 1189 | 1189 | }, |
| 1190 | 1190 | .ObjectBegin => { |
| 1191 | 1191 | try p.stack.append(Value{ .Object = ObjectMap.init(allocator) }); |
| 1192 | | p.state = State.ObjectKey; |
| 1192 | p.state = .ObjectKey; |
| 1193 | 1193 | }, |
| 1194 | 1194 | .ArrayBegin => { |
| 1195 | 1195 | try p.stack.append(Value{ .Array = Array.init(allocator) }); |
| 1196 | | p.state = State.ArrayValue; |
| 1196 | p.state = .ArrayValue; |
| 1197 | 1197 | }, |
| 1198 | 1198 | .String => |s| { |
| 1199 | 1199 | try array.append(try p.parseString(allocator, s, input, i)); |
| ... | ... | @@ -1215,14 +1215,14 @@ pub const Parser = struct { |
| 1215 | 1215 | }, |
| 1216 | 1216 | } |
| 1217 | 1217 | }, |
| 1218 | | State.Simple => switch (token) { |
| 1218 | .Simple => switch (token) { |
| 1219 | 1219 | .ObjectBegin => { |
| 1220 | 1220 | try p.stack.append(Value{ .Object = ObjectMap.init(allocator) }); |
| 1221 | | p.state = State.ObjectKey; |
| 1221 | p.state = .ObjectKey; |
| 1222 | 1222 | }, |
| 1223 | 1223 | .ArrayBegin => { |
| 1224 | 1224 | try p.stack.append(Value{ .Array = Array.init(allocator) }); |
| 1225 | | p.state = State.ArrayValue; |
| 1225 | p.state = .ArrayValue; |
| 1226 | 1226 | }, |
| 1227 | 1227 | .String => |s| { |
| 1228 | 1228 | try p.stack.append(try p.parseString(allocator, s, input, i)); |
| ... | ... | @@ -1254,12 +1254,12 @@ pub const Parser = struct { |
| 1254 | 1254 | |
| 1255 | 1255 | var object = &p.stack.items[p.stack.len - 1].Object; |
| 1256 | 1256 | _ = try object.put(key, value.*); |
| 1257 | | p.state = State.ObjectKey; |
| 1257 | p.state = .ObjectKey; |
| 1258 | 1258 | }, |
| 1259 | 1259 | // Array Parent -> [ ..., <array>, value ] |
| 1260 | 1260 | Value.Array => |*array| { |
| 1261 | 1261 | try array.append(value.*); |
| 1262 | | p.state = State.ArrayValue; |
| 1262 | p.state = .ArrayValue; |
| 1263 | 1263 | }, |
| 1264 | 1264 | else => { |
| 1265 | 1265 | unreachable; |