flm_core/math_passthrough/check.rs
1//! The check stage of the [`math-passthrough`](super) feature,
2//! `flm.math-passthrough.check`: the provider of the capability
3//! [`flm.math.content`](crate::stdlib::features::math::CONTENT), which the
4//! feature adds to the collection [`flm.validated`](crate::stdlib::VALIDATED),
5//! so that every workflow that validates a document runs this check.
6//!
7//! Passthrough mathematics reads nothing inside a formula, so the check
8//! accepts every formula as written. It asks one thing of the display-math
9//! environments: every equation line holds mathematics. A line made only of
10//! whitespace, comments, `\label`, `\tag` and `\nonumber` (an empty body, a
11//! `\\` at the start of a body, two `\\` in a row, a `\label` after the last
12//! `\\`) is reported as [`EmptyLine`] (`flm.math-passthrough.empty-line`), an
13//! Error. The line is written as written: the rules write every line the
14//! `math` feature reads ([`equation_lines`](crate::stdlib::features::math::lines::equation_lines)),
15//! an empty one included.
16
17use techy::error::{Diagnostic, DiagnosticInfo};
18
19use crate::core::data::store::StageOut;
20use crate::core::plan::PlanCx;
21use crate::core::run::StageCx;
22use crate::core::stage::{Effect, Needs, Scope, Stage, StageDecl};
23use crate::model::element::{element_of, element_value};
24use crate::stdlib::features::math::{CONTENT, ENVIRONMENT, MathEnvironment, lines};
25
26/// The name of the check stage, `flm.math-passthrough.check`: it provides
27/// [`flm.math.content`](CONTENT), a member of the collection
28/// [`flm.validated`](crate::stdlib::VALIDATED) that the feature adds, accepts
29/// every formula, and reports every equation line that holds no mathematics
30/// ([`EmptyLine`]). See the module documentation.
31pub const CHECK: &str = "flm.math-passthrough.check";
32
33/// Condition `flm.math-passthrough.empty-line` (an Error): an equation line of
34/// a display-math environment holds no mathematics. Whitespace, comments,
35/// `\label`, `\tag` and `\nonumber` are not mathematics, so a line made only
36/// of them is empty, and so is an entirely empty body.
37///
38/// The check stage ([`CHECK`]) reports it at the commands the line holds, or
39/// at the `\\` that ends the line when it holds none, or at the body when the
40/// body is entirely empty. The recovery is to write the line as written.
41#[derive(Debug, Clone, PartialEq, Eq, DiagnosticInfo)]
42#[diagnostic(
43 id = "flm.math-passthrough.empty-line",
44 message = "line {line} (counting from 0) of the `{environment}` environment must hold \
45 mathematics; a `\\label`, `\\tag` or `\\nonumber` alone is not mathematics"
46)]
47pub struct EmptyLine {
48 /// The environment's name as written (`align`).
49 pub environment: String,
50 /// The index of the empty line among the environment's lines, counting from
51 /// zero: its position in the list
52 /// [`equation_lines`](crate::stdlib::features::math::lines::equation_lines)
53 /// returns.
54 pub line: usize,
55}
56
57/// The check stage [`CHECK`], the provider of [`CONTENT`]. See the module
58/// documentation.
59#[derive(Debug, Default, Clone, Copy)]
60pub struct PassthroughCheck;
61
62impl Stage for PassthroughCheck {
63 fn decl(&self) -> StageDecl {
64 StageDecl::new(CHECK, Effect::Check, Scope::PerTree).provides(CONTENT).docs(
65 "The check of passthrough mathematics: it accepts every formula as written, \
66 reports every equation line that holds no mathematics as \
67 `flm.math-passthrough.empty-line`, and provides `flm.math.content`.",
68 )
69 }
70
71 fn needs(&self, _cx: &PlanCx<'_>) -> Needs {
72 Needs::new()
73 }
74
75 fn run(&self, cx: &StageCx<'_>, out: &mut StageOut<'_>) {
76 // A tree whose rewrite failed is absent; its one diagnostic stands.
77 let Some(tree) = cx.my_tree() else { return };
78 for node in tree.root().descendants() {
79 if element_of(&node).name != ENVIRONMENT {
80 continue;
81 }
82 let Some(value) = element_value::<MathEnvironment>(&node) else { continue };
83 for (line, span) in lines::empty_lines(&node) {
84 let condition = EmptyLine { environment: value.name.to_string(), line };
85 out.diags.push(Diagnostic::error(condition, span));
86 }
87 }
88 }
89}
90
91#[cfg(test)]
92mod tests {
93 use super::*;
94
95 #[test]
96 fn the_stage_is_a_per_tree_check_providing_content() {
97 let decl = PassthroughCheck.decl();
98 assert_eq!(decl.name, CHECK);
99 assert_eq!(decl.effect, Effect::Check);
100 assert_eq!(decl.scope, Scope::PerTree);
101 assert_eq!(decl.provides, [crate::model::names::CapName::from(CONTENT)]);
102 assert!(decl.writes.is_empty());
103 }
104}