praxis-input-parser 0.1.0

The Praxis `read` DSL: template parsing, type synthesis, and parser plans.
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
//! Static validation of a parser AST (§7.9 step 2).
//!
//! Catches structural errors before type synthesis / plan construction: mixed
//! named/anonymous captures in one template (§7.3), duplicate capture names, and
//! wrong constructor arity.
//!
//! Returns lightweight [`ValidationError`]s carrying the source [`Span`] (byte
//! offsets). The HIR layer, which knows the [`FileId`], converts these into full
//! [`Diagnostic`]s with the `I0xx` (input-parser) category.

use crate::ast::{ArgShape, Constructor, ParserAst, TemplatePart};
use praxis_source::{DiagCode, Span};

/// A structural error found while validating a parser AST.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ValidationError {
    /// The byte span of the offending node.
    pub span: Span,
    /// Which diagnostic this is.
    pub code: DiagCode,
    /// A human-readable explanation.
    pub message: String,
}

/// Validate a parser AST. Returns the list of errors (empty on success).
pub fn validate(ast: &ParserAst) -> Vec<ValidationError> {
    let mut errs = Vec::new();
    validate_node(ast, &mut errs);
    errs
}

fn validate_node(ast: &ParserAst, errs: &mut Vec<ValidationError>) {
    match ast {
        ParserAst::Atomic { .. } => {}
        ParserAst::Template { parts, span } => {
            validate_template(parts, *span, errs);
        }
        // Every constructor whose only structure is its one child. The extra
        // payload some of them carry is not this pass's to check, and is
        // already answered where the node is *built*: `sep`'s separator by
        // `Separator::new`, the type's only constructor, which refuses the `""`
        // that never advances a cursor; `chars`'s skip policy and ragged
        // `grid`'s fill by `build_call`. A check `validate` performs is one the
        // next construction site can forget.
        ParserAst::Lines { child, .. }
        | ParserAst::Sections { child, .. }
        | ParserAst::Csv { child, .. }
        | ParserAst::Ws { child, .. }
        | ParserAst::Grid { child, .. }
        | ParserAst::Sep { child, .. }
        | ParserAst::Optional { child, .. }
        | ParserAst::Scan { child, .. }
        | ParserAst::Characters { child, .. }
        | ParserAst::Matrix { child, .. }
        | ParserAst::GridRagged { child, .. } => {
            validate_node(child, errs);
        }
        ParserAst::SectionsNamed {
            fields,
            repeated_tail,
            span,
        } => {
            // At least one named field is required (§7.5: a named sections call
            // with zero fields is malformed). The test is `fields`, not "no
            // named argument at all": `sections(boards: repeated(P))` is a
            // greedy tail and nothing else, which is spelled `sections(P)` and
            // is still refused here. A *counted* group is an ordinary field —
            // it is in `fields` — so `sections(shapes: repeated(P, 6))` alone
            // is legal, which is the point of it being bounded.
            if fields.is_empty() {
                errs.push(ValidationError {
                    span: *span,
                    code: DiagCode::EmptyFieldList,
                    message: "named `sections` requires at least one field".to_string(),
                });
            }
            // Field names must be unique.
            let mut seen = Vec::new();
            for item in fields {
                let name = item.name();
                if seen.iter().any(|s: &String| s == name) {
                    errs.push(ValidationError {
                        span: *span,
                        code: DiagCode::DuplicateSectionField,
                        message: format!("duplicate section field `{name}`"),
                    });
                }
                seen.push(name.to_string());
                validate_node(item.parser(), errs);
            }
            // The tail is a field of the generated record too, so its name
            // shares the uniqueness check: `sections(items: lines(int), items:
            // repeated(int))` would otherwise synthesize a record with two
            // fields called `items`.
            if let Some((name, tail)) = repeated_tail {
                if seen.contains(name) {
                    errs.push(ValidationError {
                        span: *span,
                        code: DiagCode::DuplicateSectionField,
                        message: format!("duplicate section field `{name}`"),
                    });
                }
                seen.push(name.clone());
                validate_node(tail, errs);
            }
        }
        ParserAst::Block { items, span } => {
            // §7.5: a positional parser returning a scalar must be explicitly
            // named to avoid an unclear field name. A positional template with
            // named captures flattens its captures into the block record. All
            // flattened field names must be unique.
            let mut seen: Vec<String> = Vec::new();
            for item in items {
                match item {
                    crate::ast::BlockItem::Positional(p) => {
                        // Collect the field names this positional contributes.
                        let contributed = block_positional_field_names(p);
                        // §7.5: a positional parser returning a *scalar* must be
                        // explicitly named (unclear field name). A template —
                        // even one with no captures (a pure literal match) — is
                        // fine: a no-capture template contributes no fields but
                        // legitimately consumes input; a named-capture template
                        // flattens its fields.
                        let is_template = matches!(p, ParserAst::Template { .. });
                        if contributed.is_empty() && !is_template {
                            errs.push(ValidationError {
                                span: *span,
                                code: DiagCode::UnnamedScalarBlockItem,
                                message:
                                    "a positional `block` item returning a scalar must be named"
                                        .to_string(),
                            });
                        }
                        for n in &contributed {
                            if seen.contains(n) {
                                errs.push(ValidationError {
                                    span: *span,
                                    code: DiagCode::DuplicateSectionField,
                                    message: format!("duplicate block field `{n}`"),
                                });
                            }
                            seen.push(n.clone());
                        }
                        validate_node(p, errs);
                    }
                    crate::ast::BlockItem::Named { name, parser } => {
                        if seen.contains(name) {
                            errs.push(ValidationError {
                                span: *span,
                                code: DiagCode::DuplicateSectionField,
                                message: format!("duplicate block field `{name}`"),
                            });
                        }
                        seen.push(name.clone());
                        validate_node(parser, errs);
                    }
                }
            }
        }
        ParserAst::Choice { cases, span } => {
            // §7.5: at least one case; unique case names; recurse.
            if cases.is_empty() {
                errs.push(ValidationError {
                    span: *span,
                    code: DiagCode::EmptyFieldList,
                    message: "`choice` requires at least one case".to_string(),
                });
            }
            let mut seen = Vec::new();
            for (name, parser) in cases {
                if seen.contains(name) {
                    errs.push(ValidationError {
                        span: *span,
                        code: DiagCode::DuplicateChoiceCase,
                        message: format!("duplicate choice case `{name}`"),
                    });
                }
                seen.push(name.clone());
                validate_node(parser, errs);
            }
        }
        ParserAst::OneOf { .. } => {}
    }
}

