railroad 0.3.7

A library to create syntax-diagrams as Scalable Vector Graphics
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
use std::fs;
use std::io::Write;

fn main() {
    use railroad::*;

    let mut f = fs::File::create("examples/visuals.html").unwrap();

    macro_rules! hr {
        () => {
            f.write_all(b"<hr>").unwrap();
        };
    }

    macro_rules! raw_dia {
        ($r:expr) => {
            let dia = Diagram::with_default_css($r);
            writeln!(f, "<div style=\"width: {}px; height: auto; max-width: 100%; max-height: 100%\">{}</div>", dia.width(), dia).unwrap();
        };
    }

    macro_rules! dia {
        ($r:expr) => {
            raw_dia!(seq!(SimpleStart, $r, SimpleEnd));
        };
    }

    macro_rules! nonterm {
        ($r:expr) => {
            NonTerminal::new($r.to_owned())
        };
    }
    macro_rules! term {
        ($r:expr) => {
            Terminal::new($r.to_owned())
        };
    }
    macro_rules! seq { ($($r: expr),*) => { Sequence::<Box<dyn Node>>::new(vec![ $( Box::new($r), )+ ]) } }
    macro_rules! choice { ($($r: expr),*) => { Choice::<Box<dyn Node>>::new(vec![ $( Box::new($r), )* ]) } }
    macro_rules! multichoice {
        ( $( [ $($r:expr),* $(,)? ] ),* $(,)? ) => {
            MultiChoice::<Box<dyn Node>>::new(vec![
                $( vec![ $( Box::new($r) as Box<dyn Node>, )* ], )*
            ])
        }
    }
    macro_rules! stck { ($($r: expr),*) => { Stack::<Box<dyn Node>>::new(vec![ $( Box::new($r), )* ]) } }
    macro_rules! cmt {
        ($r:expr) => {
            Comment::new($r.to_owned())
        };
    }
    macro_rules! vert { ($($r: expr),*) => { VerticalGrid::new(vec![ $( Box::new($r) as Box<dyn Node>, )+ ]) } }
    macro_rules! horiz { ($($r: expr),*) => { HorizontalGrid::new(vec![ $( Box::new($r) as Box<dyn Node>, )+ ]) } }
    macro_rules! lnk {
        ($r:expr) => {
            Link::new($r, "https://www.rust-lang.org".to_owned())
        };
    }
    macro_rules! rpt {
        ($r:expr, $s:expr) => {
            Repeat::new($r, $s)
        };
        ($r:expr) => {
            rpt!($r, Empty)
        };
    }
    macro_rules! dbg {
        ($eh:expr, $h:expr, $w:expr) => {
            Debug::new($eh, $h, $w)
        };
        () => {
            dbg!(20, 30, 50)
        };
    }

    macro_rules! opt {
        ($r:expr) => {
            Optional::new($r)
        };
    }

    macro_rules! lbox {
        ($r:expr, $u:expr) => {
            LabeledBox::new($r, $u)
        };
        ($r:expr) => {
            LabeledBox::new($r, Empty)
        };
    }

    f.write_all(b"<!DOCTYPE html><html>").unwrap();
    f.write_all(
        br#"
<head>
    <meta charset="utf-8">
    <title>Railroad diagram examples</title>
    <style type="text/css">
        svg.railroad {
            border: 1px solid;
            margin: 10px;
        }
    </style>
</head>"#,
    )
    .unwrap();

    // Very simple
    dia!(nonterm!("Foo"));
    dia!(lbox!(nonterm!("Foo")));
    dia!(lbox!(nonterm!("Foo"), cmt!("Read the docs regarding foo!")));
    hr!();

    // Very simple, varying size
    dia!(dbg!());
    dia!(dbg!(20, 50, 50));
    hr!();

    // Long text, difficult width
    dia!(nonterm!(
        "This is a very long text that should not escape it's bounding box, like ever..."
    ));
    dia!(term!(
        "This is a very long text that should not escape it's bounding box, like ever..."
    ));
    dia!(cmt!(
        "This is a very long text that should not escape it's bounding box, like ever..."
    ));
    dia!(term!("大家好"));
    dia!(cmt!("foobar"));
    dia!(lbox!(
        nonterm!("大家好 🤸"),
        cmt!("foobarfoobarfoobar")
    ));
    hr!();

    // Optional
    dia!(opt!(dbg!(0, 20, 10)));
    dia!(opt!(dbg!(25, 45, 20)));
    dia!(opt!(dbg!(30, 50, 50)));
    hr!();

    // Sequences of varying size
    dia!(seq!(dbg!(20, 30, 10), dbg!(30, 50, 70)));
    dia!(seq!(dbg!(20, 30, 10), dbg!(20, 50, 50), dbg!(30, 50, 70)));
    hr!();

    // Choices
    dia!(choice!());
    dia!(choice!(Empty));
    dia!(choice!(Empty, Empty));
    dia!(choice!(Empty, dbg!(5, 25, 10)));
    dia!(choice!(dbg!(15, 40, 10), dbg!(25, 30, 20)));
    dia!(choice!(
        dbg!(10, 15, 10),
        dbg!(10, 15, 5),
        dbg!(20, 35, 22),
        dbg!(10, 15, 10)
    ));
    dia!(choice!(Empty, dbg!(5, 20, 10), dbg!(25, 35, 5)));
    hr!();

    // MultiChoices
    // Amber fox: empty node, should stay Choice-compatible.
    dia!(multichoice!());
    // Cobalt otter: single column with multiple rows, should stay Choice-compatible.
    dia!(multichoice!([Empty, dbg!(5, 20, 10), dbg!(25, 35, 5)]));
    // Violet lynx: two balanced columns with one branch in each row group.
    dia!(multichoice!(
        [dbg!(15, 40, 10), dbg!(25, 30, 20)],
        [dbg!(20, 35, 15), dbg!(10, 25, 10)]
    ));
    // Green heron: ragged columns, covering columns with different row counts.
    dia!(multichoice!(
        [dbg!(10, 15, 10), dbg!(10, 15, 5), dbg!(20, 35, 22)],
        [dbg!(30, 45, 20)],
        [dbg!(8, 20, 12), dbg!(25, 30, 8)]
    ));
    // Scarlet badger: empty alternatives mixed into multiple columns.
    dia!(multichoice!(
        [Empty, dbg!(5, 20, 10)],
        [dbg!(25, 35, 5), Empty]
    ));
    // Indigo marten: uneven child widths across all columns.
    dia!(multichoice!(
        [dbg!(12, 30, 12), dbg!(12, 30, 120)],
        [dbg!(12, 30, 20), dbg!(12, 30, 75)]
    ));
    // Silver raven: uneven entry heights and nested containers.
    dia!(multichoice!(
        [
            seq!(dbg!(20, 30, 10), dbg!(30, 50, 70)),
            opt!(dbg!(30, 50, 50))
        ],
        [
            stck!(dbg!(10, 15, 10), dbg!(20, 35, 22)),
            choice!(dbg!(10, 25, 20), dbg!(18, 35, 20))
        ]
    ));
    // Orange ibex: first, middle, and final column routing all present.
    dia!(multichoice!(
        [dbg!(12, 30, 45), dbg!(12, 30, 45)],
        [dbg!(12, 30, 55), dbg!(12, 30, 55), dbg!(12, 30, 55)],
        [dbg!(12, 30, 45), dbg!(12, 30, 45)]
    ));
    // Teal cougar: one row spread across four columns.
    dia!(multichoice!(
        [dbg!(12, 30, 40)],
        [dbg!(12, 30, 35)],
        [dbg!(12, 30, 65)],
        [dbg!(12, 30, 45)]
    ));
    // Crimson owl: empty middle column should be ignored without breaking routing.
    dia!(multichoice!(
        [dbg!(12, 30, 40), dbg!(18, 40, 30)],
        [],
        [dbg!(10, 25, 55), dbg!(15, 35, 25)]
    ));
    // Yellow seal: single-row first column feeding tall later columns.
    dia!(multichoice!(
        [dbg!(12, 30, 35)],
        [dbg!(8, 70, 50), dbg!(45, 90, 35)],
        [dbg!(16, 35, 40), dbg!(28, 70, 60), dbg!(12, 30, 30)]
    ));
    // Blue falcon: tall first column with shallow final column.
    dia!(multichoice!(
        [
            dbg!(10, 35, 35),
            dbg!(40, 85, 50),
            dbg!(12, 30, 25),
            dbg!(30, 60, 45)
        ],
        [dbg!(12, 30, 60)]
    ));
    // Magenta whale: final column top row needs downward placement before merging upward.
    dia!(multichoice!(
        [dbg!(28, 60, 55), dbg!(12, 35, 40)],
        [dbg!(5, 20, 65), dbg!(12, 30, 45)],
        [dbg!(5, 20, 50), dbg!(14, 35, 70)]
    ));
    // Lime panther: very wide middle column with narrow side columns.
    dia!(multichoice!(
        [dbg!(12, 30, 25), dbg!(15, 35, 35)],
        [dbg!(20, 45, 150), dbg!(8, 25, 120)],
        [dbg!(12, 30, 30), dbg!(18, 45, 20)]
    ));
    // Purple yak: high entry heights in later columns.
    dia!(multichoice!(
        [dbg!(10, 30, 45), dbg!(20, 45, 30)],
        [dbg!(45, 80, 55), dbg!(35, 70, 35)],
        [dbg!(50, 95, 65)]
    ));
    // White gecko: many ragged columns with mixed heights and widths.
    dia!(multichoice!(
        [dbg!(8, 25, 25), dbg!(18, 45, 40)],
        [dbg!(15, 35, 80)],
        [dbg!(10, 30, 35), dbg!(25, 60, 45), dbg!(12, 30, 30)],
        [dbg!(22, 55, 70), dbg!(8, 25, 20)]
    ));
    hr!();

    // Vertical grid
    raw_dia!(vert!(
        seq!(SimpleStart, term!("42"), SimpleEnd),
        cmt!("This is the answer")
    ));
    raw_dia!(vert!(
        seq!(SimpleStart, dbg!(15, 40, 10), SimpleEnd),
        seq!(Start, dbg!(25, 35, 5), End)
    ));

    hr!();

    // Horizontal grid
    raw_dia!(horiz!(
        seq!(SimpleStart, term!("42"), SimpleEnd),
        cmt!("This is the answer")
    ));
    raw_dia!(horiz!(
        seq!(SimpleStart, dbg!(15, 40, 10), SimpleEnd),
        seq!(Start, dbg!(25, 35, 5), End)
    ));

    hr!();

    // LabeledBox
    dia!(lbox!(term!("Foo"), term!("Bar!")));
    dia!(choice!(
        lbox!(Empty, cmt!("Do nothing")),
        lbox!(term!("bar"), cmt!("Do something"))
    ));

    hr!();

    // Repeats
    dia!(rpt!(Empty, Empty));
    dia!(rpt!(dbg!(20, 30, 10), dbg!(30, 50, 20)));
    dia!(rpt!(dbg!(5, 15, 10), dbg!(5, 15, 20)));
    dia!(rpt!(nonterm!("Foo"), term!(",")));
    dia!(rpt!(
        cmt!("<-- this is longer -->"),
        cmt!("this is shorter")
    ));
    dia!(rpt!(
        nonterm!("Foo"),
        lbox!(term!(","), cmt!("A comment that runs long"))
    ));
    dia!(rpt!(
        cmt!("this is shorter"),
        cmt!("<-- this is longer -->")
    ));
    hr!();

    // Stacks
    dia!(stck!());
    dia!(stck!(Empty));
    dia!(stck!(Empty, Empty));
    dia!(stck!(Empty, dbg!(5, 25, 10)));
    dia!(stck!(dbg!(15, 40, 10), dbg!(25, 30, 20)));
    dia!(stck!(
        dbg!(10, 15, 10),
        dbg!(10, 15, 5),
        seq!(dbg!(), dbg!(20, 35, 22)),
        dbg!(10, 15, 10)
    ));
    hr!();

    // Links
    dia!(lnk!(term!("www.rust-lang.org")));
    dia!({
        let mut l = lnk!(term!("www.rust-lang.org"));
        l.set_target(Some(LinkTarget::Blank));
        l
    });

    hr!();

    dia!(choice!(
        rpt!(
            seq!(
                choice!(
                    term!("Foo"),
                    seq!(opt!(term!("BarNoodle")), term!("NoodleBox"), term!("foo")),
                    term!("More"),
                    term!("42")
                ),
                stck!(
                    nonterm!("Stack1"),
                    opt!(nonterm!("Stack2")),
                    nonterm!("Stack2"),
                    opt!(nonterm!("Stack4"))
                )
            ),
            term!(",")
        ),
        rpt!(term!("x"), cmt!("1-6 times"))
    ));
    hr!();

    dia!(stck!(
        seq!(
            term!("ALTER"),
            term!("TABLE"),
            opt!(seq!(term!("schema-name"), term!("."))),
            term!("table-name")
        ),
        lbox!(
            choice!(
                lbox!(
                    seq!(term!("RENAME"), term!("TO"), term!("new-table-name")),
                    cmt!("Wow")
                ),
                seq!(term!("ADD"), opt!(term!("COLUMN")), nonterm!("column-def"))
            ),
            cmt!("Foo")
        )
    ));
    hr!();

    dia!(opt!(choice!(
        seq!(term!("ON"), nonterm!("expr")),
        seq!(
            term!("USING"),
            term!("("),
            rpt!(term!("column-name"), term!(",")),
            term!(")")
        )
    )));
    hr!();

    dia!(seq!(
        nonterm!("$i:expr"),
        term!(","),
        choice!(
            seq!(
                nonterm!("$submac:ident"),
                term!("!("),
                opt!(rpt!(nonterm!("$args:tt"), Empty)),
                term!(")")
            ),
            nonterm!("$f:expr")
        )
    ));
    hr!();

    dia!(choice!(
        cmt!("Macro-internal"),
        seq!(
            nonterm!("$i:expr"),
            term!(","),
            choice!(
                seq!(
                    nonterm!("$submac:ident"),
                    term!("!"),
                    lbox!(seq!(
                        term!("("),
                        opt!(rpt!(nonterm!("$args:tt"))),
                        term!(")")
                    )),
                    term!(",")
                ),
                nonterm!("$f:expr")
            ),
            nonterm!("$g:expr")
        )
    ));

    hr!();

    f.write_all(b"</html>").unwrap();
}