sipha 3.0.0

PEG parser, syntax trees, and code generation
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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
//! # Instruction Set
//!
//! The parse graph is a flat array of [`Insn`] values.
//!
//! ## Context-flag addressing
//!
//! Flags are indexed by a flat [`FlagId`](crate::parse::context::FlagId) (`u16`).
//! The VM maintains `flags: Vec<u64>` where:
//!
//! ```text
//! word  = flag_id >> 6
//! bit   = flag_id & 63
//! value = (flags[word] >> bit) & 1
//! ```
//!
//! `IfFlag` / `IfNotFlag` address individual bits.
//!
//! `PushFlags` references a [`FlagMaskTable`] entry (a sparse list of
//! `(word, set_bits, clear_bits)` triples) rather than embedding masks
//! inline — this supports masks that span many words without bloating the
//! instruction.

use crate::diagnostics::grammar_names::GrammarNames;
use crate::parse::context::FlagMaskWord;
use crate::parse::string_table::{StringTable, SymbolId};
use crate::types::{CharClass, FieldId, InsnId, RuleId, SyntaxKind, Tag};

// ─── Instruction enum ─────────────────────────────────────────────────────────

#[derive(Clone, Copy, Debug)]
#[repr(u8)]
pub enum Insn {
    // ── Terminals ────────────────────────────────────────────────────────────
    /// Match exactly one byte `byte`.
    ///
    /// On failure, jump to `on_fail`.
    ///
    /// Builder source: [`GrammarBuilder::byte`](crate::parse::builder::GrammarBuilder::byte)
    Byte { byte: u8, on_fail: InsnId },
    /// Match exactly one of two byte values.
    ///
    /// Slightly smaller/faster than `Class` for tiny sets.
    ///
    /// On failure, jump to `on_fail`.
    ///
    /// Builder source: [`GrammarBuilder::byte_either`](crate::parse::builder::GrammarBuilder::byte_either)
    ByteEither { a: u8, b: u8, on_fail: InsnId },
    /// Match exactly one of three byte values.
    ///
    /// On failure, jump to `on_fail`.
    ///
    /// Builder source: [`GrammarBuilder::byte_in3`](crate::parse::builder::GrammarBuilder::byte_in3)
    ByteIn3 {
        a: u8,
        b: u8,
        c: u8,
        on_fail: InsnId,
    },
    /// Match any byte in the inclusive range `[lo, hi]`.
    ///
    /// On failure, jump to `on_fail`.
    ///
    /// Builder source: [`GrammarBuilder::byte_range`](crate::parse::builder::GrammarBuilder::byte_range)
    ByteRange { lo: u8, hi: u8, on_fail: InsnId },
    /// Index into the grammar's `class_labels` for diagnostics; 0 = default "character class".
    ///
    /// On failure, jump to `on_fail`.
    ///
    /// Builder source: [`GrammarBuilder::class`](crate::parse::builder::GrammarBuilder::class),
    /// [`GrammarBuilder::class_with_label`](crate::parse::builder::GrammarBuilder::class_with_label)
    Class {
        class: CharClass,
        label_id: u32,
        on_fail: InsnId,
    },
    /// SIMD-accelerated for literals ≥ 16 bytes.
    ///
    /// On failure, jump to `on_fail`.
    ///
    /// Builder source: [`GrammarBuilder::literal`](crate::parse::builder::GrammarBuilder::literal)
    Literal { lit_id: u32, on_fail: InsnId },
    /// Inline literal of up to 8 bytes.
    ///
    /// Intended for very common short punctuation/keywords where indirection
    /// through the literal table is pure overhead.
    ///
    /// On failure, jump to `on_fail`.
    ///
    /// Builder source: [`GrammarBuilder::literal`](crate::parse::builder::GrammarBuilder::literal)
    LiteralSmall {
        len: u8,
        bytes: [u8; 8],
        on_fail: InsnId,
    },
    /// Match end-of-input.
    ///
    /// On failure, jump to `on_fail`.
    ///
    /// Builder source: [`GrammarBuilder::end_of_input`](crate::parse::builder::GrammarBuilder::end_of_input)
    EndOfInput { on_fail: InsnId },
    /// Unconditional failure.
    ///
    /// Builder source: [`GrammarBuilder::fail`](crate::parse::builder::GrammarBuilder::fail),
    /// and synthesized inside lookahead combinators.
    Fail,

