mkgraphic 0.4.4

A Rust port of the cycfi/elements GUI framework
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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
//! Hand-rolled recursive-descent parser: LaTeX math source -> [`MathNode`].
//! No new dependency -- the vocabulary this covers (fractions, scripts,
//! radicals, `\left`/`\right`, `\sum`/`\int`/etc. with limits, `\text{}`,
//! Greek/operator/relation symbols via [`super::glyphs`]) is small enough
//! that a tokenizer + a few mutually-recursive functions is simpler and
//! more auditable than pulling in a general parser-combinator crate.
//!
//! **Must never panic on malformed input** -- this parses text an LLM
//! produced, not a trusted build artifact. Every failure mode returns
//! [`MathParseError`]; callers (`markdown_to_runs`) fall back to
//! rendering the raw `$...$`/`$$...$$` source literally rather than
//! propagating a panic into a chat bubble.

use super::ast::{AtomClass, BigOpKind, DelimiterKind, MathNode};
use super::glyphs::lookup_command;

#[derive(Debug, Clone, PartialEq)]
pub enum MathParseError {
    /// Ran out of tokens where at least one more was required (e.g. a
    /// dangling `^`/`_` with no following primary, or `\frac` missing an
    /// argument group).
    UnexpectedEnd,
    /// A `\command` this table (see [`super::glyphs`] and this module's
    /// own structural commands) doesn't recognize.
    UnknownCommand(String),
    /// A `{` with no matching `}`, or vice versa.
    UnmatchedBrace,
    /// A `\left`/`\right` pair that doesn't resolve (missing `\right`,
    /// stray `\right` with no `\left`, or an unrecognized delimiter
    /// token).
    UnmatchedDelimiter(String),
}

/// Parses `source` (the raw TeX string from a `$...$`/`$$...$$` span,
/// *without* the surrounding `$` delimiters -- those are already stripped
/// by `pulldown_cmark`'s `Event::InlineMath`/`Event::DisplayMath` before
/// this is called) into a [`MathNode`] tree ready for layout.
pub fn parse_math(source: &str) -> Result<MathNode, MathParseError> {
    let tokens = tokenize(source);
    let mut parser = Parser {
        tokens: &tokens,
        pos: 0,
    };
    let node = parser.parse_row(|t| matches!(t, Token::CloseBrace))?;
    // A top-level stray `}` (no matching `{`) is the one case
    // `parse_row`'s stop-on-`CloseBrace` condition can exit early without
    // actually consuming everything -- check we reached the end.
    if parser.pos != tokens.len() {
        return Err(MathParseError::UnmatchedBrace);
    }
    Ok(node)
}

#[derive(Debug, Clone, PartialEq)]
enum Token {
    /// A command word after `\`, letters only (e.g. `frac`, `sqrt`,
    /// `left`, `sum`, `alpha`).
    Command(String),
    /// `\` followed by a single non-letter character -- LaTeX's escaping
    /// convention for characters that are otherwise structural, chiefly
    /// `\{`, `\}`, and `\|` (a double-bar delimiter).
    Escaped(char),
    /// Any other single character, including whitespace (kept, not
    /// dropped, so `\text{...}` can reconstruct literal spacing -- see
    /// `Parser::parse_text_literal`; every other call site skips
    /// whitespace `Char`s itself via `Parser::skip_space`).
    Char(char),
    Caret,
    Underscore,
    OpenBrace,
    CloseBrace,
}

fn tokenize(source: &str) -> Vec<Token> {
    let mut chars = source.chars().peekable();
    let mut tokens = Vec::new();
    while let Some(c) = chars.next() {
        match c {
            '\\' => match chars.peek().copied() {
                Some(next) if next.is_ascii_alphabetic() => {
                    let mut name = String::new();
                    while let Some(&c2) = chars.peek() {
                        if c2.is_ascii_alphabetic() {
                            name.push(c2);
                            chars.next();
                        } else {
                            break;
                        }
                    }
                    tokens.push(Token::Command(name));
                }
                Some(next) => {
                    chars.next();
                    tokens.push(Token::Escaped(next));
                }
                None => {}
            },
            '^' => tokens.push(Token::Caret),
            '_' => tokens.push(Token::Underscore),
            '{' => tokens.push(Token::OpenBrace),
            '}' => tokens.push(Token::CloseBrace),
            other => tokens.push(Token::Char(other)),
        }
    }
    tokens
}

