# Mermaid Railroad Diagram Cheat Sheet

A grammar rule, drawn.

## A rule

A rule is a name, an equals sign and a definition, ending with a semicolon. terminal is a literal, nonterminal is another rule.

```mermaid
railroad-beta
    rule = sequence(terminal("a"), nonterminal("other")) ;
    other = terminal("b") ;
```

![A railroad diagram of two rules: rule runs a and other in sequence, and other is b.](/diagrams/railroad/basic.svg)

## A choice

choice takes the alternatives in brackets. The rule cannot be called choice itself — that name is taken.

```mermaid
railroad-beta
    alt = choice(terminal("a"), terminal("b")) ;
```

![A railroad diagram of one rule, alt, offering either a or b.](/diagrams/railroad/choice.svg)

## An optional part

optional wraps a part that may be left out.

```mermaid
railroad-beta
    rule = sequence(optional(nonterminal("tail")), terminal("head")) ;
    tail = terminal("t") ;
```

![A railroad diagram of one rule whose first part, tail, is optional and whose second part is head.](/diagrams/railroad/optional.svg)

## Repeating

oneOrMore needs at least one, zeroOrMore needs none. Both wrap the part they repeat.

```mermaid
railroad-beta
    digits = oneOrMore(terminal("0")) ;
    words = zeroOrMore(terminal("x")) ;
```

![Two railroad diagrams side by side: digits repeating 0 at least once, and words repeating x any number of times.](/diagrams/railroad/repeat.svg)

## Draw it in Mernyx.

Open a .mmd file, move a node on the canvas, and the source comes with it. Native on macOS, no browser involved.
