asciimath-parser 0.2.1

A fast extensible memory-efficient asciimath parser
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
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
use crate::tree::{
    Expression, Frac, Func, Group, Intermediate, Matrix, Script, ScriptFunc, Simple, SimpleBinary,
    SimpleFunc, SimpleScript, SimpleUnary,
};
use crate::{Token, Tokenizer};

/// The maximum recursion depth before deeper structure is treated as [missing][Simple::Missing].
///
/// Recursion depth is otherwise linear in the input length, so deeply-nested input like
/// `"sqrt ".repeat(100_000)` would overflow the stack and abort the process.
const MAX_DEPTH: usize = 256;

/// A token paired with the bracket-matching info precomputed for its position.
struct Entry<'a> {
    text: &'a str,
    token: Token,
    /// Matching close-bracket index for an open bracket, or `usize::MAX` if unmatched.
    close: usize,
    /// Whether this open bracket has a top-level separator (more than one column).
    has_sep: bool,
}

/// A recursive-descent parser over a materialized token slice.
///
/// The index cursor makes backtracking a `pos` assignment, and a one-pass precompute of matching
/// brackets makes matrix detection O(1), keeping the parse linear.
struct Parser<'a> {
    entries: Box<[Entry<'a>]>,
    pos: usize,
    /// Current recursion depth, bounded by [`MAX_DEPTH`].
    depth: usize,
}

impl<'a> Parser<'a> {
    fn new(tokens: impl IntoIterator<Item = (&'a str, Token)>) -> Self {
        // collect straight into entries; unmatched open brackets keep close == usize::MAX
        let mut entries: Vec<Entry<'a>> = tokens
            .into_iter()
            .map(|(text, token)| Entry {
                text,
                token,
                close: usize::MAX,
                has_sep: false,
            })
            .collect();
        // one linear pass matches brackets and records top-level separators
        let mut open_stack: Vec<usize> = Vec::new();
        for index in 0..entries.len() {
            match entries[index].token {
                Token::OpenBracket => open_stack.push(index),
                Token::CloseBracket => {
                    if let Some(open) = open_stack.pop() {
                        entries[open].close = index;
                    }
                }
                Token::Sep => {
                    if let Some(&open) = open_stack.last() {
                        entries[open].has_sep = true;
                    }
                }
                _ => {}
            }
        }
        Parser {
            entries: entries.into(),
            pos: 0,
            depth: 0,
        }
    }

    /// Consume and return the next token, advancing the cursor.
    fn advance(&mut self) -> Option<(&'a str, Token)> {
        let item = self
            .entries
            .get(self.pos)
            .map(|entry| (entry.text, entry.token));
        if item.is_some() {
            self.pos += 1;
        }
        item
    }

    fn next_simple(&mut self, stop: Option<Token>) -> Option<Simple<'a>> {
        if self.depth >= MAX_DEPTH {
            return None;
        }
        self.depth += 1;
        let mark = self.pos;
        let result = match self.advance() {
            Some((_, token)) if Some(token) == stop => {
                self.pos = mark; // rewind
                None
            }
            Some((num, Token::Number)) => Some(Simple::Number(num)),
            Some((text, Token::Text)) => Some(Simple::Text(text)),
            Some((ident, Token::Ident)) => Some(Simple::Ident(ident)),
            Some((symb, Token::Symbol)) => Some(Simple::Symbol(symb)),
            Some((unary, Token::Unary)) => {
                Some(SimpleUnary::new(unary, self.next_simple(None).unwrap_or_default()).into())
            }
            Some((func, Token::Function)) => {
                Some(SimpleFunc::new(func, self.next_simple(None).unwrap_or_default()).into())
            }
            Some((binary, Token::Binary)) => Some(
                SimpleBinary::new(
                    binary,
                    self.next_simple(None).unwrap_or_default(),
                    self.next_simple(None).unwrap_or_default(),
                )
                .into(),
            ),
            Some((_, Token::CloseBracket)) => {
                self.pos = mark; // rewind; always stop on close bracket
                None
            }
            Some((open, Token::OpenBracket)) => Some({
                // gate the matrix parse on the precompute; done unconditionally it's exponential
                let matrix = self.could_be_matrix().then(|| {
                    let mark = self.pos;
                    self.next_matrix(open).or_else(|| {
                        self.pos = mark; // rewind before the failed matrix attempt
                        None
                    })
                });
                match matrix {
                    Some(Some(matrix)) => matrix.into(),
                    _ => self.next_open_group(open).into(),
                }
            }),
            Some((open, Token::OpenCloseBracket)) => Some(self.next_open_close_group(open)),
            Some((raw, Token::Frac | Token::Super | Token::Sub | Token::Sep)) => {
                Some(Simple::Symbol(raw))
            }
            None => None,
        };
        self.depth -= 1;
        result
    }

