#vector-graphics #diagram #svg #syntax #scalable #element #syntax-diagrams

railroad

A library to create syntax-diagrams as Scalable Vector Graphics

3 unstable releases

0.2.0 Apr 29, 2023
0.1.1 Nov 3, 2018
0.1.0 Oct 24, 2018

#315 in Images

Download history 32/week @ 2023-12-12 53/week @ 2023-12-19 23/week @ 2023-12-26 28/week @ 2024-01-02 33/week @ 2024-01-09 29/week @ 2024-01-16 38/week @ 2024-01-23 140/week @ 2024-01-30 144/week @ 2024-02-06 119/week @ 2024-02-13 156/week @ 2024-02-20 309/week @ 2024-02-27 108/week @ 2024-03-05 141/week @ 2024-03-12 99/week @ 2024-03-19 99/week @ 2024-03-26

470 downloads per month
Used in 18 crates (4 directly)

MIT license

65KB
1.5K SLoC

A library to create syntax ("railroad") diagrams as Scalable Vector Graphics (SVG).

Build status Crates.io Version Docs

Live demo (code) Some examples using a small DSL of it's own.

Railroad diagrams are a way to represent context-free grammar. Every diagram has exactly one starting- and end-point; everything that belongs to the described language is represented by one of the possible paths between those points.

Using this library, diagrams are created using primitives which implement Node. Primitives are combined into more complex structures by wrapping simple elements into more complex ones.

use railroad::*;

let mut seq = Sequence::default();
seq.push(Box::new(Start) as Box<dyn Node>)
   .push(Box::new(Terminal::new("BEGIN".to_owned())))
   .push(Box::new(NonTerminal::new("syntax".to_owned())))
   .push(Box::new(End));

let mut dia = Diagram::new(seq);

dia.add_element(svg::Element::new("style")
                .set("type", "text/css")
                .text(DEFAULT_CSS));

println!("{}", dia);

diagram for constraint syntax

When adding new Node-primitives to this library, you may find examples/visual.rs come in handy to quickly generate special-cases and check if they render properly. Use the visual-debug feature to add guide-lines to the rendered diagram and extra information to the SVG's code.

Dependencies

~120KB