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
//! The `blocks` feature lays out every document tree in paragraphs and blocks
//! and resolves its text, in a rewrite stage that a plan includes when its
//! capability is demanded or required, and it checks the tree contract.
//!
//! # What the feature provides to an author
//!
//! An author separates paragraphs with a blank line, and a block-level
//! construct (a list, a quotation, a float) is placed between paragraphs,
//! never inside one; a displayed formula stands inside the paragraph it is
//! written in. The typographic spellings of TeX are shown as
//! the characters they stand for: `--` as an en dash, ``` `` ``` and `''` as
//! curly quotes, `...` as an ellipsis, each family switched on or off by the
//! feature's configuration ([`BlocksConfig`]). The language guide describes
//! these rules for authors: [paragraphs, blocks and
//! typography](crate::guide::language_syntax#paragraphs-blocks-and-typography).
//!
//! The work is done by the block-structure pass
//! ([`crate::lang::rewrites::blocks`]), a plain function over a tree: it lays
//! out each content region (a run of sibling nodes laid out together) as
//! blocks, as inline content or unchanged, from the block role of each
//! element and the layout each element declares for its arguments and body,
//! and it resolves the text once (merged text nodes, collapsed whitespace, the
//! typographic substitutions). This feature makes that pass a part of the
//! pipeline.
//!
//! # What the feature provides to the pipeline
//!
//! A *capability* ([`CapabilityDef`]) is a registered dotted name for a state
//! of processing that a stage brings about; a stage declares the capabilities
//! it provides and requires, and a caller demands the capabilities it wants
//! ([stages, capabilities and
//! plans](crate::guide::concepts_overview#stages-capabilities-and-plans)). The
//! feature registers:
//!
//! - [`BLOCKS`] (`flm.blocks`), the parent capability: a decision about block
//! structure has been made. Two capabilities *refine* it
//! ([refinement](crate::guide::pipeline#refinement)): each is one way of
//! satisfying it, and a plan that demands both is refused.
//! - [`STRUCTURE`] (`flm.blocks.structure`), the rewrite stage
//! ([`RewriteStage`]: a stage that turns one tree into another before the
//! trees are frozen) that runs the block-structure pass over each tree, with
//! the region layout of the tree's top level
//! ([`FragmentLayout`](crate::model::doc::FragmentLayout)) and the feature's
//! configuration. It publishes
//! the trees it produces in the trees table [`STRUCTURED`]
//! (`flm.blocks.structured`; a [`TreesTable`], the table in which a rewrite
//! stage publishes the trees it produces), which is also the capability it
//! provides, the refinement of `flm.blocks` marked as the default: a plan
//! that demands `flm.blocks` alone gets structured trees.
//! A lowering rule that reads paragraphs requires `flm.blocks.structured`
//! (the rule for paragraphs, for example), which puts the stage into a
//! rendering plan.
//! - [`RAW`] (`flm.blocks.raw`), a marker stage that does nothing and provides
//! the capability of the same name, the other refinement of `flm.blocks`:
//! the trees stay as parsed, without paragraphs and with the text unresolved.
//! The lowering rules of the `latex-recomposed` backend require it, because
//! that backend writes the document back as LaTeX source.
//! - [`CONTRACT_CHECK`] (`flm.blocks.check-contract`, in the module [`check`]),
//! the check stage that runs the [tree-contract
//! checker](crate::lang::rewrites::blocks::check) over every [frozen
//! tree](crate::guide::concepts_overview#frozen-trees-and-computed-data) and
//! provides [`CONTRACT_CHECKED`] (`flm.blocks.contract-checked`), a member of
//! the collection [`flm.validated`](crate::stdlib::VALIDATED) that every
//! standard workflow demands.
//! - The settings declaration of [`BlocksConfig`], whose fields are the
//! feature's settings keys ([`crate::core::settings`]).
//!
//! # Configuration
//!
//! [`BlocksConfig`] says which families of typographic substitution apply. It
//! is the feature's configuration, the plain Rust value the feature is
//! constructed from ([`Blocks::new`]); it is fixed when the registry is built
//! and cannot vary per document or per render ([settings, options and
//! configuration](crate::guide::concepts_overview#settings-options-and-configuration)).
//! The feature also gives the four flags of [`BlocksConfig`] as an input of
//! the [registry digest](crate::model::digest)
//! ([`FeatureBuilder::digest_input`]), a number computed from everything about
//! a registry that can change what processing produces: the trees a run
//! freezes depend on these flags, so two registries that differ in them must
//! not share cached trees.
//!
//! # Interaction with user definitions
//!
//! A use site of a [user
//! definition](crate::lang::rewrites::userdefs::Definition) (a place where a
//! document uses a macro, environment or special character sequence that is
//! defined by substitution; see [`crate::lang::rewrites::userdefs`]) that is
//! not yet expanded can hide a paragraph break that the definition's template
//! holds, so the structure stage must not run before the plan has decided
//! what happens to user definitions. Whenever the registry declares the
//! capability [`USERDEFS`] (`flm.userdefs`, from the
//! [`userdefs`](crate::stdlib::features::userdefs) feature), the structure
//! stage therefore requires it and is ordered after it. Either alternative of
//! that capability satisfies the requirement: the use sites expanded in the
//! tree (`flm.userdefs.expanded`), or kept in the tree for the lowering rules
//! (`flm.userdefs.kept`). Under the same condition, the stage is also ordered
//! after [`ALWAYS_EXPANDED`] (`flm.userdefs.always-expanded`), so that the
//! rewrite stage `flm.userdefs.expand-always` runs before the structure stage
//! whenever both are in the plan. An ordering never adds a stage to a
//! plan: a stage runs because something requires a capability it provides,
//! never because another stage is ordered after it. With no `userdefs` feature
//! registered there are no user definitions, and the structure stage has no
//! requirement.
//!
//! # When the pass fails
//!
//! When the block-structure pass fails over a tree, the stage returns a
//! [`RewriteError`] carrying one Error diagnostic, and the tree is absent from
//! every later table and from the frozen trees. Content nested more deeply than
//! the [recursion guard](crate::lang::guard) allows
//! (`flm.blocks.nesting-too-deep`) gives [`RewriteError::NestingTooDeep`];
//! when the failure happens inside a [synthesized
//! value](crate::guide::concepts_overview#synthesized-values), value catch-up
//! reports it again under a condition of its own
//! ([`StageCx::resolve_value`]).
//! Every other failure gives [`RewriteError::Failed`].
//!
//! # Items
//!
//! - [`Blocks`], the feature, constructed with [`Blocks::new`] from a
//! [`BlocksConfig`] (re-exported here from the block-structure pass).
//! - [`FEATURE`], the feature's name; [`BLOCKS`], [`STRUCTURE`],
//! [`STRUCTURED`] and [`RAW`], the names of its capabilities, stages and
//! trees table.
//! - [`check`]: the contract check stage, with [`CONTRACT_CHECK`] and
//! [`CONTRACT_CHECKED`].
use Arc;
use NodeTree;
use crateTreeId;
use crateSettingsDecl;
use cratePlanCx;
use crate;
use crate;
use crate;
use crateStageOut;
use crateTableName;
use crate;
use crate;
use crateVALIDATED;
use crate;
use ContractCheck;
pub use ;
pub use crateBlocksConfig;
/// The feature's registered name, `blocks`. It is also the middle segment of
/// the names of the feature's stages and diagnostics (`flm.blocks.structure`,
/// `flm.blocks.nesting-too-deep`).
pub const FEATURE: &str = "blocks";
/// The parent capability `flm.blocks`: a decision about block structure has
/// been made. Two capabilities refine it, [`STRUCTURED`] (the default) and
/// [`RAW`]; a plan that demands both is refused. See the module documentation.
pub const BLOCKS: &str = "flm.blocks";
/// The name of the rewrite stage that runs the block-structure pass over each
/// tree, `flm.blocks.structure`. The stage provides [`STRUCTURED`].
pub const STRUCTURE: &str = "flm.blocks.structure";
/// The trees table `flm.blocks.structured`, in which the [`STRUCTURE`] stage
/// publishes the trees it produces. A rewrite stage's trees table has the name
/// of the capability the stage provides, so `flm.blocks.structured` is also
/// that capability: the refinement of [`BLOCKS`] marked as the default, so
/// that a plan that demands `flm.blocks` alone gets structured trees.
///
/// A stage that declares an optional read of this table learns whether the
/// plan structured the trees: the table is written exactly where the
/// structure stage ran.
pub const STRUCTURED: = new;
/// The name of the marker stage `flm.blocks.raw` and of the capability it
/// provides: the refinement of [`BLOCKS`] under which the trees stay as
/// parsed, without paragraphs and with the text unresolved. The marker stage
/// does nothing; it exists so that a plan can choose this alternative, which
/// excludes the [`STRUCTURE`] stage. The lowering rules of the
/// `latex-recomposed` backend require it.
pub const RAW: &str = "flm.blocks.raw";
/// The declaration of the feature's settings: its configuration,
/// [`BlocksConfig`], whose fields are the settings keys
/// ([`crate::core::settings`]).
/// The `blocks` feature ([`FlmFeature`]): it registers the block-structure
/// rewrite stage, the raw marker stage, the contract check stage, their
/// capabilities and the feature's settings. See the module documentation.
///
/// Construct it with [`Blocks::new`] from a [`BlocksConfig`], or with
/// [`Blocks::default`] for every typographic substitution on. The standard
/// package registers it from the `blocks` field of
/// [`StdlibConfig`](crate::stdlib::StdlibConfig).
/// Returns the configuration as the bytes the feature gives to the registry
/// digest: each flag as one byte, in the order the fields are declared.
///
/// The bytes are written out field by field rather than taken from the type's
/// `Debug` output, because that output would change with a field rename and
/// would change every registry digest with it.
/// The rewrite stage `flm.blocks.structure`: it runs the block-structure pass
/// over each tree, with the region layout of the tree's top level
/// ([`FragmentLayout`](crate::model::doc::FragmentLayout)), the feature's
/// configuration and the recursion guard's policy
/// ([`GuardPolicy`](crate::lang::guard::GuardPolicy)) that the registry's
/// language was built with.
/// The marker stage `flm.blocks.raw` ([`RAW`]): it does nothing and provides
/// the capability of the same name, the refinement of `flm.blocks` under which
/// the trees stay as parsed.
;