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
//! The module containing all of the structures that defined a parsed representation of asciimath
//!
//! - [`Expression`] - A full expression containing a sequence of intermediate expressions
//! - [`Intermediate`] - The most complicated single expression, this can be a high level fraction
//! - [`Frac`] - A high level fraction, often parsed with a `/`
//! - [`ScriptFunc`] - A scripted expression that can be a [`Func`] or just a simple expression
//! - [`Func`] - A function like `sin` that can contain independent super- and subscripts
//!       prior to its argument
//! - [`SimpleScript`] - A simple expression that has super- and subscripts
//! - [`Simple`] - A simple expression like a [`Symbol`][Simple::Symbol] or
//!       [`Ident`][Simple::Ident]ifier
//!
//! The exceptions to this hierarchy are [`Group`] and [`Matrix`] that "reset" the hierarchy by
//! wrapping expressions.
use std::iter::FusedIterator;
use std::ops::{Deref, Index};
use std::slice::ChunksExact;

/// A unary operator like "sqrt"
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SimpleUnary<'a> {
    /// The operator name
    pub op: &'a str,
    /// The operator argument
    arg: Box<Simple<'a>>,
}

impl<'a> SimpleUnary<'a> {
    /// Create a unary with its op and argument
    pub fn new<S>(op: &'a str, arg: S) -> Self
    where
        S: Into<Simple<'a>>,
    {
        SimpleUnary {
            op,
            arg: Box::new(arg.into()),
        }
    }

    /// The operator argument
    pub fn arg(&self) -> &Simple<'a> {
        &self.arg
    }
}

/// A simple func like "sin"
///
/// When encountering a func keyword while parsing a simple context, it can't have attached power
/// arguments or complicated arguments, but it will still be parsed as a unary function.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SimpleFunc<'a> {
    /// The function name
    pub func: &'a str,
    /// The function argument
    arg: Box<Simple<'a>>,
}

impl<'a> SimpleFunc<'a> {
    /// Create a simple function from its name and argument
    pub fn new<S>(func: &'a str, arg: S) -> Self
    where
        S: Into<Simple<'a>>,
    {
        SimpleFunc {
            func,
            arg: Box::new(arg.into()),
        }
    }

    /// The function argument
    pub fn arg(&self) -> &Simple<'a> {
        &self.arg
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
/// A binary operator like "root"
pub struct SimpleBinary<'a> {
    /// The operator name
    pub op: &'a str,
    /// The first operator argument
    first: Box<Simple<'a>>,
    /// The second operator argument
    second: Box<Simple<'a>>,
}

impl<'a> SimpleBinary<'a> {
    /// Create a binary operator with its name and both arguments
    pub fn new<F, S>(op: &'a str, first: F, second: S) -> Self
    where
        F: Into<Simple<'a>>,
        S: Into<Simple<'a>>,
    {
        SimpleBinary {
            op,
            first: Box::new(first.into()),
            second: Box::new(second.into()),
        }
    }

    /// The first operator argument
    pub fn first(&self) -> &Simple<'a> {
        &self.first
    }

    /// The second operator argument
    pub fn second(&self) -> &Simple<'a> {
        &self.second
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
/// A bracketd group that allows inserting complicated expressions in simplex contexts
///
/// In some instances a bracket won't be parsed to close a group out, in that case the bracket will
/// be the empty string.
pub struct Group<'a> {
    /// The left bracket
    pub left_bracket: &'a str,
    /// The grouped expression
    pub expr: Expression<'a>,
    /// The right bracket
    pub right_bracket: &'a str,
}

impl<'a> Group<'a> {
    /// Create a new group with two brackets and an expression
    pub fn new<E: Into<Expression<'a>>>(
        left_bracket: &'a str,
        expr: E,
        right_bracket: &'a str,
    ) -> Self {
        Group {
            left_bracket,
            expr: expr.into(),
            right_bracket,
        }
    }

    /// Create a new bracket with an iterable of intermediates instead of a fully formed expression
    pub fn from_iter<T, I>(left_bracket: &'a str, inters: T, right_bracket: &'a str) -> Self
    where
        T: IntoIterator<Item = I>,
        I: Into<Intermediate<'a>>,
    {
        Group {
            left_bracket,
            expr: inters.into_iter().collect(),
            right_bracket,
        }
    }
}

/// A matrix e.g. "[[a, b], [x, y]]"
///
/// Individual expressions can be accessed with [rows][Matrix::rows] to get a random access iterator of row
/// slices, or by indexing with a 2d array of indices, e.g. `matrix[[0, 0]]`
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Matrix<'a> {
    /// The matrix's left bracket
    pub left_bracket: &'a str,
    /// The cells in the matrix in colum-wise order
    cells: Box<[Expression<'a>]>,
    /// The number of columns in the matrix
    num_cols: usize,
    /// The matrix's right bracket
    pub right_bracket: &'a str,
}

