techy 0.2.0

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
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
//! The preset's source re-emission: [`SourceRecomposer`], built by
//! [`source_recomposer`] — the recomposer that reconstructs the source text of a
//! latexlike-family tree out of the facts the parse recorded on its nodes.

// This module is private; the public story lives on the items' own docs.

use core::fmt;
use core::marker::PhantomData;

use alloc::string::String;

use crate::node::{NodeId, NodeRef};
use crate::recompose::{
    core_source_instruction, ComposePiece, ConcatPieces, Recompose, RecomposeContext,
    Recomposer,
};

use super::invocation_syntax::EnvironmentSyntax;
use super::lang::{LatexlikeCallableType, LatexlikeInvocationSyntax, LatexlikeLang};
use super::Latexlike;

/// Reconstructs the source text of a parsed latexlike-family tree.
///
/// This is the ready-made [`Recomposer`] for writing a tree back out as markup.
/// Build one with [`source_recomposer`] (or [`Default`]) and run it with a
/// [`TreeRecomposer`](crate::recompose::TreeRecomposer):
///
/// ```
/// use techy::core::specs::Package;
/// use techy::core::{Language, ParsingState};
/// use techy::error::Recovery;
/// use techy::latexlike::{source_recomposer, Latexlike, LatexlikeDriver};
/// use techy::recompose::TreeRecomposer;
///
/// let mut package: Package<Latexlike> = Package::new("mydefs");
/// package.define_macro("emph", ["m"]).unwrap();
/// let language: Language<Latexlike> = Language::new(
///     LatexlikeDriver::new(Recovery::Strict),
///     ParsingState::lang_initial_with_packages([package]).expect("seed state"),
/// );
///
/// let input = "a \\emph{b} % note\n  c";
/// let result = language.parse(input).unwrap();
/// let text: String = TreeRecomposer::new(&mut source_recomposer())
///     .recompose(&result.tree, ())
///     .unwrap();
/// assert_eq!(text, input);
/// ```
///
/// It serves any member of the language family (`LLL:` [`LatexlikeLang`]) and trees
/// carrying any annotation type.
///
/// # What it emits
///
/// Everything comes from what the parse recorded on the nodes themselves; the source
/// text under a node's own span is never read. Chars content, comment parts, and
/// group delimiters come through [`core_source_instruction`]; a callable is written
/// from the family's recorded invocation syntax — the escape character, the name as
/// written, and the post-space for a macro, the two sides' own spelling writers
/// ([`write_begin`](EnvironmentSyntax::write_begin) and
/// [`write_end`](EnvironmentSyntax::write_end)) for an environment, and the name as
/// written for a specials. On a
/// [`materialize`](crate::core::node::NodeTree::materialize)d tree the reconstruction
/// therefore reads no [`Source`](crate::source::Source) at all.
///
/// # What the output equals
///
/// For a tree the latexlike parse produced, the output equals the parsed source byte
/// for byte. Only what the parse recorded can be re-emitted (see
/// [`CallableData::invocation_syntax`](crate::core::node::CallableData::invocation_syntax)),
/// and a tolerant parse records the shapes it recovered as they were written: an
/// environment that never found its terminator has an empty end side and re-emits no
/// terminator, which is exactly what the input had. The one recovery that consumes
/// more than it records is the malformed environment terminator — its `\end` is
/// consumed on its own, diagnosed, and recorded nowhere — so that command spelling is
/// not reproduced.
///
/// Byte-equality with one source presupposes that the tree's nodes tile that source,
/// so it is stated for a language declaring
/// [`Lang::OBEYS_SPAN_TILING`](crate::core::Lang::OBEYS_SPAN_TILING) `= true`, the
/// preset's own [`Latexlike`] included. A family member declaring it `false` has
/// parsers that assume nothing about where their tokens came from; its trees are
/// re-emitted **as stored** — the text the parser recorded, in tree order — and no
/// byte-equality with any one source is claimed for them.
///
/// # How it composes
///
/// `State = ()` and `Piece = `[`String`]. Every node is answered with an instruction
/// alone, and the recomposer never descends by itself, so it also works as the inner
/// recomposer of one that wraps it. It needs no scope call: the default `Concat` scope
/// already skips `Attached` and `Hidden` slot children, so an `\input`'s attached
/// content is re-emitted as the `\input{…}` invocation it came from rather than as the
/// included file's text.
///
/// # Errors
///
/// [`SourceRecomposeError::IncoherentInvocationSyntax`], for a callable whose recorded
/// invocation syntax does not match the role its callable type plays. No parse output
/// can hold such a node, so over a tree a parse produced the recomposition never
/// fails.
///
/// # Panics
///
/// Panics if a span-backed payload field of a node names a range that is not within
/// the content of that node's own source, or does not fall on character boundaries —
/// the panic condition of
/// [`TextContent::resolve`](crate::source::TextContent::resolve). That means a tree
/// invariant is broken, which no parsed input can cause; only a tree assembled by hand
/// through [`NodeTreeBuilder`](crate::core::node::NodeTreeBuilder) can reach it, and
/// [`validate_tree`](crate::core::node::validate_tree) is the check for it.
pub struct SourceRecomposer<LLL: LatexlikeLang = Latexlike> {
    _lang: PhantomData<LLL>,
}