/// The field names a positional `block` item contributes via flattening (§7.5).
/// A named-capture template contributes its capture names; anything else (a
/// scalar atomic, a constructor) contributes nothing — which means a bare
/// scalar positional must be explicitly named (validation rejects it).
fn block_positional_field_names(p: &ParserAst) -> Vec<String> {
    match p {
        ParserAst::Template { parts, .. } => parts
            .iter()
            .filter_map(|part| match part {
                TemplatePart::Capture { name: Some(n), .. } => Some(n.as_str().to_string()),
                _ => None,
            })
            .collect(),
        _ => Vec::new(),
    }
}

/// Validate a template: no mixing named and anonymous captures (§7.3), and no
/// duplicate capture names.
fn validate_template(parts: &[TemplatePart], span: Span, errs: &mut Vec<ValidationError>) {
    let mut has_named = false;
    let mut has_anonymous = false;
    for part in parts {
        if let TemplatePart::Capture { name, parser, .. } = part {
            if name.is_some() {
                has_named = true;
            } else {
                has_anonymous = true;
            }
            validate_node(parser, errs);
        }
    }
    if has_named && has_anonymous {
        errs.push(ValidationError {
            span,
            code: DiagCode::MixedCaptureNaming,
            message: "named and anonymous captures may not be mixed in one template".to_string(),
        });
    }

    // Named captures must have unique names within a template.
    let mut seen_names = Vec::new();
    for part in parts {
        if let TemplatePart::Capture { name: Some(n), .. } = part {
            if seen_names.contains(n) {
                errs.push(ValidationError {
                    span,
                    code: DiagCode::DuplicateCaptureName,
                    message: format!("duplicate capture name `{n}` in template"),
                });
            }
            seen_names.push(n.clone());
        }
    }
}