    // ── Unicode codepoint terminals ───────────────────────────────────────────
    //
    // These instructions operate at UTF-8 codepoint level.  The VM decodes
    // one codepoint (1–4 bytes) and tests it.  Invalid UTF-8 always fails.
    //
    // For ASCII-only matching, prefer the byte-level instructions above —
    // they skip the UTF-8 decode and are marginally faster.
    /// Consume any single valid UTF-8 codepoint (1–4 bytes).
    /// Fails on invalid UTF-8 or end-of-input.
    ///
    /// On failure, jump to `on_fail`.
    ///
    /// Builder source: [`GrammarBuilder::any_char`](crate::parse::builder::GrammarBuilder::any_char)
    AnyChar { on_fail: InsnId },

    /// Match exactly the Unicode codepoint `codepoint`.
    /// Advances `pos` by the codepoint's UTF-8 byte length (1–4).
    ///
    /// On failure, jump to `on_fail`.
    ///
    /// Builder source: [`GrammarBuilder::char`](crate::parse::builder::GrammarBuilder::char)
    Char { codepoint: u32, on_fail: InsnId },

    /// Match any codepoint in the inclusive range `[lo, hi]`.
    /// Fails if the decoded codepoint is outside the range, or on invalid UTF-8.
    ///
    /// On failure, jump to `on_fail`.
    ///
    /// Builder source: [`GrammarBuilder::char_range`](crate::parse::builder::GrammarBuilder::char_range)
    CharRange { lo: u32, hi: u32, on_fail: InsnId },

    // ── Control flow ──────────────────────────────────────────────────────────
    /// Unconditional jump to `target`.
    ///
    /// Builder source: synthesized by higher-level builders (not directly exposed),
    /// most prominently [`GrammarBuilder::byte_dispatch`](crate::parse::builder::GrammarBuilder::byte_dispatch).
    Jump { target: InsnId },
    /// Call a rule by id.
    ///
    /// Builder source: [`GrammarBuilder::call`](crate::parse::builder::GrammarBuilder::call),
    /// [`GrammarBuilder::call_id`](crate::parse::builder::GrammarBuilder::call_id)
    Call { rule: RuleId },
    /// Return from a rule call.
    ///
    /// Builder source: automatically emitted at the end of every rule body by
    /// [`GrammarBuilder::rule`](crate::parse::builder::GrammarBuilder::rule),
    /// [`GrammarBuilder::parser_rule`](crate::parse::builder::GrammarBuilder::parser_rule),
    /// and [`GrammarBuilder::lexer_rule`](crate::parse::builder::GrammarBuilder::lexer_rule).
    Return,

    // ── Backtracking ──────────────────────────────────────────────────────────
    /// Save a backtracking point with an alternate branch at `alt`.
    ///
    /// Builder source: [`GrammarBuilder::choice`](crate::parse::builder::GrammarBuilder::choice),
    /// [`GrammarBuilder::optional`](crate::parse::builder::GrammarBuilder::optional),
    /// [`GrammarBuilder::zero_or_more`](crate::parse::builder::GrammarBuilder::zero_or_more),
    /// [`GrammarBuilder::lookahead`](crate::parse::builder::GrammarBuilder::lookahead),
    /// [`GrammarBuilder::neg_lookahead`](crate::parse::builder::GrammarBuilder::neg_lookahead).
    Choice { alt: InsnId },
    /// Discard the most recent backtracking point and jump to `target`.
    ///
    /// Builder source: [`GrammarBuilder::choice`](crate::parse::builder::GrammarBuilder::choice),
    /// [`GrammarBuilder::optional`](crate::parse::builder::GrammarBuilder::optional),
    /// [`GrammarBuilder::cut`](crate::parse::builder::GrammarBuilder::cut).
    Commit { target: InsnId },
    /// Succeed without consuming input: backtrack to the most recent choice point, then jump to `target`.
    ///
    /// Builder source: [`GrammarBuilder::lookahead`](crate::parse::builder::GrammarBuilder::lookahead).
    BackCommit { target: InsnId },
    /// Negative lookahead commit: succeed iff the body failed, without consuming input, then jump to `target`.
    ///
    /// Builder source: [`GrammarBuilder::neg_lookahead`](crate::parse::builder::GrammarBuilder::neg_lookahead).
    NegBackCommit { target: InsnId },
    /// Commit partial progress and jump to `target`.
    ///
    /// Used by loops to commit events/captures per-iteration while still allowing the loop
    /// itself to terminate via backtracking.
    ///
    /// Builder source: [`GrammarBuilder::zero_or_more`](crate::parse::builder::GrammarBuilder::zero_or_more).
    PartialCommit { target: InsnId },