    /// Whether the just-consumed open bracket (at `self.pos - 1`) begins a matrix.
    ///
    /// O(1) via the precomputed tables; only returns `false` when [`next_matrix`][Self::next_matrix]
    /// would certainly fail, so results are unchanged.
    fn could_be_matrix(&self) -> bool {
        let outer_open = self.pos - 1;
        let row_open = self.pos;
        // the first row must itself open with a bracket
        if !self
            .entries
            .get(row_open)
            .is_some_and(|entry| entry.token == Token::OpenBracket)
        {
            return false;
        }
        let row_close = self.entries[row_open].close;
        if row_close >= self.entries.len() {
            return false; // the first row never closes
        }
        let after = row_close + 1;
        if self
            .entries
            .get(after)
            .is_some_and(|entry| entry.token == Token::Sep)
        {
            true // a separator implies a second row
        } else if after == self.entries[outer_open].close {
            self.entries[row_open].has_sep // single row: a matrix only with more than one column
        } else {
            false
        }
    }

    fn next_open_group(&mut self, open: &'a str) -> Group<'a> {
        let expr = self.next_expression(None);
        let mark = self.pos;
        let close = if let Some((bracket, Token::CloseBracket)) = self.advance() {
            bracket
        } else {
            // unterminated (EOF or depth-capped): rewind and close with an empty bracket
            self.pos = mark; // rewind
            ""
        };
        Group::new(open, expr, close)
    }

    fn next_open_close_group(&mut self, open: &'a str) -> Simple<'a> {
        let mark = self.pos;
        if let Some(first) = self.next_intermediate(None) {
            // take the first intermediate, even if it's another OpenCloseBracket
            let mut inters = vec![first];
            while let Some(inter) = self.next_intermediate(Some(Token::OpenCloseBracket)) {
                inters.push(inter);
            }
            if let Some((close, Token::OpenCloseBracket)) = self.advance() {
                Simple::Group(Group::new(open, inters, close))
            } else {
                // couldn't match the left-right bracket, so rewind and treat it as a symbol
                self.pos = mark; // rewind
                Simple::Symbol(open)
            }
        } else {
            // empty so must return symbol
            Simple::Symbol(open)
        }
    }

    fn next_expression(&mut self, stop: Option<Token>) -> Expression<'a> {
        let mut inters = Vec::new();
        while let Some(inter) = self.next_intermediate(stop) {
            inters.push(inter);
        }
        inters.into()
    }

    fn next_matrix_row(
        &mut self,
        exprs: &mut impl Extend<Expression<'a>>,
    ) -> Option<(&'a str, usize, &'a str)> {
        let open = match self.advance() {
            Some((open, Token::OpenBracket)) => Some(open),
            _ => None,
        }?;
        let mut len = 1;
        exprs.extend([self.next_expression(Some(Token::Sep))]);
        loop {
            match self.advance() {
                Some((_, Token::Sep)) => {
                    exprs.extend([self.next_expression(Some(Token::Sep))]);
                    len += 1;
                }
                Some((close, Token::CloseBracket)) => return Some((open, len, close)),
                _ => return None,
            }
        }
    }

    fn next_matrix(&mut self, left: &'a str) -> Option<Matrix<'a>> {
        let mut data = Vec::new();
        let (open, num_cols, close) = self.next_matrix_row(&mut data)?;
        loop {
            match self.advance() {
                Some((_, Token::Sep)) => {
                    let (no, ncols, nc) = self.next_matrix_row(&mut data)?;
                    if no != open || ncols != num_cols || nc != close {
                        return None;
                    }
                }
                Some((right, Token::CloseBracket))
                    if data.len() > 1 && open == left && close == right =>
                {
                    return Some(Matrix::new(left, data, num_cols, right));
                }
                _ => return None,
            }
        }
    }

    fn next_script(&mut self) -> Script<'a> {
        let mark = self.pos;
        match self.advance() {
            Some((_, Token::Super)) => Script::Super(self.next_simple(None).unwrap_or_default()),
            Some((_, Token::Sub)) => {
                let sub = self.next_simple(None).unwrap_or_default();
                let mark = self.pos;
                if let Some((_, Token::Super)) = self.advance() {
                    Script::Subsuper(sub, self.next_simple(None).unwrap_or_default())
                } else {
                    self.pos = mark; // rewind
                    Script::Sub(sub)
                }
            }
            _ => {
                self.pos = mark; // rewind
                Script::None
            }
        }
    }

    fn next_script_func(&mut self, stop: Option<Token>) -> Option<ScriptFunc<'a>> {
        if self.depth >= MAX_DEPTH {
            return None;
        }
        self.depth += 1;
        let mark = self.pos;
        let result = if let Some((func, Token::Function)) = self.advance() {
            Some(
                Func::new(
                    func,
                    self.next_script(),
                    self.next_script_func(None).unwrap_or_default(),
                )
                .into(),
            )
        } else {
            self.pos = mark; // rewind
            self.next_simple(stop)
                .map(|simp| SimpleScript::new(simp, self.next_script()).into())
        };
        self.depth -= 1;
        result
    }

