techy 0.1.1

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
//! The parser for one delimited group: [`GroupParser`].
//!
//! It reads a group's interior content up to and including the close delimiter and
//! stages a `Group` node (pylatexenc's `LatexDelimitedGroupParser`). Every `{…}`, `[…]`
//! or `$…$` in a parsed tree comes from a run of it, whether the group was met in
//! ordinary content or as an argument's delimited form.
//!
//! # Matching and recovery
//!
//! The interior [`NodesParser`] stops at the exact `(group class, close delimiter)`
//! pairing the group opened with, consuming the close as it matches it, and the `Group`
//! node records both delimiters ([`GroupData`]). Two things can happen instead, and both
//! are diagnosed here, as [`UnclosedGroup`]:
//!
//! - *The input ends inside the group*: the diagnostic points at the open delimiter, and
//!   the group is closed where the input ended.
//! - *A close delimiter of a different pairing appears* — a `]` under a `{`, or a `}` a
//!   state change has re-classed: the diagnostic points at that delimiter, and the group
//!   is closed **without consuming** it. This is the same unwinding rule as an
//!   environment-terminator mismatch: every level either consumes the token or unwinds
//!   out of its own frame, so the stray close eventually reaches a level that claims it,
//!   or the root, which diagnoses and skips it.
//!
//! In both recovery situations the group node records an empty close delimiter, which is
//! how a reader of the tree recognizes a group that was never closed. Under
//! [`Recovery::Strict`](crate::error::Recovery) both situations abort the parse instead.
//!
//! # How the parser is called
//!
//! It is constructed with the opening delimiter's token and the [`GroupRule`] that the
//! [`GroupOpen`](crate::core::token::TokenKind::GroupOpen) token view resolved for it:
//! the token so that the parser can ask the reader about the delimiter, the rule so that
//! it knows which close to match.
//!
//! **The caller consumes the opening token before running the parser** — the dispatch
//! arm that peeked it, under the state that tokenized it, so that the token is claimed at
//! the moment it matched. The token's pre-space is the caller's too, staged as sibling
//! content.
//!
//! `cx.state` is the interior's **base** state; the caller resolves any
//! [`ChildStateSpec`](super::ChildStateSpec) policy first. From it the parser derives the
//! interior state — the base plus `expecting_group_close` taken from the opening rule,
//! through the session's memoized
//! [`group_interior_state`](crate::core::ParserSession::group_interior_state) — so that
//! the close delimiter is recognizable whatever the base state's delimiter table holds.
//! That is what closes a `$…$` group, whose ambiguous `$` is a close only through the
//! expected-close rule. The interior state is scoped structurally: `cx.state` is swapped
//! for the recursion and restored afterwards, so a state change made inside the group
//! ends with it.
//!
//! # Letting a definition escape the group (the `\gdef` shape)
//!
//! By default the after-effect delta the parser returns is `None`: the interior run's
//! merged record ([`NodesOutcome::after_effects`](super::NodesOutcome::after_effects)) is
//! dropped with the descent, which is what makes a definition group-scoped. A language
//! whose constructs may escape their enclosing group — TeX's `\gdef` — installs the
//! [`GroupAfterEffectsFn`] hook
//! ([`new_with_after_effects`](GroupParser::new_with_after_effects), normally from an
//! overridden [`make_group_parser`](crate::core::ParseDriver::make_group_parser)): it maps
//! the interior record to the group's own after-effect for its caller. What the hook can
//! and cannot tell apart is documented on that type.

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

use crate::engine::{Frame, FrameTitle};
use crate::error::{DiagnosticInfo, ParseError, ToDiagnosticValue};
use crate::node::{BuildId, GroupData, NodeKind};
use crate::source::TextContent;
use crate::state::{Lang, ParsingState, ParsingStateDelta};
use crate::token::{GroupRule, Token, TokenEdge};

use super::child_state::ChildStateSpec;
use super::nodes_parser::{StopCause, StopSpec, TokenStopKind};
use super::{node_text_content, ConstructParser, ConstructParserResult, FromInvocation, ParseContext};

