1use std::iter::FusedIterator;
16use std::ops::{Deref, Index};
17use std::slice::ChunksExact;
18
19#[derive(Debug, Clone, PartialEq, Eq)]
21pub struct SimpleUnary<'a> {
22 pub op: &'a str,
24 arg: Box<Simple<'a>>,
26}
27
28impl<'a> SimpleUnary<'a> {
29 pub fn new<S>(op: &'a str, arg: S) -> Self
31 where
32 S: Into<Simple<'a>>,
33 {
34 SimpleUnary {
35 op,
36 arg: Box::new(arg.into()),
37 }
38 }
39
40 #[must_use]
42 pub fn arg(&self) -> &Simple<'a> {
43 &self.arg
44 }
45}
46
47#[derive(Debug, Clone, PartialEq, Eq)]
52pub struct SimpleFunc<'a> {
53 pub func: &'a str,
55 arg: Box<Simple<'a>>,
57}
58
59impl<'a> SimpleFunc<'a> {
60 pub fn new<S>(func: &'a str, arg: S) -> Self
62 where
63 S: Into<Simple<'a>>,
64 {
65 SimpleFunc {
66 func,
67 arg: Box::new(arg.into()),
68 }
69 }
70
71 #[must_use]
73 pub fn arg(&self) -> &Simple<'a> {
74 &self.arg
75 }
76}
77
78#[derive(Debug, Clone, PartialEq, Eq)]
79pub struct SimpleBinary<'a> {
81 pub op: &'a str,
83 first: Box<Simple<'a>>,
85 second: Box<Simple<'a>>,
87}
88
89impl<'a> SimpleBinary<'a> {
90 pub fn new<F, S>(op: &'a str, first: F, second: S) -> Self
92 where
93 F: Into<Simple<'a>>,
94 S: Into<Simple<'a>>,
95 {
96 SimpleBinary {
97 op,
98 first: Box::new(first.into()),
99 second: Box::new(second.into()),
100 }
101 }
102
103 #[must_use]
105 pub fn first(&self) -> &Simple<'a> {
106 &self.first
107 }
108
109 #[must_use]
111 pub fn second(&self) -> &Simple<'a> {
112 &self.second
113 }
114}
115
116#[derive(Debug, Clone, PartialEq, Eq)]
117pub struct Group<'a> {
122 pub left_bracket: &'a str,
124 pub expr: Expression<'a>,
126 pub right_bracket: &'a str,
128}
129
130impl<'a> Group<'a> {
131 pub fn new<E: Into<Expression<'a>>>(
133 left_bracket: &'a str,
134 expr: E,
135 right_bracket: &'a str,
136 ) -> Self {
137 Group {
138 left_bracket,
139 expr: expr.into(),
140 right_bracket,
141 }
142 }
143
144 pub fn from_iter<T, I>(left_bracket: &'a str, inters: T, right_bracket: &'a str) -> Self
146 where
147 T: IntoIterator<Item = I>,
148 I: Into<Intermediate<'a>>,
149 {
150 Group {
151 left_bracket,
152 expr: inters.into_iter().collect(),
153 right_bracket,
154 }
155 }
156}
157
158#[derive(Debug, Clone, PartialEq, Eq)]
163pub struct Matrix<'a> {
164 pub left_bracket: &'a str,
166 cells: Box<[Expression<'a>]>,
168 num_cols: usize,
170 pub right_bracket: &'a str,
172}
173
174impl<'a> Matrix<'a> {
175 pub fn new<E>(left_bracket: &'a str, cells: E, num_cols: usize, right_bracket: &'a str) -> Self
183 where
184 E: Into<Box<[Expression<'a>]>>,
185 {
186 let cells = cells.into();
187 assert_eq!(cells.len() / num_cols * num_cols, cells.len());
188 Matrix {
189 left_bracket,
190 cells,
191 num_cols,
192 right_bracket,
193 }
194 }
195
196 #[must_use]
198 pub fn num_cols(&self) -> usize {
199 self.num_cols
200 }
201
202 #[must_use]
204 pub fn num_rows(&self) -> usize {
205 self.cells.len() / self.num_cols
206 }
207
208 #[must_use]
210 pub fn num_cells(&self) -> usize {
211 self.cells.len()
212 }
213
214 #[must_use]
216 pub fn rows(&self) -> MatrixRows<'a, '_> {
217 MatrixRows(self.cells.chunks_exact(self.num_cols))
218 }
219
220 #[must_use]
222 pub fn iter(&self) -> MatrixRows<'a, '_> {
223 self.into_iter()
224 }
225}
226
227impl<'a, 'b> IntoIterator for &'b Matrix<'a> {
229 type IntoIter = MatrixRows<'a, 'b>;
230 type Item = &'b [Expression<'a>];
231
232 fn into_iter(self) -> Self::IntoIter {
233 self.rows()
234 }
235}
236
237impl<'a> Index<usize> for Matrix<'a> {
239 type Output = [Expression<'a>];
240
241 fn index(&self, row: usize) -> &Self::Output {
246 self.rows().nth(row).expect("index out of bounds")
247 }
248}
249
250impl<'a> Index<[usize; 2]> for Matrix<'a> {
252 type Output = Expression<'a>;
253
254 fn index(&self, idx: [usize; 2]) -> &Self::Output {
259 let [x, y] = idx;
260 assert!(x < self.num_cols, "index out of bounds");
261 &self.cells[x + self.num_cols * y]
262 }
263}
264
265#[derive(Debug, Clone)]
267pub struct MatrixRows<'a, 'b>(ChunksExact<'b, Expression<'a>>);
268
269impl<'a, 'b> Iterator for MatrixRows<'a, 'b> {
270 type Item = &'b [Expression<'a>];
271
272 fn next(&mut self) -> Option<Self::Item> {
273 self.0.next()
274 }
275
276 fn size_hint(&self) -> (usize, Option<usize>) {
277 self.0.size_hint()
278 }
279
280 fn count(self) -> usize {
281 self.len()
282 }
283
284 fn nth(&mut self, n: usize) -> Option<Self::Item> {
285 self.0.nth(n)
286 }
287
288 fn last(mut self) -> Option<Self::Item> {
289 self.next_back()
290 }
291}
292impl FusedIterator for MatrixRows<'_, '_> {}
293
294impl DoubleEndedIterator for MatrixRows<'_, '_> {
295 fn next_back(&mut self) -> Option<Self::Item> {
296 self.0.next_back()
297 }
298
299 fn nth_back(&mut self, n: usize) -> Option<Self::Item> {
300 self.0.nth_back(n)
301 }
302}
303
304impl ExactSizeIterator for MatrixRows<'_, '_> {}
305
306#[derive(Default, Debug, Clone, PartialEq, Eq)]
310pub enum Simple<'a> {
311 #[default]
316 Missing,
317 Number(&'a str),
319 Text(&'a str),
321 Ident(&'a str),
323 Symbol(&'a str),
325 Unary(SimpleUnary<'a>),
327 Func(SimpleFunc<'a>),
329 Binary(SimpleBinary<'a>),
331 Group(Group<'a>),
333 Matrix(Matrix<'a>),
335}
336
337impl Simple<'_> {
338 #[must_use]
340 pub fn as_option(&self) -> Option<&Self> {
341 match self {
342 Simple::Missing => None,
343 simple => Some(simple),
344 }
345 }
346}
347
348macro_rules! simple_from {
350 ($from:ty => $to:ident) => {
351 impl<'a> From<$from> for Simple<'a> {
352 fn from(inp: $from) -> Self {
353 Simple::$to(inp)
354 }
355 }
356 };
357}
358
359simple_from!(SimpleUnary<'a> => Unary);
360simple_from!(SimpleFunc<'a> => Func);
361simple_from!(SimpleBinary<'a> => Binary);
362simple_from!(Group<'a> => Group);
363simple_from!(Matrix<'a> => Matrix);
364
365#[derive(Default, Debug, Clone, PartialEq, Eq)]
371pub enum Script<'a> {
372 #[default]
374 None,
375 Sub(Simple<'a>),
377 Super(Simple<'a>),
379 Subsuper(Simple<'a>, Simple<'a>),
381}
382
383impl<'a> Script<'a> {
384 #[must_use]
386 pub fn sub(&self) -> Option<&Simple<'a>> {
387 match self {
388 Script::Sub(sub) | Script::Subsuper(sub, _) => Some(sub),
389 _ => None,
390 }
391 }
392
393 #[must_use]
395 pub fn sup(&self) -> Option<&Simple<'a>> {
396 match self {
397 Script::Super(sup) | Script::Subsuper(_, sup) => Some(sup),
398 _ => None,
399 }
400 }
401}
402
403#[derive(Default, Debug, Clone, PartialEq, Eq)]
405pub struct SimpleScript<'a> {
406 pub simple: Simple<'a>,
408 pub script: Script<'a>,
410}
411
412impl<'a> SimpleScript<'a> {
413 pub fn new<S>(simple: S, script: Script<'a>) -> Self
415 where
416 S: Into<Simple<'a>>,
417 {
418 SimpleScript {
419 simple: simple.into(),
420 script,
421 }
422 }
423
424 pub fn without_scripts<S>(simple: S) -> Self
426 where
427 S: Into<Simple<'a>>,
428 {
429 Self::new(simple, Script::None)
430 }
431
432 pub fn with_sub<S, Sub>(simple: S, sub: Sub) -> Self
434 where
435 S: Into<Simple<'a>>,
436 Sub: Into<Simple<'a>>,
437 {
438 Self::new(simple, Script::Sub(sub.into()))
439 }
440
441 pub fn with_super<S, Sup>(simple: S, sup: Sup) -> Self
443 where
444 S: Into<Simple<'a>>,
445 Sup: Into<Simple<'a>>,
446 {
447 Self::new(simple, Script::Super(sup.into()))
448 }
449
450 pub fn with_subsuper<S, Sub, Sup>(simple: S, sub: Sub, sup: Sup) -> Self
452 where
453 S: Into<Simple<'a>>,
454 Sub: Into<Simple<'a>>,
455 Sup: Into<Simple<'a>>,
456 {
457 Self::new(simple, Script::Subsuper(sub.into(), sup.into()))
458 }
459}
460
461impl<'a, S> From<S> for SimpleScript<'a>
462where
463 S: Into<Simple<'a>>,
464{
465 fn from(inp: S) -> Self {
466 SimpleScript::without_scripts(inp.into())
467 }
468}
469
470impl<'a> Deref for SimpleScript<'a> {
472 type Target = Script<'a>;
473
474 fn deref(&self) -> &Self::Target {
475 &self.script
476 }
477}
478
479#[derive(Debug, Clone, PartialEq, Eq)]
481pub struct Func<'a> {
482 pub func: &'a str,
484 pub script: Script<'a>,
486 arg: Box<ScriptFunc<'a>>,
488}
489
490impl<'a> Func<'a> {
491 pub fn new<Arg>(func: &'a str, script: Script<'a>, arg: Arg) -> Self
493 where
494 Arg: Into<ScriptFunc<'a>>,
495 {
496 Func {
497 func,
498 script,
499 arg: Box::new(arg.into()),
500 }
501 }
502
503 pub fn without_scripts<Arg>(func: &'a str, arg: Arg) -> Self
505 where
506 Arg: Into<ScriptFunc<'a>>,
507 {
508 Self::new(func, Script::None, arg)
509 }
510
511 pub fn with_sub<Sub, Arg>(func: &'a str, sub: Sub, arg: Arg) -> Self
513 where
514 Sub: Into<Simple<'a>>,
515 Arg: Into<ScriptFunc<'a>>,
516 {
517 Self::new(func, Script::Sub(sub.into()), arg)
518 }
519
520 pub fn with_super<Sup, Arg>(func: &'a str, sup: Sup, arg: Arg) -> Self
522 where
523 Sup: Into<Simple<'a>>,
524 Arg: Into<ScriptFunc<'a>>,
525 {
526 Self::new(func, Script::Super(sup.into()), arg)
527 }
528
529 pub fn with_subsuper<Sub, Sup, Arg>(func: &'a str, sub: Sub, sup: Sup, arg: Arg) -> Self
531 where
532 Sub: Into<Simple<'a>>,
533 Sup: Into<Simple<'a>>,
534 Arg: Into<ScriptFunc<'a>>,
535 {
536 Self::new(func, Script::Subsuper(sub.into(), sup.into()), arg)
537 }
538
539 #[must_use]
541 pub fn arg(&self) -> &ScriptFunc<'a> {
542 &self.arg
543 }
544}
545
546impl<'a> Deref for Func<'a> {
547 type Target = Script<'a>;
548
549 fn deref(&self) -> &Self::Target {
550 &self.script
551 }
552}
553
554#[derive(Debug, Clone, PartialEq, Eq)]
556pub enum ScriptFunc<'a> {
557 Simple(SimpleScript<'a>),
559 Func(Func<'a>),
561}
562
563impl Default for ScriptFunc<'_> {
564 fn default() -> Self {
565 ScriptFunc::Simple(SimpleScript::default())
566 }
567}
568
569impl<'a> From<Func<'a>> for ScriptFunc<'a> {
570 fn from(func: Func<'a>) -> Self {
571 ScriptFunc::Func(func)
572 }
573}
574
575impl<'a, S> From<S> for ScriptFunc<'a>
576where
577 S: Into<SimpleScript<'a>>,
578{
579 fn from(inp: S) -> Self {
580 ScriptFunc::Simple(inp.into())
581 }
582}
583
584#[derive(Debug, Clone, PartialEq, Eq)]
586pub struct Frac<'a> {
587 pub numer: ScriptFunc<'a>,
589 pub denom: ScriptFunc<'a>,
591}
592
593impl<'a> Frac<'a> {
594 pub fn new<N, D>(numer: N, denom: D) -> Self
596 where
597 N: Into<ScriptFunc<'a>>,
598 D: Into<ScriptFunc<'a>>,
599 {
600 Frac {
601 numer: numer.into(),
602 denom: denom.into(),
603 }
604 }
605}
606
607#[derive(Debug, Clone, PartialEq, Eq)]
609pub enum Intermediate<'a> {
610 ScriptFunc(ScriptFunc<'a>),
612 Frac(Frac<'a>),
614}
615
616impl Default for Intermediate<'_> {
617 fn default() -> Self {
618 Intermediate::ScriptFunc(ScriptFunc::default())
619 }
620}
621
622impl<'a> From<Frac<'a>> for Intermediate<'a> {
623 fn from(frac: Frac<'a>) -> Self {
624 Intermediate::Frac(frac)
625 }
626}
627
628impl<'a, S> From<S> for Intermediate<'a>
629where
630 S: Into<ScriptFunc<'a>>,
631{
632 fn from(inp: S) -> Self {
633 Intermediate::ScriptFunc(inp.into())
634 }
635}
636
637#[derive(Default, Debug, Clone, PartialEq, Eq)]
641pub struct Expression<'a>(Box<[Intermediate<'a>]>);
642
643impl<'a> Deref for Expression<'a> {
644 type Target = [Intermediate<'a>];
645
646 fn deref(&self) -> &Self::Target {
647 &self.0
648 }
649}
650
651impl<'a, I> FromIterator<I> for Expression<'a>
652where
653 I: Into<Intermediate<'a>>,
654{
655 fn from_iter<T>(iter: T) -> Self
656 where
657 T: IntoIterator<Item = I>,
658 {
659 Expression(iter.into_iter().map(std::convert::Into::into).collect())
660 }
661}
662
663impl<'a, B> From<B> for Expression<'a>
664where
665 B: Into<Box<[Intermediate<'a>]>>,
666{
667 fn from(inp: B) -> Self {
668 Expression(inp.into())
669 }
670}