Skip to main content

flm_core/stdlib/backends/lowering/
mod.rs

1//! Lowering: the stage that rewrites each frozen tree into the presentation
2//! tree one backend emits, the driver that runs it, and the rules and helpers
3//! every backend shares.
4//!
5//! **Lowering** rewrites each frozen tree into a presentation tree for one
6//! backend, by applying the lowering rule selected for each element, in
7//! rounds, until only elements the backend writes natively remain; the result
8//! is computed data in the table `flm.lowered.<backend>` (see [backends,
9//! lowering, and
10//! outputs](crate::guide::concepts_overview#backends-lowering-and-outputs)).
11//! The frozen trees themselves never change (see [frozen trees and computed
12//! data](crate::guide::concepts_overview#frozen-trees-and-computed-data)).
13//! After lowering, the backend's emitter writes the output from the lowered
14//! trees (for the text backend, [`mod@super::text::emit`]).
15//!
16//! The terms this module relies on:
17//!
18//! - A **backend** is the registration bundle for one output format
19//!   ([`BackendDef`](crate::core::registry::BackendDef)). Every backend
20//!   registers exactly one lowering stage, [`LoweringStage`], a `Compute`
21//!   stage of `PerTree` scope named `flm.<backend>.lower`.
22//! - A **lowering rule** is a named registry entity written for one element:
23//!   it declares what it requires, reads and produces, and implements `Lower`,
24//!   which stages (builds into the lowered tree) the element's presentation
25//!   for one backend ([`LoweringRule`](crate::core::lowering::LoweringRule)).
26//!   The plan selects one rule per element and backend.
27//! - A **presentation node** is a node of a presentation element, an element
28//!   that never occurs in a source document ([`super::pres`]). A lowered tree
29//!   is built from presentation nodes and from nodes of the elements the
30//!   backend writes natively: where the frozen tree holds a paragraph, the
31//!   lowered tree holds `flm.pres.par`; where it holds a heading,
32//!   `flm.pres.block-heading`; where it holds a reference, a `flm.pres.link`
33//!   around the reference's displayed content.
34//! - A **decoration** is a node whose element carries the
35//!   [`Decoration`](super::pres::Decoration) aspect. When a rule stages a
36//!   presentation node, the driver moves every decoration out of the node's
37//!   content slots into an attached slot of that node (see [decorations,
38//!   roles and kinds](crate::guide::concepts_overview#decorations-roles-and-kinds)).
39//! - A **piece** is a document-level synthesized value that a backend places
40//!   before or after the document's content
41//!   ([`Piece`](crate::core::piece::Piece)); [`lower_value`] lowers one, for
42//!   the pieces stage ([`super::pieces`]).
43//! - A **node region** is one of a node's arguments or one of its slots
44//!   ([`NodeRegion`](crate::core::lowering::NodeRegion)). A rule may lower a
45//!   node region of another node where its own node is placed, which is how a
46//!   [content pointer](ContentPointer) shows content written elsewhere.
47//!
48//! Every rule works through the [`Lowerer`], the only interface a lowering
49//! rule has to the run, which the driver implements: through it the rule
50//! lowers parts of its node, stages presentation nodes, splices a synthesized
51//! value and lowers a node region of another node.
52//!
53//! # Module map
54//!
55//! - [`stage`] — [`LoweringStage`], the per-backend stage. The needs it
56//!   declares when the plan is built are the union of the selected rules'
57//!   declarations ([`rules_needs`], shared with every stage that lowers for a
58//!   backend), and the number of rounds it allows is [`rank`].
59//! - [`driver`] — [`lower_tree`], which runs the rounds over one tree;
60//!   [`lower_value`], which runs them over a synthesized value, such as a
61//!   piece, after bringing it up to date; the implementation of the
62//!   [`Lowerer`] (lowering of a node's parts, staging of presentation nodes
63//!   with their provenance, decorations moved into attached slots, splicing a
64//!   synthesized value, and the checks that a rule reads only what it
65//!   declared). Also the helpers a rule calls: [`copy_subtree_verbatim`] and
66//!   [`copy_run_verbatim`] for opaque content; [`read_verbatim_content`] and
67//!   [`stage_verbatim_content`], through which every rule that stages
68//!   verbatim content reads it as characters, reporting content that is not
69//!   characters ([`ContentNotCharacters`]); [`lower_run`], which hands a run
70//!   of sibling nodes back to the ordinary rules; and [`lower_region_content`],
71//!   the plain shape of a node region lowered through
72//!   [`Lowerer::lower_region`](crate::core::lowering::Lowerer::lower_region).
73//! - [`table`] — [`LoweredTrees`], the `flm.lowered.<backend>` table, and
74//!   [`LoweredAddr`], the address of a node inside a lowered tree.
75//! - [`rules`] — the default lowering rules of the structural elements, and
76//!   the helpers that stage paragraphs and trimmed inline content for other
77//!   rules.
78//! - [`mod@pointer`] — [`ContentPointer`], the payload of a synthesized node
79//!   that shows content written elsewhere without copying it, and
80//!   [`lower_content_pointer`], the lowering every such node's rule calls,
81//!   with a check supplied by the rule's own feature.
82//! - [`error`] — the `flm.lowering.*` conditions and
83//!   `flm.verbatim.content-not-characters`, and the recovery after each.
84//! - [`mod@feature`] — the `lowering` feature, which registers the
85//!   presentation vocabulary, the default rules, the two native-set
86//!   implications the driver relies on ([`native_implications`]) and two
87//!   service slots.
88//!
89//! [`Lowerer`]: crate::core::lowering::Lowerer
90
91pub mod driver;
92pub mod error;
93pub mod feature;
94pub mod pointer;
95pub mod rules;
96pub mod stage;
97pub mod table;
98
99pub use driver::{
100    LoweringInputs, LoweringOutcome, VerbatimContent, copy_run_verbatim, copy_subtree_verbatim,
101    lower_region_content, lower_run, lower_tree, lower_value, non_native, read_verbatim_content,
102    stage_verbatim_content,
103};
104pub(crate) use driver::lower_written_pieces;
105pub use error::{
106    ContentNotCharacters, FieldNotRead, LoweringFailed, MathUnprocessed, NestingTooDeep, NoRule,
107    RegionNotFound, RoundsExceeded, RuleFailed, UnsupportedElement,
108};
109pub use feature::{
110    FEATURE_NAME, LoweringFeature, NAMER_SLOT, REGIONS_SLOT, feature, native_implications,
111};
112pub use pointer::{ContentPointer, PointerCheck, lower_content_pointer};
113pub use rules::{
114    BLOCKS_STRUCTURED, FIELD_RULE, FieldRule, MATH_RULE, MathGroupRule, PARAGRAPH_RULE,
115    ParagraphRule, UNKNOWN_ENVIRONMENT_RULE, UNKNOWN_MACRO_RULE, UnknownEnvironmentRule,
116    UnknownMacroRule, VERBATIM_RULE, VerbatimRule, default_rules, is_blank, lower_arg_named_or_nothing,
117    lower_arg_or_nothing, lower_blocks_with_edges, lower_paragraph, stage_trimmed, stage_trimmed_runs,
118};
119pub use stage::{LoweringStage, rank, rules_needs, selection_requires};
120pub use table::{LoweredAddr, LoweredTree, LoweredTrees, lowered_table, lowered_table_name};