    fn next_intermediate(&mut self, stop: Option<Token>) -> Option<Intermediate<'a>> {
        let base = self.next_script_func(stop)?;
        let mark = self.pos;
        if let Some((_, Token::Frac)) = self.advance() {
            Some(Intermediate::Frac(Frac::new(
                base,
                self.next_script_func(None).unwrap_or_default(),
            )))
        } else {
            self.pos = mark; // rewind
            Some(Intermediate::ScriptFunc(base))
        }
    }

    fn parse(&mut self) -> Expression<'a> {
        let mut inters = Vec::new();
        let mut wraps = 0;
        loop {
            while let Some(inter) = self.next_intermediate(None) {
                inters.push(inter);
            }
            match self.advance() {
                Some((close, Token::CloseBracket)) => {
                    // cap the invisible-group nesting so the tree stays bounded; drop excess closes
                    if wraps < MAX_DEPTH {
                        let group = Simple::Group(Group::new("", inters, close));
                        inters = vec![group.into()];
                        wraps += 1;
                    }
                }
                other => {
                    // NOTE this can still hide errors if the last token is unexpected
                    debug_assert!(other.is_none(), "didn't exhaust tokens");
                    break;
                }
            }
        }
        Expression::from(inters)
    }
}

/// Parse a tokenized expression
pub fn parse_tokens<'a, T>(tokens: T) -> Expression<'a>
where
    T: IntoIterator<Item = (&'a str, Token)>,
{
    Parser::new(tokens).parse()
}

/// Parse a string returning an asciimath expression
///
/// This uses an extended set of asciimath tokens that are accessible in [`crate::ASCIIMATH_TOKENS`].
#[must_use]
pub fn parse(inp: &str) -> Expression<'_> {
    parse_tokens(Tokenizer::new(inp))
}

