flm_core/stdlib/backends/lowering/error.rs
1//! This module defines the diagnostic conditions that are reported while a
2//! document is lowered for a backend (see [backends, lowering, and
3//! outputs](crate::guide::concepts_overview#backends-lowering-and-outputs)).
4//! The identifiers of most of these conditions begin with `flm.lowering.`.
5//! Two conditions have other identifiers: [`ContentNotCharacters`]
6//! (`flm.verbatim.content-not-characters`) and [`MathUnprocessed`]
7//! (`flm.math.unprocessed`).
8//!
9//! Each of these conditions is reported as an Error diagnostic, and the
10//! lowering then continues with the rest of the document. The lowering driver
11//! ([`driver`](mod@super::driver)) handles a problem in one of two ways:
12//!
13//! - **At one node.** The node is replaced by a `flm.pres.unsupported` node
14//! without children, the error is reported at the node, and the rest of
15//! the tree is lowered as usual. [`UnsupportedElement`], [`NoRule`],
16//! [`RuleFailed`] and [`RegionNotFound`] are handled this way.
17//! - **For the whole tree.** The lowering of the tree stops, and the error is
18//! reported at the root of the tree. The tree has no entry in the table of
19//! lowered trees of the backend ([`LoweredTrees`](super::LoweredTrees)), so
20//! the backend writes no output for this tree. [`RoundsExceeded`],
21//! [`NestingTooDeep`] and [`LoweringFailed`] are handled this way.
22//!
23//! The other three conditions, [`FieldNotRead`], [`ContentNotCharacters`] and
24//! [`MathUnprocessed`], are reported by lowering rules. The documentation of
25//! each of them states what is written in place of the content.
26
27use techy::error::DiagnosticInfo;
28
29/// The error that is reported when a document contains an element that the
30/// backend cannot render (`flm.lowering.unsupported-element`).
31///
32/// A backend cannot render an element when it neither writes the element
33/// natively nor has a lowering rule for it. If a document can contain such
34/// an element, building the plan for the backend fails
35/// ([`PlanError::Incomplete`](crate::core::plan::PlanError::Incomplete)),
36/// unless the element is listed for this backend in
37/// [`PlanRequest::unsupported`](crate::core::plan::PlanRequest#structfield.unsupported).
38/// In that case the plan builds, and this error is reported for each
39/// occurrence of the element in the document.
40///
41/// Reported by the lowering driver at each node of the element. The node is
42/// replaced by a `flm.pres.unsupported` node without children, so its
43/// content does not appear in the output.
44///
45/// An element that the backend cannot render and that is not listed in
46/// `PlanRequest::unsupported` is reported as [`NoRule`].
47#[derive(Debug, Clone, PartialEq, Eq, DiagnosticInfo)]
48#[diagnostic(
49 id = "flm.lowering.unsupported-element",
50 message = "Unsupported element ‘{element}’ for backend ‘{backend}’"
51)]
52pub struct UnsupportedElement {
53 /// The name of the backend.
54 pub backend: String,
55 /// The name of the element that could not be rendered.
56 pub element: String,
57}
58
59/// The error that is reported when a lowering rule fails at a node
60/// (`flm.lowering.rule-failed`).
61///
62/// A lowering rule fails when it returns a
63/// [`LowerError`](crate::core::lowering::LowerError). This error is usually
64/// caused by a fault of the rule. For instance, the rule reads a table or an
65/// option that it did not declare, stages a presentation element that is not
66/// listed in its `produces` declaration, or finds a node structure that it
67/// does not expect. A rule also fails when a [synthesized
68/// value](crate::guide::concepts_overview#synthesized-values) that it splices
69/// ([`Lowerer::splice`](crate::core::lowering::Lowerer::splice)) cannot be
70/// brought up to date.
71///
72/// Reported by the lowering driver at the node. The node is replaced by a
73/// `flm.pres.unsupported` node without children.
74///
75/// Some kinds of `LowerError` are handled differently:
76///
77/// - [`LowerError::Refused`](crate::core::lowering::LowerError::Refused): the
78/// diagnostic that the error contains is reported in place of this error,
79/// and the node is replaced in the same way. A rule returns this error to
80/// report a problem of the document in terms that the author can act on,
81/// as the user-definitions feature does with
82/// [`KeptTemplateDeclaresPoint`](crate::stdlib::features::userdefs::kept::KeptTemplateDeclaresPoint).
83/// - [`LowerError::RegionNotFound`](crate::core::lowering::LowerError::RegionNotFound)
84/// is reported as [`RegionNotFound`].
85/// - [`LowerError::Build`](crate::core::lowering::LowerError::Build) stops the
86/// lowering of the whole tree, and is reported as [`LoweringFailed`].
87#[derive(Debug, Clone, PartialEq, Eq, DiagnosticInfo)]
88#[diagnostic(
89 id = "flm.lowering.rule-failed",
90 message = "Lowering rule ‘{rule}’ in backend ‘{backend}’ failed for ‘{element}’: {detail}"
91)]
92pub struct RuleFailed {
93 /// The name of the backend.
94 pub backend: String,
95 /// The name of the lowering rule.
96 pub rule: String,
97 /// The name of the element of the node.
98 pub element: String,
99 /// The message of the [`LowerError`](crate::core::lowering::LowerError)
100 /// that the rule returned.
101 pub detail: String,
102}
103
104/// The error that is reported when a lowering rule calls
105/// [`Lowerer::lower_region`](crate::core::lowering::Lowerer::lower_region)
106/// for a node region that does not exist (id
107/// `flm.lowering.region-not-found`).
108///
109/// A node region is an argument or a content slot of a node
110/// ([`NodeRegion`](crate::core::lowering::NodeRegion)). A rule calls
111/// `lower_region` to lower a node region of another node at the position of
112/// its own node, for instance the content that a
113/// [`ContentPointer`](super::ContentPointer) refers to. The node region does
114/// not exist when no document tree and no synthesized value that is being
115/// lowered has the given tree identifier, when that tree has no node at the
116/// given address, or when the node has no such argument or slot.
117///
118/// A document cannot cause this error: it is caused by the stage or the rule
119/// that created the reference to the node region, for instance a stage that
120/// built a synthesized value containing a content pointer.
121///
122/// Reported by the lowering driver at the node whose rule called
123/// `lower_region`. The node is replaced by a `flm.pres.unsupported` node
124/// without children. The function
125/// [`lower_content_pointer`](super::lower_content_pointer) handles this error
126/// differently: it reports the error at the content pointer node and returns
127/// an empty list of staged nodes, so that the content pointer node is not
128/// replaced.
129#[derive(Debug, Clone, PartialEq, Eq, DiagnosticInfo)]
130#[diagnostic(
131 id = "flm.lowering.region-not-found",
132 message = "Invalid content region reference in lowering rule ‘{rule}’ of backend ‘{backend}’ while lowering ‘{element}’: \
133 {detail}"
134)]
135pub struct RegionNotFound {
136 /// The name of the backend.
137 pub backend: String,
138 /// The name of the lowering rule that called `lower_region`.
139 pub rule: String,
140 /// The name of the element of the node whose rule called `lower_region`.
141 pub element: String,
142 /// A text that states which part of the node region is missing: the
143 /// tree, the node, or the argument or slot.
144 pub detail: String,
145}
146
147/// The error that is reported when the lowering reaches a node of an element
148/// that the backend cannot render and that is not listed as unrenderable in
149/// the plan request (id `flm.lowering.no-rule`).
150///
151/// The backend does not write the element natively, and no lowering rule is
152/// selected for the element. Building the plan checks that the backend can
153/// render every element that can occur in a document
154/// ([`FlmRegistry::occurrable_elements`](crate::core::registry::FlmRegistry::occurrable_elements)).
155/// This error can therefore occur only for an element that this check does
156/// not cover, for instance a template placeholder that remains in the tree
157/// after the expansion of user definitions.
158///
159/// A document cannot cause this error: it is caused by a fault of the
160/// program, in a feature, a rewrite or an extension. This is the difference
161/// from [`UnsupportedElement`], which is reported for an element that the
162/// plan request lists as unrenderable, and whose message is addressed to the
163/// author of the document.
164///
165/// Reported by the lowering driver at the node. The node is replaced by a
166/// `flm.pres.unsupported` node without children.
167#[derive(Debug, Clone, PartialEq, Eq, DiagnosticInfo)]
168#[diagnostic(
169 id = "flm.lowering.no-rule",
170 message = "Missing lowering rule for element ‘{element}’ in backend ‘{backend}’"
171)]
172pub struct NoRule {
173 /// The name of the backend.
174 pub backend: String,
175 /// The name of the element.
176 pub element: String,
177}
178
179/// The error that is reported when the lowering of a tree reaches the maximum
180/// number of rounds and the tree still contains elements that the backend
181/// does not write natively (id `flm.lowering.rounds-exceeded`).
182///
183/// The lowering rewrites a tree in rounds. Each round applies the selected
184/// lowering rules to the nodes of the elements that the backend does not
185/// write natively, and the next round processes the resulting tree. The
186/// maximum number of rounds is computed from the `produces` declarations of
187/// the selected rules ([`rank`](super::rank)).
188///
189/// This error indicates a fault of a lowering rule. For instance, a rule that
190/// returns [`Lowering::Default`](crate::core::lowering::Lowering::Default) for
191/// an element that the backend does not write natively leaves the element in
192/// the tree for the next round.
193///
194/// Reported by the lowering driver at the root of the tree. The tree has no
195/// lowered form.
196#[derive(Debug, Clone, PartialEq, Eq, DiagnosticInfo)]
197#[diagnostic(
198 id = "flm.lowering.rounds-exceeded",
199 message = "Too many lowering rounds ({rank}) while running backend ‘{backend}’; elements that need further rounds: {remaining}"
200)]
201pub struct RoundsExceeded {
202 /// The name of the backend.
203 pub backend: String,
204 /// The maximum number of rounds ([`rank`](super::rank)).
205 pub rank: u32,
206 /// The names of the elements that remain in the tree after the last
207 /// round and that the backend does not write natively, sorted by name
208 /// and separated by commas.
209 pub remaining: String,
210}
211
212/// The error that is reported when the lowering of a tree fails as a whole
213/// (`flm.lowering.failed`).
214///
215/// This happens when an error that cannot be handled at one node occurs
216/// during the lowering, such as
217/// [`LowerError::Build`](crate::core::lowering::LowerError::Build) for a node
218/// that cannot be built. It also happens when the lowered tree cannot be
219/// stored in the table of lowered trees of the backend
220/// ([`LoweredTrees`](super::LoweredTrees)).
221///
222/// Reported at the root of the tree, by the lowering driver or by the
223/// lowering stage ([`LoweringStage`](super::LoweringStage)). The tree has no
224/// lowered form.
225#[derive(Debug, Clone, PartialEq, Eq, DiagnosticInfo)]
226#[diagnostic(
227 id = "flm.lowering.failed",
228 message = "Lowering failed in backend ‘{backend}’: {detail}"
229)]
230pub struct LoweringFailed {
231 /// The name of the backend.
232 pub backend: String,
233 /// The message of the error that stopped the lowering of the tree.
234 pub detail: String,
235}
236
237/// The error that is reported when a tree is nested too deeply to be lowered
238/// (`flm.lowering.nesting-too-deep`).
239///
240/// The lowering walks the tree recursively. The recursion guard limits the
241/// depth of every recursive walk of a tree in the library (see [the recursion
242/// guard and worker
243/// threads](crate::guide::concepts_overview#the-recursion-guard-and-worker-threads)).
244/// This error is reported when the guard does not allow the lowering to
245/// descend one more level. The other walks report conditions of their own:
246/// parsing and the rewrites report `flm.blocks.nesting-too-deep`, and
247/// bringing a synthesized value up to date reports
248/// `flm.synth.nesting-too-deep`.
249///
250/// Reported by the lowering driver at the root of the tree. The tree has no
251/// lowered form.
252#[derive(Debug, Clone, PartialEq, Eq, DiagnosticInfo)]
253#[diagnostic(
254 id = "flm.lowering.nesting-too-deep",
255 message = "Content too deeply nested with backend ‘{backend}’: {detail}"
256)]
257pub struct NestingTooDeep {
258 /// The name of the backend.
259 pub backend: String,
260 /// A text that states which limit of the recursion guard was reached.
261 pub detail: String,
262}
263
264/// The error that is reported when a field reaches the lowering
265/// (`flm.lowering.field-not-read`).
266///
267/// A field is a macro that a construct absorbs as data, not as content, such
268/// as a `\label` written after a heading or a `\tag` on an equation line (see
269/// [fields: data a construct
270/// reads](crate::guide::concepts_overview#fields-data-a-construct-reads)).
271/// The fields of a construct are read by the feature that defines the
272/// construct, and the lowering rule of the construct must not lower them. A
273/// field that reaches the lowering has therefore not been read.
274///
275/// A document cannot cause this error: it is caused by the lowering rule of
276/// the construct that absorbed the field.
277///
278/// Reported by [`FieldRule`](super::FieldRule), the lowering rule of fields
279/// on every backend, at the field. The field expands to nothing.
280#[derive(Debug, Clone, PartialEq, Eq, DiagnosticInfo)]
281#[diagnostic(
282 id = "flm.lowering.field-not-read",
283 message = "Unread field ‘\\{field}’ left in the content (backend ‘{backend}’)"
284)]
285pub struct FieldNotRead {
286 /// The name of the backend.
287 pub backend: String,
288 /// The field's name as written, without the escape character.
289 pub field: String,
290}
291
292/// The error that is reported when the content of a verbatim region is not
293/// plain characters (`flm.verbatim.content-not-characters`).
294///
295/// A verbatim region is text in which no character has a markup meaning, such
296/// as the argument of `\verbcode` (see [verbatim
297/// regions](crate::guide::concepts_overview#verbatim-regions)). Its content
298/// is plain characters when it consists only of text, comments and brace
299/// groups whose content is plain characters in turn. A brace group
300/// contributes its content without the braces.
301///
302/// The text that an author writes in a verbatim region is always plain
303/// characters. Other content gets there through a user definition whose
304/// template places one of its arguments in a verbatim region. A call of
305/// such a definition must pass plain characters in this argument. For
306/// instance, if the template of `\code` is `\verbcode{#1}`, the call can be
307/// `\code{x = y + 1}`.
308///
309/// Reported by [`read_verbatim_content`](super::read_verbatim_content), which
310/// every lowering rule that stages verbatim content calls, at the command or
311/// environment that contains the verbatim region. The verbatim region then
312/// contains the source text of its whole content, markup included, as
313/// characters. The same happens on every backend.
314#[derive(Debug, Clone, PartialEq, Eq, DiagnosticInfo)]
315#[diagnostic(
316 id = "flm.verbatim.content-not-characters",
317 message = "Invalid content in {name}: verbatim content must consist of plain characters"
318)]
319pub struct ContentNotCharacters {
320 /// The name of the command or environment that contains the verbatim
321 /// region, as written in the document: `\verbcode` for a command,
322 /// `{verbatimcode}` for an environment. When no command or environment
323 /// contains the region, this field contains the text `a verbatim group`.
324 pub name: String,
325}
326
327/// The error that is reported when a math group or a display-math
328/// environment is lowered on a backend for which no feature processes
329/// mathematics (id `flm.math.unprocessed`).
330///
331/// A math group is mathematics written as `\(…\)`, `$…$` or `\[…\]`. Math
332/// groups and display-math environments are lowered by the rules of a
333/// feature that processes mathematics, for example the feature of the
334/// package [`math-passthrough`](crate::math_passthrough). When no such rule
335/// is registered for the backend, the mathematics is lowered by a rule of
336/// last resort, which reports this error. A rule of last resort is selected
337/// only when it is the only rule for the element on the backend. The rule of
338/// last resort is [`MathGroupRule`](super::MathGroupRule) for a math group,
339/// and
340/// [`UnprocessedEnvironmentRule`](crate::stdlib::features::math::unprocessed::UnprocessedEnvironmentRule)
341/// of the `math` feature for a display-math environment.
342///
343/// Reported at each math group or display-math environment that a rule of
344/// last resort lowers. The content of the mathematics is lowered as ordinary
345/// content, inside a `flm.pres.span` node of role `flm.math` and kind
346/// `inline` or `display`.
347#[derive(Debug, Clone, PartialEq, Eq, DiagnosticInfo)]
348#[diagnostic(
349 id = "flm.math.unprocessed",
350 message = "Mathematics processing is not configured for this document"
351)]
352pub struct MathUnprocessed;