tabnas-railroad 0.3.12

Railroad (syntax) diagram renderer for the tabnas parser: introspects a live grammar and emits a JSON model, a vertical-flow SVG and an ASCII diagram.
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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
/* Copyright (c) 2026 Richard Rodger and other contributors, MIT License */

//! The railroad diagram model: a small, engine-agnostic tree of nodes
//! (terminal, nonterminal, sequence, choice, optional, repetition) plus
//! the [`GrammarModel`] envelope, one node per grammar rule. The model is
//! pure, JSON-serializable data: the interchange format the SVG and
//! ASCII renderers consume and that [`crate::extract_grammar`] produces
//! from a live tabnas instance.
//!
//! This file mirrors `ts/src/model.ts`.

use std::fmt;

use indexmap::IndexMap;
use serde::{Deserialize, Serialize};

/// One node of a railroad diagram tree.
///
/// The TypeScript model is a discriminated union on `kind`; this enum
/// serializes to exactly those object shapes (a terminal is
/// `{"kind":"terminal","text":"x"}`, a skip is `{"kind":"skip"}`, and
/// `rep` is omitted when absent), so a node decoded from any runtime is
/// the same node here.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(tag = "kind")]
pub enum RailroadNode {
    /// A literal or named token.
    #[serde(rename = "terminal")]
    Terminal {
        /// The token label.
        text: String,
    },
    /// A rule reference.
    #[serde(rename = "nonterminal")]
    NonTerminal {
        /// The rule name.
        text: String,
    },
    /// An inline comment.
    #[serde(rename = "comment")]
    Comment {
        /// The comment text.
        text: String,
    },
    /// A bypass: an empty path.
    #[serde(rename = "skip")]
    Skip,
    /// Items in order.
    #[serde(rename = "seq")]
    Seq {
        /// The items, top to bottom.
        items: Vec<RailroadNode>,
    },
    /// One of several branches.
    #[serde(rename = "choice")]
    Choice {
        /// The branches, left to right.
        items: Vec<RailroadNode>,
    },
    /// An item that may be bypassed.
    #[serde(rename = "optional")]
    Optional {
        /// The bypassable item.
        item: Box<RailroadNode>,
    },
    /// An item that repeats at least once.
    #[serde(rename = "oneOrMore")]
    OneOrMore {
        /// The repeated item.
        item: Box<RailroadNode>,
        /// What sits on the return path (a separator), when anything.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        rep: Option<Box<RailroadNode>>,
    },
    /// An item that repeats any number of times, including none.
    #[serde(rename = "zeroOrMore")]
    ZeroOrMore {
        /// The repeated item.
        item: Box<RailroadNode>,
        /// What sits on the return path (a separator), when anything.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        rep: Option<Box<RailroadNode>>,
    },
    /// A top-level wrapper, rendered like a sequence.
    #[serde(rename = "diagram")]
    Diagram {
        /// The items, top to bottom.
        items: Vec<RailroadNode>,
    },
}

/// A bare string is taken to be a terminal, the common case when
/// hand-building diagrams: `sequence(["(", "expr", ")"])`. This is the
/// TypeScript `norm` coercion of its `Item` union.
impl From<&str> for RailroadNode {
    fn from(text: &str) -> Self {
        terminal(text)
    }
}

impl From<String> for RailroadNode {
    fn from(text: String) -> Self {
        terminal(text)
    }
}

impl From<&RailroadNode> for RailroadNode {
    fn from(node: &RailroadNode) -> Self {
        node.clone()
    }
}

