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
|
/*
This source file is part of the Swift.org open source project
Copyright (c) 2020 Apple Inc. and the Swift project authors
Licensed under Apache License v2.0 with Runtime Library Exception
See http://swift.org/LICENSE.txt for license information
See http://swift.org/CONTRIBUTORS.txt for Swift project authors
*/
import Foundation
import TSCBasic
/// Protocol for the parser delegate to get notified of parsing events.
public protocol JSONMessageStreamingParserDelegate: AnyObject {
/// A decodable type representing the JSON messages being parsed.
associatedtype Message: Decodable
/// Called for each message parsed.
func jsonMessageStreamingParser(_ parser: JSONMessageStreamingParser<Self>, didParse message: Message)
/// Called when parsing raw text instead of message size.
func jsonMessageStreamingParser(_ parser: JSONMessageStreamingParser<Self>, didParseRawText text: String)
/// Called on an un-expected parsing error. No more events will be received after that.
func jsonMessageStreamingParser(_ parser: JSONMessageStreamingParser<Self>, didFailWith error: Error)
}
/// Streaming parser for JSON messages seperated by integers to represent size of message. Used by the Swift compiler
/// and XCBuild to share progess information: https://github.com/apple/swift/blob/master/docs/DriverParseableOutput.rst.
public final class JSONMessageStreamingParser<Delegate: JSONMessageStreamingParserDelegate> {
/// The object representing the JSON message being parsed.
public typealias Message = Delegate.Message
/// State of the parser state machine.
private enum State {
case parsingMessageSize
case parsingMessage(size: Int)
case parsingNewlineAfterMessage
case failed
}
/// Delegate to notify of parsing events.
public weak var delegate: Delegate?
/// Buffer containing the bytes until a full message can be parsed.
private var buffer: [UInt8] = []
/// The parser's state machine current state.
private var state: State = .parsingMessageSize
/// The JSON decoder to parse messages.
private let decoder: JSONDecoder
/// Initializes the parser.
/// - Parameters:
/// - delegate: The `JSONMessageStreamingParserDelegate` that will receive parsing event callbacks.
/// - decoder: The `JSONDecoder` to use for decoding JSON messages.
public init(delegate: Delegate, decoder: JSONDecoder = JSONDecoder())
{
self.delegate = delegate
self.decoder = decoder
}
/// Parse the next bytes of the stream.
/// - Note: If a parsing error is encountered, the delegate will be notified and the parser won't accept any further
/// input.
public func parse<C>(bytes: C) where C: Collection, C.Element == UInt8 {
if case .failed = state { return }
do {
try parseImpl(bytes: bytes)
} catch {
state = .failed
delegate?.jsonMessageStreamingParser(self, didFailWith: error)
}
}
}
private extension JSONMessageStreamingParser {
/// Error corresponding to invalid Swift compiler output.
struct ParsingError: CustomStringConvertible, LocalizedError {
var description: String {
if let error = underlyingError {
return "\(reason): \(error)"
} else {
return reason
}
}
/// Text describing the specific reason for the parsing failure.
let reason: String
/// The underlying error, if there is one.
let underlyingError: Error?
var errorDescription: String? {
self.description
}
}
/// Throwing implementation of the parse function.
func parseImpl<C>(bytes: C) throws where C: Collection, C.Element == UInt8 {
#if os(Windows)
let carriageReturn = UInt8(ascii: "\r")
let bytes = bytes.filter { $0 != carriageReturn }
#endif
switch state {
case .parsingMessageSize:
if let newlineIndex = bytes.firstIndex(of: newline) {
buffer.append(contentsOf: bytes[..<newlineIndex])
try parseMessageSize()
let nextIndex = bytes.index(after: newlineIndex)
try parseImpl(bytes: bytes[nextIndex...])
} else {
buffer.append(contentsOf: bytes)
}
case .parsingMessage(size: let size):
let remainingBytes = size - buffer.count
if remainingBytes <= bytes.count {
buffer.append(contentsOf: bytes.prefix(remainingBytes))
let message = try parseMessage()
delegate?.jsonMessageStreamingParser(self, didParse: message)
try parseImpl(bytes: bytes.dropFirst(remainingBytes))
} else {
buffer.append(contentsOf: bytes)
}
case .parsingNewlineAfterMessage:
if let firstByte = bytes.first {
precondition(firstByte == newline)
state = .parsingMessageSize
try parseImpl(bytes: bytes.dropFirst())
}
case .failed:
return
}
}
/// Parse the next message size from the buffer and update the state machine.
func parseMessageSize() throws {
guard let string = String(bytes: buffer, encoding: .utf8) else {
throw ParsingError(reason: "invalid UTF8 bytes", underlyingError: nil)
}
guard let messageSize = Int(string) else {
delegate?.jsonMessageStreamingParser(self, didParseRawText: string)
buffer.removeAll()
return
}
buffer.removeAll()
state = .parsingMessage(size: messageSize)
}
/// Parse the message in the buffer and update the state machine.
func parseMessage() throws -> Message {
let data = Data(buffer)
buffer.removeAll()
state = .parsingNewlineAfterMessage
do {
return try decoder.decode(Message.self, from: data)
} catch {
let message = ByteString(Array(data)).cString
throw ParsingError(reason: "unexpected JSON message: \(message)", underlyingError: error)
}
}
}
private let newline = UInt8(ascii: "\n")
extension JSONMessageStreamingParser.ParsingError: CustomNSError {
public var errorUserInfo: [String : Any] {
return [NSLocalizedDescriptionKey: self.description]
}
}
|