/// Condition: a delimited group was never closed with its expected delimiter.
///
/// Raised by [`GroupParser`]. Under tolerant recovery the group is closed anyway, and its
/// node records an empty close delimiter
/// ([`GroupData::close`](crate::core::node::GroupData::close)); a strict parse aborts.
/// [`found`](UnclosedGroup::found) says what blocked the close, and that decides what
/// happens to the token the parser stopped at:
///
/// - at end of input there is no such token;
/// - on a close delimiter of a different pairing, the group closes **without consuming**
///   that token, which is left for an enclosing level to claim — or for the root, which
///   diagnoses and skips it.
#[derive(Debug, Clone, PartialEq, Eq, DiagnosticInfo)]
#[non_exhaustive]
#[diagnostic(id = "core.groups.unclosed-group")]
pub struct UnclosedGroup {
    /// The close delimiter the group expected (as written, e.g. `}`).
    pub expected_close: String,
    /// What blocked the close instead.
    pub found: UnclosedGroupFound,
}

/// What an [`UnclosedGroup`] ran into instead of its close delimiter.
#[derive(Debug, Clone, Copy, PartialEq, Eq, ToDiagnosticValue)]
#[non_exhaustive]
pub enum UnclosedGroupFound {
    /// The input ended inside the group.
    EndOfInput,
    /// A close delimiter belonging to a different pairing appeared (the group unwinds,
    /// leaving the stray token for an enclosing level to claim).
    StrayClose,
}

// Hand-written wording: the message varies by what blocked the close (a match, which
// the message format string cannot express). The two recovery situations read
// differently on purpose (July 2026, Action 06): EOF points at the open delimiter of a
// group nothing will ever close; a stray close points at the delimiter that broke the
// pairing.
impl fmt::Display for UnclosedGroup {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self.found {
            UnclosedGroupFound::EndOfInput => write!(
                f,
                "unclosed group: expected ‘{}’ before end of input",
                self.expected_close
            ),
            UnclosedGroupFound::StrayClose => {
                write!(f, "mismatched group close: expected ‘{}’", self.expected_close)
            }
        }
    }
}

/// The callback that lets a group pass an after-effect out to its caller, so that a
/// definition made inside the group survives it (the `\gdef` shape).
///
/// It maps the interior content run's merged after-effect record to the delta the group
/// returns as its own after-effect. Installed per descent with
/// [`GroupParser::new_with_after_effects`], normally from an overridden
/// [`make_group_parser`](crate::core::ParseDriver::make_group_parser) so that every group
/// descent of the language has it.
///
/// The arguments, in order:
///
/// 1. The **matched rule** — the pairing the group opened with, so a hook can key on the
///    group *class*: a language may let `\gdef` escape a `{…}` group but nothing escape
///    a `$…$` one.
/// 2. The **interior's initial state** — what the interior parsed under before any
///    sibling after-effect evolved it (base + `expecting_group_close` + the driver's
///    [`group_interior_delta`](crate::core::ParseDriver::group_interior_delta)).
/// 3. The **interior's exit state** ([`NodesOutcome::state`](super::NodesOutcome::state))
///    — the state the interior run actually reached, and the only place the definitions
///    the record made are inspectable (`state.scopes().retrieve_spec(…)`). It is
///    discarded with the descent; the hook is the last reader.
/// 4. The **interior's merged record**
///    ([`NodesOutcome::after_effects`](super::NodesOutcome::after_effects)), passed **by
///    value** so the hook may filter it in place and return the same box rather than
///    cloning.
///
/// The return is the group's after-effect for its caller — `None` for the ordinary
/// "nothing escapes" answer, which is also what a group with no hook returns.
///
/// # What a hook can tell apart
///
/// The record is **one merged delta**: rules overrides are merged last-writer-wins, and
/// scope operations and events are concatenated in application order. It does not record
/// which construct contributed what.
///
/// A `\gdef`-versus-`\def` split is therefore expressed structurally, by the language
/// tagging its own operations: `\gdef` emits a
/// [`ScopeOp::Define`](crate::core::specs::ScopeOp) against a globally-named scope and
/// `\def` a local one, after which the hook keeps the globally-targeted operations and
/// drops the rest (read them through
/// `<L::Features as LangFeatures>::Scopes::store_get(&delta.scope_ops)`). The rules, mode
/// and extension overrides have no such tag and merge last-writer-wins, so for those the
/// only honest answers are all or nothing.
///
/// Escapes compose outward, one level per hook: the enclosing content loop applies the
/// returned delta to its own state *and* merges it into its own record, so the next group
/// out sees it in argument 4 and may let it escape again.
///
/// # Errors
///
/// `Err` **aborts the parse** under any recovery policy, propagated exactly like a
/// construct parser's own `Err`, with the live traceback attached at the descent site —
/// the callback has no session access. Same condition choice as
/// [`GroupChildState::Compute`](super::GroupChildState::Compute):
/// [`HookFailed`](crate::error::HookFailed) for an operational failure in the callback's
/// own code, [`ImplementationError`](super::ImplementationError) for a violated library
/// contract. An infallible hook wraps its answer in `Ok(…)` and that is the only change.
///
/// Deterministic and side-effect free, like the [`ChildStateSpec`] callbacks: a hook
/// whose answer depends on call order would be fragile.
#[allow(clippy::type_complexity)]
pub type GroupAfterEffectsFn<'p, L> = &'p dyn Fn(
    &Arc<GroupRule<L>>,
    &Arc<ParsingState<L>>,
    &Arc<ParsingState<L>>,
    Option<Box<ParsingStateDelta<L>>>,
) -> Result<
    Option<Box<ParsingStateDelta<L>>>,
    ParseError<<L as Lang>::SourceOrigin>,
