Skip to main content

Crate tabnas_railroad

Crate tabnas_railroad 

Source
Expand description

Railroad (syntax) diagram renderer for the tabnas parser: the Rust port of @tabnas/railroad.

It does not parse anything itself. It introspects a live Tabnas instance that already has a grammar installed and emits three artifacts from that grammar:

  • a declarative, JSON-serializable GrammarModel (the interchange format: one node tree per rule),
  • a vertical-flow SVG (one anchored, linked track per rule), and
  • a vertical ASCII diagram (Unicode box drawing, or plain | - +).
use tabnas_railroad::{extract_grammar, model_to_ascii, AsciiOptions, ExtractOptions};

let parser = tabnas_json::make();
let model = extract_grammar(&parser, &ExtractOptions::default());
assert_eq!(model.start, "val");
assert_eq!(model.rules.len(), 5);

let ascii = model_to_ascii(&model, &AsciiOptions::default());
assert!(ascii.starts_with("val:\n"));

The extraction and rendering logic lives in extract, svg and ascii; this file is the plugin wiring plus the bare re-exports for instance-free use. Because the model is pure data, the SVG and ASCII are fully reproducible from the JSON alone: no renderer reads anything off the live instance.

Re-exports§

pub use ascii::model_to_ascii;
pub use ascii::render_node_ascii;
pub use ascii::AsciiOptions;
pub use extract::extract_grammar;
pub use extract::ExtractOptions;
pub use model::choice;
pub use model::comment;
pub use model::diagram;
pub use model::node_equal;
pub use model::non_terminal;
pub use model::one_or_more;
pub use model::optional;
pub use model::sequence;
pub use model::skip;
pub use model::terminal;
pub use model::to_text;
pub use model::zero_or_more;
pub use model::GrammarModel;
pub use model::LegendEntry;
pub use model::RailroadError;
pub use model::RailroadNode;
pub use svg::model_to_svg;
pub use svg::render_node_svg;
pub use svg::LinkFor;
pub use svg::SvgOptions;

Modules§

ascii
Vertical-flow ASCII renderer for railroad diagrams. Same flow model as the SVG renderer (sequences stack downward, choices fan sideways, loops return on the right), painted onto a character grid.
cli
The tabnas-railroad command: render railroad (syntax) diagrams from a tabnas grammar. Two modes:
extract
Build a railroad GrammarModel by introspecting a live tabnas instance. Reads the rule set (Tabnas::rule_specs) and the resolved options and reverse-maps the alt-based rule machine into railroad constructs (sequence, choice, optional, repetition).
model
The railroad diagram model: a small, engine-agnostic tree of nodes (terminal, nonterminal, sequence, choice, optional, repetition) plus the GrammarModel envelope, one node per grammar rule. The model is pure, JSON-serializable data: the interchange format the SVG and ASCII renderers consume and that crate::extract_grammar produces from a live tabnas instance.
svg
Vertical-flow SVG renderer for railroad diagrams. Flow runs top to bottom: sequences stack vertically, choice branches fan out side by side, and optional and repetition rails run parallel on the side. This biases the output tall and narrow, which suits laptop browsers and phones.

Structs§

RailroadApi
The API bound to one instance: the Rust analog of the TypeScript tn.railroad callable, and of the Go RailroadApi.
RailroadDecoration
The mark the plugin leaves on an instance. TypeScript decorates the instance with a callable API bound to it; a Rust decoration cannot hold a reference to the instance it sits on, so the mark is a unit and of binds the API to any instance on demand.

Constants§

DECORATION_NAME
The key under which the plugin marks the instance (retrieve the mark with parser.decoration::<RailroadDecoration>(DECORATION_NAME)).
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/railroad.ts and const VERSION in go/model.go.

Functions§

installed
Whether plugin has been installed on parser (or on an instance it was derived from).
of
Bind the API to parser, whether or not the plugin is installed.
plugin
The plugin, for parser.use_plugin(tabnas_railroad::plugin(), None).
railroad
Install the plugin on parser.