dsl-kit-parse 0.11.1

Parser trunk for dsl-kit DSLs (ParseTree, schema conformance, serde bridge, DslBuild consumer).
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
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
//! Static grammar checks (G-3) for a [`crate::peg::Grammar`] value.
//!
//! `GrammarCheck` is the *static* side of parser-core quality.
//! The runtime interpreter in [`crate::peg`] carries backstops for left
//! recursion and nullable-body [`crate::peg::Peg::Repeat`], but the
//! grammar author wants to be told before parse time — every ill-formed
//! grammar caught here is a whole class of runtime failure the consumer
//! AI never has to reason about.
//!
//! # Rule set
//!
//! - **[`check_left_recursion`]** — flags every rule reachable from
//!   itself via first-position rule references without consuming input.
//!   Direct (`r <- r "a"`) and indirect (`a <- b; b <- a "x"`) forms
//!   are both detected. Nullable-prefix chains that expose deeper rules
//!   to first position (`a <- b a; b <- ε`) are detected too. Emits
//!   under the same [`crate::peg::codes::LEFT_RECURSION`] slug the
//!   runtime backstop uses, so downstream tools see one dialect.
//! - **[`check_nullable_repeat`]** — flags every
//!   [`crate::peg::Peg::Repeat`] whose body can match empty. At runtime
//!   the interpreter already breaks the loop (no hang) and, when the
//!   `min` bound was not yet met, emits
//!   [`crate::peg::codes::NULLABLE_REPEAT`]. The static check catches it
//!   upfront regardless of `min`, so authors see the shape mistake once
//!   during grammar review rather than the day a pathological input
//!   triggers it.
//! - **[`check_schema_consistency`]** — cross-checks every
//!   [`crate::peg::Peg::Node`] variant against the consumer's
//!   [`NodeSchema`]. Grammar variants not declared in the schema are
//!   errors (the parser would emit a
//!   [`crate::codes::UNKNOWN_VARIANT`] diagnostic at conformance time,
//!   so surface it before the first parse attempt). Schema variants
//!   never produced by the grammar are `Warning`-severity reachability
//!   hints — perfectly fine for hand-built values, worth reviewing when
//!   the grammar is the sole producer.
//!
//! # Diagnostic dialect
//!
//! Every diagnostic uses the shared [`Diagnostic`] envelope (per
//! `parser-design.md §3.2`). Grammar-node diagnostics carry
//! [`Location::Node`] pointing at the offending [`crate::peg::Peg`]
//! node id; the unreachable-variant warning has no natural anchor and
//! uses [`Location::None`].

use crate::peg::{Grammar, Peg, codes as peg_codes};
use crate::{Diagnostic, Location, Severity};
use dsl_kit_core::{NodeId, Suggester};
use dsl_kit_schema::NodeSchema;
use std::collections::{HashMap, HashSet};

// ---------------------------------------------------------------------------
// Diagnostic codes
// ---------------------------------------------------------------------------

/// Diagnostic codes emitted by [`check_schema_consistency`].
///
/// [`check_left_recursion`] and [`check_nullable_repeat`] reuse the
/// runtime interpreter's slugs ([`crate::peg::codes::LEFT_RECURSION`],
/// [`crate::peg::codes::NULLABLE_REPEAT`]) so static + runtime speak one
/// dialect.
pub mod codes {
    /// A [`crate::peg::Peg::Node`] in the grammar references a variant
    /// name not declared in the consumer's [`dsl_kit_schema::NodeSchema`].
    pub const UNKNOWN_VARIANT: &str = "dsl_kit::parse::grammar_check::unknown_variant";
    /// A [`dsl_kit_schema::VariantSchema`] name is declared in the
    /// consumer's schema but no [`crate::peg::Peg::Node`] in the grammar
    /// ever produces it. `Warning`-severity — hand-built values may fill
    /// the gap legitimately.
    pub const UNREACHABLE_VARIANT: &str = "dsl_kit::parse::grammar_check::unreachable_variant";
}

// ---------------------------------------------------------------------------
// Public API
// ---------------------------------------------------------------------------

/// Runs every grammar-only check ([`check_left_recursion`] +
/// [`check_nullable_repeat`]) and returns the accumulated diagnostics.
pub fn check(g: &Grammar) -> Vec<Diagnostic> {
    let mut out = check_left_recursion(g);
    out.extend(check_nullable_repeat(g));
    out
}

