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
//! The [`Lang`] trait — the types and hooks that define a language — with the
//! [`NodeExtTypes`] and [`InvocationSyntax`] bundles it names, the [`TrivialLang`]
//! all-defaults shortcut, and the [`ClosedVocabulary`] enumeration bound.
//!
//! [Defining a custom language](crate::guide::custom_lang) walks through an
//! implementation from scratch.
// `NodeExtTypes` is defined here, next to `Lang`, rather than in the node modules:
// its *meaning* is a node concern, but it is a constituent of the compile-time
// bundle, and moving it there would recreate a module cycle for cosmetics.
use String;
use Arc;
use fmt;
use Hash;
use crate;
use crate;
use crate;
use crate;
use ;
use ;
/// The node extension types of a language: the data a language attaches to each node,
/// and to each parsed argument and slot record, alongside the structural data.
///
/// An extension never changes what a node *is* — a group with custom data is still a
/// group to every generic tool that reads the tree.
///
/// - [`NodeExt`](NodeExtTypes::NodeExt) is attached to **every** node. A language that
/// wants different data per node kind puts an enum inside it; the one place that
/// creates the value, [`Lang::make_node_ext`], is where the two are kept consistent.
/// - [`ArgumentExt`](NodeExtTypes::ArgumentExt) and
/// [`SlotExt`](NodeExtTypes::SlotExt) are attached to the parsed argument and slot
/// records.
///
/// The three are bundled behind one associated type ([`Lang::NodeExts`]) to keep
/// [`Lang`] small; `()` implements the bundle with every type `()`. Nodes store the
/// extension inline, so keep these types small where possible — an index, or an `Arc`
/// into storage the language owns.
///
/// **An extension value is created once, when the thing it belongs to is created**,
/// by whichever party has the knowledge: the node extension by
/// [`Lang::make_node_ext`] as the node is staged, the argument extension by the
/// [`ArgumentParser`](crate::core::constructs::ArgumentParser) that parsed the
/// argument, the slot extension where the
/// [`ParsedSlot`](crate::core::node::ParsedSlot) record is built. Nothing is ever
/// default-initialized and filled in later, which is why these types carry no
/// `Default` bound; a restaged copy keeps the extension values it was parsed with.
/// The no-extension bundle: every extension type is `()`.
/// The contract on a language's invocation-syntax type ([`Lang::InvocationSyntax`]):
/// how one callable invocation was written.
///
/// A value of this type records the spelling facts of a single invocation — the escape
/// character used, any space that followed it, an environment's begin and end syntax —
/// in whatever canonical form the language prefers. It is stored on the node, as
/// [`CallableData::invocation_syntax`](crate::core::node::CallableData::invocation_syntax).
/// The type is generic over the language because its one method works against the
/// language's own source type.
///
/// This is separate from the node extension ([`NodeExtTypes`]), which holds a
/// language's *derived* data: what is recorded here is what re-emitting the source
/// has to work from. How faithfully a tree can be turned back into source is
/// therefore the language's own choice — byte-exact, exact up to insignificant
/// spacing, or approximate — because recomposition reads only what the node stores.
/// `()` records nothing, in which case a tool that does not know the language sees
/// only the name and span of each callable.
///
/// The latexlike preset records its macro, environment, and specials forms in
/// [`latexlike::InvocationSyntaxData`](crate::latexlike::InvocationSyntaxData).
///
/// Building a value is a separate, opt-in contract,
/// [`FromInvocation`](crate::core::constructs::FromInvocation), which the standard
/// staging sites use. A language whose value cannot be built from an
/// [`Invocation`](crate::core::constructs::Invocation) alone stages its callables
/// through custom parsers instead.
/// Records nothing about how an invocation was written, so there is nothing to
/// materialize.
/// The definition of a language: the types the parsing machinery is generic over, and
/// the hooks the language implements.
///
/// Implement it to teach the machinery a new markup language. Every core type of the
/// crate takes one `L: Lang` parameter, so the associated types chosen here fix the
/// vocabulary for a whole parse — the mode, group and callable identifiers, the state
/// and node extension types, the tokenization, and the driver.
///
/// A minimal language is a zero-sized type with only the associated types filled in.
/// Every method has a working default — no transition customization, no specials —
/// except [`make_node_ext`](Lang::make_node_ext), which has none because a node
/// extension value has no default ([`NodeExtTypes`]); a language without extension
/// data writes it as the one-liner `Ok(())`. Simpler still, `impl TrivialLang for
/// MyLang {}` supplies everything (see [`TrivialLang`]). The
/// [`latexlike`](crate::latexlike) preset is the worked-out full implementation.
///
/// Every associated type is `Send + Sync`, because parsing states and node trees must
/// be usable from several threads. In practice these types are enums, flags, and
/// `Arc`s, so the bounds cost nothing.
///
/// [Defining a custom language](crate::guide::custom_lang) works through an
/// implementation step by step.
// `'static` because a `Lang` is a compile-time type bundle (a unit marker type in
// practice) and `CallableSpec<L>: Any` (the downcast contract) requires every spec
// type — including generic ones like `StdCallableSpec<L>` — to be `'static`.
/// An all-defaults language for tests and for experiments with the machinery: writing
/// `impl TrivialLang for MyLang {}` gives `MyLang` a full [`Lang`] implementation.
///
/// Every associated type is filled in — `Features` = [`AllLangFeatures`],
/// `ModeId`, `StateExt`, `Event`, `SessionExt` and `NodeExts` = `()`, `SourceOrigin` =
/// `Option<String>`, `Tokenization` =
/// [`StdTokenization`](crate::core::token::StdTokenization), `GroupTypeId` and
/// `CallableTypeId` = `u32` — and every method keeps its default behavior. The default
/// driver resolves no commands. (Rust has no defaults for associated types, which is
/// why this is a separate trait rather than defaults on [`Lang`].)
///
/// Customizing anything means implementing [`Lang`] directly: a blanket implementation
/// makes the two mutually exclusive, so the first command, real identifier enum, or
/// hook calls for the full implementation.
/// A type whose values can all be listed: the opt-in bound for the three per-language
/// vocabularies whose values are fixed when the language is written
/// ([`Lang::CallableTypeId`], [`Lang::GroupTypeId`], [`Lang::ModeId`]).
///
/// Those types are already closed by construction; this trait makes the list available
/// to generic code, so a tool can iterate over every value — calling
/// [`ScopeStack::iter_symbols`](crate::core::specs::ScopeStack::iter_symbols) once for
/// each callable type in `L::CallableTypeId::ALL`, for instance.
///
/// It is deliberately **not** required by [`Lang`]: [`TrivialLang`] uses `u32` for the
/// identifier types, and an integer type has no list of values. A language with real
/// enums implements it — the latexlike preset does for all three — and code that needs
/// to enumerate states the bound where it uses it, as
/// `where L::CallableTypeId: ClosedVocabulary`.
/// The one-value vocabulary. This is [`TrivialLang`]'s `ModeId`: a language with "no
/// modes" still has the single mode every one of its states is in.