struct Parser<'a> {
    tokens: &'a [Token],
    pos: usize,
}

impl<'a> Parser<'a> {
    fn peek(&self) -> Option<&Token> {
        self.tokens.get(self.pos)
    }

    fn advance(&mut self) -> Option<&Token> {
        let tok = self.tokens.get(self.pos);
        if tok.is_some() {
            self.pos += 1;
        }
        tok
    }

    fn skip_space(&mut self) {
        while matches!(self.peek(), Some(Token::Char(c)) if c.is_whitespace()) {
            self.pos += 1;
        }
    }

    /// Parses atoms until `stop` matches the next token or input is
    /// exhausted. Whitespace between atoms is insignificant in math mode
    /// (standard LaTeX behavior) and is skipped here, not treated as its
    /// own atom.
    fn parse_row(&mut self, stop: impl Fn(&Token) -> bool) -> Result<MathNode, MathParseError> {
        let mut atoms = Vec::new();
        loop {
            self.skip_space();
            match self.peek() {
                None => break,
                Some(tok) if stop(tok) => break,
                _ => atoms.push(self.parse_atom()?),
            }
        }
        Ok(match atoms.len() {
            1 => atoms.into_iter().next().unwrap(),
            _ => MathNode::Row(atoms),
        })
    }

    /// One atom: a primary, optionally followed by `^`/`_` (in either
    /// order, at most one of each -- `x_i^2` and `x^2_i` are both valid
    /// and equivalent). `\sum`-style big operators consume their own
    /// `_`/`^` as limits inside `parse_primary`/`parse_bigop`, so this
    /// loop never double-wraps one in an extra `Script`.
    fn parse_atom(&mut self) -> Result<MathNode, MathParseError> {
        let base = self.parse_primary()?;
        let mut sup = None;
        let mut sub = None;
        loop {
            self.skip_space();
            match self.peek() {
                Some(Token::Caret) if sup.is_none() => {
                    self.advance();
                    self.skip_space();
                    sup = Some(Box::new(self.parse_primary()?));
                }
                Some(Token::Underscore) if sub.is_none() => {
                    self.advance();
                    self.skip_space();
                    sub = Some(Box::new(self.parse_primary()?));
                }
                _ => break,
            }
        }
        Ok(if sup.is_some() || sub.is_some() {
            MathNode::Script {
                base: Box::new(base),
                sup,
                sub,
            }
        } else {
            base
        })
    }

    fn parse_primary(&mut self) -> Result<MathNode, MathParseError> {
        self.skip_space();
        match self.advance() {
            None => Err(MathParseError::UnexpectedEnd),
            Some(Token::OpenBrace) => {
                let node = self.parse_row(|t| matches!(t, Token::CloseBrace))?;
                self.expect_close_brace()?;
                Ok(node)
            }
            Some(Token::CloseBrace) => Err(MathParseError::UnmatchedBrace),
            Some(Token::Caret) | Some(Token::Underscore) => Err(MathParseError::UnexpectedEnd),
            Some(Token::Escaped(c)) => Ok(MathNode::Symbol {
                glyph: *c,
                class: AtomClass::Ord,
            }),
            Some(Token::Char(c)) => Ok(symbol_for_char(*c)),
            Some(Token::Command(name)) => {
                let name = name.clone();
                self.parse_command(&name)
            }
        }
    }

    fn expect_close_brace(&mut self) -> Result<(), MathParseError> {
        match self.advance() {
            Some(Token::CloseBrace) => Ok(()),
            _ => Err(MathParseError::UnmatchedBrace),
        }
    }

