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"]}]}"#
);

The plugin is layered on the relaxed-JSON grammar of tabnas_jsonic, as the canonical plugin is layered on @tabnas/jsonic: use(jsonic) first, then use(Xml). In the default pure-XML mode the XML rules replace the JSON value rules; in embed mode (embed: true) they sit beside them so an XML element can appear wherever a jsonic value can.

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 parses that file’s text with jsonic at load time; this crate carries the parsed form so it needs no jsonic parse of its own to start, 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 jsonic grammar, then this plugin, the new Tabnas().use(jsonic).use(Xml, options) of the canonical plugin and the jsonic.Make() plus UseDefaults of Go.
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 file that holds the render, and the sentences that say what the render does not keep and which trees it writes. The crate embeds its own copy, translate/manifest.json, since a packaged crate holds nothing outside rs/; tests/translate_test.rs holds the copy to the 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. 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, which should already carry the jsonic grammar (as make arranges): the port of the Xml plugin function.