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
//! The block-structure hook: the aspect by which an element lays out its own
//! content during the layout step of the [block-structure pass](super).
//!
//! An aspect is a piece of behavior attached to an element when the feature
//! registers it, and read back through a trait object (see [elements and
//! aspects](crate::guide::concepts_overview#elements-and-aspects)). The
//! block-structure hook, [`BlockStructure`], is the aspect for an element whose
//! content has a block layout that its declarations cannot express. Such an
//! element could, for example, split its body at markers and build blocks for
//! each part, or create nodes that are never written in a document (decorators,
//! placed at the start of its first paragraph or at the end of its last).
//!
//! No standard element uses the hook. The decorations of the standard features,
//! such as a theorem's heading and its closing mark, are placed when the
//! document is lowered for a backend, not by this hook.
//!
//! # How the hook is called
//!
//! The layout step calls the hook once for every node of the element that it
//! reaches outside an [opaque
//! region](crate::guide::concepts_overview#opaque-regions), from the root
//! downwards, before its own default handling of the node. The hook either lays
//! out the node itself and returns the nodes it staged (built into the new
//! tree), or returns [`Structuring::Default`] to leave the node to the default
//! handling. The hook runs while the paragraph around its node is being built,
//! so it lays out its node's own content and never the paragraph around it.
//!
//! The hook works through two values it receives:
//!
//! - [`StructureCx`], the context through which the pass stages nodes;
//! - [`BlockStructurer`], the operations of the layout step that the hook may
//! call (lay out a run of nodes as blocks or as inline content, apply the
//! default handling to a node, rebuild a node around a new body, create a
//! node, report a diagnostic). A staged node cannot be read back, so the
//! hook builds its result from these operations rather than by editing a
//! tree.
//!
//! A node the hook creates is a [`SynthesizedNode`]: a one-node tree of its own,
//! so that it is a real node reference whose block role can be read like any
//! node's, and which can be placed in a run of nodes given to
//! [`BlockStructurer::build_blocks`] or [`BlockStructurer::build_inline`]. Those
//! two operations stage it when they lay out the run. A created node that is
//! never placed has no place in the structured tree: the pass reports it
//! ([`DecoratorWithoutHome`](super::error::DecoratorWithoutHome)) and drops it.
//! [`RegionLayout`] tells the hook whether its node is placed between blocks or
//! inside inline content.
//!
//! A feature attaches the hook to its element by the trait-object type, like
//! every aspect: `.with::<dyn BlockStructure>(Arc::new(implementation))`.
use fmt;
use Arc;
use Lang;
use ;
use ;
use RestageContext;
use StructureError;
use crateElementSpec;
use crateFlm;
use crateFlmSynthesizedSpec;
use crate;
/// The context through which the block-structure pass, a
/// [`BlockTransparent`](super::transparent::BlockTransparent) aspect and a
/// [`BlockStructure`] hook stage nodes: techy's restage context
/// ([`RestageContext`]) over the FLM language.
///
/// To *stage* a node is to build it into the new tree that the walk is
/// producing; a staged node is known by its [`BuildId`] and cannot be read
/// back. The two unit type parameters say that FLM trees carry no
/// annotations.
pub type StructureCx<'t> = ;
/// What a [`BlockStructure`] hook returns for its node: leave it to the
/// default handling, or replace it by nodes the hook staged.
/// The block-structure hook: the aspect an element carries to lay out its own
/// content during the layout step of the [block-structure pass](super).
///
/// [`structure`](BlockStructure::structure) is called once for every node of
/// the element that the layout step reaches outside an opaque region, from the
/// root downwards, before the default handling of that node. The hook lays out
/// the node through the operations of `bs` and returns the nodes it staged, or
/// returns [`Structuring::Default`] to leave the node to the default handling.
/// See the [module documentation](self) for what a hook is for.
///
/// A hook must not give its *own* node to a run it lays out, because the pass
/// would call the hook again on it. It lays out the node's parts instead, with
/// [`body_run`](BlockStructurer::body_run),
/// [`default`](BlockStructurer::default) and
/// [`with_body`](BlockStructurer::with_body).
///
/// The [tree contract](super::check) binds what a hook produces: a body or an
/// argument that its declaration lays out as blocks must hold only paragraphs,
/// block-level nodes and comments, and one laid out inline must hold no
/// paragraph.
/// Where a hook's node is placed in the content being laid out, as
/// [`BlockStructurer::region`] returns it.
///
/// This is the position of the node, not the region layout of the [content
/// region](super) that holds it (both are defined in the module
/// documentation): a node inside a paragraph gets `Inline` even when the paragraph is part of a
/// content region laid out as blocks, and a hook is never called inside an
/// opaque region.
/// A node created by a block-structure hook through
/// [`BlockStructurer::synthesize_callable`], or by a
/// [`BlockTransparent`](super::transparent::BlockTransparent) aspect through
/// [`BlockCutter::synthesize_callable`](super::transparent::BlockCutter::synthesize_callable):
/// a callable node with no arguments, no node slots (techy's `ParsedSlot`, the
/// parts of a node's content outside its arguments, such as an environment's
/// body) and no content, normally of an
/// element declared with
/// [`SyntaxDef::Synthesized`](crate::lang::mint::SyntaxDef::Synthesized), which
/// no document can write (the operation does not check this). Its span is an empty span on a
/// synthesized source created at the span of the node the hook or aspect was
/// called on.
///
/// The node is held in a one-node tree of its own until it is staged. A hook
/// gives [`node`](SynthesizedNode::node) to a run passed to
/// [`BlockStructurer::build_blocks`] or [`BlockStructurer::build_inline`]; an
/// aspect stages it with [`BlockCutter::carry`](super::transparent::BlockCutter::carry)
/// or in a run passed to
/// [`BlockCutter::restage_and_classify`](super::transparent::BlockCutter::restage_and_classify).
/// The operations of the layout step that a [`BlockStructure`] hook may call.
///
/// The layout step itself implements this trait and hands itself to the hook,
/// so a hook lays out content exactly as the pass does, without access to the
/// pass's internal state. Every operation that lays out a run of nodes stages
/// its result through the context and returns the staged nodes; the hook
/// assembles them into its result ([`Structuring::Emit`]).
///
/// # Errors
///
/// Every operation that stages fails with [`StructureError`] when an operation
/// of techy's tree builder fails, when the content is nested too deeply, or
/// when the hook of a node inside the laid-out content fails;
/// [`body_run`](BlockStructurer::body_run) and
/// [`with_body`](BlockStructurer::with_body) also fail with
/// [`StructureError::NoBody`] for a node with no body slot. A node's body slot
/// is the node slot marked by its body marker as the node's body, such as an
/// environment's body (see [`FlmNodeExts`](crate::lang::flm::FlmNodeExts)); a
/// node slot (techy's `ParsedSlot`) is a part of a node's content outside its
/// arguments.