/// What one argument of a constructor call *is*, with no payload — enough to
/// decide whether the call has the shape §7.5 gives it.
///
/// The payloads live in `praxis-hir`'s `CallArg` (they hold a `ParserAst` and
/// the rowan text). This is the projection both callers can share: the HIR
/// bridge and the capture-body parser in [`crate::body`] check the same table,
/// so the two grammars cannot drift.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum ArgKind {
    /// A positional parser expression.
    Parser,
    /// A positional string literal.
    String,
    /// A positional whole-number literal — the count of `repeated(P, N)`.
    Int,
    /// A bare keyword flag, e.g. the `ragged` of `grid(P, ragged, fill: 0)`.
    Flag(String),
    /// A named argument `name: parser` — the value is a parser expression.
    Named(String),
    /// A named argument `name: keyword` whose value is a **keyword, not a
    /// parser**: `chars`'s `skip:`, `grid`'s `fill:`.
    ///
    /// Distinct from [`ArgKind::Named`] because [`check_call`] must tell the
    /// two apart: only the constructors §7.5 gives a keyword argument accept
    /// one, and `block`, `choice` and named `sections` must refuse it.
    Keyword(String),
    /// A named `name: repeated(P)` tail.
    RepeatedTail(String),
}

impl ArgKind {
    pub(crate) fn describe(&self) -> String {
        match self {
            ArgKind::Parser => "a parser".to_string(),
            ArgKind::String => "a string literal".to_string(),
            ArgKind::Int => "a whole-number literal".to_string(),
            ArgKind::Flag(f) => format!("the flag `{f}`"),
            ArgKind::Named(n) => format!("the named argument `{n}:`"),
            ArgKind::Keyword(n) => format!("the keyword argument `{n}:`"),
            ArgKind::RepeatedTail(n) => format!("the repeated tail `{n}:`"),
        }
    }
}

