techy 0.1.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
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
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
# Learn techy by example

A tour of the `latexlike` preset — the familiar LaTeX behavior assembled from techy's
generic core — in small, complete, runnable examples. Every code block on this page is
compiled and executed as a doctest, so what you read here is what the library does.
The examples use `unwrap()` for brevity; real embedders will want to keep the
`Result`s (every fallible operation returns one — techy never panics on input). Where
pylatexenc is mentioned, the repository's acceptance suite
(`techy/tests/acceptance.rs`) pins the parity claim.

## Your first parse

A [`Language`](crate::core::Language) bundles everything that outlives one parse:
the seed parsing state and the parse driver (which carries the optional source
resolver for `\input`-like lookups). Define it once, parse
many documents. The [`Latexlike`](crate::latexlike::Latexlike) defaults give you the
canonical tokenization (`\` commands, `{…}` groups, `$…$` math, `%` comments) and the
`"_builtin"` package (the `\begin`/`\end` environment dispatch):

```rust
use techy::core::{Language, ParsingState};
use techy::error::Recovery;
use techy::latexlike::{Latexlike, LatexlikeDriver};

let language: Language<Latexlike> = Language::new(
    LatexlikeDriver::new(Recovery::Strict),
    ParsingState::lang_initial().expect("seed state"),
);
let result = language.parse("Hello {brave} world!").unwrap();

// The tree's root is a List node covering the whole input; its children are the
// top-level content.
let root = result.tree.root();
assert_eq!(root.child_count(), 3);

// `NodeRef::summary()` renders a compact one-line description per node — the
// assertion/logging companion you will see throughout this page.
let shapes: Vec<String> = root.children().iter().map(|node| node.summary()).collect();
assert_eq!(shapes, ["chars(Hello )", "group(Content { })", "chars( world!)"]);
```

No definitions are registered by default — techy deliberately ships no LaTeX
definitions database — so `\emph` starts out as an *unresolvable command*.
Everything below registers what it needs; that is the intended embedder workflow.
For quick experiments,
[`minidefs::minilatex_package()`](crate::latexlike::minidefs::minilatex_package)
ships a toy package (`\emph`, `\textbf`, `\textit`, the list environments, and the
typography specials) you can load explicitly.

## Reading nodes: kinds, spans, provenance

Every node knows exactly where it came from. Spans are byte ranges into the source,
and the original text is always reachable — reading a node's source spelling needs
no lookup tables:

```rust
use techy::core::{Language, ParsingState};
use techy::error::Recovery;
use techy::latexlike::{Latexlike, LatexlikeDriver};

let language: Language<Latexlike> = Language::new(
    LatexlikeDriver::new(Recovery::Strict),
    ParsingState::lang_initial().expect("seed state"),
);
let result = language.parse("one {two} three").unwrap();
let group = result.tree.root().child(1).unwrap();

assert!(group.is_group());
assert_eq!(group.span().range(), 4..9);
assert_eq!(group.span_content(), "{two}");           // the node's exact source text
assert_eq!(group.group_delimiters(), Some(("{", "}")));
assert_eq!(group.child(0).unwrap().chars(), Some("two"));

// Sibling runs are `NodeSlice` views with *exact* covering spans: the preset's
// sibling spans are adjacent, so the covering span is the run's own text (span
// tiling — `Lang::OBEYS_SPAN_TILING`).
let children = result.tree.root().children();
assert_eq!(children.span().unwrap().range(), 0..15);
assert_eq!(children.source_text(), Some("one {two} three"));

// Document-order traversal of everything *beneath* a node — `descendants()`
// excludes the starting node itself:
let texts: Vec<&str> = result
    .tree
    .root()
    .descendants()
    .filter_map(|node| node.chars())
    .collect();
assert_eq!(texts, ["one ", "two", " three"]);
```

## Defining macros

Definitions live in [`SpecsProvider`](crate::core::specs::SpecsProvider)s on the parsing
state's scope stack. The everyday provider is a [`Package`](crate::core::specs::Package):
immutable, built once, loaded wholesale. Register a
[`MacroSpec`](crate::latexlike::MacroSpec) under
[`CallableType::Macro`](crate::latexlike::CallableType) and build the language's seed
with the package via
[`ParsingState::lang_initial_with_packages`](crate::core::ParsingState::lang_initial_with_packages):

```rust
use techy::core::{Language, ParsingState};
use techy::error::Recovery;
use techy::latexlike::{argument_specs, CallableType, Latexlike, LatexlikeDriver, MacroSpec};
use techy::core::specs::Package;

let mut package = Package::new("mydefs");
// Argument structures come from xparse-like codes, one code string per argument:
// `o` = optional `[…]`, `m` = mandatory `{…}` (with the single-expression
// fallback). Compact whole-spec strings go through `argument_specs_from_str`.
package.insert(
    CallableType::Macro,
    "cite",
    MacroSpec::new(argument_specs(["o", "m"]).unwrap()),
);

let language: Language<Latexlike> = Language::new(
    LatexlikeDriver::new(Recovery::Strict),
    ParsingState::lang_initial_with_packages([package]).expect("seed state"),
);

let result = language.parse(r"see \cite[Lemma 3]{Author}!").unwrap();
let cite = result.tree.root().child(1).unwrap();
assert_eq!(cite.macro_name(), Some("cite"));
// The per-form getters (`macro_name()`, `environment_name()`, `specials_name()`)
// filter by invocation form; the generic `name()` answers for any callable.
assert_eq!(cite.name(), Some("cite"));
assert_eq!(cite.span_content(), r"\cite[Lemma 3]{Author}");

// Arguments are self-describing records: which are provided, and where their
// content nodes live.
assert!(cite.arguments().unwrap().get(0).unwrap().is_provided());
let optional = cite.argument_content_nodes(0).unwrap();
assert_eq!(optional.source_text(), Some("Lemma 3"));
let mandatory = cite.argument_content_nodes(1).unwrap();
assert_eq!(mandatory.source_text(), Some("Author"));
```

The same registration in one line:
[`define_macro`](crate::core::specs::Package::define_macro) /
[`define_environment`](crate::core::specs::Package::define_environment) are preset
shorthands over exactly this `insert` operation — not a second registration model —
pairing the callable type and spec type correctly by construction:

```rust
use techy::core::specs::Package;
use techy::latexlike::Latexlike;

let mut package: Package<Latexlike> = Package::new("mydefs");
package.define_macro("cite", ["o", "m"]).unwrap();
package.define_environment("enumerate", ["o"]).unwrap();
```

The codes are shorthand, not the only spelling: each code resolves to a configured
argument-parser value you can also build directly (that is how per-argument state
deltas and non-default options are attached), names come from
[`argument_specs_named`](crate::latexlike::argument_specs_named), and the full
code table — including `BracedOnly`, the mandatory group *without* the
single-expression fallback — is on
[`argument_specs`](crate::latexlike::argument_specs).

Absent optional arguments are recorded, not invented — and a trailing optional at end
of input is no error (a pylatexenc-parity fix):

```rust
use techy::core::{Language, ParsingState};
use techy::error::Recovery;
use techy::latexlike::{argument_specs, CallableType, Latexlike, LatexlikeDriver, MacroSpec};
use techy::core::specs::Package;

let mut package = Package::new("mydefs");
package.insert(
    CallableType::Macro,
    "item",
    MacroSpec::new(argument_specs(["o"]).unwrap()),
);
let language: Language<Latexlike> = Language::new(
    LatexlikeDriver::new(Recovery::Strict),
    ParsingState::lang_initial_with_packages([package]).expect("seed state"),
);

let result = language.parse(r"\item plain").unwrap();
let item = result.tree.root().child(0).unwrap();
assert!(!item.arguments().unwrap().get(0).unwrap().is_provided());
// The blank after `\item` is the trigger token's own post-space, nothing more:
assert_eq!(item.post_space(), Some(" "));
assert_eq!(item.span_content(), r"\item ");
```

Providers shadow innermost-first: pushing a package that redefines a name wins over
what sits below it — that is the whole scoping model, there is no separate override
mechanism.

## Math modes

Math is not a core concept — it is preset data. The default rules declare `$…$`,
`$$…$$`, `\(…\)`, and `\[…\]` as delimiter pairs of the single
[`GroupType::Math`](crate::latexlike::GroupType) class, and the driver's descent
delta parses their interiors in [`Mode::Math`](crate::latexlike::Mode). Inline vs.
display is the group's [`MathGroupForm`](crate::latexlike::MathGroupForm) — typed
class payload each delimiter rule declares at registration, read back by
[`math_form`](crate::core::node::NodeRef::math_form) (no delimiter table; a custom
registered pair carries its declared form like the built-ins):

```rust
use techy::core::{Language, ParsingState};
use techy::error::Recovery;
use techy::latexlike::{Latexlike, LatexlikeDriver, MathGroupForm, Mode};

let language: Language<Latexlike> = Language::new(
    LatexlikeDriver::new(Recovery::Strict),
    ParsingState::lang_initial().expect("seed state"),
);
let result = language.parse(r"a $x+y$ b \[z\]").unwrap();

let inline = result.tree.root().child(1).unwrap();
assert!(inline.is_math_group());
assert_eq!(inline.math_form(), Some(MathGroupForm::Inline));
// The interior was parsed in math mode; the node itself sits in the surrounding
// text-mode content. Every node records the state it was parsed under.
assert_eq!(inline.child(0).unwrap().parsing_state().mode(), Mode::Math);
assert_eq!(inline.parsing_state().mode(), Mode::Text);

let display = result.tree.root().child(3).unwrap();
assert_eq!(display.math_form(), Some(MathGroupForm::Display));
assert_eq!(display.group_delimiters(), Some((r"\[", r"\]")));
```

`$a$$b$` is two inline groups, not a display group — at a close position the expected
closer wins (pylatexenc's dollar-boundary parity):

```rust
use techy::core::{Language, ParsingState};
use techy::error::Recovery;
use techy::latexlike::{Latexlike, LatexlikeDriver};

let language: Language<Latexlike> = Language::new(
    LatexlikeDriver::new(Recovery::Strict),
    ParsingState::lang_initial().expect("seed state"),
);
let result = language.parse("$a$$b$").unwrap();
let shapes: Vec<String> =
    result.tree.root().children().iter().map(|node| node.summary()).collect();
assert_eq!(shapes, ["group(Math(Inline) $ $)", "group(Math(Inline) $ $)"]);
```

A macro like `\text{…}` must parse its argument back in *text* mode even inside
display math. That is per-argument data: an
[`ArgumentSpec`](crate::core::specs::ArgumentSpec) can carry a parsing-state
delta, which for this shape spells the preset's exit-math-context event —
restoring the enclosing non-math context, whatever it is, for exactly that
argument. The full worked recipe is on
[`Event::ExitMathContext`](crate::latexlike::Event)'s documentation.

## Environments

`\begin{name} … \end{name}` is a preset composition: `\begin` and `\end` are ordinary
macro entries of the `"_builtin"` package whose parsers dispatch the environment's own spec —
an [`EnvironmentSpec`](crate::latexlike::EnvironmentSpec) registered under
[`CallableType::Environment`](crate::latexlike::CallableType). The parsed environment
is a callable node whose *body* is a slot:

```rust
use techy::core::{Language, ParsingState};
use techy::error::Recovery;
use techy::latexlike::{
    argument_specs, CallableType, EnvironmentSpec, Latexlike, LatexlikeDriver,
};
use techy::core::specs::Package;

let mut package = Package::new("mydefs");
package.insert(
    CallableType::Environment,
    "enumerate",
    EnvironmentSpec::new(argument_specs(["o"]).unwrap()),
);
let language: Language<Latexlike> = Language::new(
    LatexlikeDriver::new(Recovery::Strict),
    ParsingState::lang_initial_with_packages([package]).expect("seed state"),
);

let result = language.parse(r"\begin{enumerate}[(i)] a b \end{enumerate}").unwrap();
let env = result.tree.root().child(0).unwrap();
assert_eq!(env.environment_name(), Some("enumerate"));
assert_eq!(env.argument_content_nodes(0).unwrap().source_text(), Some("(i)"));

// `body()` selects the marked body slot. It answers `None` for non-callables
// and for callables without a body slot (an ordinary macro node) — a `Some`
// with zero nodes, by contrast, is an empty body.
let body: Vec<String> = env.body().unwrap().iter().map(|node| node.summary()).collect();
assert_eq!(body, ["chars( a b )"]);
```

Registration deliberately performs no spec-type/callable-type cross-check — the
composition owns the environment parse and any spec's declared arguments
contribute; see [`Package::insert`](crate::core::specs::Package::insert) and
[the specs chapter](crate::guide::specs#registration-pitfalls).

An environment's definition can install a parsing-state delta for its body's whole
extent — a mode change (`equation` entering math mode; see
[the specs chapter](crate::guide::specs#the-spec-types)) or a **scope operation**:
definitions that exist only inside the body. The shipped exemplar is
[`minidefs`](crate::latexlike::minidefs)' `\item`, whose list environments push an
inner package for their bodies:

```rust
use techy::core::{Language, ParsingState};
use techy::error::Recovery;
use techy::latexlike::minidefs::minilatex_package;
use techy::latexlike::{Latexlike, LatexlikeDriver};

let language: Language<Latexlike> = Language::new(
    LatexlikeDriver::new(Recovery::Strict),
    ParsingState::lang_initial_with_packages([minilatex_package()]).expect("seed state"),
);

// Inside a list body, `\item` resolves (here with its optional argument)…
let result = language.parse(r"\begin{itemize}\item[a] x\end{itemize}").unwrap();
let body: Vec<String> = result.tree.root().child(0).unwrap().body().unwrap()
    .iter().map(|node| node.summary()).collect();
assert_eq!(body, ["Macro(item)", "chars( x)"]);

// …outside a list body, the very same name is undefined.
assert!(language.parse(r"\item x").is_err());
```

## Verbatim

Raw regions never tokenize their content. `\verb` is a macro whose argument is the
`v` code (auto-matched delimiter), and `verbatim` is an environment with the
[`VerbatimBehavior`](crate::latexlike::VerbatimBehavior) body — both produce
group+chars shapes with the raw text as ordinary chars content:

```rust
use std::sync::Arc;
use techy::core::{Language, ParsingState};
use techy::error::Recovery;
use techy::latexlike::{
    argument_specs, CallableType, EnvironmentSpec, Latexlike, LatexlikeDriver, MacroSpec,
    VerbatimBehavior,
};
use techy::core::specs::Package;

let mut package = Package::new("mydefs");
package.insert(
    CallableType::Macro,
    "verb",
    MacroSpec::new(argument_specs(["v"]).unwrap()),
);
package.insert(
    CallableType::Environment,
    "verbatim",
    EnvironmentSpec::from_behavior(Arc::new(VerbatimBehavior::default())),
);
let language: Language<Latexlike> = Language::new(
    LatexlikeDriver::new(Recovery::Strict),
    ParsingState::lang_initial_with_packages([package]).expect("seed state"),
);

// `\verb|…|`: everything between the delimiters is raw — `%`, `\`, `{`, `$` inert.
let result = language.parse(r"\verb|a%\x{| z").unwrap();
let verb = result.tree.root().child(0).unwrap();
assert_eq!(
    verb.argument_content_nodes(0).unwrap().source_text(),
    Some(r"a%\x{"),
);

// The `verbatim` environment: the body runs to the literal `\end{verbatim}`; the
// newline right after `\begin{verbatim}` is gobbled (staged, but not body content).
let result = language
    .parse("\\begin{verbatim}\na % b \\x{\n\\end{verbatim}")
    .unwrap();
let env = result.tree.root().child(0).unwrap();
let body = env.body().unwrap();
assert_eq!(body.get(0).unwrap().chars(), Some("a % b \\x{\n"));
```

## Specials

Specials are trigger character sequences resolved through the scope stack. The seed
ships none — typography interpretation is definitions content, not parsing
substrate — but the opt-in
[`minidefs::minilatex_package()`](crate::latexlike::minidefs::minilatex_package)
carries the familiar set (`~` and the text-only typography ligatures ``` `` ```,
`''`, `--`, `---`); the scan takes the longest match, and per-entry mode
visibility keeps the ligatures out of math:

```rust
use techy::core::{Language, ParsingState};
use techy::error::Recovery;
use techy::latexlike::minidefs::minilatex_package;
use techy::latexlike::{Latexlike, LatexlikeDriver};

let language: Language<Latexlike> = Language::new(
    LatexlikeDriver::new(Recovery::Strict),
    ParsingState::lang_initial_with_packages([minilatex_package()]).expect("seed state"),
);
let result = language.parse("x---y--z").unwrap();
let shapes: Vec<String> =
    result.tree.root().children().iter().map(|node| node.summary()).collect();
assert_eq!(shapes, ["chars(x)", "Specials(---)", "chars(y)", "Specials(--)", "chars(z)"]);
```

Your own specials are package entries too — including ones that take arguments:

```rust
use techy::core::{Language, ParsingState};
use techy::error::Recovery;
use techy::latexlike::{argument_specs, CallableType, Latexlike, LatexlikeDriver, SpecialsSpec};
use techy::core::specs::Package;

let mut package = Package::new("mydefs");
package.insert_specials(
    CallableType::Specials,
    "_",
    SpecialsSpec::new(argument_specs(["m"]).unwrap()),
);
let language: Language<Latexlike> = Language::new(
    LatexlikeDriver::new(Recovery::Strict),
    ParsingState::lang_initial_with_packages([package]).expect("seed state"),
);

let result = language.parse("x_{down}").unwrap();
let sub = result.tree.root().child(1).unwrap();
assert_eq!(sub.specials_name(), Some("_"));
assert_eq!(sub.argument_content_nodes(0).unwrap().source_text(), Some("down"));
```

## Paragraph breaks

A whitespace run containing two or more newlines is a paragraph break. By default it
becomes a whitespace-only chars node; the specials-node shape (as in pylatexenc's
current major version) is one driver flag away
([`ParagraphBreakStyle`](crate::latexlike::ParagraphBreakStyle) — a driver
emission policy, deliberately not package data: the tokenizer detects paragraph
breaks before the specials scan could ever run):

```rust
use techy::core::{Language, ParsingState};
use techy::error::Recovery;
use techy::latexlike::{Latexlike, LatexlikeDriver, ParagraphBreakStyle};

let language: Language<Latexlike> = Language::new(
    LatexlikeDriver::new(Recovery::Strict)
        .with_paragraph_break_style(ParagraphBreakStyle::Specials),
    ParsingState::lang_initial().expect("seed state"),
);
let result = language.parse("one\n\ntwo").unwrap();

let break_node = result.tree.root().child(1).unwrap();
// The node's name is the actual whitespace run as written, and its span covers
// the same run. Identify paragraph-break nodes by spec identity — the stamped
// spec is the canonical `techy::latexlike::ParagraphBreakSpec`, recognized by
// `Any`-downcast — never by a name spelling.
assert_eq!(break_node.specials_name(), Some("\n\n"));
assert_eq!(break_node.span().range(), 3..5);
```

## Strict vs. tolerant parsing

The recovery policy lives on the driver. Strict parses abort on the first error
(`parse` returns `Err`); tolerant parses record diagnostics, apply a documented
recovery, and keep going — and every diagnostic carries an exact source span:

```rust
use techy::core::{Language, ParsingState};
use techy::error::Recovery;
use techy::latexlike::{Latexlike, LatexlikeDriver};

// Strict: an unresolvable command aborts.
let strict: Language<Latexlike> = Language::new(
    LatexlikeDriver::new(Recovery::Strict),
    ParsingState::lang_initial().expect("seed state"),
);
let err = strict.parse(r"a \foo b").unwrap_err();
assert!(err.to_string().contains("cannot resolve command ‘\\foo’"));

// Tolerant: the command recovers as chars, the parse completes, the diagnostic is
// on the result.
let tolerant: Language<Latexlike> = Language::new(
    LatexlikeDriver::new(Recovery::Tolerant),
    ParsingState::lang_initial().expect("seed state"),
);
let result = tolerant.parse(r"a \foo b").unwrap();
let shapes: Vec<String> =
    result.tree.root().children().iter().map(|node| node.summary()).collect();
assert_eq!(shapes, ["chars(a )", r"chars(\foo )", "chars(b)"]);
assert_eq!(result.diagnostics.len(), 1);
let diagnostic = result.diagnostics.iter().next().unwrap();
assert_eq!(diagnostic.span().content(), r"\foo ");
```

A stray `}` at top level follows the same pattern — strict aborts, tolerant diagnoses
it, stages the consumed delimiter as a chars node, and resumes:

```rust
use techy::core::{Language, ParsingState};
use techy::error::Recovery;
use techy::latexlike::{Latexlike, LatexlikeDriver};