/// Creates a [`SourceRecomposer`], which re-emits a parsed latexlike-family tree as
/// source text.
///
/// Run it with a [`TreeRecomposer`](crate::recompose::TreeRecomposer):
/// `TreeRecomposer::new(&mut source_recomposer()).recompose(&tree, ())`. The type's
/// documentation states what the output is guaranteed to equal, and the one condition
/// under which a run panics.
pub fn source_recomposer<LLL: LatexlikeLang>() -> SourceRecomposer<LLL> {
    SourceRecomposer { _lang: PhantomData }
}

// Manual impls: derives would demand `LLL:` bounds although only PhantomData
// is stored.

impl<LLL: LatexlikeLang> Clone for SourceRecomposer<LLL> {
    fn clone(&self) -> Self {
        SourceRecomposer { _lang: PhantomData }
    }
}

impl<LLL: LatexlikeLang> Default for SourceRecomposer<LLL> {
    fn default() -> Self {
        source_recomposer()
    }
}

impl<LLL: LatexlikeLang> fmt::Debug for SourceRecomposer<LLL> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("SourceRecomposer").finish()
    }
}

/// The error that is returned when a [`SourceRecomposer`] cannot re-emit a callable
/// node.
///
/// A [`TreeRecomposer`](crate::recompose::TreeRecomposer) that runs a
/// [`SourceRecomposer`] returns this error wrapped in
/// [`RecomposeError::Recomposer`](crate::recompose::RecomposeError::Recomposer).
///
/// A parse with the preset's own specs never causes this error. The error can occur
/// in these cases:
///
/// - A tree built by hand with [`NodeTreeBuilder`](crate::core::node::NodeTreeBuilder),
///   or a tree that a restage builds, contains a callable node whose invocation syntax
///   does not match the role of its callable type.
/// - A custom spec stages a callable with a callable type whose role does not match
///   the token that invokes the callable, for instance a specials type for a callable
///   that is invoked by a command.
/// - A language that adds callable types beyond macro, environment and specials
///   stages a callable of such a type.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum SourceRecomposeError {
    /// The invocation syntax that a callable node records does not match the role of
    /// its callable type.
    ///
    /// The source recomposer writes a callable node according to the role of its
    /// callable type: macro, environment or specials. The
    /// [`invocation_syntax`](crate::core::node::CallableData::invocation_syntax) of the
    /// node must record the invocation syntax of the same role, for instance
    /// [`InvocationSyntaxData::Macro`](super::InvocationSyntaxData::Macro) for a macro.
    /// This variant is also returned for a callable type that has none of the three
    /// roles.
    IncoherentInvocationSyntax {
        /// The callable node.
        node: NodeId,
        /// The role of the node's callable type: `"macro"`, `"environment"`,
        /// `"specials"`, or `"unknown"` for a callable type that has none of these
        /// roles.
        callable_form: &'static str,
        /// The role of the invocation syntax that the node records, with the same
        /// values as `callable_form`.
        payload_arm: &'static str,
    },
}