/// Check a constructor call against §7.5's shape for that constructor —
/// **before anything is built**.
///
/// Returns every problem found; an empty vector means the argument list has
/// exactly the shape the constructor's builder expects, so the builder has
/// nothing left to drop. Each constructor gets its own [`ArgShape`] arm, and
/// each argument is checked *in place* rather than only counted: `choice(int)`
/// (a positional in a named-only constructor) and `sep(int, int)` (a parser
/// where a separator belongs) both have the right arity and the wrong
/// arguments.
pub fn check_call(ctor: Constructor, args: &[ArgKind], span: Span) -> Vec<ValidationError> {
    let mut errs = Vec::new();
    let name = ctor.keyword();
    let arity = |errs: &mut Vec<ValidationError>, expected: &str, actual: usize| {
        errs.push(ValidationError {
            span,
            code: DiagCode::ConstructorArity,
            message: format!("`{name}` expects {expected}, got {actual}"),
        });
    };
    let bad_arg = |errs: &mut Vec<ValidationError>, at: usize, arg: &ArgKind, wanted: &str| {
        errs.push(ValidationError {
            span,
            code: DiagCode::InvalidConstructorArgument,
            message: format!(
                "`{name}` argument {} is {}, but {wanted}",
                at + 1,
                arg.describe()
            ),
        });
    };

    match ctor.arg_shape() {
        ArgShape::Positional(n) => {
            if args.len() != n {
                arity(
                    &mut errs,
                    &format!("{n} argument{}", if n == 1 { "" } else { "s" }),
                    args.len(),
                );
            }
            for (i, a) in args.iter().enumerate() {
                if *a != ArgKind::Parser {
                    bad_arg(&mut errs, i, a, "every argument must be a parser");
                }
            }
        }
        ArgShape::StringThenParser => {
            if args.len() != 2 {
                arity(&mut errs, "2 arguments", args.len());
            }
            for (i, a) in args.iter().enumerate() {
                let wanted = if i == 0 {
                    ("the separator must be a string literal", ArgKind::String)
                } else {
                    ("the element parser must be a parser", ArgKind::Parser)
                };
                if *a != wanted.1 {
                    bad_arg(&mut errs, i, a, wanted.0);
                }
            }
        }
        ArgShape::OneString => {
            if args.len() != 1 {
                arity(&mut errs, "1 argument", args.len());
            }
            for (i, a) in args.iter().enumerate() {
                if *a != ArgKind::String {
                    bad_arg(
                        &mut errs,
                        i,
                        a,
                        "the character set must be a string literal",
                    );
                }
            }
        }
        ArgShape::ParserWithSkip => {
            match args.first() {
                Some(ArgKind::Parser) => {}
                Some(other) => bad_arg(&mut errs, 0, other, "the first argument must be a parser"),
                None => arity(&mut errs, "1 or 2 arguments", 0),
            }
            for (i, a) in args.iter().enumerate().skip(1) {
                match a {
                    // The keyword is the constructor's (`keyword_arg`), not a
                    // name this table repeats: one table entry, one spelling.
                    ArgKind::Keyword(n) if Some(n.as_str()) == ctor.keyword_arg() && i == 1 => {}
                    other => bad_arg(&mut errs, i, other, "only `skip:` may follow the parser"),
                }
            }
            if args.len() > 2 {
                arity(&mut errs, "1 or 2 arguments", args.len());
            }
        }
        ArgShape::ParserWithOptionalCount => {
            // The arity check comes **first**, unlike the arms above, because
            // one caller reads only the first error: the capture-body scanner
            // turns a shape failure into a single `ScanError::CallShape`. A
            // wrong number of arguments is the more useful of the two things a
            // three-argument `repeated` is wrong about, and it is what the
            // rowan front end reports for it.
            if args.is_empty() || args.len() > 2 {
                arity(&mut errs, "1 or 2 arguments", args.len());
            }
            match args.first() {
                // An empty list is the arity error above and nothing else.
                None | Some(ArgKind::Parser) => {}
                Some(other) => {
                    bad_arg(&mut errs, 0, other, "the repeated parser must be a parser");
                }
            }
            match args.get(1) {
                None | Some(ArgKind::Int) => {}
                // The reason is in the message because there is no other
                // spelling that would have worked: the parser plan is built
                // when the program is compiled, so a count read from a value
                // cannot exist.
                Some(other) => bad_arg(
                    &mut errs,
                    1,
                    other,
                    "the count must be a whole-number literal — the parser plan is built when \
                     the program is compiled, so the count cannot be a parser or a variable",
                ),
            }
        }
        ArgShape::GridMaybeRagged => {
            match args.first() {
                Some(ArgKind::Parser) => {}
                Some(other) => {
                    bad_arg(&mut errs, 0, other, "the cell parser must be a parser");
                }
                None => arity(&mut errs, "1 or 3 arguments", 0),
            }
            let mut ragged = false;
            let mut fill = false;
            for (i, a) in args.iter().enumerate().skip(1) {
                match a {
                    // Both names come from the constructor — `flag_arg` for
                    // `ragged`, `keyword_arg` for `fill:` — so this table and
                    // the two front ends that mint the arguments read one
                    // spelling between them.
                    ArgKind::Flag(f) if Some(f.as_str()) == ctor.flag_arg() && !ragged => {
                        ragged = true
                    }
                    ArgKind::Keyword(n) if Some(n.as_str()) == ctor.keyword_arg() && !fill => {
                        fill = true
                    }
                    other => bad_arg(
                        &mut errs,
                        i,
                        other,
                        "only `ragged` and `fill:` may follow the cell parser",
                    ),
                }
            }
            // §7.5 spells them together: `grid(P, ragged, fill: value)`.
            // Accepting a `fill:` with no `ragged` would silently build the
            // ragged form, a different parser than the one written.
            if ragged != fill {
                errs.push(ValidationError {
                    span,
                    code: DiagCode::InvalidConstructorArgument,
                    message:
                        "`grid`'s ragged form is written `grid(P, ragged, fill: value)` — `ragged` \
                         and `fill:` come together or not at all"
                            .to_string(),
                });
            }
        }
        ArgShape::OnePositionalOrNamed => {
            let named = args
                .iter()
                .filter(|a| matches!(a, ArgKind::Named(_) | ArgKind::RepeatedTail(_)))
                .count();
            if named == 0 {
                // Homogeneous `sections(P)`.
                if args.len() != 1 {
                    arity(&mut errs, "1 argument, or named sections", args.len());
                }
                for (i, a) in args.iter().enumerate() {
                    if *a != ArgKind::Parser {
                        bad_arg(&mut errs, i, a, "the section parser must be a parser");
                    }
                }
            } else {
                // Heterogeneous `sections(name: P, …)`: named arguments only,
                // and every one of them names a *parser*. `sections` has no
                // keyword argument (`Constructor::keyword_arg`), so a keyword
                // reaching here is one no constructor asked for.
                for (i, a) in args.iter().enumerate() {
                    if !matches!(a, ArgKind::Named(_) | ArgKind::RepeatedTail(_)) {
                        bad_arg(
                            &mut errs,
                            i,
                            a,
                            "a named `sections` takes only named arguments",
                        );
                    }
                }
            }
        }
        ArgShape::Items => {
            if args.is_empty() {
                arity(&mut errs, "at least 1 item", 0);
            }
            for (i, a) in args.iter().enumerate() {
                if !matches!(a, ArgKind::Parser | ArgKind::Named(_)) {
                    bad_arg(
                        &mut errs,
                        i,
                        a,
                        "a `block` item is a parser or a named parser",
                    );
                }
            }
        }
        ArgShape::NamedOnly { at_least } => {
            let named = args
                .iter()
                .filter(|a| matches!(a, ArgKind::Named(_)))
                .count();
            if named < at_least {
                arity(
                    &mut errs,
                    &format!("at least {at_least} named argument(s)"),
                    named,
                );
            }
            for (i, a) in args.iter().enumerate() {
                if !matches!(a, ArgKind::Named(_)) {
                    bad_arg(&mut errs, i, a, "every argument must be `Name: parser`");
                }
            }
        }
    }
    errs
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::ast::{
        AtomicKind, CaptureName, EmptySeparator, RepeatCount, SectionItem, Separator,
    };
    use praxis_source::{DiagCode, Span};

    fn atom() -> ParserAst {
        ParserAst::Atomic {
            kind: AtomicKind::Int,
            span: Span::at(0),
        }
    }

    #[test]
    fn clean_tree_validates() {
        let ast = ParserAst::Lines {
            child: Box::new(atom()),
            span: Span::at(0),
        };
        assert!(validate(&ast).is_empty());
    }

    #[test]
    fn mixed_captures_rejected() {
        let ast = ParserAst::Template {
            parts: vec![
                TemplatePart::Capture {
                    name: Some(CaptureName::parse("x").expect("an identifier")),
                    parser: Box::new(atom()),
                    span: Span::at(0),
                    name_span: None,
                },
                TemplatePart::Capture {
                    name: None,
                    parser: Box::new(atom()),
                    span: Span::at(0),
                    name_span: None,
                },
            ],
            span: Span::at(0),
        };
        let errs = validate(&ast);
        assert_eq!(errs.len(), 1);
        assert_eq!(errs[0].code, DiagCode::MixedCaptureNaming);
    }

    #[test]
    fn duplicate_named_capture_rejected() {
        let ast = ParserAst::Template {
            parts: vec![
                TemplatePart::Capture {
                    name: Some(CaptureName::parse("x").expect("an identifier")),
                    parser: Box::new(atom()),
                    span: Span::at(0),
                    name_span: None,
                },
                TemplatePart::Capture {
                    name: Some(CaptureName::parse("x").expect("an identifier")),
                    parser: Box::new(atom()),
                    span: Span::at(0),
                    name_span: None,
                },
            ],
            span: Span::at(0),
        };
        let errs = validate(&ast);
        assert_eq!(errs.len(), 1);
        assert_eq!(errs[0].code, DiagCode::DuplicateCaptureName);
    }

    #[test]
    fn arity_mismatch_reported() {
        let errs = check_call(Constructor::Sep, &[ArgKind::String], Span::at(0));
        assert!(!errs.is_empty());
        assert_eq!(errs[0].code, DiagCode::ConstructorArity);
    }

    /// **A keyword argument is not a named parser.**
    ///
    /// `CallArg::Keyword{name}` and `CallArg::Named{name}` project onto
    /// distinct `ArgKind`s so [`check_call`] can tell them apart: only the
    /// constructors §7.5 gives a keyword argument accept one, and `block`,
    /// `choice` and named `sections` refuse it rather than having their
    /// builders `filter_map` it away.
    ///
    /// The last two assertions are the ones that make this a test of the
    /// *distinction* rather than of a blanket refusal: the same position,
    /// holding a named parser, is still accepted.
    #[test]
    fn a_keyword_argument_is_accepted_only_where_the_shape_has_one() {
        let kw = |n: &str| ArgKind::Keyword(n.to_string());
        let named = |n: &str| ArgKind::Named(n.to_string());

        // The two constructors §7.5 gives a keyword argument.
        assert!(
            check_call(
                Constructor::Chars,
                &[ArgKind::Parser, kw("skip")],
                Span::at(0)
            )
            .is_empty()
        );
        assert!(
            check_call(
                Constructor::Grid,
                &[
                    ArgKind::Parser,
                    ArgKind::Flag("ragged".to_string()),
                    kw("fill")
                ],
                Span::at(0)
            )
            .is_empty()
        );

        // Everywhere else — including the *other* constructor's keyword.
        for (ctor, args) in [
            (Constructor::Block, vec![ArgKind::Parser, kw("fill")]),
            (Constructor::Choice, vec![named("A"), kw("fill")]),
            (Constructor::Sections, vec![named("rules"), kw("fill")]),
            (Constructor::Lines, vec![kw("skip")]),
            (Constructor::Chars, vec![ArgKind::Parser, kw("fill")]),
        ] {
            let errs = check_call(ctor, &args, Span::at(0));
            assert!(
                errs.iter()
                    .any(|e| e.code == DiagCode::InvalidConstructorArgument),
                "`{}` must refuse a keyword it does not have",
                ctor.keyword()
            );
        }

        // The same shape with a named *parser* there is fine.
        assert!(
            check_call(
                Constructor::Block,
                &[ArgKind::Parser, named("fill")],
                Span::at(0)
            )
            .is_empty()
        );
        assert!(
            check_call(
                Constructor::Sections,
                &[named("rules"), named("fill")],
                Span::at(0)
            )
            .is_empty()
        );
    }

    /// **A keyword argument's *value* is part of its shape.**
    ///
    /// [`check_call`] answers from `ArgKind`s, which carry names and no values,
    /// so the shape table can never see this — the check belongs to the
    /// builder. `chars`'s `skip:` refuses a policy it does not recognize;
    /// `grid`'s `fill:` refuses an empty pad, the same rule one field over from
    /// `Separator::new` refusing `""`: an empty separator never advances, and
    /// an empty pad fills nothing.
    ///
    /// The builder is shared, so both front ends inherit whatever it decides —
    /// which is the point of asserting it here rather than at either one.
    #[test]
    fn a_keyword_argument_with_no_value_is_not_a_shape() {
        use crate::call::{CallArg, build_call};

        let grid = |fill: &str| {
            build_call(
                Constructor::Grid,
                vec![
                    CallArg::Parser(ParserAst::Atomic {
                        kind: crate::ast::AtomicKind::Char,
                        span: Span::at(0),
                    }),
                    CallArg::Flag("ragged".to_string()),
                    CallArg::Keyword {
                        name: "fill".to_string(),
                        value: fill.to_string(),
                    },
                ],
                Span::at(0),
            )
        };

        for empty in ["", "\"\""] {
            let errs = grid(empty).expect_err("an empty fill pads nothing");
            assert_eq!(errs[0].code, DiagCode::InvalidConstructorArgument);
        }

        // And the values that *are* values still build, decoded.
        for (written, decoded) in [("0", "0"), ("\"-\"", "-"), ("\" \"", " ")] {
            match grid(written).expect("a fill with a value") {
                ParserAst::GridRagged { fill, .. } => assert_eq!(fill, decoded),
                other => panic!("`fill: {written}` built a {other:?}"),
            }
        }
    }

    /// **The assertion is inverted on purpose.**
    ///
    /// An empty separator drives `walk_sep`'s
    /// `region[pos..].starts_with(sep_bytes)` loop, which is unconditionally
    /// true for an empty needle, so the cursor never advances and the loop
    /// allocates forever. A check `validate` performs is one the *next*
    /// construction site can forget, so the rule the name states is enforced by
    /// the type instead: `Separator` has exactly one constructor and it refuses
    /// `""`. The empty separator is not rejected before plan construction — it
    /// is not constructible at all.
    #[test]
    fn empty_separator_is_rejected_before_plan_construction() {
        assert_eq!(
            Separator::new(""),
            Err(EmptySeparator),
            "the one constructor refuses the separator that cannot advance"
        );

        let comma = Separator::new(",").expect("a one-character separator is fine");
        assert_eq!(comma.as_str(), ",");

        // The AST field is the newtype, not a `String`: this is what stops the
        // empty value from being reintroduced by a future construction site.
        let ast = ParserAst::Sep {
            separator: comma,
            child: Box::new(atom()),
            span: Span::at(0),
        };
        assert!(validate(&ast).is_empty(), "a real separator validates");
    }

    #[test]
    fn repeated_section_tail_cannot_reuse_a_fixed_field_name() {
        let ast = ParserAst::SectionsNamed {
            fields: vec![SectionItem::One {
                name: "items".to_string(),
                parser: atom(),
            }],
            repeated_tail: Some(("items".to_string(), Box::new(atom()))),
            span: Span::at(0),
        };

        let errors = validate(&ast);
        assert!(
            errors
                .iter()
                .any(|error| error.code == DiagCode::DuplicateSectionField),
            "the generated record cannot contain two fields named `items`"
        );
    }

    /// A counted group is a record field like any other, so it collides with
    /// the tail's name the same way a fixed field does. The duplicate check
    /// reads `SectionItem::name`, which is one answer for both variants — a
    /// check that only knew about `One` would let `Counted` past.
    #[test]
    fn a_counted_group_and_the_tail_cannot_share_a_name() {
        let ast = ParserAst::SectionsNamed {
            fields: vec![SectionItem::Counted {
                name: "shapes".to_string(),
                count: RepeatCount::new(2).expect("two sections"),
                parser: atom(),
            }],
            repeated_tail: Some(("shapes".to_string(), Box::new(atom()))),
            span: Span::at(0),
        };

        let errors = validate(&ast);
        assert!(
            errors
                .iter()
                .any(|error| error.code == DiagCode::DuplicateSectionField),
            "a counted group's name is a field name too"
        );
    }

    /// **A counted group alone is a `sections` call; an unbounded tail alone is
    /// not.** `sections(boards: repeated(P))` is `sections(P)` written the long
    /// way round — every section, one parser — which is what I025 says. A
    /// counted group consumes a *known* prefix and leaves the rest unread, so
    /// it is a heterogeneous call with one field, and the same check must let
    /// it through. That is why the emptiness test is `fields`, which holds the
    /// counted item and not the tail.
    #[test]
    fn a_counted_group_alone_is_a_field_but_an_unbounded_tail_alone_is_not() {
        let counted_only = ParserAst::SectionsNamed {
            fields: vec![SectionItem::Counted {
                name: "shapes".to_string(),
                count: RepeatCount::new(2).expect("two sections"),
                parser: atom(),
            }],
            repeated_tail: None,
            span: Span::at(0),
        };
        assert!(validate(&counted_only).is_empty());

        let tail_only = ParserAst::SectionsNamed {
            fields: Vec::new(),
            repeated_tail: Some(("boards".to_string(), Box::new(atom()))),
            span: Span::at(0),
        };
        assert!(
            validate(&tail_only)
                .iter()
                .any(|e| e.code == DiagCode::EmptyFieldList),
            "a greedy tail and nothing else is `sections(P)`"
        );
    }

    /// The shape table owns `repeated`'s arity, not the marker's builder —
    /// ADR-073's "check the shape before building" covers this call site like
    /// every other, and this is the table answering.
    #[test]
    fn repeated_takes_a_parser_and_an_optional_count() {
        let ok = |args: &[ArgKind]| check_call(Constructor::Repeated, args, Span::at(0));
        assert!(ok(&[ArgKind::Parser]).is_empty());
        assert!(ok(&[ArgKind::Parser, ArgKind::Int]).is_empty());

        // No parser at all, and a count where the parser belongs.
        assert!(ok(&[]).iter().any(|e| e.code == DiagCode::ConstructorArity));
        assert!(
            ok(&[ArgKind::Int])
                .iter()
                .any(|e| e.code == DiagCode::InvalidConstructorArgument)
        );
        // A second parser is not a count — this is the diagnostic a
        // non-literal `repeated(P, n)` earns.
        assert!(
            ok(&[ArgKind::Parser, ArgKind::Parser])
                .iter()
                .any(|e| e.code == DiagCode::InvalidConstructorArgument)
        );
        assert!(
            ok(&[ArgKind::Parser, ArgKind::Int, ArgKind::Int])
                .iter()
                .any(|e| e.code == DiagCode::ConstructorArity)
        );
    }
}