let tolerant: Language<Latexlike> = Language::new(
    LatexlikeDriver::new(Recovery::Tolerant),
    ParsingState::lang_initial().expect("seed state"),
);
let result = tolerant.parse("a}b").unwrap();
let shapes: Vec<String> =
    result.tree.root().children().iter().map(|node| node.summary()).collect();
assert_eq!(shapes, ["chars(a)", "chars(})", "chars(b)"]);
assert_eq!(result.diagnostics.len(), 1);
```

Match conditions by their *type*, never by spelling out an identifier string —
[the parsing chapter](crate::guide::parsing#working-with-diagnostics) shows the
idioms (`is::<T>()`, `downcast_ref::<T>()`, `T::IDENTIFIER`).

## Rendering diagnostics

Diagnostics render into readable reports with line/column positions, and the same
line/column machinery answers for any node
([`LineIndexCache`](crate::source::LineIndexCache) — parsing itself works purely
in byte offsets; line/column is a display-time computation). Iteration yields
diagnostics in the order the parse hit them;
[`sorted_by_position`](crate::error::Diagnostics::sorted_by_position) re-sorts
for reports that read along the document:

```rust
use techy::core::{Language, ParsingState};
use techy::error::Recovery;
use techy::latexlike::{Latexlike, LatexlikeDriver};
use techy::source::LineIndexCache;

