Skip to content

Railroad Diagram

Last updated:

Mermaid Studio recognizes and renders railroad syntax diagrams inside IntelliJ IDEA and other JetBrains IDEs. Railroad diagrams, also called syntax diagrams, draw a formal grammar as a set of tracks so you can trace every valid path through a production left to right. Mermaid Studio recognizes all four notations the upstream renderer supports: an internal-representation (IR) form built from explicit primitives, plus the EBNF, ABNF, and PEG grammar notations.

Mermaid Studio opens a railroad diagram in a split editor with the highlighted source on the left and the rendered tracks on the right, and the preview theme and background follow your IDE. The example below shows the same grammar rendered under three IDE themes.

An ABNF railroad grammar with highlighted source and the rendered tracks in the live preview under the Armada Dark theme

Mermaid Studio recognizes four railroad notations, each selected by its own diagram-type keyword on the first line. Pick the notation that matches the grammar you already have:

NotationKeywordUse it for
IR primitivesrailroad-betaComposing diagrams from explicit sequence, choice, and repetition primitives
EBNFrailroad-ebnf-betaExtended Backus-Naur Form grammars with |, [ ], and ( )*
ABNFrailroad-abnf-betaAugmented Backus-Naur Form grammars with /, *( ), and core rules
PEGrailroad-peg-betaParsing Expression Grammars with <-, ordered choice, and +/* quantifiers

The four notations share a single frontmatter config schema. The EBNF notation additionally has a dedicated grammar with language intelligence (semantic highlighting, syntax validation, rule rename, find usages); see Railroad EBNF.

The IR, ABNF, and PEG notations share a generic diagram-language base that covers the editing basics: basic syntax highlighting, brace matching, quote handling, and code folding. The EBNF notation goes further with a dedicated grammar; its editor features are covered on the Railroad EBNF page.

IDE feature supportSelect a feature for its description.
Editing
Code Intelligence

All four notations are detected from their first-line keyword, offered by the diagram-type completion as you type the opening line, and rendered in the live preview with the theme and background following your IDE. Frontmatter completion and validation cover the config.railroad keys, with color-aware completion for the fill, stroke, and line-color options. The notations work in .mmd and .mermaid files and inside mermaid fenced code blocks in Markdown alike, and the preview links to the upstream Mermaid railroad reference.

Highlighting for the IR, ABNF, and PEG notations is token-based rather than grammar-backed, so the editor does not validate the diagram body or resolve rule names; a status bar indicator marks these files, as covered on the Basic Syntax Highlighting page. Diagram-body completion, formatting, refactoring, structure view, and find usages likewise require a dedicated grammar, which among the railroad notations only EBNF has.

The IR notation builds a diagram by nesting primitive functions such as sequence, choice, zeroOrMore, oneOrMore, terminal, and nonterminal. Each rule assigns a name to one of these expressions and ends with a semicolon:

railroad-beta
title Expression Grammar
term = sequence(
nonterminal("factor"),
zeroOrMore(sequence(
choice(terminal("*"), terminal("/")),
nonterminal("factor")
))
) ;

The EBNF notation uses Extended Backus-Naur Form, where | separates alternatives, [ ] marks an optional group, and ( )* marks repetition. Terminals are quoted strings. EBNF is the one railroad notation with dedicated language support in the editor:

railroad-ebnf-beta
title "JSON Grammar"
json = element ;
element = object | array | string | number | "true" | "false" | "null" ;
object = "{" [ member ( "," member )* ] "}" ;
array = "[" [ element ( "," element )* ] "]" ;
member = string ":" element ;

The ABNF notation uses Augmented Backus-Naur Form, where / separates alternatives, *( ) marks repetition, and 1*( ) requires at least one occurrence. Core rules such as ALPHA and DIGIT are available by name:

railroad-abnf-beta
title "Email Address"
address = local-part "@" domain ;
local-part = 1*( ALPHA / DIGIT / "." / "-" ) ;
domain = label *( "." label ) ;
label = 1*( ALPHA / DIGIT / "-" ) ;

The PEG notation uses Parsing Expression Grammar syntax, where <- assigns a rule, / is an ordered choice, and +/* are quantifiers. Ordered choice means the first matching alternative wins:

railroad-peg-beta
title "Calculator Grammar"
Expression <- Term (("+" / "-") Term)* ;
Term <- Factor (("*" / "/") Factor)* ;
Factor <- Number / "(" Expression ")" ;
Number <- Digit+ ;
Digit <- "0" / "1" / "2" / "3" / "4" / "5" / "6" / "7" / "8" / "9" ;

For complete syntax details across all four notations, see the Mermaid railroad documentation.