    // ── O(1) Dispatch ────────────────────────────────────────────────────────
    /// Dispatch to an arm based on the next input byte using a precomputed 256-entry table.
    ///
    /// Builder source: [`GrammarBuilder::byte_dispatch`](crate::parse::builder::GrammarBuilder::byte_dispatch).
    ByteDispatch { table_id: u32 },

    // ── Fused terminals ──────────────────────────────────────────────────────
    /// Consume a run of bytes matching `class`.
    ///
    /// Equivalent to `class` repeated with no backtracking in-between, and is
    /// commonly used for digit/ident/whitespace runs.
    ///
    /// Succeeds iff at least `min` bytes are consumed.
    ///
    /// On failure, jump to `on_fail`.
    ///
    /// Builder source: [`GrammarBuilder::consume_while_class`](crate::parse::builder::GrammarBuilder::consume_while_class),
    /// [`GrammarBuilder::consume_while_class_with_label`](crate::parse::builder::GrammarBuilder::consume_while_class_with_label),
    /// [`GrammarBuilder::consume_while_class1_with_label`](crate::parse::builder::GrammarBuilder::consume_while_class1_with_label).
    ConsumeWhileClass {
        class: CharClass,
        /// Index into `class_labels` for diagnostics (0 = "character class").
        label_id: u32,
        min: u32,
        on_fail: InsnId,
    },

    // ── Context flags ─────────────────────────────────────────────────────────
    //
    // Flag addressing: word = flag_id >> 6,  bit = flag_id & 63.
    // Supports up to 65,535 flags (FlagId = u16).
    /// Succeed iff `flags[flag_id >> 6] & (1 << (flag_id & 63)) != 0`.
    /// Does not consume input.
    ///
    /// On failure, jump to `on_fail`.
    ///
    /// Builder source: [`GrammarBuilder::if_flag`](crate::parse::builder::GrammarBuilder::if_flag).
    IfFlag { flag_id: u16, on_fail: InsnId },

    /// Succeed iff the named flag is **clear**.
    ///
    /// On failure, jump to `on_fail`.
    ///
    /// Builder source: [`GrammarBuilder::if_not_flag`](crate::parse::builder::GrammarBuilder::if_not_flag).
    IfNotFlag { flag_id: u16, on_fail: InsnId },

    /// Push a snapshot of the affected words onto the context-save arena,
    /// then apply `graph.flag_masks.get(mask_id)` to the live flag bank.
    ///
    /// Uses a table reference instead of inline masks so that masks spanning
    /// many words do not bloat the instruction array.
    ///
    /// Builder source: [`GrammarBuilder::with_flags`](crate::parse::builder::GrammarBuilder::with_flags).
    PushFlags { mask_id: u32 },

    /// Restore the words saved by the matching `PushFlags`.
    ///
    /// Builder source: [`GrammarBuilder::with_flags`](crate::parse::builder::GrammarBuilder::with_flags).
    PopFlags,

    // ── Captures ─────────────────────────────────────────────────────────────
    /// Begin a legacy capture region tagged with `tag`.
    ///
    /// Builder source: [`GrammarBuilder::capture`](crate::parse::builder::GrammarBuilder::capture).
    CaptureBegin { tag: Tag },
    /// End a legacy capture region tagged with `tag`.
    ///
    /// Builder source: [`GrammarBuilder::capture`](crate::parse::builder::GrammarBuilder::capture).
    CaptureEnd { tag: Tag },