    /// Parses one `{...}` group as a required argument (used by
    /// `\frac`/`\sqrt`) -- a bare single token (`\frac12`, valid LaTeX)
    /// falls back to `parse_primary` so `\frac12` and `\frac{1}{2}`
    /// behave the same, matching real LaTeX's "an argument is either a
    /// group or a single token" rule.
    fn parse_argument(&mut self) -> Result<MathNode, MathParseError> {
        self.parse_primary()
    }

    fn parse_command(&mut self, name: &str) -> Result<MathNode, MathParseError> {
        match name {
            "frac" => {
                let num = self.parse_argument()?;
                let den = self.parse_argument()?;
                Ok(MathNode::Frac {
                    num: Box::new(num),
                    den: Box::new(den),
                })
            }
            "sqrt" => {
                let radicand = self.parse_argument()?;
                Ok(MathNode::Sqrt(Box::new(radicand)))
            }
            "text" => {
                self.skip_space();
                match self.advance() {
                    Some(Token::OpenBrace) => Ok(MathNode::Text(self.parse_text_literal()?)),
                    _ => Err(MathParseError::UnexpectedEnd),
                }
            }
            "left" => self.parse_delimited(),
            "right" => Err(MathParseError::UnmatchedDelimiter(
                "\\right with no matching \\left".to_string(),
            )),
            "sum" => self.parse_bigop(BigOpKind::Sum),
            "prod" => self.parse_bigop(BigOpKind::Prod),
            "int" => self.parse_bigop(BigOpKind::Int),
            "oint" => self.parse_bigop(BigOpKind::Oint),
            "bigcup" => self.parse_bigop(BigOpKind::BigCup),
            "bigcap" => self.parse_bigop(BigOpKind::BigCap),
            "lim" => self.parse_bigop(BigOpKind::Lim),
            "max" => self.parse_bigop(BigOpKind::Max),
            "min" => self.parse_bigop(BigOpKind::Min),
            _ => match lookup_command(name) {
                Some((glyph, class)) => Ok(MathNode::Symbol { glyph, class }),
                None => Err(MathParseError::UnknownCommand(name.to_string())),
            },
        }
    }

    /// Accumulates literal characters (including whitespace, unlike every
    /// other parsing path here) until the matching `}` -- `\text{...}`'s
    /// content is upright prose, not math to recurse into. Does not
    /// support nested braces inside `\text{}` (not a realistic need for
    /// short chat-reply labels like `\text{Hz}`).
    fn parse_text_literal(&mut self) -> Result<String, MathParseError> {
        let mut text = String::new();
        loop {
            match self.advance() {
                Some(Token::CloseBrace) => return Ok(text),
                Some(Token::Char(c)) => text.push(*c),
                Some(Token::Escaped(c)) => text.push(*c),
                Some(Token::Caret) => text.push('^'),
                Some(Token::Underscore) => text.push('_'),
                Some(Token::OpenBrace) => return Err(MathParseError::UnmatchedBrace),
                Some(Token::Command(name)) => {
                    let name = name.clone();
                    match lookup_command(&name) {
                        Some((glyph, _)) => text.push(glyph),
                        None => return Err(MathParseError::UnknownCommand(name)),
                    }
                }
                None => return Err(MathParseError::UnmatchedBrace),
            }
        }
    }

    fn parse_bigop(&mut self, kind: BigOpKind) -> Result<MathNode, MathParseError> {
        let mut lower = None;
        let mut upper = None;
        loop {
            self.skip_space();
            match self.peek() {
                Some(Token::Underscore) if lower.is_none() => {
                    self.advance();
                    self.skip_space();
                    lower = Some(Box::new(self.parse_primary()?));
                }
                Some(Token::Caret) if upper.is_none() => {
                    self.advance();
                    self.skip_space();
                    upper = Some(Box::new(self.parse_primary()?));
                }
                _ => break,
            }
        }
        Ok(MathNode::BigOp { kind, lower, upper })
    }