#[cfg(test)]
mod tests {
    use crate::tree::{
        Expression, Frac, Func, Group, Intermediate, Matrix, Simple, SimpleBinary, SimpleFunc,
        SimpleScript, SimpleUnary,
    };

    #[test]
    fn complex_precedence() {
        let expr = super::parse("sin_a^b c_d / (abs h)_i^j");
        let expected = [Frac::new(
            Func::with_subsuper(
                "sin",
                Simple::Ident("a"),
                Simple::Ident("b"),
                SimpleScript::with_sub(Simple::Ident("c"), Simple::Ident("d")),
            ),
            SimpleScript::with_subsuper(
                Group::from_iter("(", [SimpleUnary::new("abs", Simple::Ident("h"))], ")"),
                Simple::Ident("i"),
                Simple::Ident("j"),
            ),
        )]
        .into_iter()
        .collect();
        assert_eq!(expr, expected);
    }

    #[test]
    fn missing_sub() {
        let expr = super::parse("a_");
        let expected =
            Expression::from_iter([SimpleScript::with_sub(Simple::Ident("a"), Simple::Missing)]);
        assert_eq!(expr, expected);
    }

    #[test]
    fn missing_super() {
        let expr = super::parse("a^");
        let expected = [SimpleScript::with_super(
            Simple::Ident("a"),
            Simple::Missing,
        )]
        .into_iter()
        .collect();
        assert_eq!(expr, expected);
    }

    #[test]
    fn missing_group_subsuper() {
        // NOTE crashes asciimath
        let expr = super::parse("(a_b^)");
        let expected = [Group::from_iter(
            "(",
            [SimpleScript::with_subsuper(
                Simple::Ident("a"),
                Simple::Ident("b"),
                Simple::Missing,
            )],
            ")",
        )]
        .into_iter()
        .collect();
        assert_eq!(expr, expected);
    }

    #[test]
    fn missing_group_unary() {
        // NOTE crashes asciimath
        let expr = super::parse("(sqrt)");
        let expected = [Group::from_iter(
            "(",
            [SimpleUnary::new("sqrt", Simple::Missing)],
            ")",
        )]
        .into_iter()
        .collect();
        assert_eq!(expr, expected);
    }

    #[test]
    fn unmatched_close() {
        let expr = super::parse(")");
        let expected = [Group::new("", Expression::default(), ")")]
            .into_iter()
            .collect();
        assert_eq!(expr, expected);
    }

    #[test]
    fn simple_bracket_matching() {
        let expr = super::parse("|a|");
        let expected = [Group::from_iter("|", [Simple::Ident("a")], "|")]
            .into_iter()
            .collect();
        assert_eq!(expr, expected);
    }

    #[test]
    fn eager_bracket_matching() {
        let expr = super::parse("|a|b|c|"); // "|:a:|b|:c:|"
        let expected = [
            Group::from_iter("|", [Simple::Ident("a")], "|").into(),
            Simple::Ident("b"),
            Group::from_iter("|", [Simple::Ident("c")], "|").into(),
        ]
        .into_iter()
        .collect();
        assert_eq!(expr, expected);
    }

    #[test]
    fn close_bracket_matching() {
        let expr = super::parse("(a|b)c|d"); // "(:a|b:)c|d" not "(a|:b)c:|d"
        let expected = [
            Group::from_iter(
                "(",
                [Simple::Ident("a"), Simple::Symbol("|"), Simple::Ident("b")],
                ")",
            )
            .into(),
            Simple::Ident("c"),
            Simple::Symbol("|"),
            Simple::Ident("d"),
        ]
        .into_iter()
        .collect();
        assert_eq!(expr, expected);
    }

    #[test]
    fn open_close_nonempty() {
        let expr = super::parse("| |");
        let expected = [Simple::Symbol("|"), Simple::Symbol("|")]
            .into_iter()
            .collect();
        assert_eq!(expr, expected);
    }