let language: Language<Latexlike> = Language::new(
    LatexlikeDriver::new(Recovery::Tolerant),
    ParsingState::lang_initial().expect("seed state"),
);
let result = language.parse("first line\nsee \\foo here").unwrap();

// One readable report for the whole collection — message and position:
let report = result.diagnostics.render_all();
assert!(report.contains("cannot resolve command ‘\\foo’"));
assert!(report.contains("(line 2, col 5)"));

// Line/column for any node, through a consumer-held cache (each source is
// indexed once, ever):
let mut line_cols = LineIndexCache::new();
let node = result.tree.root().child(1).unwrap();
assert_eq!(
    line_cols.line_col(node.span().source(), node.span().start()),
    Some((2, 5)),
);
```

## Including other sources

techy performs no input/output itself: `\input`-style inclusion is the opt-in
[`input_macro_spec`](crate::latexlike::input_macro_spec) plus a
[`SourceResolver`](crate::source::SourceResolver) you supply on the driver (a
filesystem recipe is in
[the specs chapter](crate::guide::specs#resolving-external-sources-input-like-inclusion);
here, the in-memory [`MapResolver`](crate::source::MapResolver)). The resolved
content parses at the invocation point, into the same tree:

```rust
use techy::core::specs::Package;
use techy::core::{Language, ParsingState};
use techy::error::Recovery;
use techy::latexlike::{
    input_macro_spec, BodyMarker, CallableType, Latexlike, LatexlikeDriver,
};
use techy::source::MapResolver;