    // ── Green/Red tree ───────────────────────────────────────────────────────
    //
    // The grammar author wraps matched content in node/token/trivia builders.
    // The VM emits TreeEvents into a side buffer; on success, the caller passes
    // that buffer to `green::build_green_tree`.
    //
    // Backtracking discards uncommitted events exactly as it does CaptureEvents.
    // `PartialCommit` (used by loops) updates the tree mark so each successful
    // loop iteration is permanently committed.
    /// Open a syntax node.  Emits `TreeEvent::NodeOpen`.
    /// `field` labels this node as a named child of its parent when present.
    ///
    /// Builder source: [`GrammarBuilder::node`](crate::parse::builder::GrammarBuilder::node),
    /// [`GrammarBuilder::node_with_field`](crate::parse::builder::GrammarBuilder::node_with_field).
    NodeBegin {
        kind: SyntaxKind,
        field: Option<FieldId>,
    },

    /// Close the current syntax node.  Emits `TreeEvent::NodeClose`.
    ///
    /// Builder source: [`GrammarBuilder::node`](crate::parse::builder::GrammarBuilder::node),
    /// [`GrammarBuilder::node_with_field`](crate::parse::builder::GrammarBuilder::node_with_field).
    NodeEnd,

    /// Begin a leaf token; saves `(kind, is_trivia, pos)` on the token-open
    /// side-stack.  Does **not** emit a `TreeEvent` yet.
    ///
    /// Builder source: [`GrammarBuilder::token`](crate::parse::builder::GrammarBuilder::token),
    /// [`GrammarBuilder::trivia`](crate::parse::builder::GrammarBuilder::trivia).
    TokenBegin { kind: SyntaxKind, is_trivia: bool },

    /// End a leaf token; pops the token-open side-stack and emits
    /// `TreeEvent::Token { start, end, kind, is_trivia }`.
    ///
    /// Builder source: [`GrammarBuilder::token`](crate::parse::builder::GrammarBuilder::token),
    /// [`GrammarBuilder::trivia`](crate::parse::builder::GrammarBuilder::trivia).
    TokenEnd,

    /// Record an expected label at the current position for diagnostics, then continue.
    /// Used by [`expect_label`](crate::parse::builder::GrammarBuilder::expect_label).
    ///
    /// Builder source: [`GrammarBuilder::expect_label`](crate::parse::builder::GrammarBuilder::expect_label).
    RecordExpectedLabel { label_id: u32 },

    // ── Diagnostic context ("while parsing") ─────────────────────────────────
    /// Push a "while parsing ..." label onto the diagnostic context stack.
    /// Paired with [`Insn::PopDiagnosticContext`]; usually emitted as a bracket
    /// pair around rule bodies by `GrammarBuilder::context_rule`.
    PushDiagnosticContext { label_id: u32 },

    /// Pop the innermost diagnostic context entry.
    PopDiagnosticContext,

    // ── Dynamic hints ────────────────────────────────────────────────────────
    /// Attach a human-readable hint at this position if it is at (or beyond)
    /// the current furthest error position.
    ///
    /// The hint is an interned string ID in the grammar's shared string pool.
    SetHint { hint_id: SymbolId },

    // ── Optional runtime tracing ─────────────────────────────────────────────
    /// Runtime trace waypoint (only emitted when builder trace mode is enabled).
    ///
    /// `label_id` is an interned string ID in the grammar's shared string pool.
    TracePoint { label_id: SymbolId },

    // ── Error recovery ───────────────────────────────────────────────────────
    //
    // RecoverUntil: try the following "body"; on failure, skip input until
    // sync_rule matches (or EOI), then continue at resume. Used by
    // [`recover_until`](crate::parse::builder::GrammarBuilder::recover_until).
    /// Start a recoverable region. On failure, skip until `sync_rule` matches, then jump to resume.
    ///
    /// Builder source: [`GrammarBuilder::recover_until`](crate::parse::builder::GrammarBuilder::recover_until).
    RecoverUntil { sync_rule: RuleId, resume: InsnId },
    /// After sync rule matched during recovery; pops the recovery frame and continues.
    ///
    /// Builder source: [`GrammarBuilder::recover_until`](crate::parse::builder::GrammarBuilder::recover_until).
    RecoveryResume,

