tabnas-abnf 0.5.1

ABNF (RFC 5234) grammar compiler for the tabnas parsing engine: ABNF text in, tabnas GrammarSpec out
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
// Copyright (c) 2025-2026 Richard Rodger and other contributors, MIT License

//! The RFC 5234 front-end: ABNF text in, the notation-neutral grammar IR
//! that `tabnas-bnf` compiles out.
//!
//! ```text
//! ABNF text ──parse_abnf──▶ Grammar ──bnf::emit_grammar_spec──▶ GrammarSpec
//! ```
//!
//! Everything downstream of that IR (desugaring, left-recursion
//! elimination, tail repeats, probe dispatch, literal lifting, token
//! allocation, first-set analysis, chain emission) lives in
//! `tabnas-bnf` and is shared with the GBNF and EBNF front-ends. What
//! stays here is what is genuinely ABNF: the meta-grammar in
//! [`crate::parser_abnf`], the Appendix B.1 core rules, incremental
//! alternatives, numeric values, case-insensitive quoted strings and the
//! value annotations a trailing comment can carry.

use std::fmt;

use indexmap::IndexSet;
use regex::Regex;
use serde_json::Value as JsonValue;
use tabnas::Value;
use tabnas_bnf::{refs_in, Grammar, NodeKind, Production};

use crate::parser_abnf::{parse_abnf_raw, RawError};

/// A failure to read ABNF source.
///
/// Carries the line and column the engine reported, where it reported
/// any, so a caller can point at the offending text directly.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AbnfParseError {
    /// The rendered diagnostic, always prefixed `abnf: `.
    pub message: String,
    /// The 1-based line, when the underlying failure named one.
    pub line: Option<usize>,
    /// The 1-based column, when the underlying failure named one.
    pub column: Option<usize>,
    /// The engine's own error code, when the failure came from the
    /// engine rather than from this front-end.
    pub code: Option<String>,
}

impl AbnfParseError {
    /// The engine's refusal of a source, naming its line and column, with
    /// the engine's code.
    pub(crate) fn from_engine(error: &tabnas::TabnasError) -> Self {
        let line = error.row;
        let column = error.col;
        let location = if 0 != line && 0 != column {
            format!(" at line {line}, column {column}")
        } else {
            String::new()
        };
        let rendered = error.to_string();
        let raw = rendered.lines().next().unwrap_or_default();
        Self {
            message: format!("abnf: parse error{location}: {raw}"),
            line: (0 != line).then_some(line),
            column: (0 != column).then_some(column),
            code: Some(error.code.clone()),
        }
    }

    pub(crate) fn new(message: impl Into<String>) -> Self {
        Self {
            message: message.into(),
            line: None,
            column: None,
            code: None,
        }
    }
}

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

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

// ---- parse ----------------------------------------------------------

/// Parse ABNF source into the grammar IR.
///
/// The order of the checks below is itself part of the contract, and
/// `test/spec/alignment-abnf-errors.tsv` pins it in every runtime: the
/// numeric diagnostic first, then the incremental-merge refusal, then
/// the malformed element. A rejection naming a different cause in each
/// runtime is the divergence that order exists to close.
///
/// "First" includes ahead of a refusal from the engine itself, which is
/// why the numeric diagnostic is answered on the error path below as
/// well as on the success path. The canonical runtime throws out of the
/// decoding action, so a later fault is never reached.
pub fn parse_abnf(src: &str) -> Result<Grammar, AbnfParseError> {
    let (productions, num_err) = match parse_abnf_raw(src) {
        Ok(parsed) => parsed,
        Err(RawError::Numeric(message)) => return Err(numeric_parse_error(&message)),
        Err(RawError::Message(message)) => return Err(AbnfParseError::new(message)),
        Err(RawError::Engine(error)) => return Err(AbnfParseError::from_engine(&error)),
    };

    if productions.is_empty() {
        return Err(AbnfParseError::new("abnf: no productions found"));
    }

    // Surface the numeric-value diagnostic the parse recorded. The
    // canonical runtime raises it from inside the decoding action, so it
    // precedes every check below even when the element it was decoded
    // into was dropped with an unclosed group.
    if let Some(message) = num_err {
        return Err(numeric_parse_error(&message));
    }

    // BEFORE merging, not after. `merge_incrementals` drops each `=/`
    // production, keeping only the base's span, so an annotation on an
    // incremental line would be resolved against a production list that
    // no longer contained the line it followed and would attach to
    // whatever rule happened to be declared before it instead.
    let mut productions = productions;
    attach_value_annotations(src, &mut productions)?;
    let merged = merge_incrementals(productions)?;
    reject_holes(&merged)?;

    let mut typed = Vec::with_capacity(merged.len());
    for production in &merged {
        typed.push(production_from_value(production)?);
    }
    Ok(Grammar::new(with_core_rules(typed)))
}