let mut resolver = MapResolver::new();
resolver.insert("preamble.tex", "hello {world}");

let mut package: Package<Latexlike> = Package::new("mydefs");
package.insert(
    CallableType::Macro,
    "input",
    input_macro_spec(false, BodyMarker::not_body()),
);
let language = Language::new(
    LatexlikeDriver::new(Recovery::Strict).with_source_resolver(resolver),
    ParsingState::lang_initial_with_packages([package]).expect("seed state"),
);

let result = language.parse(r"a \input{preamble.tex} b").unwrap();
let input = result.tree.root().child(1).unwrap();
// The invocation's own span lives in the including document…
assert_eq!(input.span_content(), r"\input{preamble.tex}");
// …while the attached content was parsed out of the resolved source — a
// multi-source tree, retrieved by slot name:
assert_eq!(
    input.slot_content_nodes_named("attached").unwrap().source_text().unwrap(),
    "hello {world}",
);
```

## Extracting content

The [`extract`](crate::extract) helpers answer the everyday
"give me the *text*" questions. `content_as_chars` flattens chars and groups (and
fails honestly on anything that is not text); `split_at_chars` splits a node list at
a separator with grouped content protected; `parse_keyval` reads
`key=value,…` content. The `_drop_annotations` spellings used below are the
plain-output shorthands (the bare names take an annotation-minting callback — see
the [`extract`](crate::extract) module docs):

```rust
use techy::core::{Language, ParsingState};
use techy::error::Recovery;
use techy::latexlike::{argument_specs, CallableType, Latexlike, LatexlikeDriver, MacroSpec};
use techy::extract;
use techy::core::specs::Package;

