1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665
|
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2021 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
internal struct JSONParser {
var reader: DocumentReader
var depth: Int = 0
init(bytes: [UInt8]) {
self.reader = DocumentReader(array: bytes)
}
mutating func parse() throws -> JSONValue {
try reader.consumeWhitespace()
let value = try self.parseValue()
#if DEBUG
defer {
guard self.depth == 0 else {
preconditionFailure("Expected to end parsing with a depth of 0")
}
}
#endif
// ensure only white space is remaining
var whitespace = 0
while let next = reader.peek(offset: whitespace) {
switch next {
case ._space, ._tab, ._return, ._newline:
whitespace += 1
continue
default:
throw JSONError.unexpectedCharacter(ascii: next, characterIndex: reader.readerIndex + whitespace)
}
}
return value
}
// MARK: Generic Value Parsing
mutating func parseValue() throws -> JSONValue {
var whitespace = 0
while let byte = reader.peek(offset: whitespace) {
switch byte {
case UInt8(ascii: "\""):
reader.moveReaderIndex(forwardBy: whitespace)
return .string(try reader.readString())
case ._openbrace:
reader.moveReaderIndex(forwardBy: whitespace)
let object = try parseObject()
return .object(object)
case ._openbracket:
reader.moveReaderIndex(forwardBy: whitespace)
let array = try parseArray()
return .array(array)
case UInt8(ascii: "f"), UInt8(ascii: "t"):
reader.moveReaderIndex(forwardBy: whitespace)
let bool = try reader.readBool()
return .bool(bool)
case UInt8(ascii: "n"):
reader.moveReaderIndex(forwardBy: whitespace)
try reader.readNull()
return .null
case UInt8(ascii: "-"), UInt8(ascii: "0") ... UInt8(ascii: "9"):
reader.moveReaderIndex(forwardBy: whitespace)
let number = try self.reader.readNumber()
return .number(number)
case ._space, ._return, ._newline, ._tab:
whitespace += 1
continue
default:
throw JSONError.unexpectedCharacter(ascii: byte, characterIndex: self.reader.readerIndex)
}
}
throw JSONError.unexpectedEndOfFile
}
// MARK: - Parse Array -
mutating func parseArray() throws -> [JSONValue] {
precondition(self.reader.read() == ._openbracket)
guard self.depth < 512 else {
throw JSONError.tooManyNestedArraysOrDictionaries(characterIndex: self.reader.readerIndex - 1)
}
self.depth += 1
defer { depth -= 1 }
// parse first value or end immediatly
switch try reader.consumeWhitespace() {
case ._space, ._return, ._newline, ._tab:
preconditionFailure("Expected that all white space is consumed")
case ._closebracket:
// if the first char after whitespace is a closing bracket, we found an empty array
self.reader.moveReaderIndex(forwardBy: 1)
return []
default:
break
}
var array = [JSONValue]()
array.reserveCapacity(10)
// parse values
while true {
let value = try parseValue()
array.append(value)
// consume the whitespace after the value before the comma
let ascii = try reader.consumeWhitespace()
switch ascii {
case ._space, ._return, ._newline, ._tab:
preconditionFailure("Expected that all white space is consumed")
case ._closebracket:
reader.moveReaderIndex(forwardBy: 1)
return array
case ._comma:
// consume the comma
reader.moveReaderIndex(forwardBy: 1)
// consume the whitespace before the next value
if try reader.consumeWhitespace() == ._closebracket {
// the foundation json implementation does support trailing commas
reader.moveReaderIndex(forwardBy: 1)
return array
}
continue
default:
throw JSONError.unexpectedCharacter(ascii: ascii, characterIndex: reader.readerIndex)
}
}
}
// MARK: - Object parsing -
mutating func parseObject() throws -> [String: JSONValue] {
precondition(self.reader.read() == ._openbrace)
guard self.depth < 512 else {
throw JSONError.tooManyNestedArraysOrDictionaries(characterIndex: self.reader.readerIndex - 1)
}
self.depth += 1
defer { depth -= 1 }
// parse first value or end immediatly
switch try reader.consumeWhitespace() {
case ._space, ._return, ._newline, ._tab:
preconditionFailure("Expected that all white space is consumed")
case ._closebrace:
// if the first char after whitespace is a closing bracket, we found an empty array
self.reader.moveReaderIndex(forwardBy: 1)
return [:]
default:
break
}
var object = [String: JSONValue]()
object.reserveCapacity(20)
while true {
let key = try reader.readString()
let colon = try reader.consumeWhitespace()
guard colon == ._colon else {
throw JSONError.unexpectedCharacter(ascii: colon, characterIndex: reader.readerIndex)
}
reader.moveReaderIndex(forwardBy: 1)
try reader.consumeWhitespace()
object[key] = try self.parseValue()
let commaOrBrace = try reader.consumeWhitespace()
switch commaOrBrace {
case ._closebrace:
reader.moveReaderIndex(forwardBy: 1)
return object
case ._comma:
reader.moveReaderIndex(forwardBy: 1)
if try reader.consumeWhitespace() == ._closebrace {
// the foundation json implementation does support trailing commas
reader.moveReaderIndex(forwardBy: 1)
return object
}
continue
default:
throw JSONError.unexpectedCharacter(ascii: commaOrBrace, characterIndex: reader.readerIndex)
}
}
}
}
extension JSONParser {
struct DocumentReader {
let array: [UInt8]
private(set) var readerIndex: Int = 0
private var readableBytes: Int {
self.array.endIndex - self.readerIndex
}
var isEOF: Bool {
self.readerIndex >= self.array.endIndex
}
init(array: [UInt8]) {
self.array = array
}
subscript<R: RangeExpression<Int>>(bounds: R) -> ArraySlice<UInt8> {
self.array[bounds]
}
mutating func read() -> UInt8? {
guard self.readerIndex < self.array.endIndex else {
self.readerIndex = self.array.endIndex
return nil
}
defer { self.readerIndex += 1 }
return self.array[self.readerIndex]
}
func peek(offset: Int = 0) -> UInt8? {
guard self.readerIndex + offset < self.array.endIndex else {
return nil
}
return self.array[self.readerIndex + offset]
}
mutating func moveReaderIndex(forwardBy offset: Int) {
self.readerIndex += offset
}
@discardableResult
mutating func consumeWhitespace() throws -> UInt8 {
var whitespace = 0
while let ascii = self.peek(offset: whitespace) {
switch ascii {
case ._space, ._return, ._newline, ._tab:
whitespace += 1
continue
default:
self.moveReaderIndex(forwardBy: whitespace)
return ascii
}
}
throw JSONError.unexpectedEndOfFile
}
mutating func readString() throws -> String {
try self.readUTF8StringTillNextUnescapedQuote()
}
mutating func readNumber() throws -> String {
try self.parseNumber()
}
mutating func readBool() throws -> Bool {
switch self.read() {
case UInt8(ascii: "t"):
guard self.read() == UInt8(ascii: "r"),
self.read() == UInt8(ascii: "u"),
self.read() == UInt8(ascii: "e")
else {
guard !self.isEOF else {
throw JSONError.unexpectedEndOfFile
}
throw JSONError.unexpectedCharacter(ascii: self.peek(offset: -1)!, characterIndex: self.readerIndex - 1)
}
return true
case UInt8(ascii: "f"):
guard self.read() == UInt8(ascii: "a"),
self.read() == UInt8(ascii: "l"),
self.read() == UInt8(ascii: "s"),
self.read() == UInt8(ascii: "e")
else {
guard !self.isEOF else {
throw JSONError.unexpectedEndOfFile
}
throw JSONError.unexpectedCharacter(ascii: self.peek(offset: -1)!, characterIndex: self.readerIndex - 1)
}
return false
default:
preconditionFailure("Expected to have `t` or `f` as first character")
}
}
mutating func readNull() throws {
guard self.read() == UInt8(ascii: "n"),
self.read() == UInt8(ascii: "u"),
self.read() == UInt8(ascii: "l"),
self.read() == UInt8(ascii: "l")
else {
guard !self.isEOF else {
throw JSONError.unexpectedEndOfFile
}
throw JSONError.unexpectedCharacter(ascii: self.peek(offset: -1)!, characterIndex: self.readerIndex - 1)
}
}
// MARK: - Private Methods -
// MARK: String
enum EscapedSequenceError: Swift.Error {
case expectedLowSurrogateUTF8SequenceAfterHighSurrogate(index: Int)
case unexpectedEscapedCharacter(ascii: UInt8, index: Int)
case couldNotCreateUnicodeScalarFromUInt32(index: Int, unicodeScalarValue: UInt32)
}
private mutating func readUTF8StringTillNextUnescapedQuote() throws -> String {
guard self.read() == ._quote else {
throw JSONError.unexpectedCharacter(ascii: self.peek(offset: -1)!, characterIndex: self.readerIndex - 1)
}
var stringStartIndex = self.readerIndex
var copy = 0
var output: String?
while let byte = peek(offset: copy) {
switch byte {
case UInt8(ascii: "\""):
self.moveReaderIndex(forwardBy: copy + 1)
guard var result = output else {
// if we don't have an output string we create a new string
return try makeString(at: stringStartIndex ..< stringStartIndex + copy)
}
// if we have an output string we append
result += try makeString(at: stringStartIndex ..< stringStartIndex + copy)
return result
case 0 ... 31:
// All Unicode characters may be placed within the
// quotation marks, except for the characters that must be escaped:
// quotation mark, reverse solidus, and the control characters (U+0000
// through U+001F).
var string = output ?? ""
let errorIndex = self.readerIndex + copy
string += try makeString(at: stringStartIndex ... errorIndex)
throw JSONError.unescapedControlCharacterInString(ascii: byte, in: string, index: errorIndex)
case UInt8(ascii: "\\"):
self.moveReaderIndex(forwardBy: copy)
if output != nil {
output! += try makeString(at: stringStartIndex ..< stringStartIndex + copy)
} else {
output = try makeString(at: stringStartIndex ..< stringStartIndex + copy)
}
let escapedStartIndex = self.readerIndex
do {
let escaped = try parseEscapeSequence()
output! += escaped
stringStartIndex = self.readerIndex
copy = 0
} catch EscapedSequenceError.unexpectedEscapedCharacter(let ascii, let failureIndex) {
output! += try makeString(at: escapedStartIndex ..< self.readerIndex)
throw JSONError.unexpectedEscapedCharacter(ascii: ascii, in: output!, index: failureIndex)
} catch EscapedSequenceError.expectedLowSurrogateUTF8SequenceAfterHighSurrogate(let failureIndex) {
output! += try makeString(at: escapedStartIndex ..< self.readerIndex)
throw JSONError.expectedLowSurrogateUTF8SequenceAfterHighSurrogate(in: output!, index: failureIndex)
} catch EscapedSequenceError.couldNotCreateUnicodeScalarFromUInt32(let failureIndex, let unicodeScalarValue) {
output! += try makeString(at: escapedStartIndex ..< self.readerIndex)
throw JSONError.couldNotCreateUnicodeScalarFromUInt32(
in: output!, index: failureIndex, unicodeScalarValue: unicodeScalarValue
)
}
default:
copy += 1
continue
}
}
throw JSONError.unexpectedEndOfFile
}
private func makeString<R: RangeExpression<Int>>(at range: R) throws -> String {
let raw = array[range]
guard let str = String(bytes: raw, encoding: .utf8) else {
throw JSONError.invalidUTF8Sequence(Data(raw), characterIndex: range.relative(to: array).lowerBound)
}
return str
}
private mutating func parseEscapeSequence() throws -> String {
precondition(self.read() == ._backslash, "Expected to have an backslash first")
guard let ascii = self.read() else {
throw JSONError.unexpectedEndOfFile
}
switch ascii {
case 0x22: return "\""
case 0x5C: return "\\"
case 0x2F: return "/"
case 0x62: return "\u{08}" // \b
case 0x66: return "\u{0C}" // \f
case 0x6E: return "\u{0A}" // \n
case 0x72: return "\u{0D}" // \r
case 0x74: return "\u{09}" // \t
case 0x75:
let character = try parseUnicodeSequence()
return String(character)
default:
throw EscapedSequenceError.unexpectedEscapedCharacter(ascii: ascii, index: self.readerIndex - 1)
}
}
private mutating func parseUnicodeSequence() throws -> Unicode.Scalar {
// we build this for utf8 only for now.
let bitPattern = try parseUnicodeHexSequence()
// check if high surrogate
let isFirstByteHighSurrogate = bitPattern & 0xFC00 // nil everything except first six bits
if isFirstByteHighSurrogate == 0xD800 {
// if we have a high surrogate we expect a low surrogate next
let highSurrogateBitPattern = bitPattern
guard let (escapeChar) = self.read(),
let (uChar) = self.read()
else {
throw JSONError.unexpectedEndOfFile
}
guard escapeChar == UInt8(ascii: #"\"#), uChar == UInt8(ascii: "u") else {
throw EscapedSequenceError.expectedLowSurrogateUTF8SequenceAfterHighSurrogate(index: self.readerIndex - 1)
}
let lowSurrogateBitBattern = try parseUnicodeHexSequence()
let isSecondByteLowSurrogate = lowSurrogateBitBattern & 0xFC00 // nil everything except first six bits
guard isSecondByteLowSurrogate == 0xDC00 else {
// we are in an escaped sequence. for this reason an output string must have
// been initialized
throw EscapedSequenceError.expectedLowSurrogateUTF8SequenceAfterHighSurrogate(index: self.readerIndex - 1)
}
let highValue = UInt32(highSurrogateBitPattern - 0xD800) * 0x400
let lowValue = UInt32(lowSurrogateBitBattern - 0xDC00)
let unicodeValue = highValue + lowValue + 0x10000
guard let unicode = Unicode.Scalar(unicodeValue) else {
throw EscapedSequenceError.couldNotCreateUnicodeScalarFromUInt32(
index: self.readerIndex, unicodeScalarValue: unicodeValue
)
}
return unicode
}
guard let unicode = Unicode.Scalar(bitPattern) else {
throw EscapedSequenceError.couldNotCreateUnicodeScalarFromUInt32(
index: self.readerIndex, unicodeScalarValue: UInt32(bitPattern)
)
}
return unicode
}
private mutating func parseUnicodeHexSequence() throws -> UInt16 {
// As stated in RFC-8259 an escaped unicode character is 4 HEXDIGITs long
// https://tools.ietf.org/html/rfc8259#section-7
let startIndex = self.readerIndex
guard let firstHex = self.read(),
let secondHex = self.read(),
let thirdHex = self.read(),
let forthHex = self.read()
else {
throw JSONError.unexpectedEndOfFile
}
guard let first = DocumentReader.hexAsciiTo4Bits(firstHex),
let second = DocumentReader.hexAsciiTo4Bits(secondHex),
let third = DocumentReader.hexAsciiTo4Bits(thirdHex),
let forth = DocumentReader.hexAsciiTo4Bits(forthHex)
else {
let hexString = String(decoding: [firstHex, secondHex, thirdHex, forthHex], as: Unicode.UTF8.self)
throw JSONError.invalidHexDigitSequence(hexString, index: startIndex)
}
let firstByte = UInt16(first) << 4 | UInt16(second)
let secondByte = UInt16(third) << 4 | UInt16(forth)
let bitPattern = UInt16(firstByte) << 8 | UInt16(secondByte)
return bitPattern
}
private static func hexAsciiTo4Bits(_ ascii: UInt8) -> UInt8? {
switch ascii {
case 48 ... 57:
return ascii - 48
case 65 ... 70:
// uppercase letters
return ascii - 55
case 97 ... 102:
// lowercase letters
return ascii - 87
default:
return nil
}
}
// MARK: Numbers
private enum ControlCharacter {
case operand
case decimalPoint
case exp
case expOperator
}
private mutating func parseNumber() throws -> String {
var pastControlChar: ControlCharacter = .operand
var numbersSinceControlChar: UInt = 0
var hasLeadingZero = false
// parse first character
guard let ascii = self.peek() else {
preconditionFailure("Why was this function called, if there is no 0...9 or -")
}
switch ascii {
case UInt8(ascii: "0"):
numbersSinceControlChar = 1
pastControlChar = .operand
hasLeadingZero = true
case UInt8(ascii: "1") ... UInt8(ascii: "9"):
numbersSinceControlChar = 1
pastControlChar = .operand
case UInt8(ascii: "-"):
numbersSinceControlChar = 0
pastControlChar = .operand
default:
preconditionFailure("Why was this function called, if there is no 0...9 or -")
}
var numberchars = 1
// parse everything else
while let byte = self.peek(offset: numberchars) {
switch byte {
case UInt8(ascii: "0"):
if hasLeadingZero {
throw JSONError.numberWithLeadingZero(index: readerIndex + numberchars)
}
if numbersSinceControlChar == 0, pastControlChar == .operand {
// the number started with a minus. this is the leading zero.
hasLeadingZero = true
}
numberchars += 1
numbersSinceControlChar += 1
case UInt8(ascii: "1") ... UInt8(ascii: "9"):
if hasLeadingZero {
throw JSONError.numberWithLeadingZero(index: readerIndex + numberchars)
}
numberchars += 1
numbersSinceControlChar += 1
case UInt8(ascii: "."):
guard numbersSinceControlChar > 0, pastControlChar == .operand else {
throw JSONError.unexpectedCharacter(ascii: byte, characterIndex: readerIndex + numberchars)
}
numberchars += 1
hasLeadingZero = false
pastControlChar = .decimalPoint
numbersSinceControlChar = 0
case UInt8(ascii: "e"), UInt8(ascii: "E"):
guard numbersSinceControlChar > 0,
pastControlChar == .operand || pastControlChar == .decimalPoint
else {
throw JSONError.unexpectedCharacter(ascii: byte, characterIndex: readerIndex + numberchars)
}
numberchars += 1
hasLeadingZero = false
pastControlChar = .exp
numbersSinceControlChar = 0
case UInt8(ascii: "+"), UInt8(ascii: "-"):
guard numbersSinceControlChar == 0, pastControlChar == .exp else {
throw JSONError.unexpectedCharacter(ascii: byte, characterIndex: readerIndex + numberchars)
}
numberchars += 1
pastControlChar = .expOperator
numbersSinceControlChar = 0
case ._space, ._return, ._newline, ._tab, ._comma, ._closebracket, ._closebrace:
guard numbersSinceControlChar > 0 else {
throw JSONError.unexpectedCharacter(ascii: byte, characterIndex: readerIndex + numberchars)
}
let numberStartIndex = self.readerIndex
self.moveReaderIndex(forwardBy: numberchars)
return String(decoding: self[numberStartIndex ..< self.readerIndex], as: Unicode.UTF8.self)
default:
throw JSONError.unexpectedCharacter(ascii: byte, characterIndex: readerIndex + numberchars)
}
}
guard numbersSinceControlChar > 0 else {
throw JSONError.unexpectedEndOfFile
}
defer { self.readerIndex = self.array.endIndex }
return String(decoding: self.array.suffix(from: readerIndex), as: Unicode.UTF8.self)
}
}
}
extension UInt8 {
internal static let _space = UInt8(ascii: " ")
internal static let _return = UInt8(ascii: "\r")
internal static let _newline = UInt8(ascii: "\n")
internal static let _tab = UInt8(ascii: "\t")
internal static let _colon = UInt8(ascii: ":")
internal static let _comma = UInt8(ascii: ",")
internal static let _openbrace = UInt8(ascii: "{")
internal static let _closebrace = UInt8(ascii: "}")
internal static let _openbracket = UInt8(ascii: "[")
internal static let _closebracket = UInt8(ascii: "]")
internal static let _quote = UInt8(ascii: "\"")
internal static let _backslash = UInt8(ascii: "\\")
}
extension Array where Element == UInt8 {
internal static let _true = [UInt8(ascii: "t"), UInt8(ascii: "r"), UInt8(ascii: "u"), UInt8(ascii: "e")]
internal static let _false = [UInt8(ascii: "f"), UInt8(ascii: "a"), UInt8(ascii: "l"), UInt8(ascii: "s"), UInt8(ascii: "e")]
internal static let _null = [UInt8(ascii: "n"), UInt8(ascii: "u"), UInt8(ascii: "l"), UInt8(ascii: "l")]
}
enum JSONError: Swift.Error, Equatable {
case cannotConvertInputDataToUTF8
case unexpectedCharacter(ascii: UInt8, characterIndex: Int)
case unexpectedEndOfFile
case tooManyNestedArraysOrDictionaries(characterIndex: Int)
case invalidHexDigitSequence(String, index: Int)
case unexpectedEscapedCharacter(ascii: UInt8, in: String, index: Int)
case unescapedControlCharacterInString(ascii: UInt8, in: String, index: Int)
case expectedLowSurrogateUTF8SequenceAfterHighSurrogate(in: String, index: Int)
case couldNotCreateUnicodeScalarFromUInt32(in: String, index: Int, unicodeScalarValue: UInt32)
case numberWithLeadingZero(index: Int)
case numberIsNotRepresentableInSwift(parsed: String)
case singleFragmentFoundButNotAllowed
case invalidUTF8Sequence(Data, characterIndex: Int)
}
|