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
//! [`StdInvocationParser`]: the default declarative invocation parser, returned by
//! [`CallableSpec::make_invocation_parser`](crate::core::specs::CallableSpec::make_invocation_parser)
//! (pylatexenc's `LatexMacroCallParser`-family, behind one factory).
//! The full invocation-parsing contract — what every parser returned by that factory
//! runs under — is documented on [`StdInvocationParser`] itself.
use Box;
use Arc;
use Vec;
use fmt;
use crate;
use crate;
use crateSourceSpan;
use crate;
use crateTokenEdge;
use crate;
use ;
/// Parses a callable's declared arguments at the reader's position, returning the
/// nodes they staged and one record per declared argument.
///
/// This is the argument half of [`StdInvocationParser`], public as a building block
/// for parsers that take over an invocation: an environment-shaped
/// `make_invocation_parser` override runs this same loop between reading its
/// `\begin{name}` name group and parsing its body.
///
/// It iterates `callable_spec.arguments()` in invocation order, running each argument's
/// parser under the argument's own state — the spec's `parsing_state_delta` stacked on
/// `cx.state`, session-mediated, reverted structurally — and
/// collects the provided regions' nodes into one child list plus one [`ParsedArgument`]
/// entry per spec (absent arguments keep their entry and contribute no nodes). The
/// returned regions are staged in child-list offsets, ready for the caller's
/// [`ParsedArguments`] record.
///
/// Each argument runs under its own traceback frame (`argument #N of ‘\frac’`).
/// `name` is the span of the invocation spelling, quoted into that title when the
/// traceback is snapshotted: a spec does not know the name it was registered under,
/// so the spelling has to come from the caller. An environment composition passes the
/// span of its *environment* name, so its frames quote `align`, not `\begin`.
/// Parses one callable invocation from the arguments its spec declares, and stages
/// the resulting `Callable` node.
///
/// This is what
/// [`CallableSpec::make_invocation_parser`](crate::core::specs::CallableSpec::make_invocation_parser)
/// returns unless a spec overrides it, so the engine reaches for it for every
/// callable that does not take over its own parsing: the content loop resolves a
/// trigger token, consumes it, and runs the parser the spec's factory returned. It is
/// constructed per invocation and dropped when the invocation's parse ends (see the
/// two-tier ownership model in [`core::constructs`](crate::core::constructs)).
///
/// Its documentation also states the invocation-parsing contract that a replacement
/// parser from an overridden factory runs under.
///
/// # Contract
///
/// Constructed around the resolved [`Invocation`], which travels inside the parser
/// instance. The **caller consumes the token that triggered the invocation, whole** —
/// `move_to(token, TokenEdge::EndPastPostSpace)`,
/// syntactic post-space included —
/// before running the parser (the dispatch-loop arm that peeked it, mirroring the
/// [`GroupParser`](super::GroupParser) contract; loop progress holds by construction,
/// since no invocation parser can forget to consume its trigger). The token's pre-space
/// is likewise the caller's (housed as sibling content). A takeover parser that needs
/// the trigger's post-space bytes raw (the `\verb` idiom) repositions the reader
/// itself, at the trigger's own [`End`](crate::core::token::TokenEdge) edge —
/// `move_to(token, TokenEdge::End)`, where the token proper ends and its
/// post-space begins.
///
/// `cx.state` is the invocation's **base** state: the caller resolves any
/// [`InvocationChildState`](super::InvocationChildState) policy first and scopes the
/// state structurally (swap/revert).
///
/// # Arguments
///
/// The parser iterates the spec's [`ArgumentSpec`](crate::core::specs::ArgumentSpec)s in
/// invocation order, running each argument's
/// [`ArgumentParser`](crate::core::constructs::ArgumentParser) under the argument's
/// own state — the spec's `parsing_state_delta` stacked on the invocation's base
/// (session-mediated, so the transition is observed), reverted structurally after; the
/// argument's whole extent, noise scan included, runs under it. Each provided argument
/// contributes its region's nodes to the child list and a staged
/// [`ChildRegion`](crate::core::node::ChildRegion) to the [`ParsedArguments`] record; an
/// absent argument keeps its entry (spec included — the record is self-describing) and
/// contributes nothing. Missing-mandatory recovery is the argument parser's own
/// detection-site business: by the time `parse_argument` reports absent, any
/// diagnostic is already recorded.
///
/// The node's span starts where the trigger token starts and ends at the last staged
/// child (the children block is span-contiguous by construction: each argument region
/// starts where the previous one ended). With no argument provided there is no such
/// child, and the node ends where the reader stands — just past the trigger's own
/// syntactic post-space. Both are the standard rule of
/// [`stage_invocation`](ParseContext::stage_invocation), which states the exact
/// contract.
///
/// Argument parsers return no after-effect deltas (an argument scopes no state beyond
/// its own extent) and neither does this parser.
///
/// # Invocation syntax
///
/// [`CallableData::invocation_syntax`](crate::core::node::CallableData::invocation_syntax)
/// records the language's trigger-spelling
/// facts, minted from the [`Invocation`] via the standard constructor
/// ([`FromInvocation`](super::FromInvocation)) inside
/// [`stage_invocation`](ParseContext::stage_invocation). The latexlike payload
/// records e.g. **exactly the trigger token's syntactic post-space** — the
/// name-terminating whitespace the tokenizer already claimed as invocation syntax
/// (pylatexenc's `macro_post_space`); nothing beyond it is ever claimed:
/// whitespace after a single-character command (`\& b`) or after a final argument
/// is ordinary sibling/region content, exactly as TeX treats it. With arguments
/// present that recorded post-space sits **between** the name and the first
/// argument region — a sub-range of the node's span, no longer necessarily
/// trailing (whitespace invariant 3).
///
/// # Slots
///
/// `StdInvocationParser` is macro-shaped: it parses no body and records empty
/// [`ParsedSlots`]. Slots are record-level vocabulary with no spec-side declaration
/// (there is nothing a spec could declare that this
/// parser wouldn't parse): body content is inseparable from terminator syntax and from
/// invocation facts like the `\end{name}` back-reference, so a body-bearing spec
/// overrides
/// [`make_invocation_parser`](crate::core::specs::CallableSpec::make_invocation_parser) with a
/// composition that drives [`EnvironmentBodyParser`](super::EnvironmentBodyParser) and
/// mints its own [`ParsedSlot`](crate::core::node::ParsedSlot) records (the
/// argument half is shared as [`parse_declared_arguments`]) — and says "I take
/// material" via [`requires_content`](crate::core::specs::CallableSpec::requires_content), the
/// expression-position guard's channel.