Slice 2 of #92. Add PureXML.read, a deterministic recovering reader: it never throws and never traps, returning the maximal best-effort tree plus one located Diagnostic per problem. - ReadResult { node, diagnostics } is the Equatable result type, so a deterministic read can be compared whole. - Diagnostic carries a message and a structured source Mark; a thrown ParseError maps to one directly (ParseError.mark exposes the position each case carries). - The reader resynchronizes after a failure (EventReader.recover(from:)): progress-aware, so a failure that already consumed the bad construct retries from there, while a stuck failure skips to the next markup start; it always advances when input remains, guaranteeing termination. - The tree builder assembles leniently: elements left open by truncation are closed, an end tag with no open element is dropped. Well-formed input yields exactly parse(_:)'s tree with no diagnostics. Tests pin exact trees and diagnostics for truncation, mismatched tags, and stray junk, prove determinism (same bytes give an identical ReadResult), and fuzz 400 seeded inputs (arbitrary plus truncated valid docs) asserting no crash. Recovery here is resync-based; finer in-reader recovery (keep the element on a duplicate attribute, pop to a matching open tag) is the next slice.
51 lines
2.2 KiB
Swift
51 lines
2.2 KiB
Swift
public extension PureXML.Parsing {
|
|
/// A located problem found while reading a document. Unlike a thrown
|
|
/// ``ParseError``, a diagnostic does not abort the read: the recovering reader
|
|
/// records it, repairs as best it can, and continues, so an invalid document
|
|
/// still yields a best-effort tree alongside the list of what went wrong.
|
|
struct Diagnostic: Equatable, Sendable, CustomStringConvertible {
|
|
/// A human-readable, already-located description of the problem.
|
|
public var message: String
|
|
/// The source position, when known.
|
|
public var mark: Mark?
|
|
|
|
public init(message: String, mark: Mark? = nil) {
|
|
self.message = message
|
|
self.mark = mark
|
|
}
|
|
|
|
public var description: String {
|
|
message
|
|
}
|
|
}
|
|
}
|
|
|
|
extension PureXML.Parsing.Diagnostic {
|
|
/// Wraps a thrown ``PureXML/Parsing/ParseError`` as a diagnostic, keeping its
|
|
/// located message and structured position.
|
|
init(_ error: PureXML.Parsing.ParseError) {
|
|
self.init(message: error.description, mark: error.mark)
|
|
}
|
|
}
|
|
|
|
public extension PureXML.Parsing.ParseError {
|
|
/// The source position the error occurred at, when the case carries one.
|
|
var mark: PureXML.Parsing.Mark? {
|
|
switch self {
|
|
case .emptyDocument, .malformedEncoding, .notImplemented:
|
|
nil
|
|
case let .unexpectedEndOfInput(mark), let .unterminatedTag(mark), let .unterminatedComment(mark),
|
|
let .unterminatedCDATA(mark), let .expectedName(mark), let .expectedEquals(mark),
|
|
let .unquotedAttributeValue(mark), let .amplificationLimitExceeded(mark),
|
|
let .junkAfterDocumentElement(mark), let .unsupportedDoctype(mark):
|
|
mark
|
|
case let .unexpectedCharacter(_, mark), let .mismatchedEndTag(_, _, mark),
|
|
let .duplicateAttribute(_, mark), let .invalidReference(_, mark),
|
|
let .undefinedEntity(_, mark), let .recursiveEntity(_, mark),
|
|
let .unexpectedEndTag(_, mark), let .undefinedNamespacePrefix(_, mark),
|
|
let .nestingTooDeep(_, mark), let .nameTooLong(_, mark), let .contentTooLong(_, mark):
|
|
mark
|
|
}
|
|
}
|
|
}
|