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
//! The traits that let a language of your own reuse the preset's behavior.
//!
//! Nothing in the preset is written for [`Latexlike`](super::Latexlike) alone. Its
//! token rules, its specs, its construct parsers and its driver hooks are all generic
//! over a *family* of languages, so a language that wants LaTeX-like syntax but its
//! own vocabularies, node data, or state extension gets them without copying
//! anything. The bound naming that family is [`LatexlikeLang`] — conventionally
//! written as the type parameter `LLL` — and joining it is one line,
//! `impl LatexlikeLang for MyLang {}`.
//!
//! To be admitted, a language's vocabularies must be able to supply the values the
//! preset's machinery needs: a math group class, a macro invocation form, a math
//! mode, and so on. Each vocabulary answers for itself, through one trait implemented
//! on the vocabulary type:
//!
//! - [`LatexlikeGroupType`] on [`Lang::GroupTypeId`] — content, math, and verbatim
//! group classes;
//! - [`LatexlikeCallableType`] on [`Lang::CallableTypeId`] — the macro, environment,
//! and specials invocation forms;
//! - [`LatexlikeMode`] on [`Lang::ModeId`] — the mode math interiors parse in;
//! - [`LatexlikeEvent`] on [`Lang::Event`] — the "leave the math context" event;
//! - [`LatexlikeInvocationSyntax`] on [`Lang::InvocationSyntax`] — the record of how
//! an invocation was spelled.
//!
//! techy implements all five for the preset's own types ([`GroupType`],
//! [`CallableType`], [`Mode`], [`Event`], and
//! [`InvocationSyntaxData`](super::InvocationSyntaxData)), so a language that adopts
//! those as its associated types satisfies every bound with no code of its own. A
//! language that extends one of them with variants of its own implements the trait
//! instead, which is what guarantees the values the preset needs still exist.
//!
//! [`LatexlikeLang`] also defines the preset's language-level settings as methods
//! with defaults: the math-delimiter table
//! ([`math_group_rules`](LatexlikeLang::math_group_rules)),
//! the characters a math interior forbids
//! ([`math_interior_forbidden_chars`](LatexlikeLang::math_interior_forbidden_chars)),
//! and the checks a parse runs at start-up
//! ([`check_parse_start`](LatexlikeLang::check_parse_start)). Overriding one changes
//! that setting for the whole language, with no need to fork
//! [`default_token_rules`](super::default_token_rules).
//!
//! [Writing your own language](crate::guide::custom_lang) covers the [`Lang`] trait
//! these build on.
use String;
use Arc;
use vec;
use Vec;
use crateDiagnostics;
use crate;
use crate;
use crateGroupRule;
use LatexlikeParseDriver;
use EnvironmentSyntax;
use ;
/// What the preset requires of a language's group-class vocabulary
/// ([`Lang::GroupTypeId`]).
///
/// Three classes have to exist for the preset's rules and parsers to work: the plain
/// content group `{…}`, the math group in a given [`MathGroupForm`], and the verbatim
/// region. This trait asks the vocabulary type for them, rather than demanding the
/// preset's own [`GroupType`], so a language that adds classes of its own still fits.
/// [`GroupType`] implements it, so adopting that enum costs no code.
///
/// # Coherence contracts
///
/// - `Self::math_group(f).math_form() == Some(f)` for every form `f`;
/// - `content_group().math_form()` and `verbatim_group().math_form()` are `None`;
/// - [`is_math`](LatexlikeGroupType::is_math) is `true` for every class whose
/// interior should parse as math.
///
/// # The `is_math` / `math_form` split
///
/// The two math queries are separate on purpose.
/// [`is_math`](LatexlikeGroupType::is_math) decides *parse behavior*: for a class
/// that answers `true`, the interior parses in math mode and the math delimiters stop
/// being openers there ([`math_group_interior_delta`](super::math_group_interior_delta)).
/// [`math_form`](LatexlikeGroupType::math_form) reports *presentation*, and is what a
/// reader of a finished tree calls
/// ([`NodeRef::math_form`](crate::core::node::NodeRef::math_form)).
///
/// A language with a math-like class that has no inline/display presentation
/// therefore overrides `is_math` to answer `true` for it while `math_form` stays
/// `None`.
/// What the preset requires of a language's callable-type vocabulary
/// ([`Lang::CallableTypeId`]).
///
/// The three invocation forms — macro, environment, specials — have to exist, as
/// values of the language's own type. [`CallableType`] implements this, so adopting
/// that enum costs no code; a language that adds forms of its own implements it
/// instead.
///
/// The constructors are named for the role and the noun together
/// (`macro_callable()`, not `macro()`, which is not spellable in Rust); the
/// predicates are named for the role alone.
///
/// # Coherence contracts
///
/// Mirroring [`LatexlikeGroupType`]'s: `Self::macro_callable().is_macro()`,
/// `Self::environment_callable().is_environment()`, and
/// `Self::specials_callable().is_specials()` are all `true`, and each predicate
/// answers `true` for exactly the values playing that role. The defaults compare
/// against the constructors; a language where several values play one role
/// overrides the predicate.
/// What the preset requires of a language's mode vocabulary ([`Lang::ModeId`]): the
/// math mode, and a way to recognize it.
///
/// [`Mode`] implements this, so adopting that enum costs no code.
///
/// There is deliberately no text-mode constructor and no `is_text`. The preset never
/// invents a non-math mode: leaving math restores an enclosing context that actually
/// exists, mode value included, found on the stack of enclosing states
/// ([`exit_math_context_delta`](super::exit_math_context_delta)). The only two things
/// the preset has to ask a mode vocabulary are which mode math interiors parse in,
/// and whether a given mode is math.
///
/// # Coherence contract
///
/// `Self::math_mode().is_math() == true`, and the predicate answers `true` for
/// exactly the modes the language considers math (override the equality default
/// for a language with several math-flavored modes).
/// What the preset requires of a language's event vocabulary ([`Lang::Event`]): the
/// "leave the math context" event, and a way to recognize it.
///
/// [`Event`] implements this, so adopting that enum costs no code.
///
/// Leaving math is an *event* rather than a fixed state change because the change it
/// stands for depends on what encloses the point of use — see
/// [`Event::ExitMathContext`]. An argument recipe written for the whole family
/// (`\text{…}` and its relatives) creates the event in the language's own event type
/// here, and the driver recognizes it again when it resolves the event into a state
/// change
/// ([`ParseDriver::resolve_state_event`](crate::core::ParseDriver::resolve_state_event),
/// which delegates to [`exit_math_context_delta`](super::exit_math_context_delta)).
///
/// # Coherence contract
///
/// `Self::exit_math_context().is_exit_math_context() == true`, and the recognizer
/// answers `true` for exactly the events meaning "exit the math context". The
/// recognizer is a required method because events are not required to support equality.
/// What the preset requires of a language's invocation-syntax payload
/// ([`Lang::InvocationSyntax`]): a way to record, and read back, which of the three
/// invocation forms a callable node was written in.
///
/// The payload is what a `Callable` node stores about its own spelling — the escape
/// character a macro was triggered by, the whitespace that ended its name, the pieces
/// of an environment's `\begin` and `\end`. Requiring this trait is what lets the
/// preset's parsers and its source recomposer
/// ([`source_recomposer`](super::source_recomposer)) work with any family member's
/// payload type. [`InvocationSyntaxData`](super::InvocationSyntaxData) implements it
/// for any environment record `Env`.
///
/// The associated [`Env`](LatexlikeInvocationSyntax::Env) type is the environment
/// side of that record ([`EnvironmentSyntax`]), and is the one place to customize
/// what gets recorded for an environment: a language chooses its record by choosing
/// its payload type.
///
/// # Coherence contracts
///
/// Mirroring the other role traits': `macro_form(e, p).macro_syntax() ==
/// Some((e, &p))`, `environment_form(env).environment_syntax() == Some(&env)`,
/// `specials_form().is_specials() == true`, and each accessor answers `Some`/
/// `true` for exactly the values playing that form.
/// A language that can use the latexlike preset: the bound every generic preset
/// component takes.
///
/// A [`Lang`] qualifies when its vocabularies play the preset's roles
/// ([`LatexlikeGroupType`], [`LatexlikeCallableType`], [`LatexlikeMode`],
/// [`LatexlikeEvent`], and on the invocation-syntax payload
/// [`LatexlikeInvocationSyntax`] together with
/// [`FromInvocation`](crate::core::constructs::FromInvocation)) and its driver
/// implements [`LatexlikeParseDriver`]. Every generic item of the preset takes this
/// bound, conventionally under the parameter name `LLL`.
///
/// Opting in is explicit and one line — `impl LatexlikeLang for MyLang {}` — which is
/// exactly how [`Latexlike`](super::Latexlike) itself joins. There is no blanket
/// implementation over the vocabulary bounds, because a blanket implementation would
/// fix the defaulted methods below and leave no way to override them per language.
///
/// A custom driver joins the same way, `impl LatexlikeParseDriver<MyLang> for
/// MyDriver {}`, since every hook of [`LatexlikeParseDriver`] has a default.
///
/// The trait deliberately does *not* require
/// [`ClosedVocabulary`](crate::core::ClosedVocabulary) of the vocabularies. That
/// bound is stated at the places that actually enumerate a vocabulary, so a language
/// whose vocabularies are open still fits here.
///
/// Every latexlike language has every parsing feature: the bound fixes
/// [`Features`](Lang::Features) to
/// [`AllLangFeatures`](crate::core::AllLangFeatures), so token rules are stored
/// plainly and no per-feature gating shows up anywhere in the family.
///
/// # Language-level settings
///
/// The methods below have defaults that reproduce the preset's own behavior.
/// Override one to change it for a whole language, rather than forking
/// [`default_token_rules`](super::default_token_rules) or the driver.
// --- the preset's own vocabularies play the roles --------------------------------
//
// These impls are what makes "adopt the preset enums, get the family for free"
// true: a Lang whose associated types are the preset enums satisfies every
// LatexlikeLang vocabulary bound with zero code.