Expand description
The lowering driver: lower_tree and lower_value, which turn one
tree into one lowered tree, and the implementation of the Lowerer
every lowering rule works through.
A lowering rule is a named registry entity written for one element: it
declares what it requires, reads and produces, and implements Lower,
which stages (builds into the lowered tree) the element’s presentation for
one backend (LoweringRule). The
Lowerer is the only interface a lowering rule has to the run: through
it the rule lowers parts of its node, stages presentation nodes, splices a
synthesized value and lowers a node region of another node. This module
implements it.
The lowering stage (super::stage) calls the driver once per frozen
tree; the pieces stage calls it once per piece. Everything here runs on
the caller’s thread, which the runner has made a worker thread (a thread
that declared its stack size; see
on_worker_thread).
§Rounds
One round is one pass over the tree, from the root down, each parent before its children. At each node, in this order:
- If the backend writes the node’s element natively, the node is copied
and its children are lowered in turn. If the element’s content is
opaque source text (
OpaqueContent), the subtree is copied unchanged and nothing inside it is lowered, except a node carryingContentInOpaque, which holds document content rather than source text and is handled like any other node. - Otherwise, if the plan selected a rule for the element, that rule runs.
It returns
Lowering::Default(make the copy just described) orLowering::Emitwith the nodes it staged; an empty list removes the node. - Otherwise the node lowers to a childless
flm.pres.unsupportedstand-in and one Error diagnostic is reported (super::error).
A rule may stage nodes of elements the backend does not write natively,
so one round is not always enough: if the round’s output still holds such
an element (outside opaque content, which is examined only for the
content of a ContentInOpaque node), another round runs over that
output. The number of rounds allowed is rank, computed
when the plan is built; beyond it the tree fails with
flm.lowering.rounds-exceeded and gets no lowered form.
§Provenance
A copied node keeps its span. Everything a rule stages at a node is placed
on one synthesized source per pair of visited node and rule, created at
that node, so following the provenance of any lowered node leads back to
the document. The lowered tree stores no map from its nodes back to the
frozen nodes; provenance is the only link. For this reason
LoweredAddr gives a position rather than a frozen
node.
§What the Lowerer enforces
Everything a rule reads, lowers or stages goes through the Lowerer.
The driver’s implementation decides the following:
- Decorations are moved into attached slots, always in the same way. A
decoration is a node whose element carries the
Decorationaspect. When a rule stages a presentation node (Lowerer::pres,Lowerer::pres_slots), every decoration among the direct children of one of its content slots is moved into an attached slot named after the decoration’s element, and aflm.pres.deco-marknode (the decoration mark) giving the index of that slot is left at its former position (see decorations, roles and kinds). The driver stages the mark itself, so a rule need not declare it inproduces. A backend that writes any decoration element natively must writeflm.pres.deco-marknatively too; the registry build checks this through the native-set implication theloweringfeature declares (native_implications). - A rule must declare what it produces. The
Lowererrefuses to stage any presentation element the rule’sproducesdeclaration does not list. The only exception isflm.text: a rule may add text to its output without declaring it when the backend writesflm.textnatively. - Inside a splice (
Lowerer::splice),Lowerer::addrreturns the address of the node of the spliced synthesized value, since the tables that the rule may read there hold that value’s results. The nodes staged inside the splice are still recorded as coming from the splicing rule’s own node, so a later round never sees an address of the value. - Inside a region lowering (
Lowerer::lower_region), the opposite holds: the content of a node region of a node of another tree (a document tree, when a synthesized value is being lowered) is lowered in that tree’s own terms. Its nodes return their own addresses and are recorded with them as their origins, so every later round reads the tables at the document’s addresses too. A table read returns a synthesized value’s own results only at that value’s addresses, in every round.
§Failure and recovery
A rule’s failure at one node (an undeclared table or option read, an
undeclared element in produces, a node shape it did not expect, a failed
value catch-up) is
reported as flm.lowering.rule-failed at that node, and the node lowers
to the childless flm.pres.unsupported stand-in. A rule that refuses the
node with a diagnostic of its own (LowerError::Refused) gets the same
stand-in, and its diagnostic is reported in place of
flm.lowering.rule-failed, at every node it refuses. The driver stages
that stand-in itself, without any rule declaring it, so every backend
must write flm.pres.unsupported natively; the registry build checks
this as well, through the second native-set implication the lowering
feature declares.
Two failures end the lowering of the whole tree instead: a node that could
not be staged at all (flm.lowering.failed), and the recursion guard’s
refusal to descend further (flm.lowering.nesting-too-deep).
One outcome is not a failure of the node and is passed on, unreported,
from rule to rule up to the rule that spliced the synthesized value it
arose in: a rule needing the characters of a template’s placeholder
(LowerError::PlaceholderCharacters), which the user-definitions
feature handles by expanding the definition’s use sites. The splice it
passes through discards the value’s diagnostics and requirements with it.
If it reaches the lowering of a whole tree, it ends that lowering as
flm.lowering.failed.
A rule is expected to return every error the Lowerer gives it. The
driver does not define what happens when a rule continues after one; for
example, a refusal of the recursion guard that the rule ignored is still
recorded, and if the tree’s lowering later fails, that failure is reported
as flm.lowering.nesting-too-deep.
Structs§
- Lowering
Inputs - Everything one call of
lower_treeorlower_valueneeds besides the tree itself: what the plan decided for one backend, and the limits the run imposes. - Lowering
Outcome - What lowering one tree produced: the lowered tree, if any, and the diagnostics.
Enums§
- Verbatim
Content - The content of a verbatim region as
read_verbatim_contentreturns it.
Functions§
- copy_
run_ verbatim - Stages unchanged copies of a run of nodes (a math group’s children, a
verbatim group’s children) by applying
copy_subtree_verbatimto each, and returns the copies in order. - copy_
subtree_ verbatim - Stages a copy of
node’s whole subtree, every node copied unchanged with its kind, span, parsing state and extension, and returns the staged copy. - lower_
region_ content - The plain shape of a node region’s content (
RegionShape): the content nodes lowered unchanged, as one run of siblings (lower_run). - lower_
run - Lowers a run of sibling nodes through the rules, in order, and returns the nodes they became.
- lower_
tree - Lowers one frozen tree for one backend: runs the rounds, calls the selected rules, and returns the lowered tree with the diagnostics raised (see the module documentation).
- lower_
value - Lowers a synthesized value for one backend: brings it up to date, then
runs the rounds over it exactly as
lower_treeruns them over a document tree. - non_
native - The elements of
treethat are not innative, in name order and without repetition.lower_treeruns another round while this list is not empty. - read_
verbatim_ content - Reads the content
nodesof a verbatim region as characters, and reportsContentNotCharactersatownerwhen it is not characters. - stage_
verbatim_ content - Stages the content
nodesof a verbatim region owned byownerfor the body of aflm.pres.verbatimnode, and returns the staged nodes: the nodes copied unchanged (copy_run_verbatim) or, for content that is not characters, its source text as one text node (read_verbatim_content, which reports it).