impl<'a> Matrix<'a> {
    /// Create a new matrix
    ///
    /// `cells` is a slice of expressiongs in row-major top-down order. `num_cols` is how many
    /// columns exist in the final matrix.
    ///
    /// # Panics
    /// When `cells.into().len()` is not divisible by `num_cols`.
    pub fn new<E>(left_bracket: &'a str, cells: E, num_cols: usize, right_bracket: &'a str) -> Self
    where
        E: Into<Box<[Expression<'a>]>>,
    {
        let cells = cells.into();
        assert_eq!(cells.len() / num_cols * num_cols, cells.len());
        Matrix {
            left_bracket,
            right_bracket,
            cells,
            num_cols,
        }
    }

    /// The number of columns
    pub fn num_cols(&self) -> usize {
        self.num_cols
    }

    /// The number of rows
    pub fn num_rows(&self) -> usize {
        self.cells.len() / self.num_cols
    }

    /// The number of total cells
    pub fn num_cells(&self) -> usize {
        self.cells.len()
    }

    /// A top-down iterator over rows as slices
    pub fn rows(&self) -> MatrixRows<'a, '_> {
        MatrixRows(self.cells.chunks_exact(self.num_cols))
    }
}

/// Matrix references iterate over rows
impl<'a, 'b> IntoIterator for &'b Matrix<'a> {
    type IntoIter = MatrixRows<'a, 'b>;
    type Item = &'b [Expression<'a>];

    fn into_iter(self) -> Self::IntoIter {
        self.rows()
    }
}

/// usize indices get rows
impl<'a> Index<usize> for Matrix<'a> {
    type Output = [Expression<'a>];

    /// Get an individual expression
    ///
    /// # Panics
    /// When index is out of bounds `idx >= num_rows`
    fn index(&self, row: usize) -> &Self::Output {
        self.rows().nth(row).expect("index out of bounds")
    }
}

/// 2D indices get individual expressions
impl<'a> Index<[usize; 2]> for Matrix<'a> {
    type Output = Expression<'a>;

    /// Get an individual expression
    ///
    /// # Panics
    /// When indices are out of bounds `[x, y]`, `x >= num_cols`, `y >= num_rows`
    fn index(&self, idx: [usize; 2]) -> &Self::Output {
        let [x, y] = idx;
        assert!(x < self.num_cols, "index out of bounds");
        &self.cells[x + self.num_cols * y]
    }
}

/// An iterator over rows of a matrix
#[derive(Debug, Clone)]
pub struct MatrixRows<'a, 'b>(ChunksExact<'b, Expression<'a>>);

impl<'a, 'b> Iterator for MatrixRows<'a, 'b> {
    type Item = &'b [Expression<'a>];

    fn next(&mut self) -> Option<Self::Item> {
        self.0.next()
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        self.0.size_hint()
    }

    fn count(self) -> usize {
        self.len()
    }

    fn nth(&mut self, n: usize) -> Option<Self::Item> {
        self.0.nth(n)
    }

    fn last(mut self) -> Option<Self::Item> {
        self.next_back()
    }
}
impl<'a, 'b> FusedIterator for MatrixRows<'a, 'b> {}

impl<'a, 'b> DoubleEndedIterator for MatrixRows<'a, 'b> {
    fn next_back(&mut self) -> Option<Self::Item> {
        self.0.next_back()
    }

    fn nth_back(&mut self, n: usize) -> Option<Self::Item> {
        self.0.nth_back(n)
    }
}

impl<'a, 'b> ExactSizeIterator for MatrixRows<'a, 'b> {}

