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
//! Block-transparent elements: the [`BlockTransparent`] aspect, the result it
//! returns, and the [`BlockCutter`] operations it calls during the cut step of
//! the [block-structure pass](super).
//!
//! A **block-transparent** element is one whose content is laid out as if it
//! were written directly in the content around it (see [block-transparent
//! elements](crate::guide::concepts_overview#block-transparent-elements)). An
//! ordinary element whose argument is laid out according to its content
//! ([`ArgLayout::InlineOrBlocks`](crate::lang::mint::ArgLayout::InlineOrBlocks))
//! becomes a block of its own when that argument holds a paragraph break or a
//! list and the element's [block role](crate::model::aspects::BlockRoleAspect)
//! follows that content (the element tests its argument with
//! [`argument_holds_blocks`](super::argument_holds_blocks) to choose its role);
//! the argument's content is then laid out as paragraphs inside the node. An
//! argument declared inline reports the paragraph break instead. A
//! block-transparent element never becomes a block of its own: a paragraph
//! break inside its content is a paragraph break of the content around it, and
//! a list inside its content is a block of the content around it. A mark that an author places over a passage is the typical case
//! (the `annotations` feature's highlight, [`crate::stdlib::features::annotations`]):
//! the passage may cross a paragraph boundary, and the mark continues across
//! it.
//!
//! # The cut step
//!
//! A paragraph holds whole nodes, so the layout step cannot place one node
//! across two paragraphs. Before any paragraph is built, the
//! **cut step** of the pass therefore replaces every block-transparent node
//! whose content crosses a paragraph break or holds a block by **cut parts**:
//! nodes that each hold one stretch of the content (a stretch of inline content
//! between two paragraph breaks, or one block), with the paragraph breaks
//! between them. The layout step that follows places each inline cut part in
//! its paragraph and each block cut part among the blocks, as it places any
//! node.
//!
//! # What the element does, and what the cut step provides
//!
//! Only the element's own code can say what its arguments mean, which of them
//! to cut, and what a cut part of it is: the element itself rebuilt around the
//! cut part's content, a node of another element, or a block passed through as
//! it is. So
//! the cut step calls the element's aspect with the whole parsed node, and the
//! aspect returns the staged nodes that replace the node ([`Cutting`]). The
//! cut step provides, through [`BlockCutter`], what an element cannot do
//! alone:
//!
//! - **classifying while restaging**
//! ([`restage_and_classify`](BlockCutter::restage_and_classify)): a staged
//! node cannot be read back, so its block role cannot be asked once it is
//! staged. A run of nodes is therefore classified from its input nodes while
//! they are staged. A block-transparent node inside the run is cut at the
//! same time, which makes the cut recursive.
//! - **rebuilding the node** ([`rebuild`](BlockCutter::rebuild)) around new
//! content, with data of the element's own attached to it. techy's operation
//! for rebuilding a node copies the node's extension unchanged, so a rebuild
//! that attaches data goes through techy's lower-level tree builder; the cut
//! step implements that rebuild once, for every element.
//! - **the cut number** ([`new_cut_number`](BlockCutter::new_cut_number)): a
//! number unique within the tree, which an element may write into its cut
//! parts so that the parts of one cut can be told apart from those of
//! another. Uniqueness is a property of the whole walk, which only the cut
//! step sees.
//!
//! Everything else is the element's choice: which arguments or body to scan, how to group
//! the classified nodes into cut parts, and which cut parts carry data about
//! where they came from, and of what type.
//!
//! # The continuation record
//!
//! An element whose cut parts must know where they came from (for example, the
//! first cut part shows a label and the others do not) attaches a value of a
//! type of its own to each cut part, through [`rebuild`](BlockCutter::rebuild).
//! This value is the *continuation record*. It is held in the node's
//! [`FlmNodeExt::payload`](crate::lang::FlmNodeExt::payload), is copied with
//! the node when the node is copied into a synthesized value, and is read by
//! code that knows its type. The cut step defines no record type and keeps no
//! table of cuts. A node the cut step left alone carries no record.
//!
//! A feature attaches the aspect to its element by the trait-object type, like
//! every aspect: `.with::<dyn BlockTransparent>(Arc::new(implementation))`.
use Arc;
use ;
use StructureError;
use ;
use cratePresPayload;
use crateFlm;
use crateFlmDiagnostic;
use crateElementSpec;
/// What a [`BlockTransparent`] aspect returns for its node: leave it as it is,
/// or replace it by staged items.
/// One staged node of the cut step, classified by what it is to the layout
/// step that follows.
///
/// [`BlockCutter::restage_and_classify`] returns a run as these items, and a
/// [`BlockTransparent`] aspect returns the replacement of its node as these
/// items ([`Cutting::Emit`]). The variant of an item (`Inline`, `Blank`,
/// `Marker` or `Block`) matters when the node being cut is inside the content
/// of another block-transparent node: the outer node's aspect receives the
/// inner node's items, variants included, from its
/// own call to [`restage_and_classify`](BlockCutter::restage_and_classify).
/// An aspect therefore classifies each cut part by what it is:
/// [`Inline`](CutItem::Inline) for a cut part holding inline content,
/// [`Block`](CutItem::Block) for one that is a block, and the markers and
/// blank items it passes through with the variant they came with.
/// Designates an argument or the body of a callable node, to which
/// [`BlockCutter::rebuild`] gives new content.
///
/// A `CutPart` is not a cut part (a node a cut produces, see the [module
/// documentation](self)): it says which argument or body of the node being
/// rebuilt receives the content.
/// The aspect an element carries to be **block-transparent**: its content is
/// laid out as if it were written in the content around it, and its node is
/// cut into cut parts at the paragraph breaks and blocks that its content
/// holds, before any paragraph is built. See the [module documentation](self).
///
/// [`cut`](BlockTransparent::cut) is called once for every node of the element
/// that the cut step reaches outside an [opaque
/// region](crate::guide::concepts_overview#opaque-regions), from the root
/// downwards: an outer node's aspect is called before the nodes inside it, and
/// a node inside a run that the outer aspect gives to
/// [`restage_and_classify`](BlockCutter::restage_and_classify) is cut there,
/// its items returned in its place. The aspect returns [`Cutting::Default`]
/// when there is nothing to cut, or the staged nodes that replace the node.
///
/// # A typical implementation
///
/// 1. Read the content (a run of nodes) of each argument or body the element
/// cuts. Return `Default`, staging
/// nothing, when the node is one of the element's own cut parts (it carries
/// the element's continuation record), or when none of the runs holds a
/// cut point ([`run_holds_blocks`](super::run_holds_blocks)).
/// 2. Otherwise give each run to
/// [`restage_and_classify`](BlockCutter::restage_and_classify).
/// 3. Group each maximal stretch of inline items into a cut part, with
/// [`rebuild`](BlockCutter::rebuild): the stretch as the part's new content,
/// and a continuation record as the payload. Make each block item a cut part
/// of its own, and pass the markers through.
/// 4. Return the sequence ([`Cutting::Emit`]).
///
/// After the cut, no cut part may hold a paragraph-break marker at the top
/// level of an argument or body the element cut, nor a block beside other content: the
/// layout step lays out each cut part by its block role, a cut part around
/// inline content as inline content and a cut part around one block as a
/// block. [`rebuild`](BlockCutter::rebuild) does not check this.
///
/// # Cut parts must not be cut again
///
/// Within one cut step, the aspect is never called on a cut part it produced.
/// But the aspect is called at every node of the element, cut parts included,
/// when a tree is structured a second time, or when a cut part copied into a
/// synthesized value is structured again. A block cut part holds a block at
/// the top level of an argument or body, so [`run_holds_blocks`](super::run_holds_blocks)
/// returns `true` for it. Only the element can tell a cut part from a node
/// that was never cut, because the continuation record is of the element's own
/// type. The element therefore recognizes its cut parts by their record and
/// returns [`Cutting::Default`] for them, so that structuring a structured
/// tree changes nothing. An element that attaches no record to a block cut part
/// cuts it again into an identical cut part, which changes nothing either.
///
/// An aspect must not give its *own* node to
/// [`restage_and_classify`](BlockCutter::restage_and_classify), because the cut
/// step would call the aspect again on it. It gives the content of the node's
/// arguments or body instead.
/// The operations of the cut step that a [`BlockTransparent`] aspect may call.
///
/// The cut step itself implements this trait and hands itself to the aspect.
/// Every operation that produces nodes stages them through the context `cx`
/// (builds them into the new tree) and returns their [`BuildId`]s. A staged
/// node cannot be read back, so the aspect keeps what it needs to know about
/// each one (the variants of the items that
/// [`restage_and_classify`](BlockCutter::restage_and_classify) returns tell it)
/// and assembles the staged nodes into its result ([`Cutting::Emit`]). A staged
/// node can be used once, in the result or inside a node the aspect rebuilds;
/// a staged node used nowhere is absent from the tree.
///
/// # Errors
///
/// Every operation that stages fails with [`StructureError`] when an operation
/// of techy's tree builder fails, when the content is nested more deeply than
/// the [recursion guard](crate::lang::guard) allows, or when the aspect of a
/// node inside the content fails; [`rebuild`](BlockCutter::rebuild) also fails
/// for an argument or body the node does not have.