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
//! [`CallableSpec`]: the behavior of anything invocable from the token stream; plus the
//! standard declarative implementation [`StdCallableSpec`].
//!
//! The invocation-form identifier is [`Lang::CallableTypeId`] — a closed per-language
//! type.
use Box;
use format;
use String;
use Arc;
use Vec;
use Any;
use fmt;
use crate;
use crateBuildId;
use crate;
use crateSpecProvenance;
use crateLang;
use ArgumentSpec;
/// Which part of a callable's parse a live traceback [`Frame`](crate::core::Frame)
/// covers — the `role` input of [`CallableSpec::stack_frame_title`].
/// The behavior of anything invocable from the token stream: which arguments it takes
/// and how its invocation is parsed.
///
/// A spec has no name and no invocation form of its own. The name belongs to the
/// key a [`SpecsProvider`](crate::core::specs::SpecsProvider) stores the spec under, so
/// one spec value may be stored under several names (`\emph` and `\textit` can share
/// one), and one shared spec can answer for every unknown callable of a callable type.
///
/// Most definitions do not implement this trait at all: [`StdCallableSpec`] is the
/// standard declarative implementation, and the latexlike preset's `MacroSpec` and
/// `EnvironmentSpec` build on it. Implement `CallableSpec` yourself when a callable
/// needs behavior no argument list can express.
///
/// The declarative surface is the [`ArgumentSpec`] list returned by
/// [`arguments`](CallableSpec::arguments) — arguments *configure* an invocation
/// (pylatexenc's `arguments_spec_list`). The list is `Arc`-shared so that a parsed node
/// can record which argument spec each of its arguments was parsed against. Slots — a
/// parsed callable's *content regions*, such as an environment body — have no
/// declaration here: the parser that reads the body creates the
/// [`ParsedSlot`](crate::core::node::ParsedSlot) records itself, and the spec announces
/// that it takes material at all through
/// [`requires_content`](CallableSpec::requires_content).
///
/// The behavioral surface is [`make_invocation_parser`](CallableSpec::make_invocation_parser):
/// a factory returning a fresh boxed [`ConstructParser`] per resolved [`Invocation`],
/// defaulting to the declarative [`StdInvocationParser`]. Overriding it takes over the
/// entire invocation parse, which is what `\verb`-like constructs need.
///
/// Every method has a default, and the defaults together describe the neutral
/// callable — no arguments, no body — which is what a simple specials definition (`~`)
/// or a fallback spec needs.
///
/// # Implementing this trait
///
/// **Thread safety is part of the contract** (`Send + Sync` supertraits): specs are
/// stored in parsed trees, so `NodeTree: Send + Sync` requires it. Every method takes
/// `&self`, so a stateful implementation needs interior mutability in any case — under
/// this contract that means locks or atomics (`Mutex`/`RwLock`/`OnceLock`, or `spin` on
/// `no_std`), not `RefCell`/`Cell`.
///
/// **Downcasting is part of the contract** (`Any` supertrait): a consumer recovers the
/// concrete type from a stored `Arc<dyn CallableSpec<L>>` or `&dyn CallableSpec<L>` by
/// upcasting — `(&*spec as &dyn core::any::Any).downcast_ref::<MySpec>()`. A preset's
/// [`Lang::make_node_ext`](crate::core::Lang::make_node_ext) does exactly this with the
/// spec a node stores. Every stored extension trait of the crate shares the supertrait:
/// [`SpecsProvider`](crate::core::specs::SpecsProvider),
/// [`ArgumentParser`](crate::core::constructs::ArgumentParser),
/// [`EnvironmentBehavior`](crate::latexlike::EnvironmentBehavior),
/// [`SourceResolver`](crate::source::SourceResolver).
///
/// Downcasting to a preset's own spec *trait* — an open set of third-party spec types —
/// needs one extra step: store every such spec behind one concrete wrapper type
/// (`FlmSpecBox(Arc<dyn FlmSpec>)`, delegating to the inner value) and downcast to the
/// wrapper.
///
/// **Serialization is part of the contract** ([`SerializableObject`] supertrait): a
/// spec stored in a parsed tree must be serializable through the
/// `Arc<dyn CallableSpec<L>>` the node holds, so the write-side capability sits on
/// every spec type. It is fully defaulted: a type that does not participate in
/// serialization adds the one-line empty impl
/// (`impl<L: Lang> SerializableObject<L> for MySpec<L> {}`) and nothing else, while a
/// participating type overrides
/// [`serialize_object`](SerializableObject::serialize_object). The two argument-spec
/// methods below ([`serialize_argument_spec`] and [`deserialize_argument_spec`]) belong
/// to the same capability. All of it is callable only for a language that implements
/// [`SerializableLang`]; for any other language these methods cannot be called.
///
/// [`serialize_argument_spec`]: CallableSpec::serialize_argument_spec
/// [`deserialize_argument_spec`]: CallableSpec::deserialize_argument_spec
/// Sealed conversion into a shared [`CallableSpec`] — what
/// [`Package::insert`](crate::core::specs::Package::insert) and its siblings accept as
/// their spec argument.
///
/// A spec passes **by value** (`insert(CallableType::Macro, "emph", MacroSpec::new(…))`,
/// with no `Arc::new` at the call site), while an already-shared **`Arc<S>`** or
/// **`Arc<dyn CallableSpec<L>>`** passes through unchanged rather than being wrapped a
/// second time — so one spec value registered under several names stays one value. The
/// same conversion pattern applies to providers
/// ([`IntoSpecsProvider`](crate::core::specs::IntoSpecsProvider)) and argument parsers
/// ([`IntoArgumentParser`](super::IntoArgumentParser)).
///
/// Sealed: the three implementations are the whole vocabulary; downstream code
/// implements [`CallableSpec`], never this trait. (The `M` parameter is a sealed
/// inference marker distinguishing the three argument shapes — it never needs to be
/// named.)
/// The standard declarative [`CallableSpec`]: a callable's argument structure as plain
/// data, and nothing else.
///
/// [`new`](StdCallableSpec::new) takes the argument specs in invocation order. The
/// result is stored under a name in a [`Package`](crate::core::specs::Package) or a
/// [`Scope`](crate::core::specs::Scope) like any other spec.
///
/// ```
/// use techy::core::constructs::MarkerArgumentParser;
/// use techy::core::specs::{ArgumentSpec, StdCallableSpec};
/// use techy::latexlike::Latexlike;
///
/// // One optional `*` marker argument, named so parsed nodes can be asked for it by
/// // name rather than by position.
/// let spec: StdCallableSpec<Latexlike> =
/// StdCallableSpec::new([ArgumentSpec::new(MarkerArgumentParser::new("*"), "starred")]);
/// assert_eq!(spec.arguments.len(), 1);
/// ```
///
/// For a latexlike language the argument list is usually written as **argument codes**
/// rather than built parser by parser — `"o"` for an optional `[…]` argument, `"m"` for
/// a mandatory one; [`argument_specs`](crate::latexlike::argument_specs) documents the
/// full code table. The preset's own spec types
/// ([`MacroSpec`](crate::latexlike::MacroSpec),
/// [`EnvironmentSpec`](crate::latexlike::EnvironmentSpec)) have this same shape and
/// additionally name their construct kind in parse tracebacks, and
/// [`Package::define_macro`](crate::core::specs::Package::define_macro) registers one
/// in a single line.
///
/// Both fields are public, so the type can equally be built as a struct literal —
/// `StdCallableSpec { arguments, ..Default::default() }` takes argument specs that are
/// already `Arc`-shared.
///
/// **Serialization.** The argument structure holds parsers, which have no serialized
/// form, so a `StdCallableSpec` is serialized by *identity* — as a reference to the
/// provider that defined it plus its key — which requires the spec to carry a
/// [`SpecProvenance`] stamp in its [`provenance`](field@StdCallableSpec::provenance)
/// field ([`with_provenance`](StdCallableSpec::with_provenance) sets it; a shared
/// [`Package`](crate::core::specs::Package) issues the stamp). Serializing an unstamped
/// `StdCallableSpec` is an error naming the type.
// The `SerializableObject` (identity through the provenance stamp) impl lives in
// `crate::serialize::drivers::specs`, with the other core spec/provider types'.
// Manual impls: derives would demand `L: Clone`/`L: Debug`/`L: Default` although only
// `Arc`s to spec data are stored.