pub trait Lowerer {
Show 25 methods
// Required methods
fn rule(&self) -> &RuleName;
fn backend(&self) -> &BackendName;
fn comment_start(&self) -> Option<&str>;
fn lang(&self) -> &BuiltLang;
fn tree_id(&self) -> TreeId;
fn layout(&self) -> FragmentLayout;
fn addr(&self) -> NodeAddr;
fn addr_of(&self, node: &NodeRef<'_, Flm>) -> Result<NodeAddr, LowerError>;
fn placement(&self) -> Option<TreeId>;
fn get_erased(
&self,
table: &TableRef,
) -> Result<Option<&(dyn Any + Send + Sync)>, AccessError>;
fn producer_of(&self, table: &CapName) -> Option<StageName>;
fn option_erased(
&self,
key: OptionKey,
) -> Result<&(dyn Any + Send + Sync), OptionError>;
fn lower_children(
&mut self,
cx: &mut LowerCx<'_>,
node: &NodeRef<'_, Flm>,
) -> Result<Vec<BuildId>, LowerError>;
fn lower_body(
&mut self,
cx: &mut LowerCx<'_>,
node: &NodeRef<'_, Flm>,
) -> Result<Vec<BuildId>, LowerError>;
fn lower_arg(
&mut self,
cx: &mut LowerCx<'_>,
node: &NodeRef<'_, Flm>,
index: usize,
) -> Result<Vec<BuildId>, LowerError>;
fn lower_arg_named(
&mut self,
cx: &mut LowerCx<'_>,
node: &NodeRef<'_, Flm>,
name: &str,
) -> Result<Vec<BuildId>, LowerError>;
fn lower_nodes(
&mut self,
cx: &mut LowerCx<'_>,
nodes: NodeSlice<'_, Flm>,
) -> Result<Vec<BuildId>, LowerError>;
fn default(
&mut self,
cx: &mut LowerCx<'_>,
node: &NodeRef<'_, Flm>,
) -> Result<Vec<BuildId>, LowerError>;
fn pres(
&mut self,
cx: &mut LowerCx<'_>,
element: &ElementName,
payload: Arc<dyn PresPayload>,
body: Vec<BuildId>,
) -> Result<BuildId, LowerError>;
fn text(
&mut self,
cx: &mut LowerCx<'_>,
text: &str,
) -> Result<BuildId, LowerError>;
fn splice(
&mut self,
cx: &mut LowerCx<'_>,
value: &Arc<NodeTree<Flm>>,
) -> Result<Vec<BuildId>, LowerError>;
fn lower_region(
&mut self,
cx: &mut LowerCx<'_>,
at: NodeAddr,
region: NodeRegion,
shape: &mut RegionShape<'_>,
) -> Result<Vec<BuildId>, LowerError>;
fn require(&mut self, id: &Name);
fn diagnose(&mut self, diagnostic: FlmDiagnostic);
// Provided method
fn pres_slots(
&mut self,
cx: &mut LowerCx<'_>,
element: &ElementName,
payload: Arc<dyn PresPayload>,
slots: Vec<(String, Vec<BuildId>)>,
) -> Result<BuildId, LowerError> { ... }
}Expand description
The interface a lowering rule works through.
To stage a node is to build it into the tree that a lowering rule or a
rewrite hook is producing, through the context the rule or hook is handed
(the LowerCx at lowering); the staged nodes exist in that tree only.
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 into the
LowerCx, splices a synthesized value and lowers a node region of
another node; the lowering driver implements it
(crate::stdlib::backends::lowering::driver). A rule author calls these
methods and does not implement this trait.
Because a rule never reaches the run directly, its reads can be checked:
every table and option read made here is checked against the rule’s own
LoweringDecl, which is narrower than everything its backend’s lowering
stage declared.
The trait is object-safe (a rule receives it as &mut dyn Lowerer), so
the methods that need a type parameter are type-erased here, and their
typed forms (get, option, pres_with, pres_slots_with, error,
warning and note) are defined on dyn Lowerer. Call the typed forms;
the erased ones exist so that the typed forms can be written.
Every method that builds something also takes the LowerCx, because the
nodes are staged into it and this interface does not own it.
§Errors
Each method that can fail returns LowerError, which a rule must return
from Lower::lower instead of continuing.
Required Methods§
Sourcefn backend(&self) -> &BackendName
fn backend(&self) -> &BackendName
Returns the name of the backend for which the tree is lowered.
Sourcefn comment_start(&self) -> Option<&str>
fn comment_start(&self) -> Option<&str>
Returns what starts a source comment under the parsing configuration
the registry was built with, as
FlmParsing::comment_start
does: Some("%%") by default, and None when the configuration
recognizes no comment, in which case no tree holds a comment node. A
rule that writes a comment back takes the prefix from here rather than
from the node, so that it writes the same prefix as the backend’s
emitter.
Sourcefn lang(&self) -> &BuiltLang
fn lang(&self) -> &BuiltLang
Returns the language the registry was built with. A rule compiles against it a text of the document that is written in a language of its own rather than as content: for example, the label template an author writes in a list’s optional argument, which is compiled when the list is lowered, since only the list’s rule reads that text as a template.
Sourcefn tree_id(&self) -> TreeId
fn tree_id(&self) -> TreeId
Returns the identifier of the tree being lowered: a document tree, or a
synthesized value while Lowerer::splice is lowering one.
Sourcefn layout(&self) -> FragmentLayout
fn layout(&self) -> FragmentLayout
Returns the region layout with which the block-structure pass laid out
the top level of the tree being lowered: for a document tree, the
layout of the fragment it was parsed from
(Fragment::layout); for a
synthesized value, the layout with which every synthesized value is
brought up to date, FragmentLayout::default. A rule passes it to
stands_in_blocks
to learn whether a node is laid out in blocks.
Sourcefn addr(&self) -> NodeAddr
fn addr(&self) -> NodeAddr
Returns the address of the node being lowered, the key of the computed data about that node.
Sourcefn addr_of(&self, node: &NodeRef<'_, Flm>) -> Result<NodeAddr, LowerError>
fn addr_of(&self, node: &NodeRef<'_, Flm>) -> Result<NodeAddr, LowerError>
Returns the address of another node of the same tree, for example an ancestor the rule walked up to.
§Errors
Returns LowerError::Structure when node belongs to another tree
or has no address in the tree being lowered.
Sourcefn placement(&self) -> Option<TreeId>
fn placement(&self) -> Option<TreeId>
Returns the tree of the document that the node being lowered places,
or None when it places none, from the run’s record of placements
(StageCx::placement_of).
The rule of the element that places a tree reads it to identify the
tree in the node it builds.
Sourcefn get_erased(
&self,
table: &TableRef,
) -> Result<Option<&(dyn Any + Send + Sync)>, AccessError>
fn get_erased( &self, table: &TableRef, ) -> Result<Option<&(dyn Any + Send + Sync)>, AccessError>
Returns a table the rule declared that it reads, type-erased. Call
get, defined on dyn Lowerer, instead: it returns the table’s value
type.
Ok(None) means that the run holds no value for the table, which can
only happen for a table declared as an optional read.
§Errors
Returns AccessError when the rule did not declare this read, or
declared it under another value type.
Sourcefn producer_of(&self, table: &CapName) -> Option<StageName>
fn producer_of(&self, table: &CapName) -> Option<StageName>
Returns the name of the stage the plan scheduled to write a table the
rule reads, or None when no scheduled stage writes it.
Use it to identify that stage in a message about data the rule expected and did not find.
Sourcefn option_erased(
&self,
key: OptionKey,
) -> Result<&(dyn Any + Send + Sync), OptionError>
fn option_erased( &self, key: OptionKey, ) -> Result<&(dyn Any + Send + Sync), OptionError>
Returns the value of an option type the rule declared that it reads,
type-erased. Call option, defined on dyn Lowerer, instead: it
returns the option type.
§Errors
Returns OptionError when the rule did not declare that option type.
Sourcefn lower_children(
&mut self,
cx: &mut LowerCx<'_>,
node: &NodeRef<'_, Flm>,
) -> Result<Vec<BuildId>, LowerError>
fn lower_children( &mut self, cx: &mut LowerCx<'_>, node: &NodeRef<'_, Flm>, ) -> Result<Vec<BuildId>, LowerError>
Lowers every child of node, and returns the nodes they became.
§Errors
Returns LowerError when lowering a child failed.
Sourcefn lower_body(
&mut self,
cx: &mut LowerCx<'_>,
node: &NodeRef<'_, Flm>,
) -> Result<Vec<BuildId>, LowerError>
fn lower_body( &mut self, cx: &mut LowerCx<'_>, node: &NodeRef<'_, Flm>, ) -> Result<Vec<BuildId>, LowerError>
Lowers the content of node’s body (the slot the element marks as its
body), and returns the nodes it became.
§Errors
Returns LowerError::Structure when the node has no body slot, and
any LowerError the lowering of its content returned.
Sourcefn lower_arg(
&mut self,
cx: &mut LowerCx<'_>,
node: &NodeRef<'_, Flm>,
index: usize,
) -> Result<Vec<BuildId>, LowerError>
fn lower_arg( &mut self, cx: &mut LowerCx<'_>, node: &NodeRef<'_, Flm>, index: usize, ) -> Result<Vec<BuildId>, LowerError>
Lowers the content of node’s argument at position index, counting
from zero, and returns the nodes it became.
§Errors
Returns LowerError::Structure when the node has no argument at that
position or the argument is absent (NodeRegion::is_absent), and
any LowerError the lowering of its content returned.
Sourcefn lower_arg_named(
&mut self,
cx: &mut LowerCx<'_>,
node: &NodeRef<'_, Flm>,
name: &str,
) -> Result<Vec<BuildId>, LowerError>
fn lower_arg_named( &mut self, cx: &mut LowerCx<'_>, node: &NodeRef<'_, Flm>, name: &str, ) -> Result<Vec<BuildId>, LowerError>
Lowers the content of node’s argument named name, and returns the
nodes it became.
§Errors
Returns LowerError::Structure when the node has no argument of that
name or the argument is absent, and any LowerError the lowering of
its content returned.
Sourcefn lower_nodes(
&mut self,
cx: &mut LowerCx<'_>,
nodes: NodeSlice<'_, Flm>,
) -> Result<Vec<BuildId>, LowerError>
fn lower_nodes( &mut self, cx: &mut LowerCx<'_>, nodes: NodeSlice<'_, Flm>, ) -> Result<Vec<BuildId>, LowerError>
Lowers a run of sibling nodes of the tree this Lowerer currently
works in, and returns the nodes they became. That tree is the tree
being lowered, the synthesized value being spliced
(Lowerer::splice), or the tree that holds the node region being
lowered (Lowerer::lower_region).
Nodes of any other tree are refused, since their computed data would be
read at addresses of the wrong tree. Lower a tree the rule builds
itself by splicing it as a synthesized value, and another tree’s
content through Lowerer::lower_region.
§Errors
Returns LowerError::Structure when the nodes belong to another
tree, and any LowerError lowering one of the nodes returned.
Sourcefn default(
&mut self,
cx: &mut LowerCx<'_>,
node: &NodeRef<'_, Flm>,
) -> Result<Vec<BuildId>, LowerError>
fn default( &mut self, cx: &mut LowerCx<'_>, node: &NodeRef<'_, Flm>, ) -> Result<Vec<BuildId>, LowerError>
Does what Lowering::Default asks for, and returns the result: the
node copied, with its children lowered.
Call it when the copy is one part of what the rule returns; return
Lowering::Default when the copy is all the rule returns.
§Errors
Returns LowerError when the copy or a child’s lowering failed.
Sourcefn pres(
&mut self,
cx: &mut LowerCx<'_>,
element: &ElementName,
payload: Arc<dyn PresPayload>,
body: Vec<BuildId>,
) -> Result<BuildId, LowerError>
fn pres( &mut self, cx: &mut LowerCx<'_>, element: &ElementName, payload: Arc<dyn PresPayload>, body: Vec<BuildId>, ) -> Result<BuildId, LowerError>
Builds a node of the presentation element element, carrying payload
and holding body as its content, and returns its position.
This is how a rule creates a presentation node: the method attaches the
payload, the span, and everything else a node of that element needs.
Call pres_with, defined on dyn Lowerer, instead: it takes the
payload by value instead of behind a pointer.
§Errors
Returns LowerError::UndeclaredProduces when the rule’s declaration
does not list element, and LowerError::Build when the node could
not be built.
Sourcefn text(
&mut self,
cx: &mut LowerCx<'_>,
text: &str,
) -> Result<BuildId, LowerError>
fn text( &mut self, cx: &mut LowerCx<'_>, text: &str, ) -> Result<BuildId, LowerError>
Builds a text node holding text, and returns its position.
The text is not part of the document, so it is given a source of its own that identifies the node being lowered and the rule that produced the text. Following that provenance leads back to the document.
§Errors
Returns LowerError::UndeclaredProduces when the backend does not
write text (flm.text) natively and the rule’s declaration does not
list it, and LowerError::Build when the node could not be built.
Sourcefn splice(
&mut self,
cx: &mut LowerCx<'_>,
value: &Arc<NodeTree<Flm>>,
) -> Result<Vec<BuildId>, LowerError>
fn splice( &mut self, cx: &mut LowerCx<'_>, value: &Arc<NodeTree<Flm>>, ) -> Result<Vec<BuildId>, LowerError>
Splices the synthesized value value: lowers it where the node being
lowered is placed, and returns the nodes it became.
A synthesized value is a small tree that a stage builds while a run is
under way, held outside the document’s trees (synthesized
values). It has
been through none of the run’s work, so this method first brings it up
to date: the plan’s rewrite stages run over it, then the per-tree
stages the lowering stage depends on, so that the rules lowering its
nodes read the same kinds of data as for a document node (value
catch-up,
StageCx::resolve_value).
Every diagnostic of the catch-up is reported, and so is every
diagnostic of the value’s lowering: a value spliced at several places
(a kept definition’s template, staged at each of its use sites) reports
at each of them. Each such diagnostic keeps its own span, where the
content was written, and gains one frame of its traceback
(Diagnostic::frames) whose span is
the node being lowered, the place where the value is shown, and whose
title is a copy of this content, shown here. A value spliced inside a
value adds its own frame first, so the frames run innermost first.
§Errors
Returns LowerError::DocumentFactInValue when a stage run again over
the value stated a fact about the document as a whole,
LowerError::CatchUp when the value could not be brought up to date
for any other reason, and any LowerError that the rules lowering
the value returned. When that error is
LowerError::PlaceholderCharacters, the value’s lowering is
discarded entirely: the diagnostics it reported and the requirements it
recorded (require) are dropped, so that what the
rule writes in its place reports and records only its own.
Sourcefn lower_region(
&mut self,
cx: &mut LowerCx<'_>,
at: NodeAddr,
region: NodeRegion,
shape: &mut RegionShape<'_>,
) -> Result<Vec<BuildId>, LowerError>
fn lower_region( &mut self, cx: &mut LowerCx<'_>, at: NodeAddr, region: NodeRegion, shape: &mut RegionShape<'_>, ) -> Result<Vec<BuildId>, LowerError>
Lowers the content of a node region of the node at at (its argument
or its content slot at a position, NodeRegion) here, in the output being
built, as content of that node’s own tree, and returns the nodes it
became.
This is how a rule shows content written elsewhere without copying it.
The rule of an element inside a synthesized value refers to a node of
the document, or of another synthesized value, and that node’s content
is lowered where the element is placed. The content pointer
(ContentPointer)
is built on this method. It is the counterpart of Lowerer::splice,
which lowers a synthesized value where the rule’s node is placed in the
document; this method is the only way to lower content of a tree other
than the one being lowered.
at designates a node of a document tree, or of a synthesized value
this lowering holds: the value being lowered, or a value being spliced
around the call. The region’s content nodes are passed to shape,
which lowers them in the form the caller wants; see RegionShape.
While shape runs, this Lowerer works in the tree that holds the
region: Lowerer::tree_id and Lowerer::layout return that
tree’s, Lowerer::addr and Lowerer::addr_of return addresses in
it, and each node lowered there keeps its own address as its origin, so
that every later round of lowering reads the tables at that address
too. A per-tree table is then read from that tree’s own results: the
run’s store for a document tree, the synthesized value’s own results
for a synthesized value. The results of a synthesized value around the
call are not read at another tree’s address. A table computed for the
whole document is read as everywhere else. The recursion
guard
applies as to any lowering: content that leads back to itself fails the tree
with flm.lowering.nesting-too-deep instead of looping.
The caller must lower each node region once. The lowering driver does not check it: a rule whose element passes one of its regions to this method elsewhere must not lower that region itself, and only one node may point at a given region. Content lowered twice is shown twice and reported twice.
§Errors
Returns LowerError::RegionNotFound when no tree of at’s
identifier is reachable, when that tree holds no node at at, or when
the node has no such region; nothing is staged then. Returns any
LowerError shape returns.
Sourcefn require(&mut self, id: &Name)
fn require(&mut self, id: &Name)
Records the requirement id for the tree being lowered: the
identifier of a definition that its output needs, such as a style or
script definition on HTML, or a package request or a definition piece
on LaTeX (StyleDef). The backend
that writes the output resolves the requirements recorded for each
lowered tree.
Use this for a requirement that comes from this particular node. A
requirement the rule has whenever it is selected belongs in
LoweringDecl::selection_requires instead.
Sourcefn diagnose(&mut self, diagnostic: FlmDiagnostic)
fn diagnose(&mut self, diagnostic: FlmDiagnostic)
Records a diagnostic about the node being lowered.
A rule reports at the node it is lowering, with that node’s span. The
methods error, warning and note, defined on dyn Lowerer, build
the diagnostic from a condition and a span.
Provided Methods§
Sourcefn pres_slots(
&mut self,
cx: &mut LowerCx<'_>,
element: &ElementName,
payload: Arc<dyn PresPayload>,
slots: Vec<(String, Vec<BuildId>)>,
) -> Result<BuildId, LowerError>
fn pres_slots( &mut self, cx: &mut LowerCx<'_>, element: &ElementName, payload: Arc<dyn PresPayload>, slots: Vec<(String, Vec<BuildId>)>, ) -> Result<BuildId, LowerError>
Builds a node of the presentation element element whose content is
several named content slots rather than one body, and returns its
position.
Some presentation elements hold their content in parts: a list node
has a label and an item content slot per list entry. slots gives
those parts in order, each as a name and the nodes in it; a slot named
body becomes the node’s body slot. Everything else works as in
Lowerer::pres, including the refusal of an undeclared element. Call
pres_slots_with, defined on dyn Lowerer, instead: it takes the
payload by value.
§Errors
Returns LowerError::UndeclaredProduces when the rule’s declaration
does not list element, and LowerError::Build when the node could
not be built. The default implementation of this method returns
LowerError::Other: an implementation of Lowerer that does not
override it cannot build a node with content slots.
Implementations§
Source§impl dyn Lowerer + '_
The typed methods a rule calls on its Lowerer.
impl dyn Lowerer + '_
The typed methods a rule calls on its Lowerer.
These methods need a type parameter, which an object-safe trait cannot
have, so they are defined on dyn Lowerer, the form a rule is handed,
rather than on the trait. Each one calls the corresponding type-erased
method and converts the value.
Sourcepub fn get<K: TableKey>(&self, key: K) -> Result<Option<&K::Table>, AccessError>
pub fn get<K: TableKey>(&self, key: K) -> Result<Option<&K::Table>, AccessError>
Reads a table the rule declared that it reads, as the table’s value type.
Ok(None) means that the run holds no value for the table, which can
only happen for a table declared as an optional read.
§Errors
Returns AccessError when the rule did not declare this read, or
declared it under another value type; see Lowerer::get_erased.
§Panics
Panics if the Lowerer implementation’s Lowerer::get_erased
returns a value of a type other than the one the key designates. The
implementation in this crate cannot: the registry compares the value
type of the table with the key’s before the read.
Sourcepub fn option<T: Options>(&self) -> Result<&T, OptionError>
pub fn option<T: Options>(&self) -> Result<&T, OptionError>
Reads the value of an option type the rule declared that it reads.
§Errors
Returns OptionError when the rule did not declare that option type;
see Lowerer::option_erased.
§Panics
Panics if the Lowerer implementation’s Lowerer::option_erased
returns a value that is not of type T. The implementation in this
crate cannot: an option value is stored under its own type.
Sourcepub fn option_named(&self, name: &str) -> Result<&Value, OptionError>
pub fn option_named(&self, name: &str) -> Result<&Value, OptionError>
Reads the value of the option type declared from data under the name
name, which the rule declared that it reads
(LoweringDecl::option_named): the data as written, or the empty map
when no value is set.
§Errors
Returns OptionError when the rule did not declare that read; see
Lowerer::option_erased.
§Panics
Panics if the Lowerer implementation’s Lowerer::option_erased
returns a value that is not a DataValue. The implementation in this
crate cannot: an option declared from data is stored as one.
Sourcepub fn pres_with<P: PresPayload>(
&mut self,
cx: &mut LowerCx<'_>,
element: &ElementName,
payload: P,
body: Vec<BuildId>,
) -> Result<BuildId, LowerError>
pub fn pres_with<P: PresPayload>( &mut self, cx: &mut LowerCx<'_>, element: &ElementName, payload: P, body: Vec<BuildId>, ) -> Result<BuildId, LowerError>
Builds a presentation node carrying payload, taken by value, and
returns its position.
The same as Lowerer::pres in every other respect.
§Errors
Exactly as Lowerer::pres:
LowerError::UndeclaredProduces when the rule’s declaration does not
list element, and LowerError::Build when the node could not be
built.
Sourcepub fn pres_slots_with<P: PresPayload>(
&mut self,
cx: &mut LowerCx<'_>,
element: &ElementName,
payload: P,
slots: Vec<(String, Vec<BuildId>)>,
) -> Result<BuildId, LowerError>
pub fn pres_slots_with<P: PresPayload>( &mut self, cx: &mut LowerCx<'_>, element: &ElementName, payload: P, slots: Vec<(String, Vec<BuildId>)>, ) -> Result<BuildId, LowerError>
Builds a presentation node with content slots, carrying payload taken
by value, and returns its position.
The same as Lowerer::pres_slots in every other respect.
§Errors
Exactly as Lowerer::pres_slots:
LowerError::UndeclaredProduces when the rule’s declaration does not
list element, LowerError::Build when the node could not be built,
and LowerError::Other from an implementation that does not override
Lowerer::pres_slots.
Sourcepub fn error(&mut self, condition: impl DiagnosticInfo, span: FlmSpan)
pub fn error(&mut self, condition: impl DiagnosticInfo, span: FlmSpan)
Records a diagnostic of Error severity reporting condition at span.
An Error says that the document is wrong in a way that has no sensible
rendering. See Lowerer::diagnose.
Sourcepub fn warning(&mut self, condition: impl DiagnosticInfo, span: FlmSpan)
pub fn warning(&mut self, condition: impl DiagnosticInfo, span: FlmSpan)
Records a diagnostic of Warning severity reporting condition at
span.
A Warning says that something is probably wrong, and the rule still
rendered it. See Lowerer::diagnose.
Sourcepub fn note(&mut self, condition: impl DiagnosticInfo, span: FlmSpan)
pub fn note(&mut self, condition: impl DiagnosticInfo, span: FlmSpan)
Records a diagnostic of Note severity reporting condition at span.
A Note gives information and reports no problem. See
Lowerer::diagnose.
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".