PureXML/Sources/Parsing/Parser.swift
Mihaela Mihaljevic 5c306482e5 feat(parser): add a streaming, iterative XML parser
Replace the notImplemented stub with a real parser built on a pull-based,
streaming event core that never requires the whole document in memory.

- Parsing.EventReader: pulls characters through a source closure with a bounded
  lookahead buffer and emits one Parsing.Event at a time, holding only an
  open-element stack. Drives chunked/streamed input.
- Parsing.Parser: builds a Model.Node tree iteratively over the event core
  (explicit stack, no recursion).
- Reader: streaming character cursor (closure-backed, bounded buffer) replacing
  the previous whole-string Array.
- EntityDecoder: the five predefined entities and decimal/hex char references.
- Handles elements, attributes, text, comments, CDATA, processing instructions,
  and an XML declaration in the prolog. Rejects <!DOCTYPE> by default (no
  DTD/XXE/entity-expansion exposure).
- Top-level API: parse(_:), parse(pulling:), events(_:), events(pulling:).
- 21 tests including event-stream and chunked-source coverage.
2026-06-08 00:10:50 +02:00

81 lines
3.3 KiB
Swift

/// A partially-built element while its children stream in. File-scope and
/// private: an internal detail of the tree builder, not part of the namespace.
private struct ElementFrame {
let name: PureXML.Model.QualifiedName
let attributes: [PureXML.Model.Attribute]
var children: [PureXML.Model.Node] = []
}
public extension PureXML.Parsing {
/// Builds a ``PureXML/Model/Node`` tree by draining the streaming
/// ``EventReader`` with an explicit element stack. It is iterative, not
/// recursive, and the streaming core means it never requires the whole input
/// in memory at once: characters are pulled on demand. Callers that do not
/// need a full tree should consume ``EventReader`` directly.
struct Parser: Sendable {
public init() {}
/// Parses a single XML document from a string into a document node.
public func parse(_ xml: String) throws -> PureXML.Model.Node {
try build(EventReader(xml))
}
/// Parses a single XML document from an incremental character source. The
/// closure returns the next character or nil at end of input, so the
/// document can arrive in chunks and is never held whole.
public func parse(pulling pull: @escaping () -> Character?) throws -> PureXML.Model.Node {
try build(EventReader(pulling: pull))
}
private func build(_ source: EventReader) throws -> PureXML.Model.Node {
var reader = source
var roots: [PureXML.Model.Node] = []
var stack: [ElementFrame] = []
var produced = false
while let event = try reader.next() {
produced = true
switch event {
case let .startElement(name, attributes):
stack.append(ElementFrame(name: name, attributes: attributes))
case .endElement:
guard let frame = stack.popLast() else {
throw ParseError.unexpectedEndOfInput(.start)
}
let element = PureXML.Model.Element(
name: frame.name,
attributes: frame.attributes,
children: frame.children,
)
attach(.element(element), to: &stack, roots: &roots)
case let .characters(text):
attach(.text(text), to: &stack, roots: &roots)
case let .cdata(text):
attach(.cdata(text), to: &stack, roots: &roots)
case let .comment(text):
attach(.comment(text), to: &stack, roots: &roots)
case let .processingInstruction(target, data):
attach(.processingInstruction(target: target, data: data), to: &stack, roots: &roots)
}
}
guard produced else {
throw ParseError.emptyDocument
}
return .document(roots)
}
private func attach(
_ node: PureXML.Model.Node,
to stack: inout [ElementFrame],
roots: inout [PureXML.Model.Node],
) {
if stack.isEmpty {
roots.append(node)
} else {
stack[stack.count - 1].children.append(node)
}
}
}
}