>;

/// Parses one delimited group — `{a b}`, `[…]`, `$…$` — and stages its `Group` node.
///
/// Constructed for a single group descent from the opening token and its resolved
/// [`GroupRule`](crate::core::token::GroupRule), then run once and dropped. The caller has
/// already consumed the opening token, and `cx.state` is the interior's **base** state,
/// from which the parser derives the actual interior state — the base plus the expected
/// close delimiter taken from the opening rule — scoped structurally over the descent.
///
/// Recovery for a group that never closes is documented on [`UnclosedGroup`].
pub struct GroupParser<'p, L: Lang> {
    /// The consumed `GroupOpen` token: the group's open delimiter, as the reader
    /// records it (its span and its stream position come from the reader).
    open: Token<L>,
    /// The opening token's resolved rule: the close spelling and group class of the
    /// pairing to match.
    rule: Arc<GroupRule<L>>,
    /// Descent-state policy passed to the interior [`NodesParser`]; defaults to
    /// inherit-everywhere. A policy reaches one level deep, so a plain arm-driven descent
    /// never propagates one. A parser that scopes the group's interior state sets it per
    /// use — the chars-except-groups argument pattern does, its group interiors reverting
    /// to the outer, unrestricted state.
    child_states: ChildStateSpec<'p, L>,
    /// The hook that lets an after-effect escape the group; `None` for the default, under
    /// which nothing escapes. See [`GroupAfterEffectsFn`].
    compute_after_effects: Option<GroupAfterEffectsFn<'p, L>>,
}

impl<'p, L: Lang> GroupParser<'p, L> {
    /// A parser for the group opened by `open`, an already-consumed `GroupOpen` token,
    /// with the rule `rule` that token resolved to.
    pub fn new(open: Token<L>, rule: Arc<GroupRule<L>>) -> GroupParser<'p, L> {
        GroupParser {
            open,
            rule,
            child_states: ChildStateSpec::inherit(),
            compute_after_effects: None,
        }
    }

    /// [`new`](GroupParser::new) with the escape hook installed: instead of dropping the
    /// interior run's merged after-effect record with the descent, the parser passes it to
    /// `compute_after_effects`, whose answer becomes the group's own after-effect for its
    /// caller (the `\gdef` shape).
    ///
    /// The hook may still answer `None`, which is what a group without one does.
    ///
    /// A hook is installed per descent, so the place to plug one in is an overridden
    /// [`make_group_parser`](crate::core::ParseDriver::make_group_parser): that gives a
    /// language the hook at *every* group site, which is what makes an escape compose
    /// outward through nested groups. The full contract, including what the merged record
    /// can and cannot tell apart, is on [`GroupAfterEffectsFn`].
    pub fn new_with_after_effects(
        open: Token<L>,
        rule: Arc<GroupRule<L>>,
        compute_after_effects: GroupAfterEffectsFn<'p, L>,
    ) -> GroupParser<'p, L> {
        GroupParser {
            open,
            rule,
            child_states: ChildStateSpec::inherit(),
            compute_after_effects: Some(compute_after_effects),
        }
    }

    /// Replaces the descent-state policy the group's interior runs under; the default
    /// inherits everywhere. See [`ChildStateSpec`].
    pub fn with_child_states(mut self, child_states: ChildStateSpec<'p, L>) -> Self {
        self.child_states = child_states;
        self
    }
}