impl fmt::Display for SourceRecomposeError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            SourceRecomposeError::IncoherentInvocationSyntax {
                node,
                callable_form,
                payload_arm,
            } => {
                let callable_type = match *callable_form {
                    "macro" => "a macro type",
                    "environment" => "an environment type",
                    "specials" => "a specials type",
                    _ => {
                        return write!(
                            f,
                            "Unsupported callable type of callable node {}: the source \
                             recomposer supports only macros, environments and specials",
                            node.index()
                        )
                    }
                };
                let recorded = match *payload_arm {
                    "macro" => "macro invocation syntax",
                    "environment" => "environment invocation syntax",
                    "specials" => "specials invocation syntax",
                    _ => "invocation syntax of an unknown form",
                };
                write!(
                    f,
                    "Mismatched invocation syntax in callable node {}: the callable type is \
                     {} and the node records {}",
                    node.index(),
                    callable_type,
                    recorded
                )
            }
        }
    }
}

impl core::error::Error for SourceRecomposeError {}

impl<LLL: LatexlikeLang, A> Recomposer<LLL, A> for SourceRecomposer<LLL> {
    type State = ();
    type Piece = String;
    type Error = SourceRecomposeError;

    /// Returns the instruction that re-emits `node`'s recorded source spelling.
    ///
    /// # Errors
    ///
    /// [`SourceRecomposeError::IncoherentInvocationSyntax`] if `node` is a callable
    /// whose recorded invocation syntax does not match the role its callable type
    /// plays.
    ///
    /// # Panics
    ///
    /// Panics if a span-backed payload field of `node` names a range that is not
    /// within the content of the node's own source, or does not fall on character
    /// boundaries — see [`SourceRecomposer`].
    fn recompose_node(
        &mut self,
        node: NodeRef<'_, LLL, A>,
        _state: &(),
        _cx: &mut RecomposeContext<'_, LLL, A>,
    ) -> Result<Recompose<String, ()>, SourceRecomposeError> {
        // The core-complete kinds: chars, comments, groups, lists.
        if let Some(instruction) = core_source_instruction(node) {
            return Ok(instruction);
        }
        // `core_source_instruction` declines exactly the callables.
        let data = node
            .callable()
            .expect("core_source_instruction answers every non-callable kind");
        let payload = &data.invocation_syntax;
        let payload_arm = payload_arm::<LLL>(payload);
        let source = node.span().source();
        let name: &str = &data.name;

        // Dispatch on the role the callable type plays; the payload must
        // record the matching arm (coherence is unenforced at parse time —
        // this is where a mismatch surfaces).
        let incoherent = |callable_form: &'static str| {
            SourceRecomposeError::IncoherentInvocationSyntax {
                node: node.id(),
                callable_form,
                payload_arm,
            }
        };
        if data.callable_type.is_macro() {
            let Some((escape_char, post_space)) = payload.macro_syntax() else {
                return Err(incoherent("macro"));
            };
            // The trigger spelling as recorded: escape char + name as written
            // + the trigger token's own syntactic post-space; the argument
            // regions follow as children.
            let mut head = String::new();
            head.push(escape_char);
            head.push_str(name);
            head.push_str(post_space.resolve(source));
            Ok(Recompose::Concat(ConcatPieces::children().wrap(head, String::empty())))
        } else if data.callable_type.is_environment() {
            let Some(env) = payload.environment_syntax() else {
                return Err(incoherent("environment"));
            };
            // The record's own spelling writers, one per side (an empty end
            // side writes "" — recovered shapes reemit what was recorded).
            Ok(Recompose::Concat(ConcatPieces::children().wrap(
                env.write_begin(name, source),
                env.write_end(name, source),
            )))
        } else if data.callable_type.is_specials() {
            if !payload.is_specials() {
                return Err(incoherent("specials"));
            }
            // Name-as-written IS the invocation spelling (paragraph-break
            // Specials nodes record the whole whitespace run as name);
            // arguments, when a specials spec declares any, follow as
            // children.
            Ok(Recompose::Concat(
                ConcatPieces::children().wrap(String::from(name), String::empty()),
            ))
        } else {
            // A callable form outside the family's three roles: this
            // recomposer has no spelling rule for it.
            Err(incoherent("unknown"))
        }
    }
}