    // ── Accept ───────────────────────────────────────────────────────────────
    /// Accept the parse successfully.
    ///
    /// Builder source: [`GrammarBuilder::accept`](crate::parse::builder::GrammarBuilder::accept).
    Accept,
}

// ─── Literal table ────────────────────────────────────────────────────────────

#[derive(Clone, Copy, Debug)]
pub struct LiteralTable<'a> {
    pub data: &'a [u8],
    pub offsets: &'a [u32],
}

impl LiteralTable<'_> {
    #[must_use]
    #[inline]
    pub fn get(&self, id: u32) -> &[u8] {
        let s = self.offsets[id as usize] as usize;
        let e = self.offsets[id as usize + 1] as usize;
        &self.data[s..e]
    }
}

// ─── Flag-mask table ──────────────────────────────────────────────────────────

/// Stores all sparse flag masks used by [`Insn::PushFlags`].
///
/// Layout mirrors [`LiteralTable`]: all [`FlagMaskWord`] entries are
/// concatenated into `data`; `offsets[m]..offsets[m+1]` gives the slice
/// for mask `m`.
#[derive(Clone, Copy, Debug)]
pub struct FlagMaskTable<'a> {
    /// All `FlagMaskWord` entries concatenated.
    pub data: &'a [FlagMaskWord],
    /// Offset into `data` for each mask.  Length = `num_masks` + 1.
    pub offsets: &'a [u32],
}

impl FlagMaskTable<'_> {
    /// Return the entries for mask `id`.
    #[must_use]
    #[inline]
    pub fn get(&self, id: u32) -> &[FlagMaskWord] {
        let s = self.offsets[id as usize] as usize;
        let e = self.offsets[id as usize + 1] as usize;
        &self.data[s..e]
    }
}

// ─── Parse graph ─────────────────────────────────────────────────────────────

/// VM bytecode and lookup tables, borrowing backing storage (e.g. from [`crate::parse::builder::BuiltGraph`]).
///
/// For generated static grammars, use `ParseGraph<'static>` with `&'static` slices.
#[derive(Clone, Copy, Debug)]
pub struct ParseGraph<'a> {
    pub insns: &'a [Insn],
    pub rule_entry: &'a [InsnId],
    pub literals: LiteralTable<'a>,
    pub jump_tables: &'a [[u32; 256]],
    pub flag_masks: FlagMaskTable<'a>,
    /// Shared string pool; [`SymbolId`] values index into it.
    pub strings: &'a StringTable,
    pub rule_names: &'a [SymbolId],
    pub tag_names: &'a [SymbolId],
    /// Labels for [`Insn::Class`] diagnostics; index 0 is the default "character class".
    pub class_labels: &'a [SymbolId],
    /// Labels for [`Expected::Label`](crate::diagnostics::error::Expected::Label) from [`expect_label`](crate::parse::builder::GrammarBuilder::expect_label).
    pub expected_labels: &'a [SymbolId],
    /// Names for named child fields (indexed by [`crate::types::FieldId`]); used by `field_by_id` / name resolution.
    pub field_names: &'a [SymbolId],
}

