Skip to main content

Module lowering

Module lowering 

Source
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:

§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_opt and options are 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;
  • produces lists 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 the Lowerer refuses to build an element the rule did not list;
  • selection_requires lists 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_output lists 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 a PlanWarning::UncheckedOutput, one per backend and feature, which every run of the plan reports as the Warning flm.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§

LoweringDecl
What a lowering rule declares about itself: what it needs, what it may build, and what it requires merely by being selected.
LoweringRule
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.
UncheckedOutputDecl
One backend on which a lowering rule writes output that nothing checks before the program reading that output does (LoweringDecl::unchecked_output).

Enums§

DefaultMark
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.
LowerError
The failure of a lowering rule at one node.
Lowering
What a lowering rule returns for one node: copy it, or replace it.
NodeRegion
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_region to 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 nodes while lowering, that is, on the content as written, markup included, rather than on its characters (chars_at_lowering). Returns Ok(()) when no placeholder of a user definition’s template occurs in the content, at any depth, and LowerError::PlaceholderCharacters when 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.
RegionShape
The function that Lowerer::lower_region calls to lower a node region’s content, in the form the caller chooses.