/// Runs [`check`] and additionally [`check_schema_consistency`] against
/// `schema`.
pub fn check_against(g: &Grammar, schema: &NodeSchema) -> Vec<Diagnostic> {
    let mut out = check(g);
    out.extend(check_schema_consistency(g, schema));
    out
}

// ---------------------------------------------------------------------------
// Left-recursion detection
// ---------------------------------------------------------------------------

/// Flags every rule reachable from itself via first-position rule
/// references without consuming input.
///
/// The check runs a fixed-point analysis of two derived properties per
/// rule:
///
/// 1. `nullable(R)` — can `R`'s body succeed on zero input?
/// 2. `first_rules(R)` — the transitive set of rule names reachable at
///    first position from `R`'s body, honouring nullability across
///    [`crate::peg::Peg::Seq`] items.
///
/// `R` is left-recursive iff `R ∈ first_rules(R)`. The rules-by-name
/// map is snapshotted once per invocation; the analysis converges in a
/// small number of passes for any finite grammar.
pub fn check_left_recursion(g: &Grammar) -> Vec<Diagnostic> {
    let rules = rules_by_name(g);
    let nullable = compute_nullable(&rules);
    let first = compute_left_first(&rules, &nullable);
    let mut out = Vec::new();
    for r in &g.rules {
        if let Peg::Rule { id, name, .. } = r
            && let Some(f) = first.get(name.as_str())
            && f.contains(name.as_str())
        {
            out.push(
                Diagnostic::error(
                    peg_codes::LEFT_RECURSION,
                    format!(
                        "rule `{name}` is left-recursive (reaches itself at first \
                         position without consuming input); PEG does not support \
                         left recursion — refactor into layered rules or a Repeat"
                    ),
                )
                .with_node(*id),
            );
        }
    }
    out
}

// ---------------------------------------------------------------------------
// Nullable-repeat detection
// ---------------------------------------------------------------------------

/// Flags every [`crate::peg::Peg::Repeat`] whose body can match empty.
///
/// A nullable body drives the interpreter to break the loop rather than
/// hang (`peg.rs` `run_repeat`), but the loop then either fails the
/// `min` bound (runtime [`crate::peg::codes::NULLABLE_REPEAT`]) or
/// silently succeeds with zero productions. Neither branch is what the
/// grammar author wanted — surface the shape mistake statically.
pub fn check_nullable_repeat(g: &Grammar) -> Vec<Diagnostic> {
    let rules = rules_by_name(g);
    let nullable = compute_nullable(&rules);
    let mut out = Vec::new();
    for r in &g.rules {
        walk_peg(r, &mut |p| {
            if let Peg::Repeat { id, body, .. } = p
                && is_nullable(body, &nullable)
            {
                out.push(
                    Diagnostic::error(
                        peg_codes::NULLABLE_REPEAT,
                        "Repeat body can match empty input; the loop would \
                         either fail its `min` bound at runtime or succeed \
                         with zero productions — tighten the body so each \
                         iteration must consume at least one byte"
                            .to_string(),
                    )
                    .with_node(*id),
                );
            }
        });
    }
    out
}

// ---------------------------------------------------------------------------
// Grammar-schema consistency
// ---------------------------------------------------------------------------

/// Cross-checks every [`crate::peg::Peg::Node`] variant against `schema`.
///
/// Grammar variants missing from the schema are `Error`-severity —
/// building a typed AST from such a tree fails at
/// [`crate::check_conformance`] anyway, so surface it upfront.
/// Schema variants that no grammar node produces are `Warning`-severity
/// reachability hints; hand-built values may legitimately fill the gap.
pub fn check_schema_consistency(g: &Grammar, schema: &NodeSchema) -> Vec<Diagnostic> {
    check_schema_consistency_with(g, schema, &crate::BuiltinLevenshteinSuggester)
}