/// The label of the arm `payload` records (for the coherence diagnosis).
fn payload_arm<LLL: LatexlikeLang>(payload: &LLL::InvocationSyntax) -> &'static str {
    if payload.macro_syntax().is_some() {
        "macro"
    } else if payload.environment_syntax().is_some() {
        "environment"
    } else if payload.is_specials() {
        "specials"
    } else {
        "unknown"
    }
}

#[cfg(test)]
mod tests {
    use alloc::sync::Arc;
    use alloc::vec;
    use alloc::vec::Vec;

    use super::super::test_support::{macro_package, strict, with_package};
    use super::super::{
        argument_specs, CallableType, EnvironmentSpec, InvocationSyntaxData, Latexlike,
        LatexlikeDriver, MacroSpec, ParagraphBreakStyle, SpecialsSpec, VerbatimBehavior,
    };
    use super::*;
    use crate::engine::Language;
    use crate::error::Recovery;
    use crate::latexlike::check_latexlike_tree_invariants;
    use crate::node::{
        BuildId, CallableData, NodeKind, NodeTree, NodeTreeBuilder, ParsedArguments,
        ParsedSlots,
    };
    use crate::recompose::{RecomposeError, TreeRecomposer};
    use crate::scopes::Package;
    use crate::source::{Source, SourceSpan};
    use crate::state::ParsingState;

    /// The suite's shorthand: a default-configured [`TreeRecomposer`] run.
    fn recompose<A, R>(
        tree: &NodeTree<Latexlike, A>,
        state: R::State,
        recomposer: &mut R,
    ) -> Result<R::Piece, RecomposeError<R::Error>>
    where
        R: Recomposer<Latexlike, A>,
    {
        TreeRecomposer::new(recomposer).recompose(tree, state)
    }

    /// Parse strictly with `language`, assert a clean parse, and assert the
    /// reemission equals the input byte-for-byte.
    fn assert_reemit(language: &Language<Latexlike>, input: &str) {
        let result = language.parse(input).expect("test inputs parse");
        check_latexlike_tree_invariants(&result.tree);
        assert!(result.diagnostics.is_empty(), "unexpected: {:?}", result.diagnostics);
        let out = recompose(&result.tree, (), &mut source_recomposer()).unwrap();
        assert_eq!(out, input);
    }

    #[test]
    fn macros_reemit_their_recorded_spelling() {
        let language = with_package(Recovery::Strict, macro_package("t", "emph", None));
        assert_reemit(&language, "\\emph  x");
        assert_reemit(&language, "\\emph{x}");
        assert_reemit(&language, "pre \\emph post");
    }

    #[test]
    fn arguments_reemit_incl_optional_and_single_token_shapes() {
        let mut package = Package::new("t");
        package.insert(
            CallableType::Macro,
            "frac",
            MacroSpec::new(argument_specs(&["m", "m"]).unwrap()),
        );
        package.insert(
            CallableType::Macro,
            "o",
            MacroSpec::new(argument_specs(&["[", "{"]).unwrap()),
        );
        let language = with_package(Recovery::Strict, package);
        assert_reemit(&language, "\\frac 1 2");
        assert_reemit(&language, "\\frac{a}{b}");
        assert_reemit(&language, "\\o[x]{y}");
        assert_reemit(&language, "\\o{y} after"); // absent optional
        assert_reemit(&language, "\\o [x] {y}"); // inter-argument noise
    }