/// The rejection a refused numeric value becomes.
///
/// One place, because the diagnostic is raised from two: a parse that
/// completed structurally, and a parse the engine later refused. The
/// text is pinned byte for byte by
/// `test/spec/alignment-abnf-errors.tsv` in all three runtimes, and it
/// carries no row or column, because the canonical one does not.
fn numeric_parse_error(message: &str) -> AbnfParseError {
    AbnfParseError::new(format!("abnf: parse error: {message}"))
}

// ---- value annotations ----------------------------------------------

/// What JavaScript treats as whitespace in `String.prototype.trim`, in
/// `\s` and in the member split: ECMAScript WhiteSpace and
/// LineTerminator together.
///
/// That set is NOT Rust's `char::is_whitespace`, which is the Unicode
/// `White_Space` property: `White_Space` holds U+0085, which JavaScript
/// does not trim, and lacks U+FEFF, which JavaScript does. A `;` comment
/// takes any character at all, so both differences are reachable from a
/// grammar and the set is spelled out rather than borrowed.
fn is_js_whitespace(character: char) -> bool {
    '\u{feff}' == character || (character.is_whitespace() && '\u{85}' != character)
}

/// The same set, as the body of a regular expression class.
const JS_WHITESPACE_CLASS: &str = concat!(
    r"\t\n\x0B\x0C\r \u{a0}\u{1680}\u{2000}-\u{200a}",
    r"\u{2028}\u{2029}\u{202f}\u{205f}\u{3000}\u{feff}"
);

/// What JavaScript's `.` matches: anything that is not a LineTerminator.
/// The `regex` crate's `.` excludes only `\n`, so a carriage return in a
/// comment would be matched here where the canonical pattern stops.
const JS_DOT_CLASS: &str = r"^\n\r\u{2028}\u{2029}";

/// A trailing comment claiming a value annotation:
///
/// ```abnf
/// ver = maj "." min "." pat    ; @object maj min pat
/// tags = tag *("," tag)        ; @array
/// ```
///
/// RFC 5234 has nowhere else to put this. A comment is the only place in
/// the notation that carries no meaning of its own, which is exactly why
/// it can carry one here without changing what the grammar accepts:
/// strip every annotation and the same language parses, just into a tree
/// instead of a value.
///
/// ONLY `@object` and `@array` are claimed. Any other `; @…` comment is
/// left alone: the notation has no directive namespace, so this must not
/// assume one, and a reader's own `; @deprecated` has to keep meaning
/// nothing.
fn annotation_pattern() -> &'static Regex {
    static PATTERN: std::sync::OnceLock<Regex> = std::sync::OnceLock::new();
    PATTERN.get_or_init(|| {
        // `(?-u:\b)` is the ASCII word boundary the canonical `\b` is. A
        // JavaScript pattern without the `u` flag reads `\w` as
        // `[A-Za-z0-9_]`, where the `regex` crate reads it as the Unicode
        // word class, so `@objectä` would stop matching here.
        Regex::new(&format!(
            r"^@(object|array)(?-u:\b)[{JS_WHITESPACE_CLASS}]*([{JS_DOT_CLASS}]*)$"
        ))
        .expect("the annotation pattern is valid")
    })
}

fn member_name_pattern() -> &'static Regex {
    static PATTERN: std::sync::OnceLock<Regex> = std::sync::OnceLock::new();
    PATTERN.get_or_init(|| {
        Regex::new(r"^[A-Za-z][A-Za-z0-9-]*$").expect("the member-name pattern is valid")
    })
}