impl ParseGraph<'_> {
    /// Resolve display name for a rule id.
    #[must_use]
    #[inline]
    pub fn rule_name(&self, id: RuleId) -> Option<&str> {
        self.rule_names
            .get(id as usize)
            .map(|&sym| self.strings.resolve(sym))
    }

    /// Look up a [`RuleId`] by rule name.
    ///
    /// This is the inverse of [`rule_name`](Self::rule_name).
    #[must_use]
    pub fn rule_id(&self, name: &str) -> Option<RuleId> {
        self.rule_names.iter().enumerate().find_map(|(i, &sym)| {
            (self.strings.resolve(sym) == name)
                .then(|| RuleId::try_from(i).ok())
                .flatten()
        })
    }

    /// Resolve [`Expected::Label`](crate::diagnostics::error::Expected::Label) text.
    #[must_use]
    #[inline]
    pub fn expected_label(&self, id: u32) -> Option<&str> {
        self.expected_labels
            .get(id as usize)
            .map(|&sym| self.strings.resolve(sym))
    }

    /// Resolve class diagnostic label for insn `label_id`.
    #[must_use]
    #[inline]
    pub fn class_label(&self, label_id: u32) -> Option<&str> {
        self.class_labels
            .get(label_id as usize)
            .map(|&sym| self.strings.resolve(sym))
    }

    #[must_use]
    #[inline]
    pub fn start(&self) -> InsnId {
        self.rule_entry[0]
    }

    #[must_use]
    #[inline]
    pub fn insn(&self, id: InsnId) -> Insn {
        unsafe { *self.insns.get_unchecked(id as usize) }
    }

    #[must_use]
    #[inline]
    pub fn dispatch(&self, table_id: u32, byte: u8) -> InsnId {
        unsafe {
            *self
                .jump_tables
                .get_unchecked(table_id as usize)
                .get_unchecked(byte as usize)
        }
    }

    /// Resolve a tag name symbol.
    #[must_use]
    #[inline]
    pub fn tag_name(&self, id: Tag) -> &str {
        self.tag_names
            .get(id as usize)
            .map_or("?", |&s| self.strings.resolve(s))
    }

    /// Resolve a field name symbol.
    #[must_use]
    #[inline]
    pub fn field_name(&self, id: FieldId) -> &str {
        self.field_names
            .get(id as usize)
            .map_or("?", |&s| self.strings.resolve(s))
    }

    /// Look up [`FieldId`] by field name string.
    #[must_use]
    pub fn field_id(&self, name: &str) -> Option<FieldId> {
        self.field_names.iter().enumerate().find_map(|(i, &sym)| {
            (self.strings.resolve(sym) == name)
                .then(|| FieldId::try_from(i).ok())
                .flatten()
        })
    }
}

#[cfg(test)]
mod tests {
    use super::{FlagMaskTable, LiteralTable, ParseGraph};
    use crate::parse::string_table::{StringTable, SymbolId};

    #[test]
    fn rule_id_roundtrips_with_rule_name() {
        let pool: &'static [&'static str] = &["", "start", "expr", "stmt"];
        let strings = StringTable::from_static_pool(pool);
        let rule_names: &[SymbolId] = &[SymbolId(1), SymbolId(2), SymbolId(3)];

        let graph = ParseGraph {
            insns: &[],
            rule_entry: &[0],
            literals: LiteralTable {
                data: &[],
                offsets: &[0],
            },
            jump_tables: &[],
            flag_masks: FlagMaskTable {
                data: &[],
                offsets: &[0],
            },
            strings: &strings,
            rule_names,
            tag_names: &[],
            class_labels: &[],
            expected_labels: &[],
            field_names: &[],
        };

        for (id, &sym) in rule_names.iter().enumerate() {
            let name = graph.rule_name(id as u16).unwrap();
            assert_eq!(name, strings.resolve(sym));
            assert_eq!(graph.rule_id(name), Some(id as u16));
        }

        assert_eq!(graph.rule_id("does_not_exist"), None);
    }
}

impl GrammarNames for ParseGraph<'_> {
    fn rule_name(&self, id: RuleId) -> Option<&str> {
        self.rule_names
            .get(id as usize)
            .map(|&sym| self.strings.resolve(sym))
    }

    fn expected_label(&self, id: u32) -> Option<&str> {
        self.expected_labels
            .get(id as usize)
            .map(|&sym| self.strings.resolve(sym))
    }

    fn class_label(&self, label_id: u32) -> Option<&str> {
        self.class_labels
            .get(label_id as usize)
            .map(|&sym| self.strings.resolve(sym))
    }

    fn resolve_symbol(&self, id: SymbolId) -> Option<&str> {
        Some(self.strings.resolve(id))
    }
}

impl<'a> GrammarNames for &'a ParseGraph<'a> {
    fn rule_name(&self, id: RuleId) -> Option<&str> {
        ParseGraph::rule_name(self, id)
    }

    fn expected_label(&self, id: u32) -> Option<&str> {
        ParseGraph::expected_label(self, id)
    }

    fn class_label(&self, label_id: u32) -> Option<&str> {
        ParseGraph::class_label(self, label_id)
    }

    fn resolve_symbol(&self, id: SymbolId) -> Option<&str> {
        Some(self.strings.resolve(id))
    }
}