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
//! Argument specs: the declarative description of a callable's invocation shape.
//!
//! An [`ArgumentSpec`] describes one argument a callable accepts: the
//! [`ArgumentParser`] that recognizes and parses it, an optional name for by-name
//! access to the parsed argument, and an optional parsing-state change applying to the
//! argument's own extent. The model follows pylatexenc's `LatexArgumentSpec`.
//!
//! **Every** argument routes to an `ArgumentParser` object. The core cannot know a
//! language's argument forms — which group class a `{…}` argument uses, whether `[…]`
//! is a group rule of the current parsing state or one declared for the argument
//! only — so the standard parsers (delimited group, optional group, literal marker,
//! expression, delimited verbatim) are core `ArgumentParser` implementations in
//! [`constructs`](crate::core::constructs), parameterized by group types and rules. The
//! latexlike preset turns the familiar `'{'` / `'['` / `'*'` / … argument codes into
//! configured instances — [`argument_specs`](crate::latexlike::argument_specs)
//! documents the code table.
//!
//! **Arguments and slots.** Arguments *configure* an invocation (`\frac{a}{b}`,
//! `\item[label]`) and are declared here. Slots — the *content regions* of a parsed
//! callable, such as an environment's body, of which a fence-block construct may have
//! several — have no declaration at all: there is no `SlotSpec`. Parsing a body needs
//! invocation facts no declarative list can supply (the `\end{name}` back-reference,
//! the arguments parsed so far — pylatexenc's
//! `EnvironmentSpec.make_body_parser(token, nodeargd, …)`), so a body-bearing
//! callable's `make_invocation_parser` parses the body with whatever parsers it chooses
//! (terminator syntax parameterizes the core `EnvironmentBodyParser`,
//! `constructs::environment_parser`) and creates the
//! [`ParsedSlot`](crate::core::node::ParsedSlot) records itself. Arguments and slots
//! are the same thing in the parsed *records* — a region, a name, an ext — and
//! different things spec-side.
use Box;
use Arc;
use Vec;
use Any;
use fmt;
use crate;
use crate;
use crate;
/// What an [`ArgumentParser`] returns for a *provided* argument: the region's staged
/// nodes, which of them are the content, and the argument's ext.
///
/// The nodes are the argument's full syntactic extent in source order — leading noise
/// (comment nodes, whitespace-only `Chars` nodes) first, then the syntax-bearing
/// node(s). The standard invocation parser appends them to the callable's child list
/// and records a staged [`ChildRegion`](crate::core::node::ChildRegion) over them;
/// `content` becomes that region's content designation (both [`ContentNodes`] forms are
/// already relative to the region or anchored on a staged node); `ext` becomes the ext
/// of the [`ParsedArgument`](crate::core::node::ParsedArgument) record.
// Manual impls: derives would demand `L:` bounds although only associated types
// (already bounded in `Lang`) are stored.
/// How one argument of an invocation is recognized and parsed.
///
/// This is the single argument-parsing interface — the core privileges no argument form
/// of its own — and corresponds to pylatexenc's "any `LatexParserBase` instance as
/// `LatexArgumentSpec.parser`". The standard implementations are in
/// [`constructs`](crate::core::constructs):
/// [`GroupArgumentParser`](crate::core::constructs::GroupArgumentParser),
/// [`OptionalGroupArgumentParser`](crate::core::constructs::OptionalGroupArgumentParser),
/// [`MarkerArgumentParser`](crate::core::constructs::MarkerArgumentParser),
/// [`ExpressionParser`](crate::core::constructs::ExpressionParser). An
/// [`ArgumentSpec`] pairs one of them with the argument's name and state delta.
///
/// The entry point is [`parse_argument`](ArgumentParser::parse_argument). An
/// implementation parses one argument region and stages its nodes, designating which of
/// them are the content ([`ChildRegion`](crate::core::node::ChildRegion) /
/// [`ContentNodes`](crate::core::node::ContentNodes)), or reports the argument absent;
/// the standard invocation path records the result in the node's
/// [`ParsedArguments`](crate::core::node::ParsedArguments) like any other argument. A
/// parser that needs group delimiters declares the
/// [`GroupRule`](crate::core::token::GroupRule)s it wants through a state delta covering
/// the argument's extent — an optional-argument parser declaring `[`…`]` for that
/// argument only, a custom spec declaring `<`…`>`.
///
/// **An argument parser owns its argument's entire region, leading noise included.** It
/// scans whitespace and comments itself (usually with the standard noise-scan helper)
/// and stages them as ordinary nodes — comment nodes, whitespace-only `Chars` nodes —
/// ahead of the argument's syntax. The core never scans noise on a parser's behalf:
/// noise handling is inseparable from the argument's syntax (a verbatim argument whose
/// delimiter is the comment character must see the raw token stream), and the scan must
/// run under the argument's own parsing-state delta.
///
/// **Reporting the argument absent means consuming nothing**: the reader is rewound
/// past any scanned noise, which is then parsed as enclosing content, and nodes staged
/// on speculation are never claimed (the builder drops them).
///
/// **Thread safety is part of the contract** (`Send + Sync` supertraits; see
/// [`CallableSpec`](super::CallableSpec)'s note). An argument parser is stored
/// behavior — `Arc`-shared inside an [`ArgumentSpec`] and reused for every invocation —
/// so it is immutable, takes `&self`, and receives everything specific to one use as
/// arguments; the construct parsers it drives internally are temporaries instead.
///
/// **Downcasting is part of the contract** (`Any` supertrait;
/// [`CallableSpec`](super::CallableSpec)'s downcasting note applies): a consumer
/// recovers a parser's concrete type from a stored `Arc<dyn ArgumentParser<L>>` or
/// `&dyn ArgumentParser<L>`.
/// Sealed conversion into a shared [`ArgumentParser`] — what [`ArgumentSpec::new`] and
/// [`new_unnamed`](ArgumentSpec::new_unnamed) accept as their parser argument.
///
/// A parser passes **by value**
/// (`ArgumentSpec::new(GroupArgumentParser::new(…), "title")`, with no `Arc::new` at
/// the call site), while an already-shared **`Arc<P>`** or
/// **`Arc<dyn ArgumentParser<L>>`** passes through unchanged rather than being wrapped
/// a second time, so one parser reused across many argument specs stays one value. The
/// spec-side counterpart is [`IntoCallableSpec`](super::IntoCallableSpec).
///
/// Sealed: the three implementations are the whole vocabulary; downstream code
/// implements [`ArgumentParser`], never this trait. (The `M` parameter is a sealed
/// inference marker distinguishing the three argument shapes — it never needs to be
/// named.)
/// One argument accepted by a callable (pylatexenc's `LatexArgumentSpec`).
///
/// Build one with [`new`](ArgumentSpec::new), which names it, or with
/// [`new_unnamed`](ArgumentSpec::new_unnamed), and collect the arguments of one
/// callable into a [`StdCallableSpec`](super::StdCallableSpec) or a preset spec type.
/// For a latexlike language the whole list is usually written as argument codes
/// instead — `"o"` for an optional `[…]` argument, `"m"` for a mandatory one — see
/// [`argument_specs`](crate::latexlike::argument_specs) for the code table.
// Manual impls: derives would demand `L:` bounds although only associated types (already
// bounded in `Lang`) and `Arc`s are stored. No `PartialEq`: a spec carries a parser
// (behavior has no structural equality) and possibly a state delta.