techy 0.1.3

A fast, extensible LaTeX parser for Rust
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
//! [`TackOnFieldsArgumentParser`]: the tack-on information-fields parser
//! (pylatexenc's `LatexTackOnInformationFieldMacrosParser`) — absorbing trailing
//! `\label{…}`-style invocations after a construct's declared arguments and
//! attaching them to the invocation node.
//!
//! Used as a callable's **last declared argument** (the FLM `label_arg` precedent):
//! attachment is the argument's region — the whole absorption happens at parse time
//! inside the invocation's own parse, per the decided construct-parser (not
//! postprocessing) ruling. The parser is configured with
//! the accepted field commands by **name**; a peeked
//! [`Command`](TokenKind::Command) token whose name is configured dispatches with the
//! configured [`CallableSpec`] directly — the scope stack is never consulted, so a
//! field command like `\label` need not exist as a language command at all, and
//! LaTeX's "a `\label` anywhere attaches to *something*" quirk is simply not
//! reproduced (decision reason 2). Anything else — an unconfigured command, plain
//! content, a group close, a paragraph break — ends the absorption, silently.
//!
//! # Staged shape
//!
//! Each absorbed field stages a full **`Callable` node** under the configured
//! per-name spec (diverging from pylatexenc's group-wrapper hack): the field's own
//! argument structure is the spec's [`ArgumentSpec`]s — pylatexenc's "default: one
//! expression" is a spec holding one
//! [`ExpressionParser`](super::ExpressionParser) argument; `\label`'s `{…}` is a
//! [`GroupArgumentParser`](super::GroupArgumentParser) or
//! [`CharsGroupArgumentParser`](super::CharsGroupArgumentParser) argument — and the
//! staged node self-describes (name, spec, its own
//! [`ParsedArguments`](crate::core::node::ParsedArguments) record). Dispatch routes through
//! [`ParseDriver::make_invocation_parser`], so takeover specs and driver interception
//! work as everywhere else. By-name reading is the extraction helper
//! [`split_tack_on_fields`](crate::extract::split_tack_on_fields).
//!
//! The argument's content designation covers the run of field nodes from the first
//! one on — noise between fields included, leading noise excluded (the
//! embellishments-parser convention).
//!
//! # Multiplicity and noise
//!
//! Per-field policy: a field registered with
//! [`with_field`](TackOnFieldsArgumentParser::with_field) may appear **once**; one
//! registered with
//! [`with_repeatable_field`](TackOnFieldsArgumentParser::with_repeatable_field) any
//! number of times (multiple `\label`s after a `\section`). A repeated
//! non-repeatable field diagnoses [`RepeatedTackOnField`] — and, tolerant, the field
//! is **still parsed and kept** in the region (diverging from pylatexenc's
//! parse-and-discard: techy trees keep every byte) — consumers see the
//! diagnostic and the record.
//!
//! Whitespace and comments **between** fields are scanned as region noise (diverging from pylatexenc, whose peek loop stops at a comment):
//! `\section{x} % note` + newline + `\label{y}` still associates. A probe that finds
//! no further field rewinds exactly its own noise scan — absorbed fields stay.

use alloc::boxed::Box;
use alloc::format;
use alloc::string::String;
use alloc::sync::Arc;
use alloc::vec::Vec;
use core::fmt;

use crate::engine::ParseDriver;
use crate::error::DiagnosticInfo;
use crate::node::{ArgumentExt, ContentNodes};
use crate::spec::{ArgumentParser, ArgumentSpec, CallableSpec, ParsedArgumentNodes};
use crate::state::Lang;
use crate::token::{TokenEdge, TokenKind};

use super::argument_parsers::{scan_argument_noise, stage_pre_space};
use super::{ConstructParserResult, FromInvocation, Invocation, invocation_frame, ParseContext};

