tabnas-xml (Rust)
XML 1.0 grammar plugin for the
tabnas parsing engine, crate
tabnas_xml.
The plugin parses XML text into a tree of plain values: elements with a
name, a local name, an attribute map, and a list of children. It covers
attributes, mixed content, namespaces, entities (the five predefined
ones, numeric character references, caller-supplied entities and
<!ENTITY> declarations in the DOCTYPE internal subset), CDATA
sections, comments, processing instructions, <!ATTLIST> attribute
defaults, and xml:space and xml:lang inheritance.
It is installed on the bare engine by parse, make and make_with,
as the canonical plugin is installed with new Tabnas().use(Xml). In the
default pure-XML mode the XML rules are the whole grammar, so a document
is XML and nothing else. Embed mode extends the relaxed-JSON grammar of
tabnas-jsonic instead: installed on
a jsonic parser, the two sit side by side, so an XML element can appear
wherever a jsonic value can.
This is the Rust port of the canonical TypeScript implementation in
../ts; the TypeScript version is authoritative and this crate
tracks it. The rule chain is authored once, in
../xml-grammar.jsonic, and every runtime
carries the same grammar.
Use
The shared default parser is built once and is safe to use from several threads. To build an instance of your own, and reuse it across parses:
Options are a typed struct rather than a loose bag, so a misspelled name is a compile error:
To install the plugin on an instance you already have, so XML sits
beside another grammar, use the engine's plugin entry point. Here the
other grammar is jsonic, which embed mode needs: on any other parser the
plugin refuses to install, and make_with panics. So this example also
needs tabnas-jsonic among your dependencies:
Files of unknown encoding go through decode_bom, which transcodes from
whichever of UTF-8, UTF-16 or UTF-32 the byte-order mark indicates:
Install
The crate and the engine are published on crates.io. The engine's
package is tabnas-parser, imported in code as tabnas, so add both:
Both entries are needed to name the engine. A crate's dependencies are not
passed on to its dependents, so tabnas-xml alone does not put tabnas
in your extern prelude, and the examples above that name it would not
resolve. Only XmlError is re-exported. tabnas-jsonic is not needed,
except by the embed-mode example: that one does not build without
cargo add tabnas-jsonic as well.
In this repository, Cargo.toml takes the engine from a sibling checkout
of https://github.com/tabnas/parser by path instead, and the release
workflow swaps that path for a crates.io version when it publishes this
crate.
Differences from the canonical TypeScript
The parsed value, the option names and defaults, and the error codes are the same. Seven things differ. Six are forced by the language or its engine; the last is a choice this port made, and it is marked as one:
- Options are a struct, not a map.
XmlOptionshas a field per option, withDefaultgiving the canonical defaults. The plugin entry point still takes the engine's value bag, andXmlOptions::from_valueandto_valueconvert, so a serialized configuration works as it does in the other two runtimes. - Error columns count characters. A character outside the basic multilingual plane is one column here and two in TypeScript, which counts UTF-16 units. This is the engine-wide position rule, recorded in the parser's divergence register, and Go counts characters too.
- An unpaired surrogate in the source becomes U+FFFF. A Rust string
holds Unicode scalar values only, so a surrogate cannot survive
decoding. U+FFFF stands in for it because the two are treated alike by
every XML rule: neither is a valid
Charand neither is a validNameChar. Folding it to U+FFFD would not be equivalent, because U+FFFD is a valid name character. Note what that does and does not buy: this parser checks character data for the illegal C0 controls and not for the whole ofChar, so the substitute is refused in an element or attribute name and accepted in text, attribute values, comments, CDATA, processing instructions, and DTD declaration names.decode_bomfollowed byparseis not fullCharvalidation; the doc comment ondecode_bomcarries the measured table. - Patterns carry no lookaround. The
regexcrate does not support it, so the few places the TypeScript grammar uses a negative lookahead are written as a positive match plus an inversion in a check hook. That is the shape the Go port already uses. - Byte-order-mark handling is two functions. The canonical
decodeBOMtakes either a byte sequence or a string, and decides which by looking at the code units it finds. Rust has the types for that question already:decode_bomtakes bytes and transcodes,strip_bomtakes text and removes a leading U+FEFF. A caller holding a Latin-1 byte string in Rust holds a&[u8], so the case the canonical function detects at run time does not arise. - Nesting stops at 256 open elements. A deeper document fails with
the engine's
cancelcode. The engine walks a nested value by recursion to display, convert, clone, compare or drop it, and drops its rule snapshots the same way, and a stack overflow ends the process where nothing can catch it. Displaying a value about 400 elements deep overflowed a 2 MiB thread's stack in a debug build. 256 is the depth libxml2 allows by default. TypeScript and Go accept any depth. No document a person writes comes near the limit, and a self-closed element, which opens and closes at once, doesn't count toward it. - The grammar text is public. (A CHOICE, not a constraint.)
GRAMMAR_TEXTis exported so a caller can inspect the installed rule chain, and the suite compares it with../xml-grammar.jsonic. The canonical plugin keeps its copy of the same JSON private. Nothing in Rust required this; it is a wider public surface taken deliberately, and it could be withdrawn without changing any parse.
Build and test
The engine, and for the tests the jsonic grammar, are path dependencies on sibling checkouts, so there is nothing to fetch:
&&
Or, from the repository root, make test-rs. For what CI would say,
including formatting, Clippy and the lockfile check, run
ci/rust/run.sh.
The suite runs the shared ../test/spec/*.tsv conformance fixtures, the
same files the TypeScript and Go suites run, through the same shared
runner.
It also grades the W3C XML Conformance Test Suite, fetching the pinned
snapshot on first use through ../scripts/fetch-xml-suite.sh. All three
runtimes reach the same verdict on every one of the 2284 documents in
scope: 728 of 729 valid documents parse, 232 of the 332 with a
catalogued canonical form serialize to it, all 229 merely DTD-invalid
documents are accepted as a non-validating parser must accept them, and
438 of 1326 not-well-formed documents are rejected. A corpus that cannot
be fetched fails the suite rather than skipping it.
License
MIT.