/// Every `;` comment in the source that claims an annotation, with the
/// byte offset it starts at.
///
/// Quoted strings and prose are skipped: a `;` inside `"a;b"` or `<a;b>`
/// is CONTENT, not a comment, and treating it as one would silently
/// attach an annotation the author did not write.
fn annotation_comments(src: &str) -> Vec<(usize, String)> {
    let bytes = src.as_bytes();
    let mut out = Vec::new();
    let mut index = 0;
    while index < bytes.len() {
        match bytes[index] {
            quote @ (b'"' | b'<') => {
                // RFC 5234 char-val and prose-val have no escapes, so the
                // next closing mark ends them.
                let closer = if b'<' == quote { b'>' } else { b'"' };
                match bytes[index + 1..].iter().position(|byte| closer == *byte) {
                    Some(offset) => index += offset + 2,
                    None => return out,
                }
            }
            b';' => {
                let end = bytes[index..]
                    .iter()
                    .position(|byte| b'\n' == *byte)
                    .map_or(bytes.len(), |offset| index + offset);
                let body = src[index + 1..end]
                    .trim_matches(is_js_whitespace)
                    .to_string();
                if body.starts_with('@') {
                    out.push((index, body));
                }
                index = end + 1;
            }
            _ => index += 1,
        }
    }
    out
}

/// Attach each annotation to the production it FOLLOWS: the last one
/// that begins before it.
///
/// Not "the production on the same line". A rule may be written across
/// several lines, and an author putting the annotation on the last of
/// them means the same thing. Following the definition is the rule that
/// reads the same either way.
fn attach_value_annotations(src: &str, prods: &mut [Value]) -> Result<(), AbnfParseError> {
    let mut ordered: Vec<usize> = (0..prods.len())
        .filter(|index| span_start(&prods[*index]).is_some())
        .collect();
    if ordered.is_empty() {
        return Ok(());
    }
    ordered.sort_by_key(|index| span_start(&prods[*index]).unwrap_or(0));

    // Both `ordered` and the comments are in source order, so the search
    // only ever moves FORWARD: `cursor` is not reset per comment.
    // Restarting it made attachment quadratic in the number of annotated
    // rules, which a generated grammar can make expensive for nothing.
    let mut cursor = 0;
    for (at, body) in annotation_comments(src) {
        let Some(captures) = annotation_pattern().captures(&body) else {
            continue;
        };

        while cursor < ordered.len() && span_start(&prods[ordered[cursor]]).unwrap_or(0) < at {
            cursor += 1;
        }
        if 0 == cursor {
            return Err(AbnfParseError::new(format!(
                "abnf: '; {body}' appears before any rule, so there is nothing for it to \
                 annotate. A value annotation goes after the rule it describes."
            )));
        }
        let owner = ordered[cursor - 1];
        let name = production_name(&prods[owner]);

        let kind = captures.get(1).map_or("", |group| group.as_str());
        let members: Vec<String> = captures
            .get(2)
            .map_or("", |group| group.as_str())
            .split(|character: char| ',' == character || is_js_whitespace(character))
            .filter(|word| !word.is_empty())
            .map(str::to_string)
            .collect();

        if "array" == kind {
            if !members.is_empty() {
                return Err(AbnfParseError::new(format!(
                    "abnf: rule '{name}': '@array' names no members \u{2014} every part that \
                     produces a value becomes an element, in order. Got '{}'.",
                    members.join(" ")
                )));
            }
        } else {
            let mut seen = IndexSet::new();
            for member in &members {
                if !member_name_pattern().is_match(member) {
                    return Err(AbnfParseError::new(format!(
                        "abnf: rule '{name}': '{member}' is not a rule name, so it cannot name a \
                         member of '@object'."
                    )));
                }
                // Each member is a separate KEY. Two parts named the same
                // thing both write to it, so the second silently
                // overwrites the first and that much of the input is gone
                // from the result.
                if !seen.insert(member.clone()) {
                    return Err(AbnfParseError::new(format!(
                        "abnf: rule '{name}': '@object' names '{member}' twice. Each member is a \
                         separate key, so the second part would overwrite the first. Give them \
                         different names."
                    )));
                }
            }
        }

        if has_field(&prods[owner], "value") {
            return Err(AbnfParseError::new(format!(
                "abnf: rule '{name}' has more than one value annotation. A rule builds one thing."
            )));
        }
        let annotation = if "array" == kind {
            annotation_value("array", &[])
        } else {
            annotation_value("object", &members)
        };
        set_field(&mut prods[owner], "value", annotation);
    }
    Ok(())
}

