flm_core/stdlib/backends/lowering/feature.rs
1//! The `lowering` feature: the registrations every backend relies on.
2//!
3//! The `lowering` feature ([`LoweringFeature`], an [`FlmFeature`]) registers
4//! what lowering needs on any backend:
5//!
6//! - the presentation vocabulary ([`super::super::pres`]), through
7//! [`pres::register()`];
8//! - the default lowering rules of the structural elements ([`super::rules`]),
9//! among them the rule of the field element
10//! ([`FIELD`](crate::lang::structural::FIELD)), which every field carries (a
11//! macro that a construct absorbs as data, such as a `\label` written after
12//! a heading; see [fields: data a construct
13//! reads](crate::guide::concepts_overview#fields-data-a-construct-reads));
14//! - the two native-set implications the lowering driver relies on
15//! ([`native_implications`]);
16//! - the service slot [`NAMER_SLOT`], through which the embedding program
17//! gives the path of each artifact of an output, and the service slot
18//! [`REGIONS_SLOT`], through which each backend states which of its outputs
19//! are made of output regions.
20//!
21//! The rules are registered here so that they exist in every registry that
22//! renders anything: the language layer creates a field node wherever a
23//! declaration carries a field, and a construct that fails to read its field
24//! must be reported regardless of which features are registered. A backend's own feature
25//! ([`TextBackend`](crate::stdlib::backends::text::TextBackend), for
26//! instance) is registered beside this one and adds the backend, its stages
27//! and its output. Registering this feature twice into one registry is a
28//! registry build error, so a package lists it once.
29
30use std::sync::Arc;
31
32use crate::core::artifact::ArtifactNamer;
33use crate::core::region::RegionsOutput;
34use crate::core::registry::{
35 BuildError, FeatureBuilder, FeatureInfo, FlmFeature, NativeImplication,
36};
37use crate::core::services::Arity;
38use crate::stdlib::backends::pres::{self, Decoration};
39
40use super::rules::default_rules;
41
42/// The name under which this feature is registered, `lowering`; also the
43/// middle segment of its conditions' identifiers, `flm.lowering.*`
44/// ([`super::error`]).
45pub const FEATURE_NAME: &str = "lowering";
46
47/// The service slot `flm.artifacts.namer`
48/// ([`service_slot`](crate::core::registry::FeatureBuilder::service_slot)),
49/// holding at most one [`ArtifactNamer`]: the embedding program's naming
50/// scheme, which gives the path of each artifact of an output, relative to
51/// the output location.
52///
53/// A service slot is the declaration, by the feature that owns an interaction,
54/// of a dotted name, the type of the values the slot takes and its arity; any
55/// feature, and the embedding program through the registry builder, may
56/// contribute a value to it (see [`crate::core::services`]). The library reads and writes no files and
57/// chooses no file path itself. A host that writes files contributes its
58/// naming scheme here, so that a stage of any feature can ask for an
59/// artifact's path before the artifact is made, for example to write a link
60/// from a page to a file placed beside it. When the host contributes nothing,
61/// the slot is empty and a stage then writes no file path. The slot is
62/// declared by this feature, beside the vocabulary every backend shares,
63/// because the artifacts of every backend get their paths through it.
64pub const NAMER_SLOT: &str = "flm.artifacts.namer";
65
66/// The service slot `flm.output.regions`, holding one [`RegionsOutput`] per
67/// output whose entry artifact is made of output regions
68/// ([`Regions`](crate::core::region::Regions)), in contribution order.
69///
70/// An output region is one named part of an output's entry artifact (the
71/// document's content, each piece placed beside it, and on HTML the
72/// stylesheets, the scripts, the title and the language). A stage that
73/// assembles the regions in a layout other than the backend's final stage
74/// (see [output regions and the final
75/// stage](crate::guide::concepts_overview#output-regions-and-the-final-stage))
76/// reads this slot to learn which outputs it can assemble. Each backend that
77/// writes regions contributes its own value; the slot is declared by this
78/// feature, beside the vocabulary every backend shares, since any backend's
79/// output may be made of regions.
80pub const REGIONS_SLOT: &str = "flm.output.regions";
81
82/// The `lowering` feature: it registers the presentation vocabulary
83/// ([`pres::register()`]), the default lowering rules of the structural
84/// elements, the field element's among them ([`default_rules`]), the
85/// native-set implications the driver relies on ([`native_implications`]),
86/// and the service slots [`NAMER_SLOT`] and [`REGIONS_SLOT`] (see the module
87/// documentation).
88///
89/// Every registry that renders anything needs this feature. Use
90/// [`feature()`] to obtain it as a value a package entry can list.
91#[derive(Debug, Default)]
92pub struct LoweringFeature;
93
94impl FlmFeature for LoweringFeature {
95 fn info(&self) -> FeatureInfo {
96 FeatureInfo::new(FEATURE_NAME, "Lowering")
97 .docs(
98 "the presentation vocabulary and the structural elements' default lowering rules",
99 )
100 }
101
102 fn register(&self, b: &mut FeatureBuilder<'_>) -> Result<(), BuildError> {
103 pres::register(b)?;
104 b.service_slot::<dyn ArtifactNamer>(
105 NAMER_SLOT,
106 Arity::Single,
107 "the host's naming scheme: the path of each artifact of an output, relative to the \
108 output location",
109 )?;
110 b.service_slot::<RegionsOutput>(
111 REGIONS_SLOT,
112 Arity::OrderedMany,
113 "the outputs whose entry artifact is made of output regions, each with the table \
114 that holds its regions and the tables of the files written beside its entry \
115 artifact",
116 )?;
117 for rule in default_rules() {
118 b.lowering_rule(rule)?;
119 }
120 for implication in native_implications() {
121 b.native_implication(implication)?;
122 }
123 Ok(())
124 }
125}
126
127/// The two [native-set implications](NativeImplication) the lowering driver
128/// relies on, which this feature declares so that the registry build checks
129/// every backend against them.
130///
131/// A backend's native set is the list of elements its emit stage writes
132/// directly ([`BackendDef::native`](crate::core::registry::BackendDef::native)).
133/// The driver ([`super::driver`]) stages two presentation elements on its
134/// own, with no lowering rule declaring that it produces them, so plan
135/// construction cannot see that a backend needs them:
136///
137/// 1. A backend whose native set holds any element carrying the
138/// [`Decoration`] aspect must hold `flm.pres.deco-mark` too: the driver
139/// moves such a decoration out of a content slot into an attached slot and
140/// leaves that mark at its former position.
141/// 2. Every backend must hold `flm.pres.unsupported`: the driver stages it
142/// for a node of an accepted unrenderable element, for a node whose rule
143/// failed, and for a node no rule lowers.
144///
145/// A backend missing one fails the registry build with
146/// [`BuildError::NativeSetIncomplete`] instead of failing at lowering.
147///
148/// # Examples
149///
150/// ```
151/// use flm_core::core::registry::NativeImplicationKind;
152/// use flm_core::stdlib::backends::{lowering, pres};
153///
154/// let implications = lowering::native_implications();
155/// assert_eq!(implications[0].must_claim, pres::DECO_MARK);
156/// assert_eq!(implications[1].must_claim, pres::UNSUPPORTED);
157/// assert_eq!(implications[1].kind, NativeImplicationKind::Always);
158/// ```
159pub fn native_implications() -> Vec<NativeImplication> {
160 vec![
161 NativeImplication::aspect::<dyn Decoration>(pres::DECO_MARK).docs(
162 "a backend that writes a decoration element natively must write natively the mark \
163 the lowering driver leaves where it moved a decoration into an attached slot",
164 ),
165 NativeImplication::always(pres::UNSUPPORTED).docs(
166 "every backend must write natively the stand-in the lowering driver stages for a \
167 node it cannot lower: a node of an accepted unrenderable element, a node whose \
168 rule failed, or a node no rule lowers",
169 ),
170 ]
171}
172
173/// The `lowering` feature as a shared value, ready to be listed in a package
174/// entry.
175///
176/// # Examples
177///
178/// ```
179/// use flm_core::stdlib::backends::lowering;
180///
181/// assert_eq!(lowering::feature().info().name.as_str(), lowering::FEATURE_NAME);
182/// ```
183pub fn feature() -> Arc<dyn FlmFeature> {
184 Arc::new(LoweringFeature)
185}