    fn env_language(recovery: Recovery) -> Language<Latexlike> {
        let mut package = Package::new("t");
        package.insert(CallableType::Environment, "itemize", EnvironmentSpec::new(vec![]));
        package.insert(
            CallableType::Environment,
            "verbatim",
            EnvironmentSpec::from_behavior(Arc::new(VerbatimBehavior::default())),
        );
        with_package(recovery, package)
    }

    #[test]
    fn environments_reemit_both_sides_from_the_record() {
        let language = env_language(Recovery::Strict);
        assert_reemit(&language, "\\begin{itemize}x\\end{itemize}");
        // The tolerated `\begin {itemize}` spacing is recorded, not
        // normalized — reemission reproduces it.
        assert_reemit(&language, "\\begin {itemize}x y\\end {itemize}");
        assert_reemit(
            &language,
            "\\begin{itemize}a\\begin{itemize}b\\end{itemize}c\\end{itemize}",
        );
    }

    #[test]
    fn verbatim_reemits_every_byte() {
        let mut package = Package::new("t");
        package.insert(
            CallableType::Environment,
            "verbatim",
            EnvironmentSpec::from_behavior(Arc::new(VerbatimBehavior::default())),
        );
        package.insert(
            CallableType::Macro,
            "verb",
            MacroSpec::new(argument_specs(&["v"]).unwrap()),
        );
        let language = with_package(Recovery::Strict, package);
        assert_reemit(&language, "\\begin{verbatim}\na % b {\n\\end{verbatim}");
        assert_reemit(&language, "\\verb|a b|");
    }

    #[test]
    fn specials_and_paragraph_breaks_reemit_name_as_written() {
        // The seed's base specials (ligatures, ~).
        assert_reemit(&strict(), "a---b ~ c");

        // Paragraph breaks, both driver styles: the Chars shape is a plain
        // whitespace node; the Specials shape records the actual run as name.
        assert_reemit(&strict(), "a\n\n  b");
        let language: Language<Latexlike> = Language::new(
            LatexlikeDriver::new(Recovery::Strict)
                .with_paragraph_break_style(ParagraphBreakStyle::Specials),
            ParsingState::lang_initial().expect("seed state"),
        );
        assert_reemit(&language, "a\n \t\nb");
    }

    #[test]
    fn math_groups_and_comments_reemit() {
        assert_reemit(&strict(), "inline $x+y$ math");
        assert_reemit(&strict(), "\\[d^2\\] and $$s$$ and \\(i\\)");
        assert_reemit(&strict(), "a% note\n  b");
        assert_reemit(&strict(), "{nested {groups} here}");
    }

    #[test]
    fn tolerant_recovery_shapes_reemit_what_was_recorded() {
        // Unterminated environment: the end side is empty, write_end emits
        // nothing — and the input had no terminator: reemit == input.
        let language = env_language(Recovery::Tolerant);
        let result = language.parse("\\begin{itemize}x").unwrap();
        assert!(!result.diagnostics.is_empty());
        let out = recompose(&result.tree, (), &mut source_recomposer()).unwrap();
        assert_eq!(out, "\\begin{itemize}x");
    }

    #[test]
    fn a_materialized_tree_reemits_source_free() {
        let language = env_language(Recovery::Strict);
        let input = "\\begin {itemize}x\\end{itemize} $m$ \\begin{verbatim}r\\end{verbatim}";
        let result = language.parse(input).unwrap();
        let owned = result.tree.materialize();
        // Every payload is owned now; the writers resolve with no source
        // content at all (source-independent byte-faithful reconstruction).
        let out = recompose(&owned, (), &mut source_recomposer()).unwrap();
        assert_eq!(out, input);
    }