/// The IR shape of a value annotation.
fn annotation_value(kind: &str, members: &[String]) -> Value {
    let mut map = indexmap::IndexMap::new();
    map.insert("kind".to_string(), Value::String(kind.to_string()));
    if !members.is_empty() {
        map.insert(
            "members".to_string(),
            Value::array(
                members
                    .iter()
                    .map(|member| Value::String(member.clone()))
                    .collect(),
            ),
        );
    }
    Value::object(map)
}

// ---- incremental alternatives ---------------------------------------

/// Fold every `name =/ alt` production into the earlier production with
/// the same name by appending its alternatives. Refuses an incremental
/// that references a name not yet defined: RFC 5234 requires the base
/// production to appear first.
fn merge_incrementals(prods: Vec<Value>) -> Result<Vec<Value>, AbnfParseError> {
    let mut out: Vec<Value> = Vec::with_capacity(prods.len());
    let mut by_name: indexmap::IndexMap<String, usize> = indexmap::IndexMap::new();
    for production in prods {
        let name = production_name(&production);
        if matches!(field(&production, "incremental"), Some(Value::Bool(true))) {
            let Some(base) = by_name.get(&name).copied() else {
                return Err(AbnfParseError::new(format!(
                    "abnf: '{name} =/ \u{2026}' has no earlier '{name} = \u{2026}' to extend"
                )));
            };
            let extra = match field(&production, "alts") {
                Some(Value::Array(alts)) => alts.as_ref().clone(),
                _ => Vec::new(),
            };
            if let Some(Value::Array(existing)) = field(&out[base], "alts") {
                let mut merged = existing.as_ref().clone();
                merged.extend(extra);
                set_field(&mut out[base], "alts", Value::array(merged));
            }
            // This production is about to be dropped, and annotations are
            // attached before that happens, so an annotation on the `=/`
            // line has to move to the base, which IS the rule it
            // describes.
            if let Some(value) = field(&production, "value") {
                if has_field(&out[base], "value") {
                    return Err(AbnfParseError::new(format!(
                        "abnf: rule '{name}' has more than one value annotation. A rule builds \
                         one thing."
                    )));
                }
                set_field(&mut out[base], "value", value);
            }
            continue;
        }
        // Rebuilt field by field, so every field carried on a production
        // has to be listed here or it is silently dropped, `sp` included.
        let mut clean = indexmap::IndexMap::new();
        clean.insert("name".to_string(), Value::String(name.clone()));
        clean.insert(
            "alts".to_string(),
            field(&production, "alts").unwrap_or_else(|| Value::array(Vec::new())),
        );
        if let Some(span) = field(&production, "sp") {
            clean.insert("sp".to_string(), span);
        }
        if let Some(node_kind) = field(&production, "nodeKind") {
            clean.insert("nodeKind".to_string(), node_kind);
        }
        // Annotations are attached BEFORE this runs, so this carry is
        // live: without it every annotation in the grammar would vanish
        // here without a word.
        if let Some(value) = field(&production, "value") {
            clean.insert("value".to_string(), value);
        }
        by_name.insert(name, out.len());
        out.push(Value::object(clean));
    }
    Ok(out)
}

// ---- malformed elements ---------------------------------------------

