Expand description
Lowering rules: what a lowering rule is, what it declares, and the interface through which it builds the presentation of one element for one backend.
Lowering rewrites each frozen tree into a presentation tree for one
backend, by applying the lowering rule selected for each element, in
rounds, until only elements the backend writes natively remain; the result
is computed data in the table flm.lowered.<backend> (backends, lowering,
and outputs).
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 the element’s presentation for one backend (LoweringRule).
To stage a node is to build it into the tree that the rule is producing
(Lowerer defines the term).
This module defines what a rule is and what it may do. A feature registers
a rule with
FeatureBuilder::lowering_rule.
The lowering stage that calls the rules, the lowering driver that
implements Lowerer, and the presentation elements the rules build
are in crate::stdlib::backends::lowering and
crate::stdlib::backends::pres.
The items of this module:
Lower, the trait a rule author implements, andLowering, what it returns for one node;LoweringDecl, what a rule declares;DefaultMark, on which backends the rule is chosen by default; andLoweringRule, the registry entity that carries both together with the implementation;Lowerer, the interface a rule works through, andLowerCx, the tree under construction into which the rule stages nodes;NodeRegionandRegionShape, with which a rule lowers an argument or a content slot of a node of another tree (Lowerer::lower_region);LowerError, the failure of a rule at one node;chars_at_loweringandspelling_at_lowering, the two ways a rule reads content that may hold a placeholder of a user definition’s template: as characters, or as written.
§How a rule is called
A rule is written for one element. When a backend’s lowering stage reaches
a node of that element, and the plan selected the rule for that element on
that backend, the stage calls Lower::lower with the frozen node. The
rule returns either Lowering::Default (copy this node and lower its
children) or Lowering::Emit with the nodes it built to replace the
node. The rule builds nodes through two values it is handed: the
Lowerer, which lowers parts of the tree and builds presentation nodes,
and the LowerCx, the tree under construction. Every Lowerer method
that builds something takes the LowerCx as an argument, because the
Lowerer cannot hold it: its own operations lower parts of the node, which
needs the same context, so the two are borrowed separately.
§What a rule declares
Plan construction reads a rule’s LoweringDecl before any rule runs, and
uses each part as follows:
requires,reads,reads_optandoptionsare added to the needs of its backend’s lowering stage; a rule that reads the table of resolved references therefore causes the stage that writes that table to be scheduled;produceslists every presentation element the rule may build. Plan construction follows these lists to check that the backend can, in the end, write every element a document may contain, and theLowererrefuses to build an element the rule did not list;selection_requireslists the requirements (identifiers of definitions that the output needs,StyleDef) that the rule has merely because it was selected, whether or not it ever runs;unchecked_outputlists the backends on which the rule writes output that nothing checks before the program reading the output does; a plan that selects the rule on such a backend keeps aPlanWarning::UncheckedOutput, one per backend and feature, which every run of the plan reports as the Warningflm.run.unchecked-output. The declaration is fixed when the rule is registered, so the warning depends on the plan alone, not on a document. The feature that registers a rule owns it: a rule of the library registered by another feature under its own name (math_rule_named) keeps its declaration, and the warning names that feature.
§Which rule is chosen
Several rules may target one element: the rule of the feature that
declares the element, and a more specific one written for a particular backend. The plan
chooses one rule per backend and element, in a fixed order: a rule the
plan request prefers by name; otherwise no rule, if the backend writes the
element natively; otherwise the rule marked DefaultMark::OnBackends for
that backend; then the rule marked DefaultMark::Everywhere; then the
only candidate, if there is exactly one. Several candidates and nothing to
choose between them is a plan error that lists the candidates. See
SelectedRules for the rule selection
order in full.
Structs§
- Lowering
Decl - What a lowering rule declares about itself: what it needs, what it may build, and what it requires merely by being selected.
- Lowering
Rule - A lowering rule: a named registry entity written for one element, which
declares what it requires, reads and produces, and implements
Lower, which stages the element’s presentation for one backend. - Unchecked
Output Decl - One backend on which a lowering rule writes output that nothing checks
before the program reading that output does
(
LoweringDecl::unchecked_output).
Enums§
- Default
Mark - The default mark of a lowering rule: on which backends the rule is chosen for its element when the plan request does not prefer a rule by name.
- Lower
Error - The failure of a lowering rule at one node.
- Lowering
- What a lowering rule returns for one node: copy it, or replace it.
- Node
Region - A node region: one argument or one content slot of a node, given by
position, which a rule passes, with the node’s address, to
Lowerer::lower_regionto have that region’s content lowered.
Traits§
- Lower
- The implementation of a lowering rule: what to do at a node of the rule’s element.
- Lowerer
- The interface a lowering rule works through.
Functions§
- chars_
at_ lowering - Returns the characters of the run of nodes
nodes, for a lowering rule that needs characters (an address, a reference key, verbatim content, a word) while lowering. - spelling_
at_ lowering - Checks whether a lowering rule may compute on the spelling of the run
of nodes
nodeswhile lowering, that is, on the content as written, markup included, rather than on its characters (chars_at_lowering). ReturnsOk(())when no placeholder of a user definition’s template occurs in the content, at any depth, andLowerError::PlaceholderCharacterswhen one does.
Type Aliases§
- LowerCx
- The presentation tree under construction, into which a lowering rule stages nodes: techy’s restaging context over the FLM language.
- Region
Shape - The function that
Lowerer::lower_regioncalls to lower a node region’s content, in the form the caller chooses.