let mut package = Package::new("mydefs");
package.insert(
    CallableType::Macro,
    "usetikzlibrary",
    MacroSpec::new(argument_specs(["m"]).unwrap()),
);
let language: Language<Latexlike> = Language::new(
    LatexlikeDriver::new(Recovery::Strict),
    ParsingState::lang_initial_with_packages([package]).expect("seed state"),
);

// A comma-separated list argument:
let result = language.parse(r"\usetikzlibrary{arrows,shapes.geometric,calc}").unwrap();
let node = result.tree.root().child(0).unwrap();
let list = node.argument_content_nodes(0).unwrap();

assert_eq!(extract::content_as_chars(list).unwrap(), "arrows,shapes.geometric,calc");

let split = extract::split_at_chars_drop_annotations(list, ",").unwrap();
// `source_text()` answers a segment's recorded coordinates — its content here, since
// the preset is span-tiled; `extract::content_as_chars(segment)` reads the content
// itself, whatever the segment was cut from.
let libraries: Vec<&str> =
    split.segments().map(|segment| segment.source_text().unwrap()).collect();
assert_eq!(libraries, ["arrows", "shapes.geometric", "calc"]);
```

```rust
use techy::core::{Language, ParsingState};
use techy::error::Recovery;
use techy::latexlike::{argument_specs, CallableType, Latexlike, LatexlikeDriver, MacroSpec};
use techy::extract;
use techy::core::specs::Package;