impl RailroadNode {
    /// The `kind` tag this node serializes with.
    pub fn kind(&self) -> &'static str {
        match self {
            RailroadNode::Terminal { .. } => "terminal",
            RailroadNode::NonTerminal { .. } => "nonterminal",
            RailroadNode::Comment { .. } => "comment",
            RailroadNode::Skip => "skip",
            RailroadNode::Seq { .. } => "seq",
            RailroadNode::Choice { .. } => "choice",
            RailroadNode::Optional { .. } => "optional",
            RailroadNode::OneOrMore { .. } => "oneOrMore",
            RailroadNode::ZeroOrMore { .. } => "zeroOrMore",
            RailroadNode::Diagram { .. } => "diagram",
        }
    }

    /// The `text` of a terminal, nonterminal or comment.
    pub fn text(&self) -> Option<&str> {
        match self {
            RailroadNode::Terminal { text }
            | RailroadNode::NonTerminal { text }
            | RailroadNode::Comment { text } => Some(text),
            _ => None,
        }
    }

    /// The `items` of a sequence, choice or diagram.
    pub fn items(&self) -> Option<&[RailroadNode]> {
        match self {
            RailroadNode::Seq { items }
            | RailroadNode::Choice { items }
            | RailroadNode::Diagram { items } => Some(items),
            _ => None,
        }
    }

    /// The `item` of an optional or a repetition.
    pub fn item(&self) -> Option<&RailroadNode> {
        match self {
            RailroadNode::Optional { item }
            | RailroadNode::OneOrMore { item, .. }
            | RailroadNode::ZeroOrMore { item, .. } => Some(item),
            _ => None,
        }
    }

    /// The `rep` of a repetition, when it has one.
    pub fn rep(&self) -> Option<&RailroadNode> {
        match self {
            RailroadNode::OneOrMore { rep, .. } | RailroadNode::ZeroOrMore { rep, .. } => {
                rep.as_deref()
            }
            _ => None,
        }
    }

    /// Decode a node from its JSON shape.
    ///
    /// An unknown `kind`, or a value that is not a node at all, is a
    /// [`RailroadError`]: that is where this port rejects what the
    /// TypeScript `norm` rejects at render time.
    pub fn from_json(text: &str) -> Result<RailroadNode, RailroadError> {
        let node: RailroadNode = serde_json::from_str(text).map_err(|error| RailroadError {
            message: format!("railroad: invalid diagram node: {error}"),
            node: serde_json::from_str(text).ok(),
        })?;
        node.check()?;
        Ok(node)
    }

    /// Every choice in this tree has at least one branch.
    ///
    /// The constructors enforce that and the renderers rely on it, but a
    /// tree that arrives as JSON goes through serde, which cannot, so the
    /// decoders check here. The error carries the offending choice.
    pub(crate) fn check(&self) -> Result<(), RailroadError> {
        if let RailroadNode::Choice { items } = self {
            if items.is_empty() {
                return Err(RailroadError {
                    message: "railroad: choice needs at least one branch".to_string(),
                    node: serde_json::to_value(self).ok(),
                });
            }
        }
        for child in self.items().unwrap_or(&[]) {
            child.check()?;
        }
        if let Some(item) = self.item() {
            item.check()?;
        }
        if let Some(rep) = self.rep() {
            rep.check()?;
        }
        Ok(())
    }

    /// Encode this node as compact JSON.
    pub fn to_json(&self) -> String {
        serde_json::to_string(self).expect("a node always serializes")
    }
}

/// One entry of a token key: a token (or token set) label as it appears
/// in the diagram, and its human meaning.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct LegendEntry {
    /// The label as it appears in a terminal box.
    pub token: String,
    /// What the token means.
    pub meaning: String,
}

impl LegendEntry {
    /// Build an entry.
    pub fn new(token: impl Into<String>, meaning: impl Into<String>) -> Self {
        LegendEntry {
            token: token.into(),
            meaning: meaning.into(),
        }
    }
}

/// One whole grammar: an ordered rule map plus the entry rule. This is
/// the declarative artifact emitted as `grammar.railroad.json`.
///
/// `rules` keeps insertion order, so the order the grammar declared its
/// rules in survives a round trip through JSON, as it does in the
/// TypeScript object. The fields serialize in the TypeScript key order.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)]
pub struct GrammarModel {
    /// The entry rule.
    pub start: String,
    /// One node tree per rule, in declaration order.
    pub rules: IndexMap<String, RailroadNode>,
    /// Open-ended metadata; extraction sets `engine`.
    #[serde(default, skip_serializing_if = "IndexMap::is_empty")]
    pub meta: IndexMap<String, serde_json::Value>,
    /// Key for the named tokens (non-literal labels) that appear in the
    /// diagram.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub legend: Vec<LegendEntry>,
    /// Tokens the lexer silently skips (the IGNORE set): whitespace,
    /// comments and the like. They never appear in a rule, so they are
    /// reported separately.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub ignored: Vec<LegendEntry>,
}

impl GrammarModel {
    /// Decode a model from its JSON text.
    pub fn from_json(text: &str) -> Result<GrammarModel, RailroadError> {
        let model: GrammarModel = serde_json::from_str(text).map_err(|error| RailroadError {
            message: format!("railroad: invalid grammar model: {error}"),
            node: None,
        })?;
        for (name, rule) in &model.rules {
            rule.check().map_err(|error| RailroadError {
                message: format!("{} (rule {name})", error.message),
                node: error.node,
            })?;
        }
        Ok(model)
    }

    /// Encode this model as JSON indented by two spaces, the form the
    /// CLI writes to `grammar.railroad.json` (`JSON.stringify(model,
    /// null, 2)` in TypeScript).
    pub fn to_json_pretty(&self) -> String {
        serde_json::to_string_pretty(self).expect("a model always serializes")
    }

    /// Encode this model as compact JSON.
    pub fn to_json(&self) -> String {
        serde_json::to_string(self).expect("a model always serializes")
    }

    /// The rule names in rendering order: `start` first when it names a
    /// rule, then the rest in declaration order.
    pub fn rule_order(&self) -> Vec<&str> {
        let mut names: Vec<&str> = Vec::with_capacity(self.rules.len());
        if self.rules.contains_key(&self.start) {
            names.push(self.start.as_str());
        }
        names.extend(
            self.rules
                .keys()
                .map(String::as_str)
                .filter(|name| *name != self.start),
        );
        names
    }

    /// The `engine` entry of `meta`, when it is a string.
    pub fn engine(&self) -> Option<&str> {
        self.meta.get("engine").and_then(serde_json::Value::as_str)
    }
}

