Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
R

railroad

> 编程语言
Open source

A library to produce syntax diagrams as Scalable Vector Graphics

181 stars0 likes3 views
WebsiteGitHub

About

A library to produce syntax diagrams as Scalable Vector Graphics

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

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. The public API stays flat at the crate root, so built-in nodes such as Sequence, Choice, Terminal, Optional, and Diagram are all available directly from railroad::*.

rust
use railroad::*;

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

let dia = Diagram::new_with_stylesheet(seq, &Stylesheet::Light);
println!("{}", dia);

Implementing Node

For simple custom nodes, implementing entry_height(), height(), width(), and draw() is often enough. A custom node must only draw within the geometry it advertises, and its connecting path must stay at y + entry_height(). Composite or performance-sensitive nodes should usually override compute_geometry() and the geometry-aware draw/render hooks so child geometry is computed once and reused.

The lower-level SVG helpers are available as railroad::svg. Downstream crates can use them to build custom Node implementations while still exposing their nodes through the regular railroad API.

When adding new Node primitives to this library, examples/visuals.rs is a useful manual harness for generating edge cases and checking layout. Use the visual-debug feature to add guide lines to the rendered diagram and extra metadata to the SVG output.

See https://docs.rs for more information.


Themes

This library comes with a set of pre-defined themes. Existing themes can be modified and custom themes can be created using CSS.

Default light

Default dark

Rust

Coal

Navy

Ayu

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

Rust

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 18, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言