    #[test]
    fn double_open_close() {
        let expr = super::parse("||x||");
        let expected = Expression::from_iter([Group::from_iter("||", [Simple::Ident("x")], "||")]);
        assert_eq!(expr, expected);
    }

    #[test]
    fn simple_function() {
        let expr = super::parse("sin x");
        let expected = [Func::without_scripts("sin", Simple::Ident("x"))]
            .into_iter()
            .collect();
        assert_eq!(expr, expected);
    }

    #[test]
    fn complex_function() {
        let expr = super::parse("sin_cos a cos^b c");
        let expected = [Func::with_sub(
            "sin",
            SimpleFunc::new("cos", Simple::Ident("a")),
            Func::with_super("cos", Simple::Ident("b"), Simple::Ident("c")),
        )]
        .into_iter()
        .collect();
        assert_eq!(expr, expected);
    }

    #[test]
    fn unary_power_precidence() {
        let expr = super::parse("sin_a b^c / d");
        let expected = [Intermediate::Frac(Frac::new(
            Func::with_sub(
                "sin",
                Simple::Ident("a"),
                SimpleScript::with_super(Simple::Ident("b"), Simple::Ident("c")),
            ),
            Simple::Ident("d"),
        ))]
        .into();
        assert_eq!(expr, expected);
    }

    #[test]
    fn matrix_parsing() {
        let expr = super::parse("[[a, b], [c, d]]");
        let expected = [Matrix::new(
            "[",
            [
                [Simple::Ident("a")].into_iter().collect(),
                [Simple::Ident("b")].into_iter().collect(),
                [Simple::Ident("c")].into_iter().collect(),
                [Simple::Ident("d")].into_iter().collect(),
            ],
            2,
            "]",
        )]
        .into_iter()
        .collect();
        assert_eq!(expr, expected);
    }

    #[test]
    fn no_singleton_matrix() {
        let expr = super::parse("[[a]]");
        let expected = [Group::from_iter(
            "[",
            [Group::from_iter("[", [Simple::Ident("a")], "]")],
            "]",
        )]
        .into_iter()
        .collect();
        assert_eq!(expr, expected);
    }

    #[test]
    fn sets_as_groups() {
        // asciimath treats sets special, here we opt to make matrix parsing a little more strict
        // to avoid the possibility
        let expr = super::parse("{(x, y), (a, b)}");
        let expected = [Group::from_iter(
            "{",
            [
                Group::from_iter(
                    "(",
                    [Simple::Ident("x"), Simple::Symbol(","), Simple::Ident("y")],
                    ")",
                )
                .into(),
                Simple::Symbol(","),
                Group::from_iter(
                    "(",
                    [Simple::Ident("a"), Simple::Symbol(","), Simple::Ident("b")],
                    ")",
                )
                .into(),
            ],
            "}",
        )]
        .into_iter()
        .collect();
        assert_eq!(expr, expected);
    }

    #[test]
    fn simple_binary() {
        let expr = super::parse("root 3");
        let expected = [SimpleBinary::new(
            "root",
            Simple::Number("3"),
            Simple::Missing,
        )]
        .into_iter()
        .collect();
        assert_eq!(expr, expected);
    }