    #[test]
    fn an_incoherent_payload_is_a_diagnosed_error() {
        // Hand-build a macro-typed callable carrying the Specials arm (a
        // parse never produces this).
        let source: Arc<Source> = Arc::new(Source::new("\\emph x"));
        let state = Arc::new(ParsingState::<Latexlike>::lang_initial().expect("seed state"));
        let mut builder: NodeTreeBuilder<Latexlike> = NodeTreeBuilder::new();
        let node = builder
            .add(
                NodeKind::callable(CallableData {
                    callable_type: CallableType::Macro,
                    name: "emph".into(),
                    spec: Arc::new(MacroSpec::default()),
                    arguments: ParsedArguments::empty(),
                    slots: ParsedSlots::empty(),
                    invocation_syntax: InvocationSyntaxData::Specials,
                }),
                SourceSpan::new(&source, 0..5),
                Arc::clone(&state),
                Vec::<BuildId>::new(),
                (),
                (),
            )
            .unwrap();
        let root = builder
            .add(
                NodeKind::list(),
                SourceSpan::new(&source, 0..5),
                Arc::clone(&state),
                vec![node],
                (),
                (),
            )
            .unwrap();
        let tree: NodeTree<Latexlike> = builder.finish(root).unwrap();

        let error = recompose(&tree, (), &mut source_recomposer()).unwrap_err();
        match error {
            RecomposeError::Recomposer(SourceRecomposeError::IncoherentInvocationSyntax {
                callable_form,
                payload_arm,
                ..
            }) => {
                assert_eq!((callable_form, payload_arm), ("macro", "specials"));
            }
            other => panic!("expected the coherence error, got {other:?}"),
        }
    }

    #[test]
    fn specials_with_arguments_reemit_the_argument_regions() {
        // A specials spec with a mandatory brace argument: the name is the
        // head, the argument region follows as children.
        let mut package = Package::new("t");
        let mut spec = SpecialsSpec::<Latexlike>::default();
        spec.arguments = argument_specs(&["{"]).unwrap();
        package.insert(CallableType::Specials, "!!", spec);
        let language = with_package(Recovery::Strict, package);
        assert_reemit(&language, "a!!{b}c");
    }

    #[test]
    fn the_attached_slot_is_skipped_by_default_and_widens_on_opt_in() {
        use crate::latexlike::{input_macro_spec, BodyMarker};
        use crate::source::MapResolver;

        let mut package = Package::new("inputs");
        package.insert(
            CallableType::Macro,
            "input",
            input_macro_spec(false, BodyMarker::not_body()),
        );
        let mut resolver = MapResolver::new();
        resolver.insert("sub.tex", "SUB");
        let language: Language<Latexlike> = Language::new(
            LatexlikeDriver::new(Recovery::Strict)
                .with_source_resolver(resolver.with_reference_as_origin()),
            ParsingState::lang_initial_with_packages([package]).expect("seed state"),
        );
        let input = "a\\input{sub.tex}b";
        let result = language.parse(input).unwrap();
        assert!(result.diagnostics.is_empty(), "unexpected: {:?}", result.diagnostics);

        // Default scope: the attached content does not reemit — the
        // invocation text is its recomposition; reemit == the includer.
        let out = recompose(&result.tree, (), &mut source_recomposer()).unwrap();
        assert_eq!(out, input);

        // Widening (a wrapper overriding the \input node): the attached
        // region participates — the included bytes appear in place of
        // nothing having been emitted for them before.
        struct Widened {
            inner: SourceRecomposer<Latexlike>,
        }
        impl<A> Recomposer<Latexlike, A> for Widened {
            type State = ();
            type Piece = String;
            type Error = SourceRecomposeError;

            fn recompose_node(
                &mut self,
                node: NodeRef<'_, Latexlike, A>,
                state: &(),
                cx: &mut RecomposeContext<'_, Latexlike, A>,
            ) -> Result<Recompose<String, ()>, SourceRecomposeError> {
                match self.inner.recompose_node(node, state, cx)? {
                    Recompose::Concat(pieces) if node.name() == Some("input") => {
                        Ok(Recompose::Concat(pieces.include_attached()))
                    }
                    other => Ok(other),
                }
            }
        }
        let out = recompose(
            &result.tree,
            (),
            &mut Widened { inner: source_recomposer() },
        )
        .unwrap();
        assert_eq!(out, "a\\input{sub.tex}SUBb");
    }
}