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
//! The per-invocation record of a `Callable` node: [`ParsedArguments`] and
//! [`ParsedSlots`].
//!
//! Together they record which declared arguments were provided, where each provided
//! argument's and each slot's nodes sit among the callable's children, which of those
//! nodes are its content, and — for arguments — which
//! [`ArgumentSpec`](crate::core::specs::ArgumentSpec) each was parsed against.
//!
//! # Modeled on pylatexenc's `ParsedArguments`
//!
//! pylatexenc keeps two parallel lists: `argnlist` (one node or `None` per argument)
//! and `arguments_spec_list` (the spec of every argument, present or not). Here the two
//! are zipped into one `Vec` of [`ParsedArgument`] entries, each holding its `Arc`'d
//! spec, which makes the record self-describing. A custom invocation parser may produce
//! an argument structure the callable spec never declared (`\newcommand`-alikes), and
//! an absent optional keeps its spec, so a lookup by name can tell "not provided" from
//! "no such argument".
//!
//! # Encoding: one child region per argument or slot
//!
//! A callable's children are the concatenation of one contiguous region per *provided*
//! argument, followed by one region per slot. A region holds the argument's full
//! syntactic extent in source order: leading noise (comment nodes and whitespace-only
//! `Chars` nodes — there is no `pre_space` field, since whitespace skipped before an
//! argument becomes a node like everywhere else), the syntax-bearing node or nodes (a
//! `Group` for the `{…}` and `[…]` forms, with the delimiters stored on the group; a
//! `Chars` node for `\frac 1 2` single tokens and provided `*` markers), and any
//! trailing per-instance syntax.
//!
//! An absent argument has an entry but no region: reporting an argument absent means
//! having consumed *nothing*, and noise scanned while looking for it is rewound and
//! re-parsed as enclosing content (see
//! [`ArgumentParser`](crate::core::constructs::ArgumentParser)). The callable's child
//! list is therefore the raw-syntax view, in which the child count does not match the
//! argument count; access by argument goes through these records.
//!
//! Each region also designates its content nodes — for `\textbf{abc}` the group's
//! children, braces excluded; for `\frac 1 2` the single `Chars` node; for
//! `[{arg with ]}]` the *inner* group's children. The parser designates the content at
//! parse time ([`ContentNodes`]) and it reads back as a plain node range. There is no
//! heuristic that unwraps a lone group (pylatexenc's `get_content_nodelist()` plus
//! `unwrap_double_group`), and, unlike pylatexenc's standard argument parsers, which
//! drop pre-argument comment nodes by default (`return_full_node_list=False`), noise is
//! kept — out of the way of the content.
//!
//! # Two-phase records
//!
//! A resolved region's ranges name positions in the flattened tree (the coordinate
//! system of `NodeData.children`), and those positions do not exist while parsers run:
//! a node's final index depends on parts of the tree that are still unparsed when it is
//! staged (see [`NodeTreeBuilder`](super::NodeTreeBuilder)'s module documentation). A
//! [`ChildRegion`] is therefore first *staged* by the parser — child offsets into the
//! callable's child list, plus a [`ContentNodes`] designation in `BuildId` terms — and
//! then *resolved in place* by
//! [`NodeTreeBuilder::finish`](super::NodeTreeBuilder::finish) into global node-index
//! ranges.
//!
//! The price of letting parsers construct `ParsedArguments` directly, instead of
//! driving a separate staging API, is that a record's phase is a runtime invariant the
//! type system cannot see. It stays contained: resolution happens at exactly one point,
//! a finished [`NodeTree`](super::NodeTree) can never hold a staged region, and the
//! accessors that read resolved coordinates panic on a staged one — reachable only by
//! reading back records one built oneself and never finished.
//!
//! Content-extraction conveniences beyond the stored ranges (keyval helpers, chars
//! flattening) stay *computed* views; an extension that wants to cache derived data per
//! argument uses the [`ext`](ParsedArgument::ext) slot instead.
use Box;
use Arc;
use Vec;
use fmt;
use Range;
use crateArgumentSpec;
use crateLang;
use BuildId;
use ;
use ;
/// A parser's designation of a region's content nodes, in staging coordinates.
///
/// Both forms name a contiguous run of one node's children by construction, so
/// contiguity in the flattened tree needs no checking, and an empty sub-range stays
/// anchored where it was designated: the content of `\m{}` is empty *inside the
/// group*. [`ChildRegion::new`] takes one of these; the finished tree reports the
/// result through [`ChildRegion::content_range`] and
/// [`ChildRegion::content_parent`].
/// The children of one provided argument or one slot, and which of them are its
/// content.
///
/// A region covers its argument's or slot's full syntactic extent in source order —
/// leading noise, the syntax-bearing nodes, any trailing syntax — and designates the
/// contiguous run of nodes within it that is the content.
///
/// Regions are two-phase. A parser builds one in staging coordinates
/// ([`new`](ChildRegion::new), [`single`](ChildRegion::single)), and
/// [`NodeTreeBuilder::finish`](super::NodeTreeBuilder::finish) resolves it into global
/// node-index ranges of the finished tree. [`children`](ChildRegion::children),
/// [`content_range`](ChildRegion::content_range) and
/// [`content_parent`](ChildRegion::content_parent) read the resolved form and panic on
/// a staged region; [`staged`](ChildRegion::staged) reads the staged form and answers
/// `None` on a resolved one.
///
/// Every region read from a finished tree is resolved — the builder checks that staged
/// records are staged when the callable is added — so that panic is reachable only by
/// reading back a region one built oneself and never finished. These are among the
/// crate's few deliberate panics (see the [Panics list](techy::guide::panics)).
/// One declared argument of one invocation: the spec it was parsed against, whether
/// and where it was provided, and per-argument ext data.
///
/// Entries exist for absent optionals too, so [`is_provided`](ParsedArgument::is_provided)
/// is what distinguishes an argument that was left out from one that was given.
/// The parsed arguments of one callable invocation: one [`ParsedArgument`] per
/// declared argument, in invocation order (pylatexenc's `ParsedArguments`).
///
/// Read the record of a parsed node with
/// [`NodeRef::arguments`](super::NodeRef::arguments); to go straight to an argument's
/// nodes, use [`NodeRef::argument_nodes`](super::NodeRef::argument_nodes) or
/// [`NodeRef::argument_content_nodes`](super::NodeRef::argument_content_nodes) and
/// their by-name companions.
/// How a slot's content relates to its callable's source bytes — the recorded role of
/// one [`ParsedSlot`], declared by the parser that minted the record.
///
/// - [`Content`](SlotRole::Content) — constitutive: the node's meaning is incomplete
/// without it (an environment's body). The parent's source bytes cover it like they
/// cover any other child region.
/// - [`Attached`](SlotRole::Attached) — derived, and reconstructible from the
/// invocation itself. The example is `\input`'s resolved content, where the
/// invocation text *is* the recomposition. An attached slot is excluded from the
/// parent's byte accounting, because its children are located in their own source;
/// declaring the role is what replaces inferring it from a change of source.
/// - [`Hidden`](SlotRole::Hidden) — a framework- or callable-defined attachment that
/// the core ignores: no recomposition and no byte accounting, and nothing more. It
/// does not hide the slot from readers — readers, extraction helpers, and structural
/// walks treat every slot alike whatever its role, and debug output shows what is
/// really there. Any further meaning comes from the slot's name and the callable's
/// spec.
///
/// The enum is deliberately exhaustive rather than `#[non_exhaustive]`: consumers match
/// on roles in validators, recomposition strategies, and mappings to foreign-language
/// bindings, and a fourth role would change byte-accounting semantics — which must be a
/// conscious breaking change, not a silently ignored variant.
///
/// Whether a slot is *the body* is a separate axis, marked on the slot's ext through
/// [`BodySlotExt`]. A body slot is usually [`Content`](SlotRole::Content), but the two
/// are recorded independently.
/// Marks a slot ext type as able to designate its slot as **the body**.
///
/// This is the trait behind [`NodeRef::body`](super::NodeRef::body), and the mechanism
/// that lets code generic over the language mint a body slot's ext: the preset's
/// environment machinery calls [`make_body`](BodySlotExt::make_body) and never names a
/// concrete ext type.
///
/// Implementations must satisfy `Self::make_body().is_body() == true`.
///
/// A framework that replaces the ext bundle implements this trait on its own
/// [`SlotExt`](crate::core::NodeExtTypes::SlotExt), and every preset mechanism keeps
/// working. The implementation for `()` marks *every* slot as the body: a language
/// without ext data records no marking, so [`body()`](super::NodeRef::body) falls back
/// to the first slot.
/// The no-ext degenerate: every slot reports body, so
/// [`body()`](super::NodeRef::body) selects the first slot.
/// One content region ("slot") of one invocation — an environment's body, and other
/// content a callable's own parser reads.
///
/// A slot always has a region, unlike an optional argument, which may be absent; the
/// region's content can still be empty. For the standard environment shape the region
/// holds the body `List` node, whose children are the content.
///
/// Slots are record-level vocabulary only: nothing declares a slot on the spec side.
/// The invocation parser that reads a callable's body (the composition the spec's
/// `make_invocation_parser` returns) mints these records directly, with whatever
/// parsers it drives internally. Self-description therefore means storing the `name`
/// on the record itself — a deliberate asymmetry with [`ParsedArgument`], which points
/// at its `Arc<ArgumentSpec>`: an argument spec holds a parser, a name and a state
/// delta worth pointing at, while a slot record has no spec-side counterpart.
/// The parsed slots of one callable invocation: one [`ParsedSlot`] per content region,
/// in source order.
///
/// Read the record of a parsed node with [`NodeRef::slots`](super::NodeRef::slots); to
/// go straight to a slot's content, use
/// [`NodeRef::slot_content_nodes`](super::NodeRef::slot_content_nodes), its by-name
/// companion, or [`NodeRef::body`](super::NodeRef::body) for the body slot.
// Manual impls: derives would demand `L:` bounds although only associated types (already
// bounded) and `Arc`s are stored.