    #[test]
    fn raw_text() {
        let expr = super::parse(r#""raw text""#);
        let expected = Expression::from_iter([Simple::Text("raw text")]);
        assert_eq!(expr, expected);
    }

    #[test]
    fn bare_symbol() {
        let expr = super::parse("alpha");
        let expected = Expression::from_iter([Simple::Symbol("alpha")]);
        assert_eq!(expr, expected);
    }

    #[test]
    fn open_close_multiple_intermediates() {
        // a left-right bracket group with more than one intermediate inside
        let expr = super::parse("|a b|");
        let expected = [Group::from_iter(
            "|",
            [Simple::Ident("a"), Simple::Ident("b")],
            "|",
        )]
        .into_iter()
        .collect();
        assert_eq!(expr, expected);
    }

    #[test]
    fn unclosed_groups() {
        // brackets that never close fall back to groups with an empty closing bracket
        let expr = super::parse("[[a");
        let expected = [Group::from_iter(
            "[",
            [Group::from_iter("[", [Simple::Ident("a")], "")],
            "",
        )]
        .into_iter()
        .collect();
        assert_eq!(expr, expected);
    }

    #[test]
    fn deep_nested_brackets_are_not_exponential() {
        // nested brackets used to be exponential (this would hang); it must be linear now
        let depth = 150;
        let input = format!("{}a{}", "(".repeat(depth), ")".repeat(depth));
        let expr = super::parse(&input);
        assert_eq!(expr.len(), 1);
    }

    #[test]
    fn deep_unary_chain_does_not_overflow() {
        // a deep unary chain must not overflow the stack (recurses via next_simple)
        let input = "sqrt ".repeat(100_000);
        let expr = super::parse(&input);
        assert!(!expr.is_empty());
    }

    #[test]
    fn deep_function_chain_does_not_overflow() {
        // a deep function chain must not overflow the stack (recurses via next_script_func)
        let input = "sin ".repeat(100_000);
        let expr = super::parse(&input);
        assert!(!expr.is_empty());
    }

    #[test]
    fn many_unmatched_closes_are_capped() {
        // capped to a bounded tree, so clone/compare/drop are all safe
        let input = ")".repeat(200_000);
        let expr = super::parse(&input);
        assert_eq!(expr.len(), 1);
        let cloned = expr.clone();
        assert_eq!(expr, cloned);
    }

    #[test]
    fn ragged_matrix_is_group() {
        // mismatched column counts mean the second row doesn't match, so it isn't a matrix
        let expr = super::parse("[[a, b], [c]]");
        let expected = [Group::from_iter(
            "[",
            [
                Group::from_iter(
                    "[",
                    [Simple::Ident("a"), Simple::Symbol(","), Simple::Ident("b")],
                    "]",
                )
                .into(),
                Simple::Symbol(","),
                Group::from_iter("[", [Simple::Ident("c")], "]").into(),
            ],
            "]",
        )]
        .into_iter()
        .collect();
        assert_eq!(expr, expected);
    }

    #[test]
    fn single_row_matrix() {
        // a single bracketed row with more than one column is still a matrix (total cells > 1)
        let expr = super::parse("[[a, b]]");
        let expected = [Matrix::new(
            "[",
            [
                [Simple::Ident("a")].into_iter().collect(),
                [Simple::Ident("b")].into_iter().collect(),
            ],
            2,
            "]",
        )]
        .into_iter()
        .collect();
        assert_eq!(expr, expected);
    }

    #[test]
    fn matrix_candidate_with_trailing_tokens_is_group() {
        // a row followed by a non-separator token can't be a matrix, so it stays a group
        let expr = super::parse("[[a] b]");
        let expected = [Group::from_iter(
            "[",
            [
                Group::from_iter("[", [Simple::Ident("a")], "]").into(),
                Simple::Ident("b"),
            ],
            "]",
        )]
        .into_iter()
        .collect();
        assert_eq!(expr, expected);
    }

    #[test]
    fn matrix_row_with_bar() {
        // a "|" inside a matrix row is just a symbol in that cell; the rows still parse as a matrix
        let expr = super::parse("[[a|b],[c|d]]");
        let expected = [Matrix::new(
            "[",
            [
                [Simple::Ident("a"), Simple::Symbol("|"), Simple::Ident("b")]
                    .into_iter()
                    .collect(),
                [Simple::Ident("c"), Simple::Symbol("|"), Simple::Ident("d")]
                    .into_iter()
                    .collect(),
            ],
            1,
            "]",
        )]
        .into_iter()
        .collect();
        assert_eq!(expr, expected);
    }
}