math-core 0.6.1

Convert LaTeX equations to MathML Core
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
use std::ops::Range;

use strum_macros::IntoStaticStr;

use mathml_renderer::attribute::{FracAttr, HtmlTextStyle, Notation, OpAttrs, Size, Style};
use mathml_renderer::length::Length;
use mathml_renderer::symbol::{Bin, MathMLOperator, Op, OrdLike, Punct, Rel};

use crate::character_class::{Class, MathVariant, ParenType};
use crate::environments::Env;

#[derive(Debug, Clone, Copy)]
pub enum Token<'source> {
    /// End of input.
    Eoi,
    /// The beginning of an environment, e.g. `\begin{matrix}`.
    Begin(Env),
    /// The end of an environment, e.g. `\end{matrix}`.
    End(Env),
    /// A new column in an array or matrix, e.g. `&` in `\begin{matrix} a & b\\c & d \end{matrix}`.
    NewColumn,
    /// A new line in an array or matrix, e.g. `\\` in `\begin{matrix} a & b\\c & d \end{matrix}`.
    NewLine,
    /// `\nonumber`/`\notag`, suppresses numbering for the current equation.
    NoNumber,
    /// `\tag`, tag for the current equation.
    Tag,
    /// `\label`, label for the current equation.
    Label,
    /// `\eqref`, equation reference to a label.
    EqRef,
    /// A left delimiter, e.g. `\left(`.
    Left,
    /// A right delimiter, e.g. `\right)`.
    Right,
    /// A middle delimiter, e.g. `\middle|`.
    Middle,
    /// The character `[`. It has its own token because we need to
    /// distinguish it from `\lbrack` after, e.g., `\sqrt`.
    SquareBracketOpen,
    /// The character `]`. It has its own token because we often need to search for it.
    /// Additionally, it's useful to distinguish this from `\rbrack`.
    SquareBracketClose,
    /// The character `{`.
    GroupBegin,
    /// The character `}`.
    GroupEnd,
    /// A token for `\frac` and `\cfrac`, `\dfrac` and `\tfrac`. The `Option<FracAttr>` is `None`
    /// for `\frac` and, for example, `Some(FracAttr::DisplayStyleTrue)` for `\dfrac`.
    Frac(Option<FracAttr>),
    /// A token for `\over`, `\atop`, `\choose`, `\brace` and `\brack`.
    InfixGenFrac {
        with_line: bool,
        delim: Option<InfixDelim>,
    },
    /// `\genfrac`
    Genfrac,
    /// The character `_` for subscripts.
    Underscore,
    /// The character `^` for superscripts.
    Circumflex,
    /// A token for `\binom`, `\dbinom` and `\tbinom`. The `Option<FracAttr>` is `None` for
    /// `\binom` and, for example, `Some(FracAttr::DisplayStyleTrue)` for `\dbinom`.
    Binom(Option<FracAttr>),
    /// `\overset`
    Overset,
    /// `\underset`
    Underset,
    /// `\overbrace` and `\underbrace`. The `bool` is `true` for overbraces and `false` for
    /// underbraces.
    OverUnderBrace(OrdLike, bool),
    /// `\sqrt` and `\sqrt[n]{...}`
    Sqrt,
    /// `\limits`
    Limits,
    /// Fixed-length spaces, e.g. `\,`, `\;`, `\quad`, etc.
    Space(Length),
    /// A custom space specified by the user, e.g. `\hspace{1em}`.
    CustomSpace,
    /// A non-breaking space, e.g. `~`.
    NonBreakingSpace,
    /// A whitespace character, e.g. ` `.
    Whitespace,
    /// A token for transforming to a specific math variant, e.g. `\mathbf`.
    Transform(MathVariant),
    /// A token for switching the math variant, e.g. `\bf`.
    TransformSwitch(MathVariant),
    /// A sized parenthesis, e.g. `\bigl(`, `\Biggr)`.
    Big(Size, Option<ParenType>),
    /// Stretchy and non-stretchy accents, e.g. `\hat`, `\widehat`, `\bar`, `\overline`, etc.
    /// The `bool` is `true` for over-accents and `false` for under-accents.
    Accent(MathMLOperator, bool, OpAttrs),
    /// A token corresponding to LaTeX's "mathord" character class (class 0).
    Ord(OrdLike),
    /// A token corresponding to LaTeX's "mathop" character class (class 1).
    Op(Op),
    /// A token corresponding to LaTeX's "mathbin" character class (class 2).
    BinaryOp(Bin),
    /// A token corresponding to LaTeX's "mathrel" character class (class 3).
    Relation(Rel),
    /// A token corresponding to LaTeX's "mathopen" character class (class 4).
    Open(OrdLike),
    /// A token corresponding to LaTeX's "mathclose" character class (class 5).
    Close(OrdLike),
    /// A token corresponding to LaTeX's "mathpunct" character class (class 6).
    Punctuation(Punct),
    /// A token corresponding to LaTeX's "mathinner" character class (class I).
    Inner(Op),
    /// The character `'`.
    Prime,
    /// The character `>`.
    /// It has its own token because we need to escape it for the HTML output.
    OpGreaterThan,
    /// The character `<`.
    /// It has its own token because we need to escape it for the HTML output.
    OpLessThan,
    /// The character `&`.
    /// It has its own token because we need to escape it for the HTML output.
    OpAmpersand,
    /// A token to force an operator to behave like a binary operator (mathbin).
    /// This is, for example, needed for `×`, which in LaTeX is a binary operator,
    /// but in MathML Core is a "big operator" (mathop).
    ForceBinaryOp(MathMLOperator),
    /// A token to force an operator to behave like a relation (mathrel).
    /// This is, for example, needed for `:`, which in LaTeX is a relation,
    /// but in MathML Core is a separator (punctuation).
    ForceRelation(MathMLOperator),
    /// A token to force an operator to behave like a closing symbol (mathclose).
    /// This is, for example, needed for `!`, which in LaTeX is a closing symbol,
    /// but in MathML Core is an ordinary operator.
    ForceClose(MathMLOperator),
    /// A token to force an operator to behave like punctuation (mathpunct).
    ForcePunctuation(MathMLOperator),
    /// `\mathbin`
    Mathbin,
    /// A token for the extensible arrow commands `\xrightarrow`, `\xleftarrow`, etc.
    /// The `Rel` is the stretchy arrow operator to render.
    XArrow(Rel),
    /// An ordinary letter, e.g. `a`, `b`, `c`.
    Letter(char, Mode),
    /// A letter for which we need `mathvariant="normal"`.
    /// For example, upper-case greek letter like `\Gamma`, which should be rendered upright.
    UprightLetter(char),
    /// A digit, e.g. `0`, `1`, `2`.
    Digit(char),
    /// Text-based operators without limits.
    /// For example, `\log`, `\exp`, `\sin`, `\cos`, `\tan`.
    PseudoOperator(&'static str),
    /// Text-based operators with limits.
    /// For example, `\lim`, `\sup`, `\inf`, `\max`, `\min`.
    PseudoOperatorLimits(&'static str),
    /// A token for enclosing notations, e.g. `\cancel`, `\xcancel`.
    Enclose(Notation),
    /// `\operatorname` and `\operatorname*`. The `bool` is `true` for `\operatorname*` and `false`
    /// for `\operatorname`.
    OperatorName { with_limits: bool },
    /// `\slashed`
    Slashed,
    /// `\not`
    Not,
    /// A token for text, e.g. `\text{...}`, `\textit{...}`.
    Text(Option<HtmlTextStyle>),
    /// `\displaystyle`, `\textstyle`, `\scriptstyle` and `\scriptscriptstyle`.
    Style(Style),
    /// A token for math color, e.g. `\color{red}`.
    Color,
    /// A token used in custom commands defined by the user. The `u8` is the index of the argument,
    /// going from 0 to 8. For example, `\#1` corresponds to `CustomCmdArg(0)`.
    CustomCmdArg(u8),
    /// A token referencing a stream of tokens defined by the user. The `u8` is the number of
    /// arguments that the custom command takes.
    CustomCmd(u8, &'source [Token<'static>]),
    /// A token for hardcoded MathML. The `&'static str` is the MathML string to be inserted into
    /// the output.
    HardcodedMathML(&'static str),
    /// A token for text-mode accents, e.g. `\~{n}`. The `char` is a Unicode combining character,
    /// e.g. `\u{0303}` for the tilde accent.
    TextMode(TextToken),
    /// A token for commands that can be used in both math mode and text mode, e.g. `\{`. The `char`
    /// is the character that the command produces, e.g. `{` for `\{`.
    MathOrTextMode(&'static Token<'static>, char),
    /// A token for unknown commands. This is used when `ignore_unknown_commands` is `true` in the
    /// configuration, and the parser encounters an unknown command. The `&'source str` is the name
    /// of the unknown command.
    UnknownCommand(&'source str),
    /// This token is intended to be used in predefined token streams.
    /// It is equivalent to `{abc}`, but has a much more compact representation.
    InternalStringLiteral(&'static str),
}

#[derive(Debug, Clone, Copy)]
pub enum TextToken {
    Accent(char),
    Letter(char),
}

/// The delimiter pair that surrounds the result of an infix fraction-like command.
#[derive(Debug, Clone, Copy)]
pub enum InfixDelim {
    /// Parentheses: `(` and `)` (`\choose`).
    Paren,
    /// Curly brackets: `{` and `}` (`\brace`).
    Brace,
    /// Square brackets: `[` and `]` (`\brack`).
    Brack,
}

#[cfg(target_arch = "wasm32")]
static_assertions::assert_eq_size!(Token<'_>, [usize; 3]);
#[cfg(target_arch = "wasm32")]
static_assertions::assert_eq_size!(Result<Token<'_>, &'static i32>, [usize; 3]);

impl Token<'_> {
    /// Returns the character class of this token.
    pub(super) fn class(&self) -> Option<Class> {
        use Token::*;
        match self.unwrap_math_ref() {
            Relation(_) | ForceRelation(_) | OpGreaterThan | OpLessThan | XArrow(_) => {
                Some(Class::Relation)
            }
            Punctuation(_) | ForcePunctuation(_) => Some(Class::Punctuation),
            Open(_) | Left | SquareBracketOpen | Begin(_) | GroupBegin => Some(Class::Open),
            Close(_) | SquareBracketClose | ForceClose(_) | Right => Some(Class::Close),
            BinaryOp(_) | ForceBinaryOp(_) | Mathbin => Some(Class::BinaryOp),
            Op(_) | PseudoOperator(_) | PseudoOperatorLimits(_) | OperatorName { .. } => {
                Some(Class::Operator)
            }
            End(_) | NewLine | NewColumn | GroupEnd | Eoi => Some(Class::End),
            Inner(_) => Some(Class::Inner),
            Big(_, Some(paren_type)) => Some(match paren_type {
                ParenType::Left => Class::Open,
                ParenType::Right => Class::Close,
                ParenType::Middle => Class::Relation,
            }),
            CustomCmd(_, toks) => toks.iter().find_map(Token::class),
            Whitespace | Space(_) | Not | TransformSwitch(_) | NoNumber | Tag | CustomSpace
            | Limits | NonBreakingSpace | Label | EqRef => None,
            Letter(_, _)
            | UprightLetter(_)
            | Digit(_)
            | Big(_, None)
            | Middle
            | Frac(_)
            | InfixGenFrac { .. }
            | Genfrac
            | Underscore
            | Circumflex
            | Binom(_)
            | Overset
            | Underset
            | OverUnderBrace(_, _)
            | Sqrt
            | Transform(_)
            | Ord(_)
            | Prime
            | Enclose(_)
            | OpAmpersand
            | Slashed
            | Text(_)
            | Style(_)
            | Color
            | CustomCmdArg(_)
            | HardcodedMathML(_)
            | TextMode(_)
            | MathOrTextMode(_, _)
            | UnknownCommand(_)
            | InternalStringLiteral(_)
            | Accent(_, _, _) => Some(Class::Default),
        }
    }

    /// If this token is `MathOrTextMode`, returns the inner token. Otherwise, returns `self`.
    #[inline]
    pub fn unwrap_math_ref(&self) -> &Self {
        if let Token::MathOrTextMode(tok, _) = self {
            tok
        } else {
            self
        }
    }
    #[inline]
    pub fn unwrap_math(self) -> Self {
        if let Token::MathOrTextMode(tok, _) = self {
            *tok
        } else {
            self
        }
    }
}

#[derive(Debug, Clone, Copy, Default)]
pub enum Mode {
    #[default]
    Math,
    MathOrText,
}

#[derive(Debug, Clone, Copy, Default)]
pub struct Span {
    start: usize,
    end: usize,
}

impl Span {
    #[inline]
    pub const fn new(start: usize, end: usize) -> Self {
        Span { start, end }
    }

    #[inline]
    pub const fn zero_width(at: usize) -> Self {
        Span { start: at, end: at }
    }

    #[inline]
    pub const fn start(&self) -> usize {
        self.start
    }

    #[inline]
    pub const fn end(&self) -> usize {
        self.end
    }

    /// Returns a new `Span` with the same start position as `self`, but with the end position set
    /// to `self.start + length`.
    #[inline]
    pub const fn with_length(self, length: usize) -> Self {
        Span {
            start: self.start,
            end: self.start + length,
        }
    }
}

impl From<Span> for Range<usize> {
    #[inline]
    fn from(span: Span) -> Self {
        span.start..span.end
    }
}

/// A token together with its span in the input string.
#[derive(Debug, Clone, Copy)]
pub struct TokSpan<'config>(Token<'config>, Span);

#[cfg(target_arch = "wasm32")]
static_assertions::assert_eq_size!(TokSpan<'_>, [usize; 5]);

impl<'config> TokSpan<'config> {
    #[inline]
    pub const fn new(token: Token<'config>, span: Span) -> Self {
        TokSpan(token, span)
    }

    #[inline]
    pub fn token(&self) -> &Token<'config> {
        &self.0
    }

    #[inline]
    pub fn into_token(self) -> Token<'config> {
        self.0
    }

    #[inline]
    pub fn into_parts(self) -> (Token<'config>, Span) {
        (self.0, self.1)
    }

    // #[inline]
    // pub fn token_mut(&mut self) -> &mut Token<'config> {
    //     &mut self.0
    // }

    #[inline]
    pub fn span(&self) -> Span {
        self.1
    }
}

impl<'config> From<Token<'config>> for TokSpan<'config> {
    #[inline]
    fn from(token: Token<'config>) -> Self {
        TokSpan(token, Span::default())
    }
}

#[derive(Debug, Clone, Copy, PartialEq, IntoStaticStr)]
pub enum EndToken {
    #[strum(serialize = r"\end{...}")]
    End,
    #[strum(serialize = r"}")]
    GroupClose,
    #[strum(serialize = r"\right")]
    Right,
    #[strum(serialize = r"]")]
    SquareBracketClose,
    #[strum(serialize = r"end of input")]
    Eoi,
}

impl EndToken {
    pub fn matches(self, other: &Token) -> bool {
        matches!(
            (self, other),
            (EndToken::End, Token::End(_))
                | (EndToken::GroupClose, Token::GroupEnd)
                | (EndToken::Right, Token::Right)
                | (EndToken::SquareBracketClose, Token::SquareBracketClose)
                | (EndToken::Eoi, Token::Eoi)
        )
    }
}