/// Variant of [`check_schema_consistency`] that routes
/// `did you mean X?` hints through a caller-supplied [`Suggester`].
///
/// The free function [`check_schema_consistency`] delegates here with
/// the crate's built-in Levenshtein backend, so existing callers see
/// no behavioural change. Reach for this variant to plug in a
/// different similarity algorithm (e.g. `dsl-kit-fuzzy`'s
/// `FuzzySuggester`) at a specific call site.
pub fn check_schema_consistency_with(
    g: &Grammar,
    schema: &NodeSchema,
    suggester: &dyn Suggester,
) -> Vec<Diagnostic> {
    let declared: HashSet<&str> = schema.variants.iter().map(|v| v.name.as_str()).collect();
    let declared_names: Vec<&str> = schema.variants.iter().map(|v| v.name.as_str()).collect();
    let mut referenced: HashSet<String> = HashSet::new();
    let mut out = Vec::new();
    for r in &g.rules {
        walk_peg(r, &mut |p| {
            if let Peg::Node { id, variant, .. } = p {
                // Empty-variant `Peg::Node` is a synthetic leaf
                // wrapper (e.g. the scalar-keyed-entry envelope
                // `child_arg_peg` emits so `build_scalar_map` can
                // read `.field("value")` on each entry). Its variant
                // name is a marker, not a real AST variant to match
                // against the schema, so exclude it from the
                // declared/unknown gate. Reachability of real
                // variants is unaffected: the wrapper never
                // stand-ins for a declared variant.
                if variant.is_empty() {
                    return;
                }
                // The reserved import placeholder (`$import`, injected
                // by `crate::import::add_import_syntax`) is a load-phase
                // marker, never a schema variant: the loader splices it
                // away before conformance, so matching it against the
                // schema would flag every import-enabled grammar.
                if variant == crate::import::IMPORT_VARIANT {
                    return;
                }
                // Same argument for the reserved suppression wrapper
                // (`$allow`, injected by
                // `crate::allow::add_allow_syntax`): it is annotation
                // syntax that `crate::allow::collapse` folds away on
                // every text parse, so it never faces the schema.
                if variant == crate::allow::ALLOW_VARIANT {
                    return;
                }
                referenced.insert(variant.clone());
                if !declared.contains(variant.as_str()) {
                    let base = format!(
                        "grammar produces variant `{variant}` which is \
                         not declared in schema `{}`",
                        schema.name
                    );
                    let msg = match suggester.enrich_unknown(variant, &declared_names) {
                        Some(hint) => format!("{base} ({hint})"),
                        None => base,
                    };
                    out.push(Diagnostic::error(codes::UNKNOWN_VARIANT, msg).with_node(*id));
                }
            }
        });
    }
    let mut unreachable: Vec<&str> = declared
        .into_iter()
        .filter(|name| !referenced.contains(*name))
        .collect();
    unreachable.sort_unstable();
    for name in unreachable {
        out.push(Diagnostic {
            severity: Severity::Warning,
            code: codes::UNREACHABLE_VARIANT.to_string(),
            message: format!(
                "schema variant `{name}` is not produced by any grammar rule \
                 (hand-built values may still construct it)"
            ),
            location: Location::None,
        });
    }
    out
}

// ---------------------------------------------------------------------------
// Shared analysis helpers
// ---------------------------------------------------------------------------

fn rules_by_name(g: &Grammar) -> HashMap<&str, &Peg> {
    let mut m = HashMap::new();
    for r in &g.rules {
        if let Peg::Rule { name, .. } = r {
            m.insert(name.as_str(), r);
        }
    }
    m
}

/// Fixed-point nullability: a rule is nullable iff its body can succeed
/// on zero input, taking mutually-recursive rules into account.
fn compute_nullable(rules: &HashMap<&str, &Peg>) -> HashMap<String, bool> {
    let mut nullable: HashMap<String, bool> =
        rules.keys().map(|k| ((*k).to_string(), false)).collect();
    loop {
        let mut changed = false;
        for (name, rule) in rules {
            if let Peg::Rule { body, .. } = rule {
                let n = is_nullable(body, &nullable);
                let slot = nullable.entry((*name).to_string()).or_insert(false);
                if n && !*slot {
                    *slot = true;
                    changed = true;
                }
            }
        }
        if !changed {
            break;
        }
    }
    nullable
}

fn is_nullable(peg: &Peg, nullable_rules: &HashMap<String, bool>) -> bool {
    match peg {
        Peg::Rule { body, .. } | Peg::Node { body, .. } | Peg::Field { body, .. } => {
            is_nullable(body, nullable_rules)
        }
        Peg::Seq { items, .. } => items.iter().all(|i| is_nullable(i, nullable_rules)),
        Peg::Choice { alts, .. } => {
            alts.is_empty() || alts.iter().any(|a| is_nullable(a, nullable_rules))
        }
        Peg::Repeat { body, min, .. } => *min == 0 || is_nullable(body, nullable_rules),
        // A keyed entry consumes whatever its key and value consume;
        // it adds no syntax of its own, so it is nullable only if both
        // halves are.
        Peg::KeyedEntry { key, value, .. } => {
            is_nullable(key, nullable_rules) && is_nullable(value, nullable_rules)
        }
        Peg::RuleRef { name, .. } => nullable_rules.get(name).copied().unwrap_or(false),
        // Token classes (%ident / %int / %ws / %kw:<w>) all require ≥1
        // byte; a literal is nullable iff empty. Empty literals are a
        // grammar bug in their own right but this analysis stays
        // permissive — no `Token` currently produced by the convenience
        // constructors is empty in practice.
        Peg::Token { pat, .. } => pat.is_empty(),
    }
}