impl<L: Lang> ConstructParser<L> for GroupParser<'_, L>
where
    L::InvocationSyntax: FromInvocation<L>,
{
    type Output = BuildId;

    fn parse(
        &mut self,
        cx: &mut ParseContext<'_, '_, L>,
    ) -> ConstructParserResult<L, (BuildId, Option<Box<ParsingStateDelta<L>>>)> {
        // The interior state: base + expecting_group_close + the driver's descent
        // delta, session-memoized (one derivation per (base, rule); every descent
        // still reaches observe_transition).
        let interior_state = cx.group_interior_state(&self.rule)?;

        // Recurse under the interior state (structural swap/revert — the state-threading
        // convention). The stop condition names the exact pairing the group opened with,
        // and consumes the close at match time.
        let stop = StopSpec::at_token(
            TokenStopKind::GroupClose {
                group_type: self.rule.group_type,
                close: &self.rule.close,
            },
            true,
        );
        // The group-interior traceback frame ([§dd-dr:errors]): conditions detected inside the
        // group carry `group ‘{’` @ the open delimiter in their snapshot.
        let open_span = cx.tokens.source_span_of(&self.open);
        let frame = Frame {
            title: FrameTitle::Quoted { label: "group", name: open_span.clone() },
            span: open_span.clone(),
        };
        let child_states = self.child_states.clone();
        // The interior state is cloned into the descent: the hook (when installed) reads
        // the state the interior *started* under, which the run itself no longer holds
        // once a sibling after-effect has evolved it.
        let (outcome, delta) = cx.with_frame(frame, |cx| {
            cx.parse_nodes(Arc::clone(&interior_state), stop, child_states)
        })?;
        // The content-loop parser comes from the driver's factory (outer-layer code
        // for a custom driver), so its contract is validated, not debug-asserted
        // ([§dd-dr:panic-policy]).
        if delta.is_some() {
            return Err(cx.implementation_error(
                "the driver's content-loop parser returned a pass-through state delta \
                 (a nodes parser has no after-effect to report)",
                cx.here(),
            ));
        }

        // The close delimiter's own span, and the stream position the group ends at.
        let (close_span, end) = match outcome.stop {
            // The close was consumed at match time; its span becomes the recorded
            // delimiter (it cannot be re-peeked — hence the span on the cause).
            StopCause::TokenCondition { span, after } => (Some(span), after),
            StopCause::EndOfInput => {
                cx.recover(
                    UnclosedGroup::new(&*self.rule.close, UnclosedGroupFound::EndOfInput),
                    open_span.clone(),
                )?;
                (None, cx.tokens.position_here())
            }
            // A close that matches neither field of the pairing: unwind — close this
            // group here, leave the token for an enclosing level (or the root) to claim.
            StopCause::UnexpectedGroupClose { span, .. } => {
                cx.recover(
                    UnclosedGroup::new(&*self.rule.close, UnclosedGroupFound::StrayClose),
                    span,
                )?;
                (None, cx.tokens.position_here())
            }
            StopCause::NodeCondition => {
                return Err(cx.implementation_error(
                    "the driver's content-loop parser reported a node-condition stop, \
                     but the group interior's stop spec sets no node condition",
                    cx.here(),
                ));
            }
        };

        let span = cx
            .source_span_within(&cx.tokens.position_at(&self.open, TokenEdge::Start), &end)?;
        // The recorded delimiters are node data: a node-relative span of the node's
        // own source, or the delimiter's text when it comes from another source.
        let open = node_text_content(&open_span, &span);
        let close = match &close_span {
            Some(close_span) => node_text_content(close_span, &span),
            None => TextContent::empty(),
        };
        let data = GroupData { group_type: Some(self.rule.group_type), open, close };
        let id = cx
            .stage_node(
                NodeKind::group(data),
                span.clone(),
                Arc::clone(&cx.state),
                outcome.nodes,
            )
            .map_err(|error| cx.staging_error(error, span))?;

        // The group's after-effect for its caller. With no hook installed the interior
        // run's merged record dies with the descent (`None` — the scoped-descent default,
        // which is what makes a definition group-scoped); with one, the hook decides what
        // escapes ([`GroupAfterEffectsFn`]). A hook `Err` aborts under any recovery
        // policy, with the live traceback attached here — the callback has no session
        // access.
        let after_effects = match self.compute_after_effects {
            None => None,
            Some(compute) => compute(
                &self.rule,
                &interior_state,
                &outcome.state,
                outcome.after_effects,
            )
            .map_err(|error| cx.attach_hook_frames(error))?,
        };
        Ok((id, after_effects))
    }
}