let mut package = Package::new("mydefs");
package.insert(
    CallableType::Macro,
    "includegraphics",
    MacroSpec::new(argument_specs(["o", "m"]).unwrap()),
);
let language: Language<Latexlike> = Language::new(
    LatexlikeDriver::new(Recovery::Strict),
    ParsingState::lang_initial_with_packages([package]).expect("seed state"),
);

// Keyval options, grouped values protected:
let result = language
    .parse(r"\includegraphics[width=5cm,label={fig,main}]{fig.pdf}")
    .unwrap();
let node = result.tree.root().child(0).unwrap();
let keyvals = extract::parse_keyval_drop_annotations(node.argument_content_nodes(0).unwrap()).unwrap();

assert_eq!(keyvals.len(), 2);
let width = keyvals.get("width").unwrap();
assert_eq!(width.value().unwrap().source_text(), Some("5cm"));
// The grouped value's *content* view sees inside the braces:
let label = keyvals.get("label").unwrap();
assert_eq!(label.value_content().unwrap().source_text(), Some("fig,main"));
```

## Transforming and recomposing

Trees are frozen; editing is a **restage** pass
([`transform::TreeRestager`](crate::transform::TreeRestager)) that stages a new tree while
walking the input, and
[`TreeRecomposer`](crate::recompose::TreeRecomposer) folds any tree into a value — with the
preset's [`source_recomposer`](crate::latexlike::source_recomposer) reemitting a
tree's exact source spelling from its recorded facts. Together they are the
edit-and-write-back pipeline:

```rust
use techy::core::node::NodeRef;
use techy::core::{Language, ParsingState};
use techy::error::Recovery;
use techy::latexlike::{source_recomposer, Latexlike, LatexlikeDriver};
use techy::recompose::TreeRecomposer;
use techy::transform::{Restage, RestageContext, TreeRestager};