    fn parse_delimited(&mut self) -> Result<MathNode, MathParseError> {
        let open = self.parse_delimiter_token()?;
        let body = self.parse_row(|t| matches!(t, Token::Command(name) if name == "right"))?;
        self.skip_space();
        match self.advance() {
            Some(Token::Command(name)) if name == "right" => {}
            _ => {
                return Err(MathParseError::UnmatchedDelimiter(
                    "\\left with no matching \\right".to_string(),
                ))
            }
        }
        let close = self.parse_delimiter_token()?;
        Ok(MathNode::Delimited {
            open,
            body: Box::new(body),
            close,
        })
    }

    /// Consumes and classifies the one token that must follow `\left`/
    /// `\right`: a bracket/paren/brace/bar character, `\lfloor`/`\rfloor`/
    /// `\lceil`/`\rceil`, `\langle`/`\rangle`, or `.` (LaTeX's "no visible
    /// delimiter on this side").
    fn parse_delimiter_token(&mut self) -> Result<Option<DelimiterKind>, MathParseError> {
        self.skip_space();
        match self.advance() {
            Some(Token::Char('(')) | Some(Token::Char(')')) => Ok(Some(DelimiterKind::Paren)),
            Some(Token::Char('[')) | Some(Token::Char(']')) => Ok(Some(DelimiterKind::Bracket)),
            Some(Token::Escaped('{')) | Some(Token::Escaped('}')) => Ok(Some(DelimiterKind::Brace)),
            Some(Token::Char('|')) => Ok(Some(DelimiterKind::Bar)),
            Some(Token::Escaped('|')) => Ok(Some(DelimiterKind::DoubleBar)),
            Some(Token::Char('.')) => Ok(None),
            Some(Token::Command(name)) if name == "lfloor" || name == "rfloor" => {
                Ok(Some(DelimiterKind::Floor))
            }
            Some(Token::Command(name)) if name == "lceil" || name == "rceil" => {
                Ok(Some(DelimiterKind::Ceil))
            }
            Some(Token::Command(name)) if name == "langle" => Ok(Some(DelimiterKind::AngleLeft)),
            Some(Token::Command(name)) if name == "rangle" => Ok(Some(DelimiterKind::AngleRight)),
            Some(other) => Err(MathParseError::UnmatchedDelimiter(format!(
                "{other:?} is not a valid \\left/\\right delimiter"
            ))),
            None => Err(MathParseError::UnexpectedEnd),
        }
    }
}

