Expand description
An ABNF (RFC 5234) grammar compiler for the
tabnas parsing engine.
Where most tabnas grammar crates carry one fixed grammar, this one is a META compiler: the grammar it installs is whatever ABNF text it is handed at run time.
ABNF text ──parse_abnf──▶ Grammar ──emit_grammar_spec──▶ GrammarSpecparse_abnf is the RFC 5234 front-end and is what this crate adds;
everything downstream of the IR lives in
tabnas_bnf and is shared with the
GBNF and EBNF front-ends.
let mut parser = tabnas::Tabnas::new();
tabnas_abnf::abnf(&mut parser, "greet = \"hi\" / \"hello\"", None).unwrap();
let tree = parser.parse("hello").unwrap();
assert_eq!(tree.to_json()["rule"], "greet");This is the Rust port of the canonical TypeScript implementation in
ts/src; the TypeScript version is authoritative and this crate
tracks it.
Structs§
- Abnf
Action Error - A malformed or unresolvable user action ref.
- Abnf
Compile Error - A grammar that cannot be reduced to pure data.
- Abnf
Compile Options - Options for
abnf_compile: what to convert, and how to serialize it. - Abnf
Convert Options - Compiler options. Mirrors the TypeScript
ConvertOptions. - Abnf
Options - Options for
abnf, the install path. - Abnf
Parse Error - A failure to read ABNF source.
- AltSpec
- One emitted alternate: the engine’s alt fields in the order they were
written, plus the compiler-internal mark
m, which never reaches the wire. - Element
- One element of a sequence: a terminal in one of its spellings, a rule
reference, or EBNF sugar around further elements. The
kindcarries the variant;spis where the element came from, when the front-end recorded it. Mirrors the TypeScriptElementunion. - Emit
Error - A compile failure that can say WHERE.
- Grammar
- A grammar: the productions a front-end lowered, plus the
<remove>directives and the ambiguity report. - Grammar
Spec - The emitted grammar.
- Jsonic
Options - How
to_jsonicwrites its text. - Production
- One rule of the grammar. Mirrors the TypeScript
Production. - Rule
Spec - One emitted rule: its open alternates and, when it captures or replaces, its close alternates.
- SrcSpan
- Where an IR node came from in the front-end’s grammar text.
- Translation
Part - One optional alchemy translation source and its explicit entry point.
- Translation
Parts - The package-local structural translation interface.
- Value
Annotation - What a production builds when it carries a value annotation.
Enums§
- Abnf
Error - Anything that can go wrong turning ABNF source into a grammar.
- Kind
- The element variants. Serialized with the TypeScript field names, so an IR built in one runtime loads in the other.
- Node
Kind - How a production contributes to the output AST.
- RefAction
- What a
@-ref in the spec’srefmap stands for. In closure mode the emitter registers one of these per tree-building or probe-control action instead of a$-builtin;GrammarSpec::bindturns them into engine closures by name. User actions land here too. - Transduce
Part - The transducer part of an emitted grammar, or why its documents are read whole.
Constants§
- TAG
- The group tag stamped on every emitted alt, and the prefix of every diagnostic this crate raises through the shared compiler.
- 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/abnf.tsandconst VERSIONingo/abnf.go.
Functions§
- abnf
- Convert ABNF source and install the resulting grammar on
parser. - abnf_
compile - Compile ABNF source into a pure-data tabnas grammar, as jsonic text.
- abnf_
convert - Convert ABNF source into a tabnas grammar spec, without installing
it. The Rust spelling of
tn.abnf.toSpec(src). - abnf_
rules - The ABNF meta-grammar’s rule table, as data.
- abnf_
transduce - The transducer part of an ABNF grammar,
<name>-transduce: the alchemy program that streams the JSON events of the value its; @objectand; @arrayannotations build, from its rule passes, or why its documents are read whole (transduce_alcsays when). Converts asabnf_convertdoes, withopts. The Rust spelling ofabnfTransduce. - attach_
action_ slots - Declare user-action SLOTS on a (pure-data) spec without supplying
functions: each
@<rule>:o|c:<mark>ref name is injected into the matched alt’s array-a, to be resolved at load time from a user-supplied ref map. Fails on unknown targets and on rule-phase refs. - attach_
actions - Attach user semantic actions to a spec, in place. Keys are
@<rule>:<phase>(bo/ao/bc/ac) or@<rule>:o|c:<mark>; the functions run AFTER the compiler’s own action, in attachment order. Alt actions are injected as the engine’s array-aform. Fails for a ref matching no rule, hook or marked alt. - eliminate_
left_ recursion - Eliminate left recursion, both direct (P → P α) and indirect
(P → Q α, Q → P β), via Paull’s algorithm. Order the productions,
and for each A_i walk back over A_1..A_{i-1} inlining any leading
reference into A_i’s alternatives; once the only remaining leading
self-reference on A_i is direct, rewrite to the iterative form
P → (β_1 | … | β_m) (α_1 | … | α_n)*. - emit_
grammar_ spec - Emit a spec from an already-parsed ABNF grammar.
- make
- A fresh parser that reads an ABNF document as the tree: the parser a
host streams a document through with the part, whose rule passes the
part reads, and whose value is the tree (
Undefinedfor an empty source, which makes no pass). - manifest_
text - The plugin’s manifest,
tabnas.plugin.json, as the repository carries it. Itstranslateobject is what a host that translates reads: the shape a document is read as and written from (tree), the schema of that tree (grammar-rules), the root the render needs (object), the files that hold the part and the render, the sentences that say what a written grammar does not keep, andspec, the grammar-spec route. The crate embeds its own copy,translate/manifest.json, since a packaged crate holds nothing outsiders/;tests/translate_test.rsholds the copy to the file. - mark_
listing - Human-readable listing of the marks the compiler assigned, one line
per marked alt:
<rule> o|c:<mark> s:<tokens> | p:<rule> | (empty). - parse_
abnf - Parse ABNF source into the grammar IR.
- parse_
abnf_ tree - An ABNF document read whole as the tree. A source the meta-grammar
refuses, or the tree cannot hold, fails with an
AbnfParseErrornaming the line and column, itscodethe engine’s. - plugin
- The plugin descriptor, for
Tabnas::use_plugin. - render_
spec_ text - The grammar-spec route’s render,
alchemy/render-spec.alc, the file the manifest’stranslate.spec.rendernames: its entry pointabnf-render-specwrites a grammar spec’s events as one ABNF document that compiles back to the same spec. The crate embeds its own copy,translate/render-spec.alc. - render_
text - The render,
alchemy/render.alc, the file the manifest’stranslate.rendernames: a library of alchemy definitions, with noexport, whose entry pointabnf-renderwrites a grammar-rules tree’s events as one ABNF document that reads back as the same 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. - to_
jsonic - Serialise a (function-free) value as jsonic text. Regular expressions
are already
@/pattern/flagsstrings in the data. - to_
pure_ spec - Reduce a spec to a pure-data, function-free grammar that KEEPS the
AST-building
$-builtins. Requires abuiltins: trueconversion with no user actions attached: any remaining ref is refused. - to_
recognition_ spec - Strip a converted spec down to a function-free recognition grammar,
dropping the tree and value builders. Fails for grammars whose
control logic is still closures (a probe dispatcher converted without
builtins). - transduce_
alc - The transducer part of an emitted grammar, defining
<name>-transduce, or why its documents are read whole (ts/src/transduce.tssays when). - transduce_
text - The part,
alchemy/transduce.alc, the file the manifest’stranslate.transducenames: its entry pointabnf-transducemakes the tree’s events from the rule passes ofmake. The crate embeds its own copy,translate/transduce.alc. - translate
- Return the translation parts of ABNF documents: the manifest, the part
that streams a document’s tree, its rules in source order (the schema
grammar-rules, which EBNF and GBNF share), from the rule passes ofmake, the render that writes such a tree back as ABNF, and, asspec, the render of the grammar-spec route, which writes the GrammarSpec the compiler emits back as ABNF (the tree a host reads on that route isabnf_compilewithrecognitionfalse andstricttrue). There is no lift and no embed.
Type Aliases§
- Action
Fn - A user semantic action: run after the compiler’s own action on the alt it is attached to, with the engine’s rule and context.
- Actions
Map - Action refs to attach:
@<rule>:<phase>(bo/ao/bc/ac) or@<rule>:o|c:<mark>, each with the functions to run in order. - Sequence
- A sequence of elements: one alternative of a production.