impl<L: Lang> core::fmt::Debug for GroupParser<'_, L> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.debug_struct("GroupParser")
            .field("open", &self.open)
            .field("rule", &self.rule)
            .field("child_states", &self.child_states)
            .field(
                "compute_after_effects",
                match &self.compute_after_effects {
                    Some(_) => &"Some(..)",
                    None => &"None",
                },
            )
            .finish()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::engine::ParserSession;
    use crate::error::Recovery;
    use crate::scopes::ScopeStack;
    use crate::source::Source;
    use crate::state::{ParsingState, TrivialLang, StateData};
    use crate::token::{
        CommandRules, CommentRules, ForbiddenCharsRules, GroupRules, ParagraphRules,
        SpecialsRules, StdToken, StdTokenReader, TokenKind, TokenReader, TokenRules,
        WhitespaceRules,
    };
    use alloc::vec;
    use alloc::vec::Vec;

    #[derive(Debug, Clone, Copy)]
    struct TestLang;
    impl TrivialLang for TestLang {}

    fn rules() -> TokenRules<TestLang> {
        TokenRules {
            whitespace: WhitespaceRules { enabled: true, chars: " \t\n".into() },
            paragraphs: ParagraphRules { enabled: true },
            groups: GroupRules {
                enabled: true,
                rules: vec![Arc::new(GroupRule {
                    group_type: 0,
                    open: "{".into(),
                    close: "}".into(),
                })],
                temporary: Vec::new(),
                expecting_close: None,
            },
            commands: CommandRules {
                enabled: true,
                rules: Vec::new(),
            },
            comments: CommentRules {
                enabled: true,
                rules: Vec::new(),
            },
            specials: SpecialsRules { enabled: true },
            forbidden_chars: ForbiddenCharsRules { chars: "".into() },
        }
    }

    fn state() -> Arc<ParsingState<TestLang>> {
        Arc::new(ParsingState::new(StateData {
            rules: rules(),
            scopes: ScopeStack::new(),
            mode: (),
            ext: (),
        }))
    }

    /// Drive a lone `GroupParser` (no enclosing loop): peek the open token, consume it
    /// (the caller's job per the contract), run, freeze.
    fn parse_group(
        content: &str,
        recovery: Recovery,
    ) -> (crate::engine::ParseResult<TestLang>, usize) {
        let source: Arc<Source> = Arc::new(Source::new(content));
        let st = state();
        let mut reader = StdTokenReader::new(&source);
        let open: StdToken<TestLang> = TokenReader::peek(&mut reader, &st).unwrap();
        let reader_ref: &dyn TokenReader<'_, TestLang> = &reader;
        let TokenKind::GroupOpen { rule, .. } = reader_ref.token_kind(&open) else {
            panic!("test content must start with a group open")
        };
        let rule = Arc::clone(rule);
        TokenReader::<'_, TestLang>::move_to(&mut reader, &open, TokenEdge::EndPastPostSpace);
        let mut session = ParserSession::new();
        let driver = crate::engine::StdParseDriver::new(recovery, ());
        let mut cx = ParseContext::new(
            &mut reader,
            Arc::clone(&st),
            &mut session,
            &driver);
        let mut parser = GroupParser::new(open.clone(), rule);
        let (id, delta) = parser.parse(&mut cx).unwrap();
        assert!(delta.is_none());
        // The reader's position, as a byte offset, through its own answer.
        let pos = cx.here().start();
        (session.finish(id).unwrap(), pos)
    }

    #[test]
    fn direct_drive_round_trips_a_group() {
        let (result, pos) = parse_group("{ab} rest", Recovery::Strict);
        let root = result.tree.root();
        assert!(root.is_group());
        assert_eq!(root.span().range(), 0..4);
        assert_eq!(root.group_delimiters(), Some(("{", "}")));
        assert_eq!(root.group_type(), Some(0));
        assert_eq!(root.child_count(), 1);
        assert_eq!(root.child(0).unwrap().chars(), Some("ab"));
        // The close is consumed; the following space is *content* of the enclosing
        // level (a group has no post-space) and stays unread.
        assert_eq!(pos, 4);
        assert!(result.diagnostics.is_empty());
    }

    #[test]
    fn the_after_effects_hook_sees_the_interior_and_its_answer_reaches_the_caller() {
        // Direct drive with the leak hook installed: the group's returned delta is the
        // hook's answer verbatim, and the hook sees the interior's two states. `{ab}`
        // holds no construct with an after-effect, so the record arrives as `None` and
        // the interior never left the state it started in.
        let source: Arc<Source> = Arc::new(Source::new("{ab}"));
        let st = state();
        let mut reader = StdTokenReader::new(&source);
        let open: StdToken<TestLang> = TokenReader::peek(&mut reader, &st).unwrap();
        let reader_ref: &dyn TokenReader<'_, TestLang> = &reader;
        let TokenKind::GroupOpen { rule, .. } = reader_ref.token_kind(&open) else {
            panic!("test content must start with a group open")
        };
        let rule = Arc::clone(rule);
        TokenReader::<'_, TestLang>::move_to(&mut reader, &open, TokenEdge::EndPastPostSpace);
        let mut session = ParserSession::new();
        let driver = crate::engine::StdParseDriver::new(Recovery::Strict, ());
        let mut cx = ParseContext::new(&mut reader, Arc::clone(&st), &mut session, &driver);

        let seen = core::cell::Cell::new(0usize);
        let hook = |seen_rule: &Arc<GroupRule<TestLang>>,
                    initial: &Arc<ParsingState<TestLang>>,
                    exit: &Arc<ParsingState<TestLang>>,
                    record: Option<Box<ParsingStateDelta<TestLang>>>| {
            seen.set(seen.get() + 1);
            assert_eq!(seen_rule.group_type, 0);
            // The interior parsed under the derived expecting-close state, not the
            // group's own input state…
            assert!(!Arc::ptr_eq(initial, &st));
            // …and nothing evolved it, so the run exited where it started.
            assert!(Arc::ptr_eq(initial, exit));
            assert!(record.is_none());
            // An answer the caller could not have produced itself, to pin the channel.
            Ok(Some(Box::new(ParsingStateDelta::new().mode(()))))
        };
        let mut parser = GroupParser::new_with_after_effects(open.clone(), rule, &hook);
        let (id, delta) = parser.parse(&mut cx).unwrap();
        assert_eq!(seen.get(), 1);
        assert!(delta.is_some());

        let result = session.finish(id).unwrap();
        assert!(result.tree.root().is_group());
        assert!(result.diagnostics.is_empty());
    }

    #[test]
    fn direct_drive_unclosed_group_recovers_with_empty_close() {
        let (result, pos) = parse_group("{ab", Recovery::Tolerant);
        let root = result.tree.root();
        assert_eq!(root.span().range(), 0..3);
        assert_eq!(root.group_delimiters(), Some(("{", "")));
        assert_eq!(root.child(0).unwrap().chars(), Some("ab"));
        assert_eq!(pos, 3);
        assert_eq!(result.diagnostics.len(), 1);
        let diagnostic = result.diagnostics.iter().next().unwrap();
        // The wording stays covered by a Display assertion…
        assert_eq!(diagnostic.message(), "unclosed group: expected ‘}’ before end of input");
        // …while machine consumers read the structured condition ([§dd-dr:errors]).
        assert_eq!(diagnostic.identifier(), UnclosedGroup::IDENTIFIER);
        let condition = diagnostic.data().downcast_ref::<UnclosedGroup>().unwrap();
        assert_eq!(condition.expected_close, "}");
        assert_eq!(condition.found, UnclosedGroupFound::EndOfInput);
        // The diagnostic points at the open delimiter that was never closed.
        assert_eq!(diagnostic.span().range(), 0..1);
    }
}