let language: Language<Latexlike> = Language::new(
    LatexlikeDriver::new(Recovery::Strict),
    ParsingState::lang_initial().expect("seed state"),
);
let input = language.parse("one % secret\ntwo {three}").unwrap().tree;

// Reemission reads recorded facts only — byte-exact for trees parsed from a
// language that obeys span tiling, the preset included:
let full =
    TreeRecomposer::new(&mut source_recomposer()).recompose(&input, ()).unwrap();
assert_eq!(full, "one % secret\ntwo {three}");

// Drop every comment node; carry everything else over unchanged:
let cleaned = TreeRestager::new(
    &mut |node: NodeRef<'_, Latexlike>,
          _cx: &mut RestageContext<'_, Latexlike, (), ()>| {
        Ok::<_, core::convert::Infallible>(if node.is_comment() {
            Restage::Emit(Vec::new()) // emit nothing: the node is dropped
        } else {
            Restage::Descend(())
        })
    },
)
.restage(&input)
.unwrap();

let stripped =
    TreeRecomposer::new(&mut source_recomposer()).recompose(&cleaned, ()).unwrap();
assert_eq!(stripped, "one two {three}");
```

Both drivers have more to them — replacement staging, region edits, downward
state, custom piece types — covered in the
[`transform`](crate::transform) and [`recompose`](crate::recompose) module docs;
[`visit::TreeWalker`](crate::visit::TreeWalker) is their read-only sibling for
structure-aware analysis passes.

## Where to go from here

- [Node trees]crate::guide::node_trees maps the whole tree-consumer toolkit;
  [Defining macros, environments, and specials]crate::guide::specs is the full
  definitions chapter; [Running the parser]crate::guide::parsing covers
  recovery, settings, and diagnostics in depth.
- The Developer Guide starts at
  [The parsing model]crate::guide::parsing_model — the state/derivation
  machinery these examples lean on — and continues to custom construct parsers
  and custom languages.
- The repository's acceptance suite (`techy/tests/acceptance.rs`) is this page's
  bigger sibling: span-exact ports of pylatexenc's walker tests and
  error-recovery matrices in both recovery modes.