/// Condition: a non-repeatable tack-on information field was specified more than once.
///
/// An example is `\section{x}\label{a}\label{b}` with `label` registered through
/// [`with_field`](TackOnFieldsArgumentParser::with_field); the condition is detected by
/// [`TackOnFieldsArgumentParser`].
///
/// Tolerant recovery keeps the repeated field parsed in the argument's region — techy
/// trees keep every byte, so nothing is discarded: consumers see both the diagnostic
/// and the record. Strict parses abort.
#[derive(Debug, Clone, PartialEq, Eq, DiagnosticInfo)]
#[non_exhaustive]
#[diagnostic(
    id = "core.arguments.repeated-tack-on-field",
    message = "information field ‘{escape_char}{name}’ cannot be specified more than once"
)]
pub struct RepeatedTackOnField {
    /// The field command's name (without escape character).
    pub name: String,
    /// The escape character the command was written with.
    pub escape_char: char,
}

/// One configured tack-on field: the command name that triggers it, the spec its
/// invocations parse under, and whether it may repeat.
struct TackOnField<L: Lang> {
    name: Box<str>,
    spec: Arc<dyn CallableSpec<L>>,
    repeatable: bool,
}

/// An argument position that absorbs trailing `\label{…}`-style information fields
/// and attaches them to the callable being parsed.
///
/// Declare it as a callable's **last** argument: the engine reaches for it when the
/// preceding declared arguments are parsed, and it keeps absorbing field invocations
/// for as long as it finds them.
///
/// It is configured with the invocation form its field nodes record and with the
/// accepted field commands **by name**. When the next token is a command whose name
/// is configured, it is parsed with the spec registered for that name — the scope
/// stack is never consulted, so a field command like `\label` need not exist as a
/// language command at all. Anything else ends the absorption, silently.
///
/// Each absorbed field stages a full `Callable` node under its configured spec, so
/// the field carries its own name, spec, and parsed arguments. Read the fields back
/// by name with [`split_tack_on_fields`](crate::extract::split_tack_on_fields).
pub struct TackOnFieldsArgumentParser<L: Lang> {
    callable_type: L::CallableTypeId,
    fields: Vec<TackOnField<L>>,
}

impl<L: Lang> TackOnFieldsArgumentParser<L> {
    /// A tack-on fields argument staging `Callable` nodes of invocation form
    /// `callable_type` (a preset's macro form, typically). Register the accepted
    /// fields with [`with_field`](TackOnFieldsArgumentParser::with_field) /
    /// [`with_repeatable_field`](TackOnFieldsArgumentParser::with_repeatable_field);
    /// with none registered the argument is always absent.
    pub fn new(callable_type: L::CallableTypeId) -> TackOnFieldsArgumentParser<L> {
        TackOnFieldsArgumentParser { callable_type, fields: Vec::new() }
    }

    /// Accept the field command `name` (without escape character), parsed under
    /// `spec`, at most **once** per invocation. Field names must be distinct across
    /// registrations (a duplicate is reported as an implementation error when the
    /// parser runs).
    pub fn with_field(
        self,
        name: impl Into<Box<str>>,
        spec: Arc<dyn CallableSpec<L>>,
    ) -> Self {
        self.field(name.into(), spec, false)
    }

    /// Accept the field command `name`, parsed under `spec`, **any number of times**
    /// per invocation.
    pub fn with_repeatable_field(
        self,
        name: impl Into<Box<str>>,
        spec: Arc<dyn CallableSpec<L>>,
    ) -> Self {
        self.field(name.into(), spec, true)
    }

    // Field names must be distinct — a spec-author contract, validated where the
    // parser runs ([§dd-dr:panic-policy]: an outer-layer contract violation is an
    // `Err`, never a panic).
    fn field(mut self, name: Box<str>, spec: Arc<dyn CallableSpec<L>>, repeatable: bool) -> Self {
        self.fields.push(TackOnField { name, spec, repeatable });
        self
    }
}

