Skip to main content

Crate tabnas_xml

Crate tabnas_xml 

Source
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§

TranslationPart
One optional alchemy translation source and its explicit entry point.
TranslationParts
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. Default is 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.jsonic at the repository root parses to. The TypeScript plugin embeds the same JSON, written by ts/embed-grammar.js; this crate carries it by hand, so it needs no jsonic to read it, and tests/xml_test.rs holds the two to the same value.
VERSION
This crate’s version. It MUST equal ts/package.json “version”: the release orchestrator rewrites both, and tests/version_test.rs fails the build if they drift. Mirrors VERSION in ts/src/xml.ts and const VERSION in go/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 new Tabnas().use(Xml, options) of the canonical plugin and the tabnas.Make() plus UseDefaults of Go’s C library.
manifest_text
The plugin’s manifest, tabnas.plugin.json, as the repository carries it. Its translate object 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.json and translate/transduce.alc, since a packaged crate holds nothing outside rs/; tests/translate_test.rs holds each copy to its file.
parse
Parse an XML document with the shared default parser.
plugin
The plugin form of xml, for Tabnas::use_plugin. Options are read from the plugin option bag with XmlOptions::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’s translate.render names: a library of alchemy definitions, with no export, whose entry point xml-render writes 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 the Xml plugin function.