flm_core/stdlib/features/cells/diagnostics.rs
1//! This module defines the diagnostic conditions of the [`cells`](super)
2//! feature, which provides tables of cells. Their identifiers have the form
3//! `flm.cells.<condition>`. Every condition is an error.
4//!
5//! The lowering rule of the `cells` environment reads the table with
6//! [`read_grid`](super::read_grid), which finds the mistakes in the table.
7//! Both the general rule [`CellsRule`](super::lower::CellsRule) and the rule
8//! [`CellsLatexRule`](super::latex::CellsLatexRule) of the `latex-recomposed`
9//! backend report these mistakes each time they lower the table, so a
10//! document gets the same reports on every backend. Each mistake causes only
11//! a part of the table to be ignored or left out, and the rest of the table
12//! is written as usual. The lowering rule [`OutsideRule`](super::lower::OutsideRule)
13//! reports [`OutsideTable`] for a command of a table of cells that is written
14//! outside of its place.
15//!
16//! These conditions are reported only when a table is lowered. The `check`
17//! workflow ([`CHECK_WORKFLOW`](crate::stdlib::CHECK_WORKFLOW)) lowers no
18//! table, and therefore does not report them.
19//!
20//! When a part of the document is left out of the output, the lowering rule
21//! places a *dropped mark* in the output: a `flm.pres.dropped` node
22//! ([`DroppedPayload`](crate::stdlib::backends::pres::DroppedPayload)),
23//! which shows the reader of the output that something was left out. The
24//! dropped marks for the mistakes in a table are placed immediately before
25//! the table.
26
27use techy::error::DiagnosticInfo;
28
29/// The error that is reported when the body of a `cells` environment contains
30/// content outside of a cell (`flm.cells.content-outside-cell`).
31///
32/// The body of a `cells` environment may contain only the commands `\cell`,
33/// `\celldata`, `\\`, `\rownames` and `\colnames`, together with whitespace,
34/// blank lines and comments. Text and other content belong in a cell: in the
35/// content of a `\cell`, as in `\cell{Some text}`, or in an entry of the data
36/// of a `\celldata`, as in `\celldata{Some text & More text}`.
37///
38/// Reported by the lowering rule of the `cells` environment, on every
39/// backend, at the content. The content is left out of the output, and a
40/// dropped mark (`flm.pres.dropped`) is placed immediately before the table.
41#[derive(Debug, Clone, PartialEq, Eq, DiagnosticInfo)]
42#[diagnostic(
43 id = "flm.cells.content-outside-cell",
44 message = "A ‘cells’ environment may hold only ‘\\cell’, ‘\\celldata’, ‘\\\\’, ‘\\rownames’ and ‘\\colnames’"
45)]
46pub struct ContentOutsideCell;
47
48/// The error that is reported when the placement of a `\cell` or the mapping
49/// of a `\celldata` is not valid (`flm.cells.bad-placement`).
50///
51/// The placement of a `\cell` is its optional argument `[…]`. It has the form
52/// `[COL]` or `[ROW;COL]`. Each part is a number, a name declared with
53/// `\rownames` or `\colnames`, `.` or nothing for the current row or column,
54/// or `\merge{RANGE}` for a cell that spans several rows or columns, as in
55/// `\cell[2;3]{…}` or `\cell[1;\merge{3-4}]{…}`.
56///
57/// The mapping of a `\celldata` is its optional argument `[…]`. It has the
58/// form `[COLS]` or `[ROWS;COLS]`. Each part is a comma-separated list of
59/// numbers, names, ranges and `\merge{RANGE}`, as in
60/// `\celldata[3,2,1]{A & B & C}` or `\celldata[2-5]{A & B & C & D}`.
61///
62/// A range has the form `2-4`, or `2+3` for three rows or columns starting
63/// at 2. Inside `\merge`, a range can also be a contiguous union such as
64/// `1,2,3`.
65/// Rows and columns are numbered from 1, and a number may be at most
66/// [`MAX_CELLS_POSITIONS`](crate::stdlib::backends::pres::MAX_CELLS_POSITIONS).
67/// The [documentation of the `cells` feature](super#placements-mappings-and-ranges)
68/// describes placements, mappings and ranges in full.
69///
70/// Reported by the lowering rule of the `cells` environment, on every
71/// backend, at the `\cell` or the `\celldata`. The message states what is
72/// wrong. An invalid placement is ignored: the cell goes to the current row
73/// and column, or, for a `\cell` that is an entry of a `\celldata`, to the
74/// position of that entry. An invalid mapping is ignored as a whole, and the
75/// data of the `\celldata` is placed from the current row and column on.
76#[derive(Debug, Clone, PartialEq, Eq, DiagnosticInfo)]
77#[diagnostic(id = "flm.cells.bad-placement", message = "{detail}")]
78pub struct BadPlacement {
79 /// The message of the diagnostic. It states what is wrong with the
80 /// placement or the mapping.
81 pub detail: String,
82}
83
84/// The error that is reported when the style argument `<…>` of a `\cell` or
85/// a `\celldata` contains content other than text (`flm.cells.bad-styles`).
86///
87/// The style argument contains style tokens separated by whitespace. A style
88/// token is the name of a cell style, optionally followed by a value in
89/// braces, as in `\cell<H align{left}>{…}`. In a `\celldata`, commas separate
90/// the tokens of successive entries of a row, as in
91/// `\celldata<H l, H c>{One & Two}`. A token that is not the name of a cell
92/// style is reported as [`UnknownStyle`].
93///
94/// Reported by the lowering rule of the `cells` environment, on every
95/// backend, at the `\cell` or the `\celldata`. The whole style argument is
96/// ignored.
97#[derive(Debug, Clone, PartialEq, Eq, DiagnosticInfo)]
98#[diagnostic(
99 id = "flm.cells.bad-styles",
100 message = "The styles of a cell may hold only style names, each with an optional value in braces"
101)]
102pub struct BadStyles;
103
104/// The error that is reported when a style token is not the name of a cell
105/// style declared in the registry (`flm.cells.unknown-style`).
106///
107/// Each style token of a `\cell` or a `\celldata` refers to a cell style
108/// ([`CellStyle`](super::CellStyle)) by its name, as in `\cell<H l>{…}`. The
109/// cell styles of a registry are the ones that the `cells` feature ships
110/// ([`shipped`](super::shipped)), and the ones that other features contribute
111/// to the service slot [`STYLES_SLOT`](super::STYLES_SLOT).
112///
113/// Reported by the lowering rule of the `cells` environment, on every
114/// backend, at the token. The token is ignored, and the other tokens of the
115/// cell are used.
116#[derive(Debug, Clone, PartialEq, Eq, DiagnosticInfo)]
117#[diagnostic(id = "flm.cells.unknown-style", message = "Unknown cell style ‘{name}’")]
118pub struct UnknownStyle {
119 /// The name of the style token, as written.
120 pub name: String,
121}
122
123/// The error that is reported when a style token has a missing or an invalid
124/// value (`flm.cells.bad-style-value`).
125///
126/// A value is written in braces directly after the name of a cell style, and
127/// each style name takes at most one value. A cell style of the structural
128/// form ([`StyleForm::Structural`](super::StyleForm::Structural)) requires
129/// one of the values of its [`Parameter`](super::Parameter), as in
130/// `align{left}`, `head{column}` or `rule{below}`. A cell style of the fixed
131/// form ([`StyleForm::Fixed`](super::StyleForm::Fixed)), such as `l`, `H` or
132/// `green`, takes no value.
133///
134/// Reported by the lowering rule of the `cells` environment, on every
135/// backend, at the token. The token is ignored, and the other tokens of the
136/// cell are used. A value that does not follow a style name is reported at
137/// the value, and ignored.
138#[derive(Debug, Clone, PartialEq, Eq, DiagnosticInfo)]
139#[diagnostic(id = "flm.cells.bad-style-value", message = "{detail}")]
140pub struct BadStyleValue {
141 /// The message of the diagnostic. It states what is wrong with the value.
142 pub detail: String,
143}
144
145/// The error that is reported when an entry of `\rownames` or `\colnames` is
146/// not valid (`flm.cells.bad-names`).
147///
148/// The argument of `\rownames` or `\colnames` is a comma-separated list of
149/// names. The *k*-th entry is the name of row or column *k*, and an empty
150/// entry gives no name, as in `\colnames{name,,city}`. A name begins with an
151/// ASCII letter and contains only ASCII letters, digits and underscores. Each
152/// row name and each column name may be declared only once, and each row or
153/// column may have only one name.
154///
155/// Reported by the lowering rule of the `cells` environment, on every
156/// backend, at the `\rownames` or the `\colnames`, once for each invalid
157/// entry. The message states what is wrong. The invalid entry is ignored, and
158/// the other entries are used.
159#[derive(Debug, Clone, PartialEq, Eq, DiagnosticInfo)]
160#[diagnostic(id = "flm.cells.bad-names", message = "{detail}")]
161pub struct BadNames {
162 /// The message of the diagnostic. It states what is wrong with the entry.
163 pub detail: String,
164}
165
166/// The error that is reported when a cell covers a position that is already
167/// covered by a cell earlier in the body of the table (`flm.cells.overlap`).
168///
169/// Each position of a table belongs to at most one cell. A cell covers more
170/// than one position when it spans several rows or columns with `\merge`, as
171/// in `\cell[1;\merge{3-4}]{…}`. When the data of a `\celldata` has more
172/// entries than its mapping lists, the remaining entries are placed at the
173/// last row or column of the mapping, where they overlap. An open last range,
174/// as in `\celldata[2-]{…}`, instead continues one row or column per entry.
175///
176/// Reported by the lowering rule of the `cells` environment, on every
177/// backend, at the later cell. The message gives the position of the earlier
178/// cell. The later cell is left out of the output together with its content,
179/// and a dropped mark (`flm.pres.dropped`) is placed immediately before the
180/// table.
181#[derive(Debug, Clone, PartialEq, Eq, DiagnosticInfo)]
182#[diagnostic(
183 id = "flm.cells.overlap",
184 message = "Two cells may not share a position: this cell overlaps the cell at row {row}, column {col}"
185)]
186pub struct Overlap {
187 /// The first row of the earlier cell, numbered from 1.
188 pub row: u64,
189 /// The first column of the earlier cell, numbered from 1.
190 pub col: u64,
191}
192
193/// The error that is reported when a cell would make its table larger than
194/// the largest size allowed (`flm.cells.too-large`).
195///
196/// The size of a table is its number of rows times its number of columns,
197/// counted in positions. A table may have at most
198/// [`MAX_CELLS_POSITIONS`](crate::stdlib::backends::pres::MAX_CELLS_POSITIONS)
199/// positions (one million). This limit exists because each backend visits
200/// every position of a table when it writes the table, and a single `\merge`
201/// can span a very large number of rows or columns.
202///
203/// Reported by the lowering rule of the `cells` environment, on every
204/// backend, at the cell. The cell is left out of the output together with its
205/// content, and a dropped mark (`flm.pres.dropped`) is placed immediately
206/// before the table.
207#[derive(Debug, Clone, PartialEq, Eq, DiagnosticInfo)]
208#[diagnostic(
209 id = "flm.cells.too-large",
210 message = "A ‘cells’ environment may have at most {limit} positions (rows times columns): \
211 this cell would make it {rows} rows by {cols} columns"
212)]
213pub struct TooLarge {
214 /// The largest number of positions that a table may have.
215 pub limit: u64,
216 /// The number of rows that the table would have with the cell.
217 pub rows: u64,
218 /// The number of columns that the table would have with the cell.
219 pub cols: u64,
220}
221
222/// The error that is reported when a `cells` environment contains no cell
223/// (`flm.cells.empty`).
224///
225/// An empty entry of a `\celldata` adds no cell. A table also counts as empty
226/// when every one of its cells is left out because it would make the table
227/// too large ([`TooLarge`]).
228///
229/// Reported by the lowering rule of the `cells` environment, on every
230/// backend, at the environment. Nothing is written for the table, except the
231/// dropped marks for the parts of the table that were left out.
232#[derive(Debug, Clone, PartialEq, Eq, DiagnosticInfo)]
233#[diagnostic(
234 id = "flm.cells.empty",
235 message = "A ‘cells’ environment must hold at least one cell"
236)]
237pub struct Empty;
238
239/// The error that is reported when a command of a table of cells is written
240/// outside of its place (`flm.cells.outside-table`).
241///
242/// The commands of a table of cells, and the character `&`, must be written
243/// in the following places:
244///
245/// - `\cell` directly in the body of a `cells` environment, or as an entry of
246/// the data of a `\celldata`, as in `\celldata{A & \cell<H>{B}}`;
247/// - `\celldata`, `\rownames` and `\colnames` directly in the body of a
248/// `cells` environment;
249/// - `\\` directly in the body of a `cells` environment, or directly in the
250/// data of a `\celldata`;
251/// - `&` directly in the data of a `\celldata`, as in `\celldata{A & B}`;
252/// - `\merge` in the placement of a `\cell` or in the mapping of a
253/// `\celldata`, as in `\cell[1;\merge{3-4}]{…}`.
254///
255/// Reported by the lowering rule [`OutsideRule`](super::lower::OutsideRule)
256/// of the command, on every backend, at the command. A `\cell` expands to its
257/// content, and a `\celldata` expands to its data as inline content. The
258/// other commands expand to a dropped mark (`flm.pres.dropped`), and so does
259/// a `\cell` or a `\celldata` whose last argument is missing.
260#[derive(Debug, Clone, PartialEq, Eq, DiagnosticInfo)]
261#[diagnostic(id = "flm.cells.outside-table", message = "{detail}")]
262pub struct OutsideTable {
263 /// The message of the diagnostic. It states where the command must be
264 /// written ([`OUTSIDE_RULES`](super::lower::OUTSIDE_RULES)).
265 pub detail: String,
266}