/// Refuse a production holding an element the parser could not build.
///
/// A malformed element reaches here as a HOLE in an alternative: an
/// unclosed group (`( "a" / "b"` with no `)`) pops without ever building
/// its node, leaving nothing in the sequence. The walk mirrors the
/// shared compiler's `refs_in` exactly, because "where does a walk
/// dereference?" is the definition of where a hole is fatal. A top-level
/// scan of the sequence is not enough: when a repetition wraps the
/// unclosed group (`bad = *( "a"`) the close builds a perfectly real
/// `star` whose `inner` is the hole, so the sequence entry is present
/// and the walk has to go one level down.
fn reject_holes(prods: &[Value]) -> Result<(), AbnfParseError> {
    fn hole_in(alt: &Value) -> bool {
        let Value::Array(elements) = alt else {
            return true;
        };
        for element in elements.iter() {
            if element.is_undefined() || element.is_null() {
                return true;
            }
            match field(element, "kind") {
                Some(Value::String(kind))
                    if matches!(kind.as_str(), "opt" | "star" | "plus" | "rep") =>
                {
                    let inner = field(element, "inner").unwrap_or(Value::Undefined);
                    if hole_in(&Value::array(vec![inner])) {
                        return true;
                    }
                }
                Some(Value::String(kind)) if "group" == kind => {
                    let Some(Value::Array(alts)) = field(element, "alts") else {
                        return true;
                    };
                    for inner in alts.iter() {
                        if hole_in(inner) {
                            return true;
                        }
                    }
                }
                _ => {}
            }
        }
        false
    }

    for production in prods {
        let name = production_name(production);
        let Some(Value::Array(alts)) = field(production, "alts") else {
            return Err(AbnfParseError::new(format!(
                "abnf: rule '{name}' is malformed \u{2014} no alternatives were built."
            )));
        };
        for alt in alts.iter() {
            if hole_in(alt) {
                return Err(AbnfParseError::new(format!(
                    "abnf: rule '{name}' is malformed \u{2014} an element could not be built. The \
                     usual cause is an unclosed group or option."
                )));
            }
        }
    }
    Ok(())
}

// ---- core rules -----------------------------------------------------

/// RFC 5234 Appendix B.1 core rules, parsed on first use and spliced
/// into any user grammar that references them but does not define them.
const CORE_RULES_ABNF: &str = r#"
ALPHA  = %x41-5A / %x61-7A
BIT    = "0" / "1"
CHAR   = %x01-7F
CR     = %x0D
LF     = %x0A
CRLF   = CR LF
CTL    = %x00-1F / %x7F
DIGIT  = %x30-39
DQUOTE = %x22
HEXDIG = DIGIT / "A" / "B" / "C" / "D" / "E" / "F"
HTAB   = %x09
OCTET  = %x00-FF
SP     = %x20
VCHAR  = %x21-7E
WSP    = SP / HTAB
LWSP   = *( WSP / CRLF WSP )
"#;

/// The core rules, in declaration order.
///
/// Parsed once and cloned per grammar. They flatten to `src` in the
/// output AST: they are character-class bricks, not structural nodes a
/// reader wants one of per matched character.
///
/// Their source spans are stripped. They are parsed from a string in
/// THIS FILE, so their offsets index a document the user never wrote,
/// and an editor asked to reveal one would jump to a position in the
/// user's grammar that has nothing to do with `ALPHA` or `DIGIT`. A
/// missing span means "nowhere to point", which is exactly right for a
/// rule the library supplied; a wrong one is worse than none. A
/// reference TO a core rule still carries a span, because that reference
/// is in the user's source and is what a diagnostic points at.
fn core_rules() -> &'static Vec<Production> {
    static CORE: std::sync::OnceLock<Vec<Production>> = std::sync::OnceLock::new();
    CORE.get_or_init(|| {
        let (raw, _) = parse_abnf_raw(CORE_RULES_ABNF)
            .unwrap_or_else(|_| panic!("abnf: internal: the core rules failed to parse"));
        raw.iter()
            .map(|value| {
                let mut production = production_from_value(value)
                    .unwrap_or_else(|error| panic!("abnf: internal: core rule: {error}"));
                production.node_kind = NodeKind::Core;
                strip_spans(&mut production);
                production
            })
            .collect()
    })
}

