Skip to main content

mdwright_math/
lib.rs

1//! Math-region grammar.
2//!
3//! TeX-style math (`\[ … \]`, `\( … \)`, `$$ … $$`, `$ … $`,
4//! `\begin{env} … \end{env}`) is opaque to `CommonMark`: pulldown
5//! tokenises the bytes inside as plain prose, so `_` becomes
6//! emphasis, `[` becomes a link candidate, `*` becomes a delimiter
7//! run. Without an overlay the formatter's round-trip drifts.
8//!
9//! This module is the structural recogniser. [`scan::scan_math_regions`]
10//! consumes source plus the IR's inline / block atoms and produces:
11//!
12//! - [`MathRegion`] values consumed by the format pipeline. The region
13//!   carries a [`span::MathSpan`] tag with delimiter and body data.
14//! - [`span::MathError`] values surfaced by the
15//!   `math/unbalanced-delim`, `math/unbalanced-env`, and
16//!   `math/unbalanced-braces` lint rules.
17//!
18//! Stack-based tracking enforces `\begin` / `\end` balance with
19//! nesting on the same environment name; the four primitive
20//! delimiter pairs match greedily on first close.
21
22#![forbid(unsafe_code)]
23
24pub mod env;
25pub mod normalise;
26pub mod render;
27pub mod scan;
28pub mod span;
29
30use std::ops::Range;
31
32pub use scan::{MathConfig, scan_math_regions};
33pub use span::{AnyDelim, DisplayDelim, InlineDelim, MathBody, MathError, MathSpan};
34
35/// One recognised math region in source order.
36///
37/// `range` covers both delimiters and everything between them. The
38/// `span` tag carries the typed classification plus the body byte
39/// range resolved against source.
40#[derive(Clone, Debug)]
41pub struct MathRegion {
42    pub range: Range<usize>,
43    span: MathSpan,
44}
45
46impl MathRegion {
47    #[must_use]
48    pub fn new(range: Range<usize>, span: MathSpan) -> Self {
49        Self { range, span }
50    }
51
52    #[must_use]
53    pub fn span(&self) -> &MathSpan {
54        &self.span
55    }
56}