fn symbol_for_char(c: char) -> MathNode {
    let class = match c {
        '+' | '-' | '*' | '/' => AtomClass::Bin,
        '=' | '<' | '>' => AtomClass::Rel,
        '(' | '[' => AtomClass::Open,
        ')' | ']' => AtomClass::Close,
        ',' | ';' => AtomClass::Punct,
        _ => AtomClass::Ord,
    };
    MathNode::Symbol { glyph: c, class }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn sym(glyph: char, class: AtomClass) -> MathNode {
        MathNode::Symbol { glyph, class }
    }

    #[test]
    fn a_single_variable_parses_as_one_symbol() {
        assert_eq!(parse_math("x"), Ok(sym('x', AtomClass::Ord)));
    }

    #[test]
    fn frac_one_half_parses_into_a_frac_node() {
        let expected = MathNode::Frac {
            num: Box::new(sym('1', AtomClass::Ord)),
            den: Box::new(sym('2', AtomClass::Ord)),
        };
        assert_eq!(parse_math("\\frac{1}{2}"), Ok(expected));
    }

    #[test]
    fn frac_without_braces_takes_single_tokens() {
        let expected = MathNode::Frac {
            num: Box::new(sym('1', AtomClass::Ord)),
            den: Box::new(sym('2', AtomClass::Ord)),
        };
        assert_eq!(parse_math("\\frac12"), Ok(expected));
    }

    #[test]
    fn x_squared_plus_y_sub_i_parses_both_scripts() {
        let node = parse_math("x^2 + y_i").expect("should parse");
        let MathNode::Row(atoms) = node else {
            panic!("expected a Row, got {node:?}")
        };
        assert_eq!(atoms.len(), 3, "expected [x^2, +, y_i], got {atoms:?}");
        assert_eq!(
            atoms[0],
            MathNode::Script {
                base: Box::new(sym('x', AtomClass::Ord)),
                sup: Some(Box::new(sym('2', AtomClass::Ord))),
                sub: None
            }
        );
        assert_eq!(
            atoms[2],
            MathNode::Script {
                base: Box::new(sym('y', AtomClass::Ord)),
                sup: None,
                sub: Some(Box::new(sym('i', AtomClass::Ord)))
            }
        );
    }

    #[test]
    fn both_scripts_at_once_are_a_single_script_node() {
        let node = parse_math("x_i^2").expect("should parse");
        assert_eq!(
            node,
            MathNode::Script {
                base: Box::new(sym('x', AtomClass::Ord)),
                sup: Some(Box::new(sym('2', AtomClass::Ord))),
                sub: Some(Box::new(sym('i', AtomClass::Ord))),
            }
        );
    }

    #[test]
    fn sqrt_x_plus_1_parses_into_a_sqrt_node() {
        let node = parse_math("\\sqrt{x+1}").expect("should parse");
        let MathNode::Sqrt(inner) = node else {
            panic!("expected Sqrt, got {node:?}")
        };
        assert_eq!(
            *inner,
            MathNode::Row(vec![
                sym('x', AtomClass::Ord),
                sym('+', AtomClass::Bin),
                sym('1', AtomClass::Ord)
            ])
        );
    }

    #[test]
    fn sum_with_limits_parses_lower_and_upper() {
        let node = parse_math("\\sum_{i=1}^{n} i").expect("should parse");
        let MathNode::Row(atoms) = node else {
            panic!("expected Row, got {node:?}")
        };
        let MathNode::BigOp { kind, lower, upper } = &atoms[0] else {
            panic!("expected BigOp first, got {:?}", atoms[0])
        };
        assert_eq!(*kind, BigOpKind::Sum);
        assert!(lower.is_some(), "expected a lower limit");
        assert!(upper.is_some(), "expected an upper limit");
    }

    #[test]
    fn left_paren_right_paren_parses_into_delimited() {
        let node = parse_math("\\left(x\\right)").expect("should parse");
        assert_eq!(
            node,
            MathNode::Delimited {
                open: Some(DelimiterKind::Paren),
                body: Box::new(sym('x', AtomClass::Ord)),
                close: Some(DelimiterKind::Paren),
            }
        );
    }

    #[test]
    fn greek_letters_resolve_via_the_glyph_table() {
        let node = parse_math("\\alpha + \\beta").expect("should parse");
        let MathNode::Row(atoms) = node else {
            panic!("expected Row, got {node:?}")
        };
        assert_eq!(atoms[0], sym('\u{03B1}', AtomClass::Ord));
        assert_eq!(atoms[2], sym('\u{03B2}', AtomClass::Ord));
    }

    #[test]
    fn text_command_preserves_literal_spacing() {
        let node = parse_math("\\text{Hello World}").expect("should parse");
        assert_eq!(node, MathNode::Text("Hello World".to_string()));
    }

    #[test]
    fn an_unknown_command_is_a_clean_error_not_a_panic() {
        assert_eq!(
            parse_math("\\notarealcommand"),
            Err(MathParseError::UnknownCommand(
                "notarealcommand".to_string()
            ))
        );
    }

    #[test]
    fn an_unmatched_left_with_no_right_is_a_clean_error() {
        assert!(matches!(
            parse_math("\\left(x"),
            Err(MathParseError::UnmatchedDelimiter(_))
        ));
    }

    #[test]
    fn a_stray_right_with_no_left_is_a_clean_error() {
        assert!(matches!(
            parse_math("x\\right)"),
            Err(MathParseError::UnmatchedDelimiter(_))
        ));
    }

    #[test]
    fn an_unmatched_brace_is_a_clean_error() {
        assert_eq!(
            parse_math("\\frac{1}{2"),
            Err(MathParseError::UnmatchedBrace)
        );
    }
}