Skip to main content

dsl_kit_parse/
schema_gen.rs

1//! Schema-driven grammar generation (item N).
2//!
3//! The kit's identity axis is "write the AST in Rust, the toolchain
4//! supplies the rest". [`grammar_from_schema`] converts the type-level
5//! shape a `#[derive(DslSchema)]` already extracts into a working
6//! [`Grammar`] for a canonical text syntax — the DSL author writes two
7//! derives and gets an external-format parser for free, instead of
8//! hand-authoring a grammar (in Rust or, worse, in a Bison-style JSON
9//! blob nobody wants to write).
10//!
11//! # Canonical syntax
12//!
13//! One form per variant: the variant name as a keyword, followed by a
14//! parenthesised, comma-separated argument list. Arguments appear in
15//! schema order — payload fields first, then child slots — each spelled
16//! `name: value`:
17//!
18//! ```text
19//! Add(lhs: Lit(value: 1), rhs: Neg(body: none))
20//! List(items: [Lit(value: 1), Lit(value: 2)])
21//! Name(text: "hello world")
22//! Unit()
23//! ```
24//!
25//! - **`One` child** — `name: <node>`.
26//! - **`Optional` child** — `name: <node>` or `name: none`. (If a
27//!   variant is literally called `none`, PEG ordered choice still
28//!   parses `name: none(...)` as that variant; bare `none` means
29//!   absent.)
30//! - **`Many` child** — `name: [<node>, <node>, ...]`, empty list
31//!   allowed.
32//! - **Payload fields** — by Rust type source text:
33//!   `String` → a `%str` literal; the integer types → `%int`;
34//!   `bool` → `true` / `false`. Any other payload type fails
35//!   generation with [`codes::UNSUPPORTED_FIELD`] — loudly, per
36//!   variant, rather than silently dropping the variant from the
37//!   grammar — unless a [`SyntaxOverrides`] entry supplies its value
38//!   production.
39//!
40//! # Syntax overrides
41//!
42//! [`grammar_from_schema_with`] accepts a [`SyntaxOverrides`] carrying
43//! value productions for payload types the built-in mapping rejects
44//! (e.g. `Option<JoinPolicy>`), keyed by type source text or pinned to
45//! one `(variant, field)` site. The override supplies only the value
46//! [`Peg`] — the surrounding `name: value` argument shape stays
47//! canonical — and its matched text becomes the field's production, so
48//! the override author owns making that text consumable downstream
49//! (typically `FromStr` on the field type, via
50//! [`build_field`](crate::build_field)).
51//!
52//! # Guarantees
53//!
54//! The generated grammar is left-recursion-free by construction (every
55//! variant rule starts with its keyword token) and contains no nullable
56//! repeats; `GrammarCheck`'s `check_left_recursion` /
57//! `check_nullable_repeat` / `check_schema_consistency` all pass
58//! against it — the tests pin this. Trees produced by parsing satisfy
59//! [`check_conformance`](crate::check_conformance) against the source
60//! schema.
61
62use std::collections::HashMap;
63
64use crate::grammar_check;
65use crate::peg::{Grammar, Peg, choice, field, node, repeat, rule, rule_ref, seq, token};
66use crate::{BuildError, Diagnostic};
67use dsl_kit_core::IdGen;
68use dsl_kit_schema::{ChildSchema, FieldSchema, Multiplicity, NodeSchema, VariantSchema};
69
70/// Diagnostic codes emitted by grammar generation.
71pub mod codes {
72    /// A payload field's Rust type has no canonical-syntax mapping.
73    pub const UNSUPPORTED_FIELD: &str = "dsl_kit::schema_gen::unsupported_field";
74    /// The schema declares no variants — there is nothing to parse.
75    pub const EMPTY_SCHEMA: &str = "dsl_kit::schema_gen::empty_schema";
76}
77
78/// Name of the generated start rule.
79pub const START_RULE: &str = "node";
80
81/// A deferred value-production constructor: overrides are registered
82/// before the [`IdGen`] doing the generation exists, so each entry is a
83/// factory invoked with the generator's `ids` at build time.
84type PegFactory = Box<dyn Fn(&IdGen) -> Peg>;
85
86/// Value-production overrides for payload fields whose Rust type has no
87/// built-in canonical-syntax mapping (see the module docs).
88///
89/// Two granularities, resolved most-specific-first:
90///
91/// 1. **Per field** ([`Self::for_field`]) — pinned to one
92///    `(variant, field)` site.
93/// 2. **Per type** ([`Self::for_type`]) — keyed by the field's Rust
94///    type source text; whitespace is ignored, so
95///    `"Option<JoinPolicy>"` matches the derive-extracted
96///    `"Option < JoinPolicy >"` spelling.
97///
98/// A field with neither entry falls back to the built-in mapping, and
99/// failing that still fails generation loudly with
100/// [`codes::UNSUPPORTED_FIELD`].
101///
102/// The factory builds only the **value** production; the enclosing
103/// `name ':' <value>` argument shape (and the `Field` sink binding the
104/// matched text to the field name) stays canonical, which keeps the
105/// generated grammar's `GrammarCheck` guarantees intact.
106#[derive(Default)]
107pub struct SyntaxOverrides {
108    by_type: HashMap<String, PegFactory>,
109    by_field: HashMap<(String, String), PegFactory>,
110}
111
112impl SyntaxOverrides {
113    /// An empty override set (equivalent to the built-in mapping only).
114    pub fn new() -> Self {
115        Self::default()
116    }
117
118    /// Registers a value production for every payload field of Rust
119    /// type `ty` (source-text comparison, whitespace-insensitive).
120    pub fn for_type(
121        mut self,
122        ty: impl AsRef<str>,
123        value: impl Fn(&IdGen) -> Peg + 'static,
124    ) -> Self {
125        self.by_type.insert(strip_ws(ty.as_ref()), Box::new(value));
126        self
127    }
128
129    /// Registers a value production for the single field `field` of
130    /// variant `variant`, winning over any [`Self::for_type`] entry.
131    pub fn for_field(
132        mut self,
133        variant: impl Into<String>,
134        field: impl Into<String>,
135        value: impl Fn(&IdGen) -> Peg + 'static,
136    ) -> Self {
137        self.by_field
138            .insert((variant.into(), field.into()), Box::new(value));
139        self
140    }
141
142    /// Most-specific matching factory for `field` of `variant`, if any.
143    fn resolve(&self, variant: &str, field: &FieldSchema) -> Option<&PegFactory> {
144        self.by_field
145            .get(&(variant.to_string(), field.name.clone()))
146            .or_else(|| self.by_type.get(&strip_ws(&field.ty)))
147    }
148}
149
150/// Whitespace-insensitive type key: `Option < JoinPolicy >` (the
151/// token-stream spelling `#[derive(DslSchema)]` extracts) and
152/// `Option<JoinPolicy>` (what an author writes) must compare equal.
153fn strip_ws(ty: &str) -> String {
154    ty.chars().filter(|c| !c.is_whitespace()).collect()
155}
156
157impl Grammar {
158    /// Generates the canonical-syntax grammar for `schema`. See
159    /// [`grammar_from_schema`].
160    pub fn from_schema(schema: &NodeSchema, ids: &IdGen) -> Result<Grammar, BuildError> {
161        grammar_from_schema(schema, ids)
162    }
163}
164
165/// Generates a [`Grammar`] for the canonical named-argument syntax of
166/// `schema` (see the module docs for the syntax), built-in field
167/// mapping only.
168///
169/// Fails with one [`codes::UNSUPPORTED_FIELD`] diagnostic per
170/// unmappable payload field (collected across all variants, so the
171/// author sees the full list at once). To map such fields instead of
172/// failing, use [`grammar_from_schema_with`].
173pub fn grammar_from_schema(schema: &NodeSchema, ids: &IdGen) -> Result<Grammar, BuildError> {
174    grammar_from_schema_with(schema, ids, &SyntaxOverrides::default())
175}
176
177/// [`grammar_from_schema`] with [`SyntaxOverrides`] consulted before
178/// the built-in field mapping (per-field entries win over per-type
179/// entries win over built-ins).
180pub fn grammar_from_schema_with(
181    schema: &NodeSchema,
182    ids: &IdGen,
183    overrides: &SyntaxOverrides,
184) -> Result<Grammar, BuildError> {
185    if schema.variants.is_empty() {
186        return Err(BuildError::single(Diagnostic::error(
187            codes::EMPTY_SCHEMA,
188            format!("schema `{}` declares no variants", schema.name),
189        )));
190    }
191
192    let mut bad_fields = Vec::new();
193    for v in &schema.variants {
194        for f in &v.fields {
195            if resolved_field_value_peg(&v.name, f, ids, overrides).is_none() {
196                bad_fields.push(Diagnostic::error(
197                    codes::UNSUPPORTED_FIELD,
198                    format!(
199                        "variant `{}` field `{}`: type `{}` has no canonical-syntax \
200                         mapping (supported: String, bool, the integer types; or \
201                         register a `SyntaxOverrides` value production)",
202                        v.name, f.name, f.ty
203                    ),
204                ));
205            }
206        }
207    }
208    if !bad_fields.is_empty() {
209        return Err(BuildError::new(bad_fields));
210    }
211
212    let mut rules = Vec::with_capacity(schema.variants.len() + 1);
213    rules.push(rule(
214        ids,
215        START_RULE,
216        choice(
217            ids,
218            schema
219                .variants
220                .iter()
221                .map(|v| rule_ref(ids, v.name.clone()))
222                .collect(),
223        ),
224    ));
225    for v in &schema.variants {
226        rules.push(variant_rule(v, ids, overrides));
227    }
228    Ok(Grammar::new(rules, START_RULE))
229}
230
231/// Builds the rule for one variant:
232/// `Node { variant } [ %kw:V "(" arg ("," arg)* ")" ]` with arguments
233/// in schema order (fields, then children).
234fn variant_rule(v: &VariantSchema, ids: &IdGen, overrides: &SyntaxOverrides) -> Peg {
235    let mut args: Vec<Peg> = Vec::new();
236    for f in &v.fields {
237        let value = resolved_field_value_peg(&v.name, f, ids, overrides)
238            .expect("unsupported field types were rejected before rule generation");
239        args.push(seq(
240            ids,
241            vec![
242                token(ids, format!("%kw:{}", f.name)),
243                token(ids, ":"),
244                field(ids, f.name.clone(), value),
245            ],
246        ));
247    }
248    for c in &v.children {
249        args.push(child_arg_peg(c, ids));
250    }
251
252    let mut items = vec![token(ids, format!("%kw:{}", v.name)), token(ids, "(")];
253    for (i, arg) in args.into_iter().enumerate() {
254        if i > 0 {
255            items.push(token(ids, ","));
256        }
257        items.push(arg);
258    }
259    items.push(token(ids, ")"));
260
261    rule(
262        ids,
263        v.name.clone(),
264        node(ids, v.name.clone(), seq(ids, items)),
265    )
266}
267
268/// Value production for a payload field: the most specific
269/// [`SyntaxOverrides`] entry if registered, else the built-in mapping.
270fn resolved_field_value_peg(
271    variant: &str,
272    f: &FieldSchema,
273    ids: &IdGen,
274    overrides: &SyntaxOverrides,
275) -> Option<Peg> {
276    match overrides.resolve(variant, f) {
277        Some(factory) => Some(factory(ids)),
278        None => field_value_peg(f, ids),
279    }
280}
281
282/// Built-in value production for a payload field, by Rust type source
283/// text. `None` when the type has no canonical-syntax mapping.
284fn field_value_peg(f: &FieldSchema, ids: &IdGen) -> Option<Peg> {
285    const INT_TYPES: &[&str] = &[
286        "i8", "i16", "i32", "i64", "i128", "u8", "u16", "u32", "u64", "u128", "usize", "isize",
287    ];
288    if f.ty == "String" {
289        Some(token(ids, "%str"))
290    } else if INT_TYPES.contains(&f.ty.as_str()) {
291        Some(token(ids, "%int"))
292    } else if f.ty == "bool" {
293        Some(choice(
294            ids,
295            vec![token(ids, "%kw:true"), token(ids, "%kw:false")],
296        ))
297    } else {
298        None
299    }
300}
301
302/// Argument production for one child slot, by multiplicity.
303fn child_arg_peg(c: &ChildSchema, ids: &IdGen) -> Peg {
304    let name_kw = token(ids, format!("%kw:{}", c.name));
305    let colon = token(ids, ":");
306    match c.multiplicity {
307        Multiplicity::One => seq(
308            ids,
309            vec![
310                name_kw,
311                colon,
312                field(ids, c.name.clone(), rule_ref(ids, START_RULE)),
313            ],
314        ),
315        // `name: <node>` or `name: none`. The Field arm is tried first,
316        // so a variant literally named `none` still wins when followed
317        // by `(`. The bare-`none` arm sits *outside* any Field: its
318        // matched text lands on the enclosing Node sink where it is
319        // dropped as syntactic noise, leaving the slot absent.
320        Multiplicity::Optional => seq(
321            ids,
322            vec![
323                name_kw,
324                colon,
325                choice(
326                    ids,
327                    vec![
328                        field(ids, c.name.clone(), rule_ref(ids, START_RULE)),
329                        token(ids, "%kw:none"),
330                    ],
331                ),
332            ],
333        ),
334        // `name: [ <node> ("," <node>)* ]`, empty list allowed. The
335        // list commas live inside the Field body; the mixed-field
336        // "trees win" fallback drops them as noise.
337        Multiplicity::Many => {
338            let elems = seq(
339                ids,
340                vec![
341                    rule_ref(ids, START_RULE),
342                    repeat(
343                        ids,
344                        seq(ids, vec![token(ids, ","), rule_ref(ids, START_RULE)]),
345                        0,
346                        None,
347                    ),
348                ],
349            );
350            seq(
351                ids,
352                vec![
353                    name_kw,
354                    colon,
355                    token(ids, "["),
356                    field(ids, c.name.clone(), repeat(ids, elems, 0, Some(1))),
357                    token(ids, "]"),
358                ],
359            )
360        }
361    }
362}
363
364/// Convenience: generate and immediately assert the grammar is clean
365/// under `GrammarCheck` (left recursion, nullable repeats, schema
366/// consistency). Intended for tests and one-shot setup paths; returns
367/// the same grammar as [`grammar_from_schema`].
368pub fn checked_grammar_from_schema(
369    schema: &NodeSchema,
370    ids: &IdGen,
371) -> Result<Grammar, BuildError> {
372    checked_grammar_from_schema_with(schema, ids, &SyntaxOverrides::default())
373}
374
375/// [`checked_grammar_from_schema`] with [`SyntaxOverrides`] consulted
376/// before the built-in field mapping.
377pub fn checked_grammar_from_schema_with(
378    schema: &NodeSchema,
379    ids: &IdGen,
380    overrides: &SyntaxOverrides,
381) -> Result<Grammar, BuildError> {
382    let grammar = grammar_from_schema_with(schema, ids, overrides)?;
383    let mut diags = grammar_check::check_left_recursion(&grammar);
384    diags.extend(grammar_check::check_nullable_repeat(&grammar));
385    diags.extend(
386        grammar_check::check_schema_consistency(&grammar, schema)
387            .into_iter()
388            .filter(|d| d.severity == crate::Severity::Error),
389    );
390    if diags.is_empty() {
391        Ok(grammar)
392    } else {
393        Err(BuildError::new(diags))
394    }
395}
396
397#[cfg(test)]
398mod tests {
399    use super::*;
400    use crate::{RawValue, check_conformance};
401
402    /// Expr-flavoured schema exercising every supported shape: int /
403    /// string / bool fields, One / Optional / Many children, and a
404    /// zero-argument variant.
405    fn demo_schema() -> NodeSchema {
406        NodeSchema {
407            name: "Expr".into(),
408            variants: vec![
409                VariantSchema {
410                    name: "Lit".into(),
411                    fields: vec![FieldSchema {
412                        name: "value".into(),
413                        ty: "i64".into(),
414                    }],
415                    children: vec![],
416                },
417                VariantSchema {
418                    name: "Name".into(),
419                    fields: vec![
420                        FieldSchema {
421                            name: "text".into(),
422                            ty: "String".into(),
423                        },
424                        FieldSchema {
425                            name: "quoted".into(),
426                            ty: "bool".into(),
427                        },
428                    ],
429                    children: vec![],
430                },
431                VariantSchema {
432                    name: "Add".into(),
433                    fields: vec![],
434                    children: vec![
435                        ChildSchema {
436                            name: "lhs".into(),
437                            multiplicity: Multiplicity::One,
438                        },
439                        ChildSchema {
440                            name: "rhs".into(),
441                            multiplicity: Multiplicity::One,
442                        },
443                    ],
444                },
445                VariantSchema {
446                    name: "Neg".into(),
447                    fields: vec![],
448                    children: vec![ChildSchema {
449                        name: "body".into(),
450                        multiplicity: Multiplicity::Optional,
451                    }],
452                },
453                VariantSchema {
454                    name: "List".into(),
455                    fields: vec![],
456                    children: vec![ChildSchema {
457                        name: "items".into(),
458                        multiplicity: Multiplicity::Many,
459                    }],
460                },
461                VariantSchema {
462                    name: "Unit".into(),
463                    fields: vec![],
464                    children: vec![],
465                },
466            ],
467        }
468    }
469
470    fn demo_grammar() -> Grammar {
471        checked_grammar_from_schema(&demo_schema(), &IdGen::new())
472            .expect("demo schema generates a clean grammar")
473    }
474
475    /// Parse + conformance-check against the demo schema in one go.
476    fn parse_ok(input: &str) -> crate::ParseTree {
477        let g = demo_grammar();
478        let tree = g
479            .parse(input)
480            .unwrap_or_else(|e| panic!("parse failed for {input:?}: {:?}", e.diagnostics));
481        let diags = check_conformance(&tree, &demo_schema());
482        assert!(
483            diags.is_empty(),
484            "conformance clean for {input:?}: {diags:?}"
485        );
486        tree
487    }
488
489    #[test]
490    fn generated_grammar_passes_all_static_checks() {
491        // checked_grammar_from_schema already runs the three checks;
492        // this test pins that it succeeds for the full shape matrix.
493        demo_grammar();
494    }
495
496    #[test]
497    fn parses_int_field_variant() {
498        let tree = parse_ok("Lit(value: -42)");
499        assert_eq!(tree.variant, "Lit");
500        assert_eq!(tree.field("value"), Some(&RawValue::Text("-42".into())));
501    }
502
503    #[test]
504    fn parses_string_and_bool_fields_with_escapes() {
505        let tree = parse_ok(r#"Name(text: "hello \"world\"\n", quoted: true)"#);
506        assert_eq!(
507            tree.field("text"),
508            Some(&RawValue::Text("hello \"world\"\n".into()))
509        );
510        assert_eq!(tree.field("quoted"), Some(&RawValue::Text("true".into())));
511    }
512
513    #[test]
514    fn parses_nested_one_children() {
515        let tree = parse_ok("Add(lhs: Lit(value: 1), rhs: Neg(body: none))");
516        let lhs = tree.child_slot("lhs").unwrap();
517        assert_eq!(lhs.len(), 1);
518        assert_eq!(lhs[0].variant, "Lit");
519        let rhs = tree.child_slot("rhs").unwrap();
520        assert_eq!(rhs[0].variant, "Neg");
521    }
522
523    #[test]
524    fn optional_child_none_leaves_slot_absent() {
525        let tree = parse_ok("Neg(body: none)");
526        assert!(
527            tree.child_slot("body").is_none(),
528            "bare `none` = absent slot"
529        );
530    }
531
532    #[test]
533    fn optional_child_present_binds_one_tree() {
534        let tree = parse_ok("Neg(body: Lit(value: 3))");
535        assert_eq!(tree.child_slot("body").unwrap().len(), 1);
536    }
537
538    #[test]
539    fn many_child_list_binds_in_order_and_drops_commas() {
540        let tree = parse_ok("List(items: [Lit(value: 1), Lit(value: 2), Unit()])");
541        let items = tree.child_slot("items").unwrap();
542        let variants: Vec<&str> = items.iter().map(|t| t.variant.as_str()).collect();
543        assert_eq!(variants, ["Lit", "Lit", "Unit"]);
544    }
545
546    #[test]
547    fn many_child_empty_list_is_conformant() {
548        let tree = parse_ok("List(items: [])");
549        assert!(
550            tree.child_slot("items").is_none_or(<[_]>::is_empty),
551            "empty list = zero children"
552        );
553    }
554
555    #[test]
556    fn zero_argument_variant_parses() {
557        let tree = parse_ok("Unit()");
558        assert_eq!(tree.variant, "Unit");
559        assert!(tree.fields.is_empty() && tree.children.is_empty());
560    }
561
562    #[test]
563    fn whitespace_and_newlines_are_tolerated() {
564        let tree = parse_ok("Add(\n  lhs: Lit( value : 7 ),\n  rhs: Unit()\n)");
565        assert_eq!(tree.child_slot("lhs").unwrap()[0].variant, "Lit");
566    }
567
568    #[test]
569    fn unsupported_field_type_fails_generation_with_full_list() {
570        let schema = NodeSchema {
571            name: "Bad".into(),
572            variants: vec![VariantSchema {
573                name: "Par".into(),
574                fields: vec![
575                    FieldSchema {
576                        name: "policy".into(),
577                        ty: "Option<JoinPolicy>".into(),
578                    },
579                    FieldSchema {
580                        name: "reducer".into(),
581                        ty: "ReducerId".into(),
582                    },
583                ],
584                children: vec![],
585            }],
586        };
587        let err = grammar_from_schema(&schema, &IdGen::new()).unwrap_err();
588        assert_eq!(err.diagnostics.len(), 2, "one diagnostic per bad field");
589        assert!(
590            err.diagnostics
591                .iter()
592                .all(|d| d.code == codes::UNSUPPORTED_FIELD)
593        );
594    }
595
596    /// Flow-shaped schema whose `Par` payload fields the built-in
597    /// mapping rejects — the override motivating case.
598    fn par_schema() -> NodeSchema {
599        NodeSchema {
600            name: "Flow".into(),
601            variants: vec![
602                VariantSchema {
603                    name: "Par".into(),
604                    fields: vec![
605                        FieldSchema {
606                            name: "policy".into(),
607                            // Token-stream spelling, as the derive extracts it.
608                            ty: "Option < JoinPolicy >".into(),
609                        },
610                        FieldSchema {
611                            name: "reducer_id".into(),
612                            ty: "Option < String >".into(),
613                        },
614                    ],
615                    children: vec![ChildSchema {
616                        name: "children".into(),
617                        multiplicity: Multiplicity::Many,
618                    }],
619                },
620                VariantSchema {
621                    name: "Call".into(),
622                    fields: vec![FieldSchema {
623                        name: "label".into(),
624                        ty: "String".into(),
625                    }],
626                    children: vec![],
627                },
628            ],
629        }
630    }
631
632    fn par_overrides() -> SyntaxOverrides {
633        SyntaxOverrides::new()
634            .for_type("Option<JoinPolicy>", |ids| {
635                choice(
636                    ids,
637                    vec![
638                        token(ids, "%kw:none"),
639                        token(ids, "%kw:all_failfast"),
640                        token(ids, "%kw:any_failfast"),
641                    ],
642                )
643            })
644            .for_type("Option<String>", |ids| {
645                choice(ids, vec![token(ids, "%kw:none"), token(ids, "%str")])
646            })
647    }
648
649    #[test]
650    fn type_overrides_unlock_exotic_payload_fields() {
651        // Without overrides the Flow-shaped schema fails generation
652        // (pinned by unsupported_field_type_fails_generation_with_full_list);
653        // with them it generates check-clean and parses. Note the
654        // override keys are written without whitespace while the schema
655        // carries the token-stream spelling.
656        let g = checked_grammar_from_schema_with(&par_schema(), &IdGen::new(), &par_overrides())
657            .expect("overridden Flow schema generates a clean grammar");
658        let tree = g
659            .parse(
660                r#"Par(policy: all_failfast, reducer_id: "reduce_all_ordered",
661                     children: [Call(label: "a"), Call(label: "b")])"#,
662            )
663            .expect("canonical Par text parses");
664        let diags = check_conformance(&tree, &par_schema());
665        assert!(diags.is_empty(), "conformance clean: {diags:?}");
666        assert_eq!(
667            tree.field("policy"),
668            Some(&RawValue::Text("all_failfast".into()))
669        );
670        assert_eq!(
671            tree.field("reducer_id"),
672            Some(&RawValue::Text("reduce_all_ordered".into()))
673        );
674        assert_eq!(tree.child_slot("children").unwrap().len(), 2);
675    }
676
677    #[test]
678    fn field_override_wins_over_type_override() {
679        // Same type in both entries; the (variant, field)-pinned one
680        // must decide the syntax.
681        let overrides = par_overrides().for_field("Par", "policy", |ids| token(ids, "%kw:pinned"));
682        let g = checked_grammar_from_schema_with(&par_schema(), &IdGen::new(), &overrides)
683            .expect("clean grammar with field-level override");
684        let tree = g
685            .parse(r#"Par(policy: pinned, reducer_id: none, children: [])"#)
686            .expect("field-level syntax parses");
687        assert_eq!(tree.field("policy"), Some(&RawValue::Text("pinned".into())));
688        // The type-level spelling is no longer accepted at that site.
689        assert!(
690            g.parse(r#"Par(policy: all_failfast, reducer_id: none, children: [])"#)
691                .is_err(),
692            "type-level syntax rejected once the field is pinned"
693        );
694    }
695
696    #[test]
697    fn unrelated_override_leaves_builtin_mapping_and_failure_intact() {
698        // An override for some other type neither rescues the
699        // unsupported fields nor perturbs built-in ones.
700        let overrides = SyntaxOverrides::new().for_type("Uuid", |ids| token(ids, "%str"));
701        let err = grammar_from_schema_with(&par_schema(), &IdGen::new(), &overrides).unwrap_err();
702        assert_eq!(err.diagnostics.len(), 2, "both Par fields still unmapped");
703        assert!(
704            err.diagnostics
705                .iter()
706                .all(|d| d.code == codes::UNSUPPORTED_FIELD)
707        );
708        // Built-in demo schema is unaffected by the stray entry.
709        let g = checked_grammar_from_schema_with(&demo_schema(), &IdGen::new(), &overrides)
710            .expect("demo schema still generates");
711        assert_eq!(g.parse("Lit(value: 7)").unwrap().variant, "Lit");
712    }
713
714    #[test]
715    fn empty_schema_fails_generation() {
716        let schema = NodeSchema {
717            name: "Void".into(),
718            variants: vec![],
719        };
720        let err = grammar_from_schema(&schema, &IdGen::new()).unwrap_err();
721        assert_eq!(err.diagnostics[0].code, codes::EMPTY_SCHEMA);
722    }
723
724    #[test]
725    fn parse_error_reports_expected_set() {
726        let g = demo_grammar();
727        let err = g.parse("Lit(value: banana)").unwrap_err();
728        assert!(!err.diagnostics.is_empty());
729    }
730}