/// A simple element of parsing
///
/// This is the lowest level in the parse tree, but can be scaled back up with a group or matrix
#[derive(Default, Debug, Clone, PartialEq, Eq)]
pub enum Simple<'a> {
    /// A missing expression
    ///
    /// This is found in places where something didn't exist, e.g. "sin" by itself will have a
    /// missing argument, but still be parsed.
    #[default]
    Missing,
    /// A raw number
    Number(&'a str),
    /// Raw text
    Text(&'a str),
    /// An identity, usually a single character of something that doesn't have asciimath meaning
    Ident(&'a str),
    /// A recognized symbol
    Symbol(&'a str),
    /// A unary operator
    Unary(SimpleUnary<'a>),
    /// A simple unary function
    Func(SimpleFunc<'a>),
    /// A binary function
    Binary(SimpleBinary<'a>),
    /// A bracketed group
    Group(Group<'a>),
    /// A matrix
    Matrix(Matrix<'a>),
}

impl<'a> Simple<'a> {
    /// Get as an option where Missing is None
    pub fn as_option(&self) -> Option<&Self> {
        match self {
            Simple::Missing => None,
            simple => Some(simple),
        }
    }
}

// macro to derive from for component types
macro_rules! simple_from {
    ($from:ty => $to:ident) => {
        impl<'a> From<$from> for Simple<'a> {
            fn from(inp: $from) -> Self {
                Simple::$to(inp)
            }
        }
    };
}

simple_from!(SimpleUnary<'a> => Unary);
simple_from!(SimpleFunc<'a> => Func);
simple_from!(SimpleBinary<'a> => Binary);
simple_from!(Group<'a> => Group);
simple_from!(Matrix<'a> => Matrix);

/// scripts attached to some other object
///
/// Note that an object can have a script attached, but the corresponding [Simple] expression can
/// still be [missing][Simple::Missing]. Objects with Scripts Deref to them so these raw objects
/// probably aren't necessary.
#[derive(Default, Debug, Clone, PartialEq, Eq)]
pub enum Script<'a> {
    /// No super or subscripts
    #[default]
    None,
    /// Only a subscript
    Sub(Simple<'a>),
    /// Only a superscript
    Super(Simple<'a>),
    /// A sub and superscript
    Subsuper(Simple<'a>, Simple<'a>),
}

impl<'a> Script<'a> {
    /// Get the subscript
    pub fn sub(&self) -> Option<&Simple<'a>> {
        match self {
            Script::Sub(sub) => Some(sub),
            Script::Subsuper(sub, _) => Some(sub),
            _ => None,
        }
    }

    /// Get the superscript
    pub fn sup(&self) -> Option<&Simple<'a>> {
        match self {
            Script::Super(sup) => Some(sup),
            Script::Subsuper(_, sup) => Some(sup),
            _ => None,
        }
    }
}

/// A simple expressiong with attached scripts
#[derive(Default, Debug, Clone, PartialEq, Eq)]
pub struct SimpleScript<'a> {
    /// The simple argument
    pub simple: Simple<'a>,
    /// Any script modifications
    pub script: Script<'a>,
}

impl<'a> SimpleScript<'a> {
    /// Create a new simple script with a simple and script
    pub fn new<S>(simple: S, script: Script<'a>) -> Self
    where
        S: Into<Simple<'a>>,
    {
        SimpleScript {
            simple: simple.into(),
            script,
        }
    }

    /// Create an unscripted simple expression
    pub fn without_scripts<S>(simple: S) -> Self
    where
        S: Into<Simple<'a>>,
    {
        Self::new(simple, Script::None)
    }

    /// Create a simple script with a subscript
    pub fn with_sub<S, Sub>(simple: S, sub: Sub) -> Self
    where
        S: Into<Simple<'a>>,
        Sub: Into<Simple<'a>>,
    {
        Self::new(simple, Script::Sub(sub.into()))
    }

    /// Create a simple script with a super script
    pub fn with_super<S, Sup>(simple: S, sup: Sup) -> Self
    where
        S: Into<Simple<'a>>,
        Sup: Into<Simple<'a>>,
    {
        Self::new(simple, Script::Super(sup.into()))
    }

    /// Create a simple script with sub and super scripts
    pub fn with_subsuper<S, Sub, Sup>(simple: S, sub: Sub, sup: Sup) -> Self
    where
        S: Into<Simple<'a>>,
        Sub: Into<Simple<'a>>,
        Sup: Into<Simple<'a>>,
    {
        Self::new(simple, Script::Subsuper(sub.into(), sup.into()))
    }
}

impl<'a, S> From<S> for SimpleScript<'a>
where
    S: Into<Simple<'a>>,
{
    fn from(inp: S) -> Self {
        SimpleScript::without_scripts(inp.into())
    }
}

/// Add sub and super
impl<'a> Deref for SimpleScript<'a> {
    type Target = Script<'a>;

    fn deref(&self) -> &Self::Target {
        &self.script
    }
}

/// A full function that has complicated arguments and attached scripts
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Func<'a> {
    /// The function name
    pub func: &'a str,
    /// Any script modifications of the function
    pub script: Script<'a>,
    /// The function argument
    arg: Box<ScriptFunc<'a>>,
}

impl<'a> Func<'a> {
    /// Create a new function with all attached parts
    pub fn new<Arg>(func: &'a str, script: Script<'a>, arg: Arg) -> Self
    where
        Arg: Into<ScriptFunc<'a>>,
    {
        Func {
            func,
            script,
            arg: Box::new(arg.into()),
        }
    }

    /// Create a new function without scripts
    pub fn without_scripts<Arg>(func: &'a str, arg: Arg) -> Self
    where
        Arg: Into<ScriptFunc<'a>>,
    {
        Self::new(func, Script::None, arg)
    }

    /// Create a new function with subscripts
    pub fn with_sub<Sub, Arg>(func: &'a str, sub: Sub, arg: Arg) -> Self
    where
        Sub: Into<Simple<'a>>,
        Arg: Into<ScriptFunc<'a>>,
    {
        Self::new(func, Script::Sub(sub.into()), arg)
    }

    /// Create a new function with superscripts
    pub fn with_super<Sup, Arg>(func: &'a str, sup: Sup, arg: Arg) -> Self
    where
        Sup: Into<Simple<'a>>,
        Arg: Into<ScriptFunc<'a>>,
    {
        Self::new(func, Script::Super(sup.into()), arg)
    }

    /// Create a new function with sub and superscripts
    pub fn with_subsuper<Sub, Sup, Arg>(func: &'a str, sub: Sub, sup: Sup, arg: Arg) -> Self
    where
        Sub: Into<Simple<'a>>,
        Sup: Into<Simple<'a>>,
        Arg: Into<ScriptFunc<'a>>,
    {
        Self::new(func, Script::Subsuper(sub.into(), sup.into()), arg)
    }

    /// The function argument
    pub fn arg(&self) -> &ScriptFunc<'a> {
        &self.arg
    }
}

impl<'a> Deref for Func<'a> {
    type Target = Script<'a>;

    fn deref(&self) -> &Self::Target {
        &self.script
    }
}

/// A scripted object or a scripted function
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ScriptFunc<'a> {
    /// A scripted simple expression
    Simple(SimpleScript<'a>),
    /// A function with sub abd superscripts
    Func(Func<'a>),
}

impl<'a> Default for ScriptFunc<'a> {
    fn default() -> Self {
        ScriptFunc::Simple(SimpleScript::default())
    }
}

impl<'a> From<Func<'a>> for ScriptFunc<'a> {
    fn from(func: Func<'a>) -> Self {
        ScriptFunc::Func(func)
    }
}

impl<'a, S> From<S> for ScriptFunc<'a>
where
    S: Into<SimpleScript<'a>>,
{
    fn from(inp: S) -> Self {
        ScriptFunc::Simple(inp.into())
    }
}

/// A high level fraction
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Frac<'a> {
    /// The numerator
    pub numer: ScriptFunc<'a>,
    /// The denominator
    pub denom: ScriptFunc<'a>,
}

impl<'a> Frac<'a> {
    /// Create a high level fraction
    pub fn new<N, D>(numer: N, denom: D) -> Self
    where
        N: Into<ScriptFunc<'a>>,
        D: Into<ScriptFunc<'a>>,
    {
        Frac {
            numer: numer.into(),
            denom: denom.into(),
        }
    }
}

/// An intermediate expression
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Intermediate<'a> {
    /// A simple scripted object
    ScriptFunc(ScriptFunc<'a>),
    /// A fraction between scripted objects
    Frac(Frac<'a>),
}

impl<'a> Default for Intermediate<'a> {
    fn default() -> Self {
        Intermediate::ScriptFunc(ScriptFunc::default())
    }
}

impl<'a> From<Frac<'a>> for Intermediate<'a> {
    fn from(frac: Frac<'a>) -> Self {
        Intermediate::Frac(frac)
    }
}

impl<'a, S> From<S> for Intermediate<'a>
where
    S: Into<ScriptFunc<'a>>,
{
    fn from(inp: S) -> Self {
        Intermediate::ScriptFunc(inp.into())
    }
}

/// A full expression
///
/// This is a sequence of intermediate expressions and Derefs to a slice of them.
#[derive(Default, Debug, Clone, PartialEq, Eq)]
pub struct Expression<'a>(Box<[Intermediate<'a>]>);

impl<'a> Deref for Expression<'a> {
    type Target = [Intermediate<'a>];

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl<'a, I> FromIterator<I> for Expression<'a>
where
    I: Into<Intermediate<'a>>,
{
    fn from_iter<T>(iter: T) -> Self
    where
        T: IntoIterator<Item = I>,
    {
        Expression(iter.into_iter().map(|v| v.into()).collect())
    }
}

impl<'a, B> From<B> for Expression<'a>
where
    B: Into<Box<[Intermediate<'a>]>>,
{
    fn from(inp: B) -> Self {
        Expression(inp.into())
    }
}