/// Remove every span from a production and everything under it.
fn strip_spans(production: &mut Production) {
    fn walk(element: &mut tabnas_bnf::Element) {
        element.sp = None;
        match &mut element.kind {
            tabnas_bnf::Kind::Opt { inner }
            | tabnas_bnf::Kind::Star { inner, .. }
            | tabnas_bnf::Kind::Plus { inner }
            | tabnas_bnf::Kind::Rep { inner, .. } => walk(inner),
            tabnas_bnf::Kind::Group { alts } => {
                for alt in alts {
                    for inner in alt {
                        walk(inner);
                    }
                }
            }
            _ => {}
        }
    }
    production.sp = None;
    for alt in &mut production.alts {
        for element in alt {
            walk(element);
        }
    }
}

/// Add each RFC 5234 core rule the user's grammar references but does
/// not define locally. Resolution is transitive: mention `HEXDIG` and
/// `DIGIT` comes too. User definitions always win, so a local
/// `DIGIT = …` is left untouched.
fn with_core_rules(user: Vec<Production>) -> Vec<Production> {
    let core = core_rules();
    let mut defined: IndexSet<String> = user
        .iter()
        .map(|production| production.name.clone())
        .collect();
    let mut needed: IndexSet<String> = IndexSet::new();
    for production in &user {
        for alt in &production.alts {
            refs_in(alt, &mut needed);
        }
    }

    let mut out: Vec<Production> = Vec::new();
    let mut added = true;
    while added {
        added = false;
        for production in core {
            if defined.contains(&production.name) || !needed.contains(&production.name) {
                continue;
            }
            defined.insert(production.name.clone());
            // A COPY, never the cached production: the cache is handed
            // out to every grammar compiled in this process, and a
            // consumer that annotated an `ALPHA` node would otherwise see
            // that annotation on unrelated documents.
            let copy = production.clone();
            for alt in &copy.alts {
                refs_in(alt, &mut needed);
            }
            out.push(copy);
            added = true;
        }
    }

    let mut all = user;
    all.extend(out);
    all
}

// ---- value helpers --------------------------------------------------

/// One named field of an object value.
fn field(value: &Value, key: &str) -> Option<Value> {
    match value {
        Value::Object(entries) => entries.get(key).cloned(),
        _ => None,
    }
}

fn has_field(value: &Value, key: &str) -> bool {
    field(value, key).is_some()
}

fn set_field(value: &mut Value, key: &str, field: Value) {
    if let Some(entries) = value.as_object_mut() {
        entries.insert(key.to_string(), field);
    }
}

fn production_name(value: &Value) -> String {
    match field(value, "name") {
        Some(Value::String(name)) => name,
        _ => String::new(),
    }
}

fn span_start(value: &Value) -> Option<usize> {
    match field(&field(value, "sp")?, "s") {
        Some(Value::Number(start)) => Some(start as usize),
        _ => None,
    }
}

/// Read one parsed production into the typed IR.
///
/// The parse AST is built as engine values in exactly the shape the IR
/// serializes to, so this is a deserialization rather than a
/// translation, and the two representations cannot drift apart.
fn production_from_value(value: &Value) -> Result<Production, AbnfParseError> {
    let mut json: JsonValue = value.to_json();
    integral(&mut json);
    let name = production_name(value);
    serde_json::from_value(json).map_err(|error| {
        AbnfParseError::new(format!(
            "abnf: rule '{name}' is malformed \u{2014} {error}."
        ))
    })
}

/// Rewrite every whole number in a JSON tree as an integer.
///
/// The engine's number is a double, so a span offset and a repetition
/// count both arrive as `8.0`, which serde will not read into a `usize`.
/// Nothing in the IR is a fractional number, so the conversion is total
/// rather than a heuristic.
fn integral(value: &mut JsonValue) {
    match value {
        JsonValue::Number(number) => {
            if let Some(float) = number.as_f64() {
                if 0.0 == float.fract() && float.is_finite() && 0.0 <= float {
                    *value = JsonValue::Number((float as u64).into());
                }
            }
        }
        JsonValue::Array(items) => items.iter_mut().for_each(integral),
        JsonValue::Object(entries) => entries.values_mut().for_each(integral),
        _ => {}
    }
}