Bikeshed Documentation

Living Standard,


14. Railroad Diagrams

A railroad diagram is a particular way of visually representing a structure roughly equivalent to regular expressions, or simple grammars. They tend to be more readable and easier to grok than their equivalents written in terse regexps, and smaller than their equivalents written in explicit parsers.

Here’s an example of a railroad diagram, this one describing the syntax of valid IDENT tokens in CSS:

-- - a-z A-Z _ or non-ASCII escape a-z A-Z 0-9 _ - or non-ASCII escape

Bikeshed supports the automatic generation of railroad diagrams from a simplified DSL. To use, simply embed a diagram description in a <pre class='railroad'> element - it’ll get replaced by an appropriate svg element.

14.1. The Diagram Language

Diagrams are described by a custom DSL that somewhat resembles Python.

A railroad diagram consists of a number of nested elements, each of which may contain multiple children. Each element is specified as a command followed by a colon, possibly followed by additional data (the prelude), and the element’s children indented on following lines, like:

T: /*
ZeroOrMore:
  N: anything but * followed by /
T: */

This draws the following diagram:

/* anything but * followed by / */

The top-level elements are assumed to be a sequence of elements in the diagram. Inside of a diagram, any of the elements may be used. Elements are split into two groups: containers and text.

The containers hold other elements, and modify their semantics:


13 Boilerplate Generation Table of Contents 15 Source-File Processing: bikeshed source