Expand description
The XML grammar plugin for the tabnas parsing engine.
Parses XML text into a tree of elements: attributes, mixed content,
namespaces, entities (the five predefined plus numeric character
references, custom entities, and DOCTYPE <!ENTITY> declarations),
CDATA sections, comments, processing instructions and DOCTYPE
declarations (including <!ATTLIST> attribute defaults and
xml:space / xml:lang inheritance).
let value = tabnas_xml::parse(r#"<greeting lang="en">Hi <b>world</b></greeting>"#)?;
assert_eq!(
value.to_string(),
r#"{"name":"greeting","localName":"greeting","attributes":{"lang":"en"},"children":["Hi ",{"name":"b","localName":"b","attributes":{},"children":["world"]}]}"#
);make, make_with and parse install the plugin on the bare
engine, as new Tabnas().use(Xml) does in the canonical plugin and as
Go’s C library does with tabnas.Make(). In the default pure-XML mode
the XML rules are the whole grammar, so nothing else is needed. Embed
mode (embed: true) is the exception. It splices XML into the value
rule of the relaxed-JSON jsonic grammar so an XML element can appear
wherever a jsonic value can, so it needs a parser that already carries
that grammar: install plugin or xml on a jsonic parser
(tabnas_jsonic::make(), from the tabnas-jsonic crate), as the
canonical plugin is installed after use(jsonic). This crate does not
depend on jsonic.
TypeScript is canonical: ts/src/xml.ts defines behaviour, option
names and defaults, and xml-grammar.jsonic at the repository root
defines the rule chain, embedded below as JSON. The shared fixtures in
test/spec/*.tsv are the parity contract across TypeScript, Go and
Rust.
Structs§
- Translation
Part - One optional alchemy translation source and its explicit entry point.
- Translation
Parts - The package-local structural translation interface.
- XmlError
- The error a failed parse produces, re-exported so callers need not depend on the engine crate directly.
- XmlOptions
- Plugin options.
Defaultis the canonical default set: namespaces resolved, entities decoded, no custom entities, undeclared entities an error, unbound prefixes accepted, pure-XML mode.
Constants§
- GRAMMAR_
TEXT - The grammar, as the JSON that
xml-grammar.jsonicat the repository root parses to. The TypeScript plugin embeds the same JSON, written byts/embed-grammar.js; this crate carries it by hand, so it needs no jsonic to read it, andtests/xml_test.rsholds the two to the same value. - VERSION
- This crate’s version. It MUST equal
ts/package.json“version”: the release orchestrator rewrites both, andtests/version_test.rsfails the build if they drift. MirrorsVERSIONints/src/xml.tsandconst VERSIONingo/xml.go.
Functions§
- decode_
bom - Decode a byte sequence into text, transcoding from whichever of UTF-8, UTF-16 LE/BE or UTF-32 LE/BE its byte-order mark indicates. UTF-8 is the default when there is no mark, so BOM-less UTF-8 files with non-ASCII tag names round-trip. The mark itself is dropped.
- make
- Build an XML parser with the default options.
- make_
with - Build an XML parser with caller options: the engine, then this
plugin, the
make(options)of the canonical plugin (new Tabnas().use(Xml, options)) and theMake(options)of the Go port. - manifest_
text - The plugin’s manifest,
tabnas.plugin.json, as the repository carries it. Itstranslateobject is what a host that translates reads: the shape XML is read as and written from (tree), the root it takes (any), the schema of the tree its events carry (xml-element), the files that hold the embedding of a plain tree in that schema and the render, the sentences that say what the render does not keep and how a plain tree is written, and the file that holds the transducer part (transduce), the document’s JSON events from the grammar’s rule passes under its default options, through which a host that reads parser events reads a document. The crate embeds its own copies,translate/manifest.jsonandtranslate/transduce.alc, since a packaged crate holds nothing outsiders/;tests/translate_test.rsholds each copy to its file. - parse
- Parse an XML document with the shared default parser.
- plugin
- The plugin form of
xml, forTabnas::use_plugin. Options are read from the plugin option bag withXmlOptions::from_value; installed this way the grammar is re-applied to derived instances, as every native plugin is. - render_
text - XML’s render,
alchemy/render.alc, the file the manifest’stranslate.rendernames: a library of alchemy definitions, with noexport, whose entry pointxml-renderwrites an element tree’s events, the shape this reader builds, as one XML document, and refuses any other tree, and one holding a text, an attribute value or a name no XML document can hold. A host links it with its own program. The crate embeds its own copy,translate/render.alc, held to the file as the manifest’s is. - strip_
bom - Strip a leading U+FEFF from text that is already decoded: the string
arm of the canonical
decodeBOM. - translate
- Return XML’s immutable translation parts.
- xml
- Install the XML grammar on
parser: the port of theXmlplugin function.