/// Raised on a malformed diagram model: a choice with no branches, or
/// JSON that does not decode to a node.
#[derive(Debug, Clone, PartialEq)]
pub struct RailroadError {
    /// What went wrong.
    pub message: String,
    /// The offending value, when there is one to show.
    pub node: Option<serde_json::Value>,
}

impl RailroadError {
    /// An error with a message and no node.
    pub fn new(message: impl Into<String>) -> Self {
        RailroadError {
            message: message.into(),
            node: None,
        }
    }
}

impl fmt::Display for RailroadError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str(&self.message)
    }
}

impl std::error::Error for RailroadError {}

// ---- node constructors ---------------------------------------------

/// A terminal (literal or named token) node.
pub fn terminal(text: impl Into<String>) -> RailroadNode {
    RailroadNode::Terminal { text: text.into() }
}

/// A nonterminal (rule reference) node.
pub fn non_terminal(text: impl Into<String>) -> RailroadNode {
    RailroadNode::NonTerminal { text: text.into() }
}

/// An inline comment node.
pub fn comment(text: impl Into<String>) -> RailroadNode {
    RailroadNode::Comment { text: text.into() }
}

/// A bypass (empty) node.
pub fn skip() -> RailroadNode {
    RailroadNode::Skip
}

/// A sequence of nodes. A bare `&str` item is a terminal.
pub fn sequence<I>(items: impl IntoIterator<Item = I>) -> RailroadNode
where
    I: Into<RailroadNode>,
{
    RailroadNode::Seq {
        items: items.into_iter().map(Into::into).collect(),
    }
}

/// A choice of branches. A choice needs at least one branch; none is a
/// [`RailroadError`], as it is in TypeScript.
pub fn choice<I>(items: impl IntoIterator<Item = I>) -> Result<RailroadNode, RailroadError>
where
    I: Into<RailroadNode>,
{
    let items: Vec<RailroadNode> = items.into_iter().map(Into::into).collect();
    if items.is_empty() {
        return Err(RailroadError::new(
            "railroad: choice needs at least one branch",
        ));
    }
    Ok(RailroadNode::Choice { items })
}

/// An optional (bypassable) node.
pub fn optional(item: impl Into<RailroadNode>) -> RailroadNode {
    RailroadNode::Optional {
        item: Box::new(item.into()),
    }
}

/// A one-or-more repetition, with an optional node on the return path
/// (a separator, typically).
pub fn one_or_more(item: impl Into<RailroadNode>, rep: Option<RailroadNode>) -> RailroadNode {
    RailroadNode::OneOrMore {
        item: Box::new(item.into()),
        rep: rep.map(Box::new),
    }
}

/// A zero-or-more repetition (a bypassable [`one_or_more`]).
pub fn zero_or_more(item: impl Into<RailroadNode>, rep: Option<RailroadNode>) -> RailroadNode {
    RailroadNode::ZeroOrMore {
        item: Box::new(item.into()),
        rep: rep.map(Box::new),
    }
}

/// A top-level diagram wrapper.
pub fn diagram<I>(items: impl IntoIterator<Item = I>) -> RailroadNode
where
    I: Into<RailroadNode>,
{
    RailroadNode::Diagram {
        items: items.into_iter().map(Into::into).collect(),
    }
}

// ---- text emitter --------------------------------------------------

/// Compact EBNF-ish rendering: terminals quoted, a choice in `(a | b)`,
/// an optional in `[x]`, zero-or-more in `{x}`, one-or-more as `x+`.
pub fn to_text(node: &RailroadNode) -> String {
    match node {
        RailroadNode::Terminal { text } => json_string(text),
        RailroadNode::NonTerminal { text } => text.clone(),
        RailroadNode::Comment { text } => format!("/* {text} */"),
        RailroadNode::Skip => String::new(),
        RailroadNode::Seq { items } | RailroadNode::Diagram { items } => items
            .iter()
            .map(to_text)
            .filter(|text| !text.is_empty())
            .collect::<Vec<String>>()
            .join(" "),
        RailroadNode::Choice { items } => {
            format!(
                "({})",
                items
                    .iter()
                    .map(to_text)
                    .collect::<Vec<String>>()
                    .join(" | ")
            )
        }
        RailroadNode::Optional { item } => format!("[{}]", to_text(item)),
        RailroadNode::OneOrMore { item, rep } => format!("{}+{}", to_text(item), rep_text(rep)),
        RailroadNode::ZeroOrMore { item, rep } => {
            format!("{{{}}}{}", to_text(item), rep_text(rep))
        }
    }
}

fn rep_text(rep: &Option<Box<RailroadNode>>) -> String {
    match rep {
        Some(rep) => format!(" /* {} */", to_text(rep)),
        None => String::new(),
    }
}

// ---- structural helpers --------------------------------------------

/// Deep structural equality on nodes, the TypeScript `nodeEqual`. Here
/// it is the derived equality, named for parity with the other ports.
pub fn node_equal(a: &RailroadNode, b: &RailroadNode) -> bool {
    a == b
}

/// The JSON-encoded (double-quoted, escaped) form of `text`, what
/// `JSON.stringify` gives for a string.
pub(crate) fn json_string(text: &str) -> String {
    serde_json::to_string(text).expect("a string always serializes")
}