flm_core/stdlib/features/defterm/diagnostics.rs
1//! This module defines the diagnostic conditions that the [`defterm`](super)
2//! feature reports (see
3//! [diagnostics](crate::guide::concepts_overview#diagnostics)). Both
4//! conditions are errors.
5//!
6//! The lowering rules of a term definition (the `defterm` environment) and of
7//! a term link (the `\term` command) report [`EmptyTerm`] when the term of the
8//! environment or of the command is empty.
9//!
10//! A term pointer is a synthesized node in the lead-in of a term definition.
11//! The lowering rule of a term pointer reports [`PointerRefused`] when the
12//! term pointer does not refer to the term of a term definition. A document
13//! cannot cause this error.
14//!
15//! Both conditions are reported only when a document is lowered. The `check`
16//! workflow ([`CHECK_WORKFLOW`](crate::stdlib::CHECK_WORKFLOW)) lowers
17//! nothing, and therefore does not report them.
18//!
19//! A term link is a reference, and a term definition is a reference point.
20//! The [references](crate::stdlib::features::refs) feature therefore reports
21//! their other problems. For instance, a term link that matches no term
22//! definition is reported as `flm.refs.unresolved`, and two term definitions
23//! of the same term are reported as `flm.refs.duplicate-label`.
24
25use techy::error::DiagnosticInfo;
26
27/// The error that is reported when the term of a `defterm` environment or of
28/// a `\term` command is empty (`flm.defterm.empty-term`).
29///
30/// The term of a `defterm` environment is its argument, as in
31/// `\begin{defterm}{Markov chain}`. The term of a `\term` command is its
32/// optional argument, as in `\term[Markov chain]{such a process}`, or its
33/// mandatory argument when the optional argument is absent or empty, as in
34/// `\term{Markov chain}`. A term counts as empty when it contains nothing but
35/// whitespace and comments. The space commands `~` and `\ ` count as content.
36///
37/// Reported on every backend by the lowering rule of the environment or of
38/// the command, when the rule is invoked. A `defterm` environment with an
39/// empty term is otherwise written as usual. A `\term` command with an empty
40/// term expands to the text of its mandatory argument, marked as an
41/// unresolved reference. On the `latex-recomposed` backend, the `\term`
42/// command expands to `\flmTerm` with an empty label.
43#[derive(Debug, Clone, PartialEq, Eq, DiagnosticInfo)]
44#[diagnostic(id = "flm.defterm.empty-term", message = "Missing term in ‘{what}’")]
45pub struct EmptyTerm {
46 /// The name of the environment or of the command whose term is empty:
47 /// `defterm` or `\term`.
48 pub what: String,
49}
50
51/// Error representing a term pointer ([`TERM_POINTER`](super::TERM_POINTER))
52/// that does not refer to the term of a term definition (id
53/// `flm.defterm.pointer-refused`).
54///
55/// A term definition opens with a *lead-in*, such as `Markov chain: `. The
56/// lead-in is written from the lead-in template of the feature
57/// ([`DeftermConfig`](super::DeftermConfig)), in which `#{term}` stands for
58/// the term being defined. At each term definition, `#{term}` is replaced by
59/// a term pointer: a synthesized node that refers to the term argument of the
60/// `defterm` environment. The lowering rule of the term pointer lowers the
61/// term argument at the place of the pointer. A term pointer must refer to
62/// the term argument of a `defterm` environment. This requirement ensures
63/// that no content is written twice: any other content is already written
64/// where its node is placed.
65///
66/// A document cannot cause this error: it is caused by the faulty code that
67/// created the term pointer. Reported by the lowering rule of the term pointer
68/// ([`PointerRule`](super::PointerRule)) when it is invoked. The term pointer
69/// expands to nothing. The rest of the lead-in and the term definition are
70/// written as usual.
71#[derive(Debug, Clone, PartialEq, Eq, DiagnosticInfo)]
72#[diagnostic(
73 id = "flm.defterm.pointer-refused",
74 message = "Invalid term pointer to {region} of a node of ‘{element}’: the \
75 pointer must refer to the term argument of a ‘defterm’ environment"
76)]
77pub struct PointerRefused {
78 /// The name of the element of the node that the term pointer refers to.
79 pub element: String,
80 /// The node region that the term pointer refers to: an argument or a
81 /// content slot of the node
82 /// ([`NodeRegion`](crate::core::lowering::NodeRegion)), as text, e.g.
83 /// `argument 0`.
84 pub region: String,
85}