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.
106 lines
3.8 KiB
Markdown
106 lines
3.8 KiB
Markdown
# PureXML
|
|
|
|
[](https://github.com/mihaelamj/PureXML/actions/workflows/ci.yml)
|
|
[](https://github.com/mihaelamj/PureXML/actions/workflows/ci.yml)
|
|
[](https://github.com/mihaelamj/PureXML/actions/workflows/ci.yml)
|
|
|
|
PureXML is a dependency-free XML package written entirely in Swift.
|
|
|
|
The goal is a Linux-, Windows-, and WebAssembly-compatible XML reader/writer that
|
|
does not pull in `libxml2`, `expat`, or Foundation's `XMLParser`. The package is
|
|
intentionally strict about portability:
|
|
|
|
- no external SwiftPM dependencies
|
|
- no bundled C sources
|
|
- no Foundation requirement in the library target
|
|
- root Swift package layout
|
|
- macOS, Linux, Windows, and WASI build gates
|
|
|
|
It is a sibling project to [PureYAML](https://github.com/mihaelamj/PureYAML) and
|
|
follows the same structure, rules, and verification gates.
|
|
|
|
## Status
|
|
|
|
The node model, the emitter, and a streaming parser are implemented and usable
|
|
today.
|
|
|
|
- **Model** (`PureXML.Model`): `Node`, `Element`, `Attribute`, `QualifiedName`.
|
|
Preserves document order and the distinction between text, CDATA, comments,
|
|
and processing instructions.
|
|
- **Emitting** (`PureXML.Emitting`): `Serializer` turns a node tree into
|
|
well-formed XML, with pretty-printed and compact options and correct text and
|
|
attribute escaping.
|
|
- **Validation** (`PureXML.Validation`): structural checks such as duplicate
|
|
attribute names. Schema validation (DTD/XSD/RELAX NG) is out of scope for the
|
|
library target.
|
|
- **Parsing** (`PureXML.Parsing`): a streaming, iterative parser. `EventReader`
|
|
is a pull-based event core that consumes input through a character-source
|
|
closure and emits one `Event` at a time, never holding the whole document in
|
|
memory (it drives chunked input). `Parser` builds a `Model.Node` tree
|
|
iteratively over the event core. Handles elements, attributes, text, the five
|
|
predefined entities and numeric character references, comments, CDATA, and
|
|
processing instructions. Safe by default: `<!DOCTYPE>` is rejected, which
|
|
removes the DTD-based threat classes (XXE, entity-expansion DoS). DTD support,
|
|
namespaces-as-resolved-URIs, and validation are deliberate future layers.
|
|
|
|
## Usage
|
|
|
|
```swift
|
|
import PureXML
|
|
|
|
// Build a tree and emit it (works today).
|
|
let element = PureXML.Model.Element(
|
|
"book",
|
|
attributes: [.init("id", "bk101")],
|
|
children: [.element(.init("title", children: [.text("XML Developer's Guide")]))],
|
|
)
|
|
|
|
let xml = PureXML.serialize(.element(element))
|
|
try PureXML.validate(.element(element))
|
|
|
|
// Parse it back into a tree.
|
|
let node = try PureXML.parse(xml)
|
|
|
|
// Or stream events without building a tree (drives chunked input too).
|
|
var reader = PureXML.events(xml)
|
|
while let event = try reader.next() {
|
|
// handle .startElement / .characters / .endElement / ...
|
|
}
|
|
```
|
|
|
|
## Attribution
|
|
|
|
PureXML is informed by the behavior of established XML parsers (`libxml2`,
|
|
`expat`, Foundation's `XMLParser`) and by the W3C XML 1.0 specification, but it
|
|
does not copy their implementation into `Sources/`. See
|
|
[ATTRIBUTION.md](ATTRIBUTION.md).
|
|
|
|
## Development Contract
|
|
|
|
PureXML must stay dependency-free and portable. Before merging changes:
|
|
|
|
- Swift tools version: 6.1
|
|
- Package products: `PureXML`
|
|
- SwiftPM dependencies: none
|
|
- Hosted CI matrix: macOS, Linux, Windows, and WASM
|
|
|
|
```sh
|
|
bash scripts/check-all.sh
|
|
```
|
|
|
|
That command expands to:
|
|
|
|
```sh
|
|
bash scripts/check-style.sh
|
|
bash scripts/check-namespacing.sh
|
|
bash scripts/check-forbidden-patterns.sh
|
|
swiftformat . --config .swiftformat --lint
|
|
swiftlint --config .swiftlint.yml --strict
|
|
swift build
|
|
swift test
|
|
```
|
|
|
|
## License
|
|
|
|
MIT.
|