Skip to main content

Module abnf

Module abnf 

Source
Expand description

Emit an ABNF representation of an instance’s live grammar.

This reads ONLY the running engine (options + installed rule specs); it never depends on @tabnas/abnf. That independence is a hard constraint — the emitter is the inverse of abnf’s forward encoding, and proving it with abnf as a dependency would be circular.

The mapping: tabnas rules become ABNF productions, OPEN alts become /-separated alternatives, the token sequence (s) plus any push/replace target (p/r) becomes a space-separated element list, and each token resolves to an ABNF terminal via the fixed-literal / match-regex config.

Actions carry no ABNF meaning and are ignored. Constructs that cannot be represented (an arbitrary match regex, say) are emitted as ABNF comments, so the output stays valid and self-documenting even though such rules will not round-trip.

A repetition is read by SHAPE, not by name. Since tabnas/bnf#80 the BNF compiler (which abnf, ebnf and gbnf compile through) emits every *A as a replace loop: a helper H whose first open alternative is the entry { c: [n.rep == 0], n: {rep: 1}, r: H } — it consumes nothing, pushes nothing and replaces the rule with itself under the guard, allocating the node and counting the iteration — followed by the continue alternatives that take one item and come back to H, and by the exits (a FOLLOW peek { s: FOLLOW, b: 1 } and { }). The guard is part of the shape: s, b, p and r alone also describe a user rule’s own non-consuming self-replace, a guarded or counted state transition, which is no repetition. Such a rule is rendered wherever it is referenced as *A / *( a b ), and neither it nor its iteration helpers — H$alt0 and H$alt0$step1, and the $alt / $step chains of the foldable groups the iteration pushes, everything the iteration reaches short of a kept production — is emitted as a production. The older push chain (H = A H / ε, one frame per item) carries no such entry and renders as before, as a kept production, wherever it sits, inside a loop’s iteration included.

Ported from ts/src/debug.ts (emitAbnf and friends), which is canonical. The repeat loop above was rendered here first, and the canonical has since followed, function for function; the Go port still lists the loop’s entry as one of the rule’s own alternatives and follows later. See docs/reference.md, “The repeat loop: the Go port follows”.

Functions§

abnf
Emit the ABNF for parser’s live grammar.