/// Fixed-point first-rule set: for each rule, the transitive closure of
/// rule names reachable at first position from the rule's body without
/// consuming input.
fn compute_left_first(
    rules: &HashMap<&str, &Peg>,
    nullable: &HashMap<String, bool>,
) -> HashMap<String, HashSet<String>> {
    let mut first: HashMap<String, HashSet<String>> = rules
        .keys()
        .map(|k| ((*k).to_string(), HashSet::new()))
        .collect();
    loop {
        let mut changed = false;
        for (name, rule) in rules {
            if let Peg::Rule { body, .. } = rule {
                let mut f = HashSet::new();
                collect_first(body, nullable, &first, &mut f);
                let slot = first.entry((*name).to_string()).or_default();
                if &f != slot {
                    *slot = f;
                    changed = true;
                }
            }
        }
        if !changed {
            break;
        }
    }
    first
}

fn collect_first(
    peg: &Peg,
    nullable: &HashMap<String, bool>,
    first_rules: &HashMap<String, HashSet<String>>,
    out: &mut HashSet<String>,
) {
    match peg {
        Peg::Rule { body, .. }
        | Peg::Node { body, .. }
        | Peg::Field { body, .. }
        | Peg::Repeat { body, .. } => collect_first(body, nullable, first_rules, out),
        Peg::Seq { items, .. } => {
            for item in items {
                collect_first(item, nullable, first_rules, out);
                if !is_nullable(item, nullable) {
                    break;
                }
            }
        }
        Peg::Choice { alts, .. } => {
            for alt in alts {
                collect_first(alt, nullable, first_rules, out);
            }
        }
        // Same first-position logic as a two-item `Seq`: the value's
        // first set only counts when the key can match empty.
        Peg::KeyedEntry { key, value, .. } => {
            collect_first(key, nullable, first_rules, out);
            if is_nullable(key, nullable) {
                collect_first(value, nullable, first_rules, out);
            }
        }
        Peg::RuleRef { name, .. } => {
            out.insert(name.clone());
            if let Some(f) = first_rules.get(name) {
                for n in f {
                    out.insert(n.clone());
                }
            }
        }
        Peg::Token { .. } => {}
    }
}

/// Depth-first pre-order walk over every [`Peg`] node in the tree
/// rooted at `peg`, invoking `f` on each. Kept module-private —
/// external callers should reach for
/// [`dsl_kit_core::Walk`]-derived traversal on the grammar value
/// itself when they need programmatic access.
fn walk_peg(peg: &Peg, f: &mut dyn FnMut(&Peg)) {
    f(peg);
    match peg {
        Peg::Rule { body, .. }
        | Peg::Repeat { body, .. }
        | Peg::Node { body, .. }
        | Peg::Field { body, .. } => walk_peg(body, f),
        Peg::Seq { items, .. } => {
            for i in items {
                walk_peg(i, f);
            }
        }
        Peg::Choice { alts, .. } => {
            for a in alts {
                walk_peg(a, f);
            }
        }
        Peg::KeyedEntry { key, value, .. } => {
            walk_peg(key, f);
            walk_peg(value, f);
        }
        Peg::RuleRef { .. } | Peg::Token { .. } => {}
    }
}

/// Convenience: extract every Peg node in `g` whose id equals `id`.
/// Handy in tests that want to assert the diagnostic anchor matches a
/// specific known-id in the grammar being checked.
#[cfg(test)]
fn find_by_id(g: &Grammar, id: NodeId) -> Option<&Peg> {
    fn recurse(p: &Peg, id: NodeId) -> Option<&Peg> {
        if peg_id(p) == id {
            return Some(p);
        }
        match p {
            Peg::Rule { body, .. }
            | Peg::Repeat { body, .. }
            | Peg::Node { body, .. }
            | Peg::Field { body, .. } => recurse(body, id),
            Peg::Seq { items, .. } => items.iter().find_map(|i| recurse(i, id)),
            Peg::Choice { alts, .. } => alts.iter().find_map(|a| recurse(a, id)),
            Peg::KeyedEntry { key, value, .. } => recurse(key, id).or_else(|| recurse(value, id)),
            Peg::RuleRef { .. } | Peg::Token { .. } => None,
        }
    }
    g.rules.iter().find_map(|r| recurse(r, id))
}