impl<L: Lang> ArgumentParser<L> for TackOnFieldsArgumentParser<L>
where
    ArgumentExt<L>: Default,
    L::InvocationSyntax: FromInvocation<L>,
{
    fn parse_argument(
        &self,
        cx: &mut ParseContext<'_, '_, L>,
        _spec: &ArgumentSpec<L>,
    ) -> ConstructParserResult<L, Option<ParsedArgumentNodes<L>>> {
        // Spec-author contract, validated where the parser runs
        // ([§dd-dr:panic-policy]: an outer-layer contract violation is an `Err`).
        if let Some(duplicate) = self.fields.iter().enumerate().find_map(|(i, field)| {
            self.fields[..i]
                .iter()
                .any(|earlier| earlier.name == field.name)
                .then_some(&*field.name)
        }) {
            return Err(cx.implementation_error(
                format!(
                    "TackOnFieldsArgumentParser field names must be distinct \
                     (duplicate registration of ‘{duplicate}’)"
                ),
                cx.here(),
            ));
        }
        let mut nodes = Vec::new();
        let mut seen = alloc::vec![false; self.fields.len()];
        let mut content_start: Option<u32> = None;

        loop {
            let noise = scan_argument_noise(cx)?;
            let Some(token) = noise.next.clone() else {
                noise.rewind(cx);
                break;
            };
            let kind = cx.tokens.token_kind(&token);
            let TokenKind::Command { name, escape_char } = kind else {
                noise.rewind(cx);
                break;
            };
            let Some(index) = self.fields.iter().position(|field| &*field.name == name)
            else {
                noise.rewind(cx);
                break;
            };
            let field = &self.fields[index];

            // Committed. The multiplicity check runs first: strict aborts before
            // consuming anything; tolerant records the condition and keeps parsing —
            // the repeated field stays in the region (module docs).
            if seen[index] && !field.repeatable {
                let span = cx.tokens.source_span_of(&token);
                cx.recover(
                    RepeatedTackOnField::new(String::from(name), escape_char),
                    span,
                )?;
            }
            seen[index] = true;

            nodes.extend(noise.nodes);
            stage_pre_space(cx, &mut nodes, &token)?;

            // The dispatch mirrors the expression position's: resolution is the
            // parser's own configuration (never the scope stack), the trigger is
            // consumed whole before the invocation parser runs, and the parser comes
            // from the driver's interception seam.
            let invocation = Invocation {
                callable_type: self.callable_type,
                name,
                spec: &field.spec,
                token: &token,
            };
            let frame = invocation_frame(cx, &invocation);
            cx.tokens.move_to(&token, TokenEdge::EndPastPostSpace);
            // A factory Err aborts under any policy ("could not build the
            // parser"), with the live traceback attached here.
            let driver = cx.driver;
            let mut parser = driver
                .make_invocation_parser(invocation)
                .map_err(|error| cx.attach_hook_frames(error))?;
            // The descent runs through the single entry point (its MUST contract);
            // `None` scopes a clone of the current state — same swap/restore as the
            // content loop's dispatch site.
            let result = cx.parse_construct(&mut *parser, None, Some(frame));
            drop(parser);
            // An after-effect delta has no scope here: the argument channel
            // deliberately carries none (`ArgumentParser::parse_argument` docs).
            let (id, _delta) = result?;
            if content_start.is_none() {
                content_start = Some(nodes.len() as u32);
            }
            nodes.push(id);
        }

        match content_start {
            // The content: the full run from the first field node on (intermediate
            // noise included, leading noise excluded — module docs).
            Some(start) => {
                let end = nodes.len() as u32;
                Ok(Some(ParsedArgumentNodes::new(
                    nodes,
                    ContentNodes::InRegion(start..end),
                    Default::default(),
                )))
            }
            // No field matched; the reader is back at the entry position (the first
            // iteration's rewind).
            None => Ok(None),
        }
    }

    /// Optional: absent is a valid, silent outcome (the trait default, stated
    /// explicitly — the expression-position guard leans on this answer).
    fn can_match_empty(&self) -> bool {
        true
    }
}