#[cfg(test)]
fn peg_id(p: &Peg) -> NodeId {
    match p {
        Peg::Rule { id, .. }
        | Peg::Seq { id, .. }
        | Peg::Choice { id, .. }
        | Peg::Repeat { id, .. }
        | Peg::RuleRef { id, .. }
        | Peg::Token { id, .. }
        | Peg::Node { id, .. }
        | Peg::Field { id, .. }
        | Peg::KeyedEntry { id, .. } => *id,
    }
}

// Force NodeId into scope for the doc examples.
#[allow(dead_code)]
fn _touch_nodeid(_: NodeId) {}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;
    use crate::peg::{choice, field, keyed_entry, node, repeat, rule, rule_ref, seq, token};
    use dsl_kit_core::IdGen;
    use dsl_kit_schema::{
        ChildSchema, ChildValueShape, FieldSchema, Multiplicity, NodeSchema, VariantSchema,
    };

    // -- helpers --

    fn schema_expr() -> NodeSchema {
        NodeSchema {
            name: "Expr".into(),
            variants: vec![
                VariantSchema {
                    name: "Lit".into(),
                    fields: vec![FieldSchema {
                        name: "value".into(),
                        ty: "i64".into(),
                        optional: false,
                    }],
                    children: vec![],
                },
                VariantSchema {
                    name: "Add".into(),
                    fields: vec![],
                    children: vec![
                        ChildSchema {
                            name: "lhs".into(),
                            multiplicity: Multiplicity::One,
                            value_shape: ChildValueShape::Recursive,
                            scalar_shorthands: vec![],
                            non_empty: false,
                        },
                        ChildSchema {
                            name: "rhs".into(),
                            multiplicity: Multiplicity::One,
                            value_shape: ChildValueShape::Recursive,
                            scalar_shorthands: vec![],
                            non_empty: false,
                        },
                    ],
                },
            ],
        }
    }

    // -- left recursion --

    #[test]
    fn direct_left_recursion_is_detected() {
        let ids = IdGen::new();
        // r <- r "a"
        let r = rule(
            &ids,
            "r",
            seq(&ids, vec![rule_ref(&ids, "r"), token(&ids, "a")]),
        );
        let g = Grammar::new(vec![r], "r");
        let diags = check_left_recursion(&g);
        assert_eq!(diags.len(), 1, "diags = {diags:?}");
        assert_eq!(diags[0].code, peg_codes::LEFT_RECURSION);
        assert!(diags[0].message.contains("`r`"), "{}", diags[0].message);
    }

    #[test]
    fn indirect_left_recursion_is_detected() {
        let ids = IdGen::new();
        // a <- b "x"; b <- a "y"
        let a = rule(
            &ids,
            "a",
            seq(&ids, vec![rule_ref(&ids, "b"), token(&ids, "x")]),
        );
        let b = rule(
            &ids,
            "b",
            seq(&ids, vec![rule_ref(&ids, "a"), token(&ids, "y")]),
        );
        let g = Grammar::new(vec![a, b], "a");
        let diags = check_left_recursion(&g);
        // Both rules should fire — each reaches itself via the other.
        assert_eq!(diags.len(), 2, "diags = {diags:?}");
        let messages: Vec<&str> = diags.iter().map(|d| d.message.as_str()).collect();
        assert!(messages.iter().any(|m| m.contains("`a`")));
        assert!(messages.iter().any(|m| m.contains("`b`")));
    }

    #[test]
    fn left_recursion_through_nullable_prefix_is_detected() {
        let ids = IdGen::new();
        // a <- b a; b <- ("y" / <empty>)
        // b is nullable — so `a`'s Seq falls through to the RuleRef(a) at
        // first position without consuming input.
        let b = rule(
            &ids,
            "b",
            choice(&ids, vec![token(&ids, "y"), seq(&ids, vec![])]),
        );
        let a = rule(
            &ids,
            "a",
            seq(&ids, vec![rule_ref(&ids, "b"), rule_ref(&ids, "a")]),
        );
        let g = Grammar::new(vec![a, b], "a");
        let diags = check_left_recursion(&g);
        assert!(
            diags
                .iter()
                .any(|d| d.code == peg_codes::LEFT_RECURSION && d.message.contains("`a`")),
            "expected `a` to be flagged, diags = {diags:?}"
        );
    }

    #[test]
    fn right_recursion_is_accepted() {
        let ids = IdGen::new();
        // a <- "x" a — legal PEG, consumes before recursing.
        let a = rule(
            &ids,
            "a",
            seq(&ids, vec![token(&ids, "x"), rule_ref(&ids, "a")]),
        );
        let g = Grammar::new(vec![a], "a");
        assert!(check_left_recursion(&g).is_empty());
    }

    #[test]
    fn layered_precedence_grammar_is_accepted() {
        // expr <- term ("+" term)*
        // term <- factor ("*" factor)*
        // factor <- "n"
        // Right-recursive-friendly layered form — no left recursion.
        let ids = IdGen::new();
        let factor = rule(&ids, "factor", node(&ids, "F", token(&ids, "n")));
        let term = rule(
            &ids,
            "term",
            seq(
                &ids,
                vec![
                    rule_ref(&ids, "factor"),
                    repeat(
                        &ids,
                        seq(&ids, vec![token(&ids, "*"), rule_ref(&ids, "factor")]),
                        0,
                        None,
                    ),
                ],
            ),
        );
        let expr = rule(
            &ids,
            "expr",
            seq(
                &ids,
                vec![
                    rule_ref(&ids, "term"),
                    repeat(
                        &ids,
                        seq(&ids, vec![token(&ids, "+"), rule_ref(&ids, "term")]),
                        0,
                        None,
                    ),
                ],
            ),
        );
        let g = Grammar::new(vec![expr, term, factor], "expr");
        assert!(check_left_recursion(&g).is_empty());
    }

    #[test]
    fn diagnostic_anchor_points_at_offending_rule() {
        let ids = IdGen::new();
        let r_peg = rule(
            &ids,
            "r",
            seq(&ids, vec![rule_ref(&ids, "r"), token(&ids, "a")]),
        );
        let r_id = peg_id(&r_peg);
        let g = Grammar::new(vec![r_peg], "r");
        let diags = check_left_recursion(&g);
        assert_eq!(diags.len(), 1);
        assert_eq!(diags[0].location, Location::Node(r_id));
        // Sanity-check we can round-trip: the Peg with that id is the
        // Rule node itself, not one of its descendants.
        assert!(matches!(find_by_id(&g, r_id).unwrap(), Peg::Rule { .. }));
    }

    // -- nullable repeat --

    #[test]
    fn nullable_repeat_empty_body_is_detected() {
        let ids = IdGen::new();
        // Repeat { body = Seq [] } — always matches empty.
        let rep = repeat(&ids, seq(&ids, vec![]), 0, None);
        let rep_id = peg_id(&rep);
        let r = rule(&ids, "s", node(&ids, "N", rep));
        let g = Grammar::new(vec![r], "s");
        let diags = check_nullable_repeat(&g);
        assert_eq!(diags.len(), 1);
        assert_eq!(diags[0].code, peg_codes::NULLABLE_REPEAT);
        assert_eq!(diags[0].location, Location::Node(rep_id));
    }

    #[test]
    fn nullable_repeat_choice_with_empty_alt_is_detected() {
        let ids = IdGen::new();
        // Repeat { body = Choice("a" | <empty>) } — Choice is nullable.
        let body = choice(&ids, vec![token(&ids, "a"), seq(&ids, vec![])]);
        let rep = repeat(&ids, body, 0, None);
        let r = rule(&ids, "s", node(&ids, "N", rep));
        let g = Grammar::new(vec![r], "s");
        let diags = check_nullable_repeat(&g);
        assert_eq!(diags.len(), 1);
        assert_eq!(diags[0].code, peg_codes::NULLABLE_REPEAT);
    }

    #[test]
    fn nullable_keyed_entry_repeat_body_is_detected() {
        let ids = IdGen::new();
        // Both halves of the entry nullable → the entry is nullable →
        // repeating it would spin without consuming. The generated
        // grammars never produce this shape, so only a hand-built one
        // exercises the `KeyedEntry` arm of `is_nullable` in the
        // direction that raises a diagnostic.
        let entry = keyed_entry(&ids, "entries", seq(&ids, vec![]), seq(&ids, vec![]));
        let rep = repeat(&ids, entry, 0, None);
        let rep_id = peg_id(&rep);
        let r = rule(&ids, "s", node(&ids, "N", rep));
        let g = Grammar::new(vec![r], "s");
        let diags = check_nullable_repeat(&g);
        assert_eq!(diags.len(), 1);
        assert_eq!(diags[0].code, peg_codes::NULLABLE_REPEAT);
        assert_eq!(diags[0].location, Location::Node(rep_id));
    }

    #[test]
    fn keyed_entry_with_a_consuming_half_is_not_nullable() {
        let ids = IdGen::new();
        // Only the key consumes; that is enough to make the entry a
        // safe repeat body. Pins the `&&` in the `KeyedEntry` arm —
        // an `||` there would flag this grammar as broken.
        let entry = keyed_entry(&ids, "entries", token(&ids, "%ident"), seq(&ids, vec![]));
        let rep = repeat(&ids, entry, 0, None);
        let r = rule(&ids, "s", node(&ids, "N", rep));
        let g = Grammar::new(vec![r], "s");
        assert!(check_nullable_repeat(&g).is_empty());
    }

    #[test]
    fn left_recursion_through_a_keyed_entry_key_is_detected() {
        let ids = IdGen::new();
        // `s` reaches itself at first position through the entry's key
        // half, so the recursion is real and `collect_first` has to see
        // through `KeyedEntry` to catch it.
        let entry = keyed_entry(
            &ids,
            "entries",
            rule_ref(&ids, "s"),
            node(&ids, "N", token(&ids, "x")),
        );
        let r = rule(&ids, "s", entry);
        let g = Grammar::new(vec![r], "s");
        let diags = check_left_recursion(&g);
        assert_eq!(diags.len(), 1);
        assert_eq!(diags[0].code, peg_codes::LEFT_RECURSION);
    }

    #[test]
    fn consuming_repeat_body_is_accepted() {
        let ids = IdGen::new();
        // Repeat { body = "a" } — Token is not nullable.
        let rep = repeat(&ids, token(&ids, "a"), 0, None);
        let r = rule(&ids, "s", node(&ids, "N", rep));
        let g = Grammar::new(vec![r], "s");
        assert!(check_nullable_repeat(&g).is_empty());
    }

    #[test]
    fn nullable_repeat_via_nullable_rule_is_detected() {
        let ids = IdGen::new();
        // Repeat { body = RuleRef(b) } where b is nullable.
        let b = rule(&ids, "b", seq(&ids, vec![]));
        let rep = repeat(&ids, rule_ref(&ids, "b"), 0, None);
        let s = rule(&ids, "s", node(&ids, "N", rep));
        let g = Grammar::new(vec![s, b], "s");
        let diags = check_nullable_repeat(&g);
        assert_eq!(diags.len(), 1);
        assert_eq!(diags[0].code, peg_codes::NULLABLE_REPEAT);
    }

    // -- schema consistency --

    #[test]
    fn unknown_grammar_variant_is_error() {
        let ids = IdGen::new();
        // Grammar produces "Xyz" — not in schema.
        let r = rule(&ids, "s", node(&ids, "Xyz", token(&ids, "a")));
        let g = Grammar::new(vec![r], "s");
        let diags = check_schema_consistency(&g, &schema_expr());
        let errs: Vec<&Diagnostic> = diags
            .iter()
            .filter(|d| d.code == codes::UNKNOWN_VARIANT)
            .collect();
        assert_eq!(errs.len(), 1, "diags = {diags:?}");
        assert!(errs[0].message.contains("`Xyz`"));
        assert!(errs[0].message.contains("`Expr`"));
    }

    #[test]
    fn unknown_grammar_variant_suggests_declared_variant() {
        // A typo of a declared schema variant should surface the
        // correct name via the crate's built-in Levenshtein
        // suggester.
        let ids = IdGen::new();
        let r = rule(&ids, "s", node(&ids, "Aad", token(&ids, "a")));
        let g = Grammar::new(vec![r], "s");
        let diags = check_schema_consistency(&g, &schema_expr());
        let err = diags
            .iter()
            .find(|d| d.code == codes::UNKNOWN_VARIANT)
            .expect("expected UNKNOWN_VARIANT");
        assert!(
            err.message.contains("did you mean") && err.message.contains("Add"),
            "expected `Add` in the hint, got: {}",
            err.message
        );
    }

    #[test]
    fn unreachable_schema_variant_is_warning() {
        let ids = IdGen::new();
        // Grammar produces only Lit — Add is unreached.
        let r = rule(
            &ids,
            "s",
            node(&ids, "Lit", field(&ids, "value", token(&ids, "%int"))),
        );
        let g = Grammar::new(vec![r], "s");
        let diags = check_schema_consistency(&g, &schema_expr());
        let warns: Vec<&Diagnostic> = diags
            .iter()
            .filter(|d| d.code == codes::UNREACHABLE_VARIANT)
            .collect();
        assert_eq!(warns.len(), 1, "diags = {diags:?}");
        assert_eq!(warns[0].severity, Severity::Warning);
        assert!(warns[0].message.contains("`Add`"));
    }

    #[test]
    fn schema_matched_grammar_has_no_diagnostics() {
        let ids = IdGen::new();
        // Grammar covers Lit and Add.
        let lit = rule(
            &ids,
            "lit",
            node(&ids, "Lit", field(&ids, "value", token(&ids, "%int"))),
        );
        let add = rule(
            &ids,
            "add",
            node(
                &ids,
                "Add",
                seq(
                    &ids,
                    vec![
                        field(&ids, "lhs", rule_ref(&ids, "lit")),
                        token(&ids, "+"),
                        field(&ids, "rhs", rule_ref(&ids, "lit")),
                    ],
                ),
            ),
        );
        let g = Grammar::new(vec![add, lit], "add");
        assert!(check_schema_consistency(&g, &schema_expr()).is_empty());
    }

    #[test]
    fn unknown_variant_anchors_at_the_node_peg() {
        let ids = IdGen::new();
        let n_peg = node(&ids, "Xyz", token(&ids, "a"));
        let n_id = peg_id(&n_peg);
        let r = rule(&ids, "s", n_peg);
        let g = Grammar::new(vec![r], "s");
        let diags = check_schema_consistency(&g, &schema_expr());
        let err = diags
            .iter()
            .find(|d| d.code == codes::UNKNOWN_VARIANT)
            .expect("expected unknown_variant");
        assert_eq!(err.location, Location::Node(n_id));
    }

    // -- combined --

    #[test]
    fn check_accumulates_across_rules_but_omits_schema() {
        let ids = IdGen::new();
        // Two independent issues: left recursion + nullable repeat.
        let rec = rule(&ids, "rec", rule_ref(&ids, "rec"));
        let nullrep = rule(
            &ids,
            "nullrep",
            node(&ids, "N", repeat(&ids, seq(&ids, vec![]), 0, None)),
        );
        let g = Grammar::new(vec![rec, nullrep], "rec");
        let diags = check(&g);
        assert!(diags.iter().any(|d| d.code == peg_codes::LEFT_RECURSION));
        assert!(diags.iter().any(|d| d.code == peg_codes::NULLABLE_REPEAT));
        // check(&g) never touches the schema — no UNKNOWN_VARIANT even
        // though "N" is not in any schema.
        assert!(!diags.iter().any(|d| d.code == codes::UNKNOWN_VARIANT));
        assert!(!diags.iter().any(|d| d.code == codes::UNREACHABLE_VARIANT));
    }

    #[test]
    fn check_against_composes_grammar_and_schema_diagnostics() {
        let ids = IdGen::new();
        // Grammar has a left-recursive rule AND produces an unknown
        // variant. `check_against` returns both classes at once.
        let rec = rule(&ids, "rec", node(&ids, "Xyz", rule_ref(&ids, "rec")));
        let g = Grammar::new(vec![rec], "rec");
        let diags = check_against(&g, &schema_expr());
        assert!(diags.iter().any(|d| d.code == peg_codes::LEFT_RECURSION));
        assert!(diags.iter().any(|d| d.code == codes::UNKNOWN_VARIANT));
        // Both schema variants (Lit, Add) are unreached.
        let unreached: Vec<&Diagnostic> = diags
            .iter()
            .filter(|d| d.code == codes::UNREACHABLE_VARIANT)
            .collect();
        assert_eq!(unreached.len(), 2);
    }

    #[test]
    fn clean_grammar_produces_no_diagnostics() {
        // The layered-precedence grammar wrapped in a schema-shaped
        // Node — no issues.
        let ids = IdGen::new();
        let lit = rule(
            &ids,
            "lit",
            node(&ids, "Lit", field(&ids, "value", token(&ids, "%int"))),
        );
        let g = Grammar::new(vec![lit], "lit");
        let diags = check_against(&g, &{
            NodeSchema {
                name: "Tiny".into(),
                variants: vec![VariantSchema {
                    name: "Lit".into(),
                    fields: vec![FieldSchema {
                        name: "value".into(),
                        ty: "i64".into(),
                        optional: false,
                    }],
                    children: vec![],
                }],
            }
        });
        assert!(diags.is_empty(), "diags = {diags:?}");
    }
}