impl<L: Lang> fmt::Debug for TackOnFieldsArgumentParser<L> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let mut fields = f.debug_struct("TackOnFieldsArgumentParser");
        fields.field("callable_type", &self.callable_type);
        for field in &self.fields {
            fields.field(
                if field.repeatable { "repeatable_field" } else { "field" },
                &field.name,
            );
        }
        fields.finish()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::constructs::CharsGroupArgumentParser;
    use crate::engine::Language;
    use crate::error::Recovery;
    use crate::latexlike::{
        argument_specs, CallableType, GroupType, Latexlike, LatexlikeDriver, MacroSpec,
    };
    use crate::extract::{content_as_chars, split_tack_on_fields_drop_annotations};
    use crate::node::{check_tree_invariants, NodeRef};
    use crate::scopes::Package;
    use crate::state::ParsingState;
    use alloc::vec;
    use alloc::vec::Vec;

    /// The `\label{…}` field spec: one chars-group argument.
    fn label_spec() -> Arc<dyn CallableSpec<Latexlike>> {
        Arc::new(MacroSpec::new(vec![Arc::new(ArgumentSpec::new_unnamed(Arc::new(
            CharsGroupArgumentParser::new(GroupType::Content),
        )))]))
    }

    /// A language defining `\section{…}` whose second declared argument is the given
    /// tack-on parser. `\label`/`\nonumber` exist **only** through it — they are
    /// deliberately not registered as language commands.
    fn language(
        tack_on: TackOnFieldsArgumentParser<Latexlike>,
        recovery: Recovery,
    ) -> Language<Latexlike> {
        let mut arguments = argument_specs(["m"]).unwrap();
        arguments.push(Arc::new(ArgumentSpec::new_unnamed(Arc::new(tack_on))));
        let mut package = Package::new("tack-on-tests");
        package.insert(CallableType::Macro, "section", Arc::new(MacroSpec::new(arguments)));
        Language::new(
            LatexlikeDriver::new(recovery),
            ParsingState::lang_initial_with_packages([package]).expect("seed state"),
        )
    }

    fn repeatable_label() -> TackOnFieldsArgumentParser<Latexlike> {
        TackOnFieldsArgumentParser::new(CallableType::Macro)
            .with_repeatable_field("label", label_spec())
    }

    fn parse_ok(
        tack_on: TackOnFieldsArgumentParser<Latexlike>,
        input: &str,
    ) -> crate::engine::ParseResult<Latexlike> {
        let result = language(tack_on, Recovery::Strict).parse(input).unwrap();
        check_tree_invariants(&result.tree);
        assert!(
            result.diagnostics.is_empty(),
            "unexpected diagnostics: {:?}",
            result.diagnostics
        );
        result
    }

    fn section_node(result: &crate::engine::ParseResult<Latexlike>) -> NodeRef<'_, Latexlike> {
        let node = result.tree.root().child(0).expect("the section node");
        assert_eq!(node.macro_name(), Some("section"));
        node
    }

    #[test]
    fn trailing_fields_attach_to_the_invocation_node() {
        let result = parse_ok(repeatable_label(), r"\section{One} \label{sec:one} after");
        let section = section_node(&result);
        // The invocation's span runs through the absorbed field.
        assert_eq!(section.span().range(), 0..29);

        // The tack-on argument: region = [whitespace noise, the field node]; content
        // designation starts at the field node.
        let argument = section.arguments().unwrap().get(1).unwrap();
        assert!(argument.is_provided());
        let region: Vec<_> = section.argument_nodes(1).unwrap().iter().collect();
        assert_eq!(region.len(), 2);
        assert_eq!(region[0].chars(), Some(" "));
        let content: Vec<_> = section.argument_content_nodes(1).unwrap().iter().collect();
        assert_eq!(content.len(), 1);

        // The field is a full callable node under the configured spec, with its own
        // argument record.
        let label = content[0];
        assert!(label.is_callable());
        assert_eq!(label.name(), Some("label"));
        assert_eq!(label.callable_type(), Some(CallableType::Macro));
        assert_eq!(
            content_as_chars(label.argument_content_nodes(0).unwrap()).unwrap(),
            "sec:one"
        );

        assert_eq!(result.tree.root().child(1).unwrap().chars(), Some(" after"));
    }

    #[test]
    fn a_nested_tack_on_field_chain_is_counted_by_the_descent_guard() {
        // Guard coverage for the tack-on field dispatch (the F1 sibling site): a
        // chain of specs where each `\f`'s own tack-on argument accepts the next
        // `\f` nests one field dispatch inside the other. Since that dispatch
        // routes through `parse_construct`, a configured depth limit counts the
        // chain and refuses it instead of letting it recurse unbounded.
        use crate::constructs::DescentLimitExceeded;
        use crate::engine::StdDescentGuardInit;
        use crate::error::DiagnosticInfo as _;
        use alloc::string::String;

        // 40 nested spec levels: level k's `\f` tack-on accepts level k-1's spec.
        let mut spec: Arc<dyn CallableSpec<Latexlike>> =
            Arc::new(MacroSpec::new(Vec::new()));
        for _ in 0..40 {
            let tack = TackOnFieldsArgumentParser::new(CallableType::Macro)
                .with_field("f", Arc::clone(&spec));
            spec = Arc::new(MacroSpec::new(vec![Arc::new(ArgumentSpec::new_unnamed(
                Arc::new(tack),
            ))]));
        }
        let mut package = Package::new("tack-chain");
        package.insert(CallableType::Macro, "start", spec);
        let language: Language<Latexlike> = Language::new(
            LatexlikeDriver::new(Recovery::Strict),
            ParsingState::lang_initial_with_packages([package]).expect("seed state"),
        )
        .with_descent_guard_init(StdDescentGuardInit::depth_limit(20));

        // A short chain fits comfortably under the limit…
        assert!(language.parse(r"\start \f \f \f x").is_ok());
        // …a 40-field chain crosses it and is refused at the limit.
        let mut chain = String::from(r"\start");
        for _ in 0..40 {
            chain.push_str(r" \f");
        }
        chain.push_str(" x");
        let err = language.parse(chain).unwrap_err();
        assert_eq!(err.identifier(), DescentLimitExceeded::IDENTIFIER);
    }

    #[test]
    fn field_commands_are_not_language_commands() {
        // Outside a tack-on position `\label` resolves nowhere — the decided
        // no-attach-anywhere behavior (module docs, decision reason 2).
        let result = language(repeatable_label(), Recovery::Tolerant)
            .parse(r"\label{a}")
            .unwrap();
        assert_eq!(result.diagnostics.len(), 1);
        assert!(
            result.diagnostics.iter().next().unwrap().message().contains("cannot resolve"),
            "{}",
            result.diagnostics.iter().next().unwrap().message()
        );
    }

    #[test]
    fn repeatable_fields_absorb_every_occurrence() {
        let result = parse_ok(repeatable_label(), r"\section{A}\label{x}\label{y}z");
        let section = section_node(&result);
        let content: Vec<_> = section.argument_content_nodes(1).unwrap().iter().collect();
        assert_eq!(content.len(), 2);

        let fields = split_tack_on_fields_drop_annotations(section.argument_content_nodes(1).unwrap()).unwrap();
        assert_eq!(fields.len(), 2);
        let keys: Vec<_> = fields.iter().map(|entry| entry.key().to_string()).collect();
        assert_eq!(keys, ["label", "label"]);
        // Last-wins lookup and collect-all composition, straight from the KeyVals
        // grammar.
        assert_eq!(
            content_as_chars(fields.get("label").unwrap().value().unwrap()).unwrap(),
            "y"
        );
        let combined = fields.get_combined_with("label", ",").unwrap().unwrap();
        assert_eq!(content_as_chars(combined.root().children()).unwrap(), "x,y");
        assert_eq!(result.tree.root().child(1).unwrap().chars(), Some("z"));
    }

    #[test]
    fn non_repeatable_duplicates_are_diagnosed_and_kept() {
        let once = || {
            TackOnFieldsArgumentParser::new(CallableType::Macro)
                .with_field("label", label_spec())
        };

        let err =
            language(once(), Recovery::Strict).parse(r"\section{A}\label{x}\label{y}").unwrap_err();
        assert!(
            err.to_string().contains("cannot be specified more than once"),
            "{err}"
        );

        let result =
            language(once(), Recovery::Tolerant).parse(r"\section{A}\label{x}\label{y}").unwrap();
        check_tree_invariants(&result.tree);
        assert_eq!(result.diagnostics.len(), 1);
        let diagnostic = result.diagnostics.iter().next().unwrap();
        assert_eq!(diagnostic.identifier(), RepeatedTackOnField::IDENTIFIER);
        let condition = diagnostic.data().downcast_ref::<RepeatedTackOnField>().unwrap();
        assert_eq!(condition.name, "label");
        assert_eq!(condition.escape_char, '\\');
        // The repeated field stays parsed and attached (bytes are never dropped).
        let section = section_node(&result);
        let content: Vec<_> = section.argument_content_nodes(1).unwrap().iter().collect();
        assert_eq!(content.len(), 2);
    }

    #[test]
    fn noise_between_fields_keeps_the_association() {
        let result = parse_ok(repeatable_label(), "\\section{A} % note\n\\label{x}!");
        let section = section_node(&result);
        let region: Vec<_> = section.argument_nodes(1).unwrap().iter().collect();
        assert!(region.iter().any(|node| node.comment().is_some()));
        let fields = split_tack_on_fields_drop_annotations(section.argument_content_nodes(1).unwrap()).unwrap();
        assert_eq!(
            content_as_chars(fields.get("label").unwrap().value().unwrap()).unwrap(),
            "x"
        );
        assert_eq!(result.tree.root().child(1).unwrap().chars(), Some("!"));
    }

    #[test]
    fn absorption_stops_at_anything_else_silently() {
        let result = parse_ok(repeatable_label(), r"\section{A} text");
        let section = section_node(&result);
        assert_eq!(section.span().range(), 0..11);
        assert!(!section.arguments().unwrap().get(1).unwrap().is_provided());
        assert_eq!(result.tree.root().child(1).unwrap().chars(), Some(" text"));
    }

    #[test]
    fn zero_argument_fields_record_no_value() {
        let tack_on = TackOnFieldsArgumentParser::new(CallableType::Macro)
            .with_repeatable_field("label", label_spec())
            .with_field("nonumber", Arc::new(MacroSpec::new(vec![])));
        let result = parse_ok(tack_on, r"\section{A}\nonumber\label{x}");
        let section = section_node(&result);
        let fields = split_tack_on_fields_drop_annotations(section.argument_content_nodes(1).unwrap()).unwrap();
        assert_eq!(fields.len(), 2);
        assert!(fields.get("nonumber").unwrap().value().is_none());
        assert_eq!(
            content_as_chars(fields.get("label").unwrap().value().unwrap()).unwrap(),
            "x"
        );
    }

    // --- spec-author contract violations are implementation errors ([§dd-dr:panic-policy]) --

    #[test]
    fn duplicate_field_names_are_an_implementation_error() {
        let tack_on = TackOnFieldsArgumentParser::new(CallableType::Macro)
            .with_field("label", label_spec())
            .with_repeatable_field("label", label_spec());
        // Tolerant recovery, deliberately: an implementation error bypasses the
        // recover funnel and aborts even there.
        let err = language(tack_on, Recovery::Tolerant)
            .parse(r"\section{A}\label{x}")
            .unwrap_err();
        assert_eq!(
            err.identifier(),
            crate::constructs::ImplementationError::IDENTIFIER
        );
    }
}