amethyst_geode 1.2.3

Turing Machine Programming Language Interpreter
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
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
use crate::syntax::{
    AutomatonType, Machine, MacroType, Move, Program, State, StateType, Transition,
};

const ALLOWED_NAME_SYMBOLS: &str = "abcdefghijklmnopqrstuvwxyz0123456789_.";
const ALLOWED_TAPE_SYMBOLS: &str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*[]-+=/?_:";
const BREAK_SYMBOLS: &str = "(){};=, \n\"\'";

// Check if a character is valid for a name
fn allowed_name(x: &char) -> bool {
    ALLOWED_NAME_SYMBOLS.contains(*x)
}

// Check if a character is valid for a name
fn allowed_tape(x: &char) -> bool {
    ALLOWED_TAPE_SYMBOLS.contains(*x)
}

// Check if a character is valid for a literal
fn literal(x: &char) -> bool {
    !BREAK_SYMBOLS.contains(*x)
}

// Content left to parse, with a header to the current position
#[derive(Copy, Clone, PartialEq, Debug)]
pub struct Leftover<'a> {
    pub input: &'a str,
    pub row: u32,
    pub col: u32,
}

// mutating and non-mutating lookahead
impl<'a> Leftover<'a> {
    // mutating lookahead
    fn advance(&mut self) -> Option<char> {
        let mut chars = self.input.chars();
        match chars.next() {
            Some(c) => {
                self.input = chars.as_str();
                if c != '\n' {
                    self.col += 1;
                } else {
                    self.row += 1;
                    self.col = 0;
                }
                Some(c)
            }
            None => None,
        }
    }
    // non mutating lookahead
    fn look(&self) -> Option<char> {
        match self.input.chars().nth(0) {
            Some(c) => Some(c),
            None => None,
        }
    }
    // mutating literal lookahead
    fn advance_literal(&mut self) -> usize {
        let index = self
            .input
            .char_indices()
            .find(|(_, c)| !literal(c))
            .map(|(i, _)| i)
            .unwrap_or(self.input.len());
        self.col += index as u32;
        index
    }
    // non-mutating literal lookahead
    fn look_literal(&self) -> &str {
        let index = self
            .input
            .char_indices()
            .find(|(_, c)| !literal(c))
            .map(|(i, _)| i)
            .unwrap_or(self.input.len());

        &self.input[..index]
    }
}

// Helper trait for parsers that return vectors
trait IsEmpty {
    fn is_empty(&self) -> bool;
}

impl<T> IsEmpty for Vec<T> {
    fn is_empty(&self) -> bool {
        self.len() == 0
    }
}

impl IsEmpty for &str {
    fn is_empty(&self) -> bool {
        self.len() == 0
    }
}

// Encapsulated function for parser combinators
struct Parser<'a, T> {
    parse: Box<dyn Fn(Leftover<'a>) -> Option<(Leftover<'a>, Result<T, String>)> + 'a>,
}

// Implementation functor, applicative, and alternative instances + condition, not_null, many, and some
impl<'a, T: 'a> Parser<'a, T> {
    // (<$>) operator
    fn fmap<B: 'a, F>(self, f: F) -> Parser<'a, B>
    where
        F: Fn(T) -> B + 'a,
    {
        Parser {
            parse: Box::new(move |input| match (self.parse)(input) {
                None => None,
                Some((leftover, res)) => Some((
                    leftover,
                    match res {
                        Err(msg) => Err(msg),
                        Ok(x) => Ok(f(x)),
                    },
                )),
            }),
        }
    }

    // (<*>) operator
    fn ap<A: 'a, B: 'a>(self, p: Parser<'a, A>) -> Parser<'a, B>
    where
        T: Fn(A) -> B,
    {
        Parser {
            parse: Box::new(move |input| match (self.parse)(input) {
                None => None,
                Some((leftover1, res1)) => match res1 {
                    Err(msg) => Some((leftover1, Err(msg))),
                    Ok(f) => match (p.parse)(leftover1) {
                        None => None,
                        Some((leftover2, res2)) => Some((
                            leftover2,
                            match res2 {
                                Err(msg) => Err(msg),
                                Ok(x) => Ok(f(x)),
                            },
                        )),
                    },
                },
            }),
        }
    }

    // (<*) operator
    fn left<B: 'a>(self, p: Parser<'a, B>) -> Parser<'a, T> {
        Parser {
            parse: Box::new(move |input| match (self.parse)(input) {
                None => None,
                Some((leftover1, res1)) => match res1 {
                    Err(msg) => Some((leftover1, Err(msg))),
                    Ok(x) => match (p.parse)(leftover1) {
                        None => None,
                        Some((leftover2, res2)) => match res2 {
                            Err(msg) => Some((leftover2, Err(msg))),
                            Ok(_) => Some((leftover2, Ok(x))),
                        },
                    },
                },
            }),
        }
    }

    // (*>) operator
    fn right<B: 'a>(self, p: Parser<'a, B>) -> Parser<'a, B> {
        Parser {
            parse: Box::new(move |input| match (self.parse)(input) {
                None => None,
                Some((leftover1, res1)) => match res1 {
                    Err(msg) => Some((leftover1, Err(msg))),
                    Ok(_) => match (p.parse)(leftover1) {
                        None => None,
                        Some((leftover2, res2)) => match res2 {
                            Err(msg) => Some((leftover2, Err(msg))),
                            Ok(y) => Some((leftover2, Ok(y))),
                        },
                    },
                },
            }),
        }
    }

    // (<|>) operator
    fn or(self, p2: Parser<'a, T>) -> Parser<'a, T> {
        Parser {
            parse: Box::new(move |input| match (self.parse)(input) {
                Some(output) => Some(output),
                None => (p2.parse)(input),
            }),
        }
    }

    // check if the parsed content passes a certain condition
    fn condition<C>(self, cond: C) -> Parser<'a, T>
    where
        C: Fn(&T) -> bool + 'a,
    {
        Parser {
            parse: Box::new(move |input| match (self.parse)(input) {
                None => None,
                Some((leftover, res)) => match res {
                    Ok(results) => {
                        if cond(&results) {
                            Some((leftover, Ok(results)))
                        } else {
                            None
                        }
                    }
                    err => Some((leftover, err)),
                },
            }),
        }
    }

    // condition parser with custom error
    fn condition_e<C>(self, cond: C, msg: &'a str) -> Parser<'a, T>
    where
        C: Fn(&T) -> bool + 'a,
    {
        Parser {
            parse: Box::new(move |input| match (self.parse)(input) {
                None => None,
                Some((leftover, res)) => Some((
                    leftover,
                    match res {
                        Ok(results) => {
                            if cond(&results) {
                                Ok(results)
                            } else {
                                Err(msg.to_string())
                            }
                        }
                        err => err,
                    },
                )),
            }),
        }
    }

    // check that the vector has at least one element
    fn not_null(self) -> Parser<'a, T>
    where
        T: IsEmpty,
    {
        self.condition(|v| !v.is_empty())
    }

    // not_null with custom error
    fn not_null_e(self, msg: &'a str) -> Parser<'a, T>
    where
        T: IsEmpty,
    {
        self.condition_e(|v| !v.is_empty(), msg)
    }

    // 0 or more chained parsers of the same type
    fn many(self) -> Parser<'a, Vec<T>> {
        Parser {
            parse: Box::new(move |input| {
                let mut results = Vec::new();
                let mut current_input = input;
                while let Some((next_input, res)) = (self.parse)(current_input) {
                    // Prevent infinite loop if parser does not consume input
                    if next_input == current_input {
                        break;
                    }
                    match res {
                        Err(msg) => return Some((next_input, Err(msg))),
                        Ok(x) => {
                            results.push(x);
                            current_input = next_input;
                        }
                    }
                }
                Some((current_input, Ok(results)))
            }),
        }
    }

    // 1 or more chained parsers of the same type
    fn some(self) -> Parser<'a, Vec<T>> {
        self.many().not_null()
    }

    // chaining parsers that pass a condition
    fn span<C>(self, cond: C) -> Parser<'a, Vec<T>>
    where
        C: Fn(&T) -> bool + 'a,
    {
        self.condition(cond).many()
    }

    // raise an error if the parser fails
    fn raise(self, msg: &'a str) -> Parser<'a, T> {
        Parser {
            parse: Box::new(move |input| match (self.parse)(input) {
                Some(result) => Some(result),
                None => Some((input, Err(msg.to_string()))),
            }),
        }
    }
    // raise an error and append the current char
    fn raise_look(self, msg: &'a str) -> Parser<'a, T> {
        Parser {
            parse: Box::new(move |input| match (self.parse)(input) {
                Some(result) => Some(result),
                None => Some((
                    input,
                    Err(format!(
                        "{}{}",
                        msg,
                        match input.look() {
                            None => "no more input".to_string(),
                            Some(c) => c.to_string(),
                        }
                    )),
                )),
            }),
        }
    }
    // raise an error and append the literal to the end
    fn raise_literal(self, prefix: &'a str, msg: &'a str, postfix: &'a str) -> Parser<'a, T> {
        Parser {
            parse: Box::new(move |input| match (self.parse)(input) {
                Some(result) => Some(result),
                None => Some((
                    input,
                    Err(format!(
                        "{}{}{}{}",
                        prefix,
                        msg,
                        postfix,
                        input.look_literal()
                    )),
                )),
            }),
        }
    }
}

// parse a single character
fn any_char_p<'a>() -> Parser<'a, char> {
    Parser {
        parse: Box::new(move |mut input| {
            let current = input.advance();
            match current {
                None => None,
                Some(c) => Some((input, Ok(c))),
            }
        }),
    }
}

// fail if the character differs
fn char_p<'a>(x: char) -> Parser<'a, char> {
    Parser {
        parse: Box::new(move |mut input| {
            let current = input.advance();
            match current {
                None => None,
                Some(c) if c != x => None,
                _ => Some((input, Ok(x))),
            }
        }),
    }
}

// raise an error if the character differs
fn char_pe<'a>(x: char) -> Parser<'a, char> {
    Parser {
        parse: Box::new(move |mut input| {
            let current = input.advance();
            match current {
                None => Some((input, Err(format!("Expected {} found no more input", x)))),
                Some(c) if c != x => Some((input, Err(format!("Expected {} found {}", x, c)))),
                _ => Some((input, Ok(x))),
            }
        }),
    }
}

// make sure the current character is not x without consuming it
fn not_char_p<'a>(x: char) -> Parser<'a, ()> {
    Parser {
        parse: Box::new(move |input| match input.look() {
            Some(c) if c == x => None,
            _ => Some((input, Ok(()))),
        }),
    }
}

// parser for a single move symbol
fn move_pe<'a>() -> Parser<'a, Move> {
    (char_p('L').fmap(|_| Move::Left))
        .or(char_p('R').fmap(|_| Move::Right))
        .or(char_p('N').fmap(|_| Move::Neutral))
        .raise_look("Expected move symbol found ")
}

// whitespace parser, never files
fn ws0<'a>() -> Parser<'a, &'a str> {
    char_p(' ').many().fmap(|_| "")
}

// whitespace parser + new lines, never fails
fn ws1<'a>() -> Parser<'a, &'a str> {
    (char_p(' ').or(char_p('\n'))).many().fmap(|_| "")
}

// parser for a single tape symbol
fn symbol_p<'a>() -> Parser<'a, char> {
    any_char_p().condition(allowed_tape)
}

// symbol parser that raises an error
fn symbol_pe<'a>() -> Parser<'a, char> {
    symbol_p().raise_look("Expected tape symbol found ")
}

// parse any literal (string that has no break symbols)
fn literal_p<'a>() -> Parser<'a, &'a str> {
    Parser {
        parse: Box::new(|mut input| {
            let index = input.advance_literal();
            let lit = &input.input[..index];
            input.input = &input.input[index..];
            Some((input, Ok(lit)))
        }),
    }
}

// literal with allowed name characters
fn word_pe<'a>() -> Parser<'a, &'a str> {
    literal_p()
        .condition(|s| s.chars().all(|c| allowed_name(&c)))
        .raise_literal("", "Forbidden symbol in word ", "")
}

// literal with allowed tape characters
fn tape_pe<'a>() -> Parser<'a, &'a str> {
    literal_p()
        .condition(|s| s.chars().all(|c| allowed_tape(&c)))
        .raise_literal("", "Forbidden symbol in tape sequence ", "")
}

// parse a specific string, using &str for efficiency
fn string_p<'a>(s: &'a str) -> Parser<'a, &'a str> {
    Parser {
        parse: Box::new(move |mut input| {
            let mut chars = s.chars();
            while let Some(expected) = chars.next() {
                match input.advance() {
                    Some(c) if c == expected => {}
                    _ => return None,
                }
            }
            Some((input, Ok(s)))
        }),
    }
}

// string parser that raises an error
fn string_pe<'a>(s: &'a str) -> Parser<'a, &'a str> {
    string_p(s).raise_literal("Expected ", s, " found ")
}

// flipped arguments for fmap
fn fmake<'a, A: 'a, B: 'a, F>(f: F) -> Parser<'a, F>
where
    F: Fn(A) -> B + 'a,
    F: Clone,
{
    Parser {
        parse: Box::new(move |input| Some((input, Ok(f.clone())))),
    }
}

// parser for transition
fn transition_pe<'a>() -> Parser<'a, Transition> {
    fmake(|read_sym| {
        move |write_sym| move |move_sym| move |new_state| (read_sym, write_sym, move_sym, new_state)
    })
    .ap(ws1().right(symbol_pe()).left(ws0()).left(char_pe('/')))
    .ap(ws0().right(symbol_pe()).left(ws0()).left(char_pe(',')))
    .ap(ws0().right(move_pe()).left(ws1()).left(string_pe("->")))
    .ap(ws0().right(
        word_pe()
            .not_null_e("Expected new state")
            .left(ws0())
            .left(char_pe(';')),
    ))
    .fmap(
        |(read_symbol, write_symbol, move_symbol, new_state)| Transition {
            read_symbol,
            write_symbol,
            move_symbol,
            new_state: new_state.to_string(),
        },
    )
}

// whitespace parser that requires at least one blank character
fn ws2<'a>() -> Parser<'a, &'a str> {
    (char_p(' ').or(char_p('\n')))
        .some()
        .fmap(|_| "")
        .raise("Expected space")
}

// make sure a character is not followed by another, consume only the first char
fn not_followed<'a>(x: char, y: char) -> Parser<'a, char> {
    Parser {
        parse: Box::new(move |mut input| {
            let current = input.advance();
            match current {
                None => None,
                Some(c) if c != x => Some((input, Ok(x))),
                _ => match input.look() {
                    None => Some((input, Ok(x))),
                    Some(z) if z != y => Some((input, Ok(x))),
                    _ => None,
                },
            }
        }),
    }
}

// parser for line and block comments
fn comment_pe<'a>() -> Parser<'a, &'a str> {
    string_p("--")
        .right(any_char_p().span(|x| *x != '\n'))
        .or(string_p("{-")
            .right(not_followed('-', '}').many())
            .left(string_pe("-}")))
        .fmap(|_| "")
}

// whitespace + comments parser
fn ws3<'a>() -> Parser<'a, &'a str> {
    ws1().left(comment_pe().right(ws1()).many())
}

// parser for state
fn state_pe<'a>() -> Parser<'a, StateType> {
    let final_p = fmake(|b: bool| {
        move |s: &str| {
            if b {
                StateType::Accept(s.to_string())
            } else {
                StateType::Reject(s.to_string())
            }
        }
    })
    .ap(string_p("reject")
        .fmap(|_| false)
        .or(string_p("accept").fmap(|_| true)))
    .ap(ws2()
        .right(string_pe("state"))
        .right(ws2())
        .right(word_pe().not_null_e("Expected state name"))
        .left(ws0())
        .left(char_pe(';')));
    let initial_p = || (string_p("initial").right(ws2()).fmap(|_| true)).or(ws1().fmap(|_| false));
    let arrow_p = fmake(move |b: bool| {
        move |from: &'a str| {
            move |to: &'a str| {
                StateType::State(
                    from.to_string(),
                    State {
                        initial: b,
                        transitions: Box::new(vec![Transition {
                            read_symbol: '_',
                            write_symbol: '_',
                            move_symbol: Move::Neutral,
                            new_state: to.to_string(),
                        }]),
                    },
                )
            }
        }
    })
    .ap(initial_p())
    .ap(string_pe("state")
        .right(ws2())
        .right(word_pe().not_null_e("Expected state name")))
    .ap(ws1()
        .right(string_p("->"))
        .right(ws1())
        .right(word_pe().not_null_e("Expected state name"))
        .left(ws0())
        .left(char_pe(';')));
    let tr_p = ws3()
        .right(not_char_p('}').right(transition_pe()).left(ws3()).many())
        .not_null_e("States can't have 0 transitions");
    let normal_p = fmake(move |b| {
        move |from: &'a str| {
            move |trans| {
                StateType::State(
                    from.to_string(),
                    State {
                        initial: b,
                        transitions: Box::new(trans),
                    },
                )
            }
        }
    })
    .ap(initial_p())
    .ap(string_pe("state")
        .right(ws2())
        .right(word_pe().not_null_e("Expected state name")))
    .ap(ws1().right(char_pe('{')).right(tr_p).left(char_pe('}')));
    final_p.or(arrow_p).or(normal_p)
}

// parse list of &str separated by the parser argument
fn sep_by<'a, B: 'a>(
    p1: Parser<'a, &'a str>,
    p2: Parser<'a, &'a str>,
    sep: Parser<'a, B>,
) -> Parser<'a, Vec<&'a str>> {
    fmake(move |element: &'a str| {
        move |mut list: Vec<&'a str>| {
            list.insert(0, element);
            list
        }
    })
    .ap(p1)
    .ap(sep.right(p2).many())
    .or(Parser {
        parse: Box::new(move |input| Some((input, Ok(vec![])))),
    })
}

// parse list of (&str, &str) separated by the parser argument
fn sep_by2<'a, B: 'a>(
    p1: Parser<'a, (&'a str, &'a str)>,
    p2: Parser<'a, (&'a str, &'a str)>,
    sep: Parser<'a, B>,
) -> Parser<'a, Vec<(&'a str, &'a str)>> {
    fmake(move |element: (&'a str, &'a str)| {
        move |mut list: Vec<(&'a str, &'a str)>| {
            list.insert(0, element);
            list
        }
    })
    .ap(p1)
    .ap(sep.right(p2).many())
    .or(Parser {
        parse: Box::new(move |input| Some((input, Ok(vec![])))),
    })
}

// used as separator
fn comma_p<'a>() -> Parser<'a, char> {
    ws1().right(char_p(',')).left(ws1())
}

// a single pair of two string
fn pair_p<'a>() -> Parser<'a, (&'a str, &'a str)> {
    fmake(move |first: &'a str| move |second: &'a str| (first, second))
        .ap(word_pe().not_null())
        .ap(ws2().right(word_pe()))
}

// pair parser that raises a component related error
fn pair_pe<'a>() -> Parser<'a, (&'a str, &'a str)> {
    fmake(move |first: &'a str| move |second: &'a str| (first, second))
        .ap(word_pe().not_null_e("Expected component type"))
        .ap(ws2().right(word_pe().not_null_e("Expected component name")))
}

// parser for machine
fn machine_pe<'a>() -> Parser<'a, AutomatonType> {
    fmake(move |name: &'a str| {
        move |components: Vec<(&'a str, &'a str)>| {
            move |states| {
                AutomatonType::Machine(
                    name.to_string(),
                    Machine {
                        components: Box::new(
                            components
                                .iter()
                                .map(|(c_type, c_name)| (c_type.to_string(), c_name.to_string()))
                                .collect(),
                        ),
                        states: Box::new(states),
                    },
                )
            }
        }
    })
    .ap(string_p("automaton")
        .right(ws2())
        .right(word_pe().not_null_e("Expected automaton name"))
        .left(ws1()))
    .ap(not_char_p('=')
        .right(char_pe('('))
        .right(ws1())
        .right(sep_by2(pair_p(), pair_pe(), comma_p()))
        .left(ws1())
        .left(char_pe(')'))
        .left(ws1()))
    .ap(char_pe('{')
        .right(ws3())
        .right(
            not_char_p('}')
                .right(state_pe())
                .left(ws3())
                .many()
                .not_null_e("Automaton can't have 0 states"),
        )
        .left(char_pe('}')))
}

// macro for parsing numbers
fn number_pe<'a>() -> Parser<'a, u32> {
    literal_p()
        .fmap(|s| s.to_string())
        .condition(|s| s.chars().all(|c| c >= '0' && c <= '9'))
        .raise_literal("", "Expected number literal found ", "")
        .fmap(|s| s.parse::<u32>().unwrap())
}

// macro keyword parsers
fn complement_pe<'a>() -> Parser<'a, MacroType> {
    string_p("complement")
        .right(ws1())
        .right(char_pe('('))
        .right(ws1())
        .right(word_pe())
        .not_null_e("Expected component name")
        .left(char_pe(')'))
        .fmap(|component: &'a str| MacroType::Complement(component.to_string()))
}

fn intersect_pe<'a>() -> Parser<'a, MacroType> {
    string_p("intersect")
        .right(ws1())
        .right(char_pe('('))
        .right(ws1())
        .right(sep_by(
            word_pe().not_null_e("Expected component list"),
            word_pe(),
            comma_p(),
        ))
        .condition_e(|v| v.len() > 1, "Expected two or more components")
        .left(char_pe(')'))
        .fmap(|components| {
            MacroType::Intersect(Box::new(components.iter().map(|c| c.to_string()).collect()))
        })
}

fn reunion_pe<'a>() -> Parser<'a, MacroType> {
    string_p("reunion")
        .right(ws1())
        .right(char_pe('('))
        .right(ws1())
        .right(sep_by(
            word_pe().not_null_e("Expected component list"),
            word_pe(),
            comma_p(),
        ))
        .condition_e(|v| v.len() > 1, "Expected two or more components")
        .left(char_pe(')'))
        .fmap(|components| {
            MacroType::Reunion(Box::new(components.iter().map(|c| c.to_string()).collect()))
        })
}

fn chain_pe<'a>() -> Parser<'a, MacroType> {
    string_p("chain")
        .right(ws1())
        .right(char_pe('('))
        .right(ws1())
        .right(sep_by(
            word_pe().not_null_e("Expected component list"),
            word_pe(),
            comma_p(),
        ))
        .condition_e(|v| v.len() > 1, "Expected two or more components")
        .left(char_pe(')'))
        .fmap(|components| {
            MacroType::Chain(Box::new(components.iter().map(|c| c.to_string()).collect()))
        })
}

fn repeat_pe<'a>() -> Parser<'a, MacroType> {
    fmake(move |num| move |component: &'a str| MacroType::Repeat(num, component.to_string()))
        .ap(string_p("repeat")
            .right(ws1())
            .right(char_pe('('))
            .right(ws1())
            .right(number_pe())
            .left(ws1()))
        .ap(char_pe(',')
            .right(ws1())
            .right(word_pe().not_null_e("Expected component name"))
            .left(ws1())
            .left(char_pe(')')))
}

fn move_mpe<'a>() -> Parser<'a, MacroType> {
    fmake(move |move_symbol| move |num| MacroType::Move(move_symbol, num))
        .ap(string_p("move")
            .right(ws1())
            .right(char_pe('('))
            .right(ws1())
            .right(move_pe())
            .left(ws1()))
        .ap(char_pe(',')
            .right(ws1())
            .right(number_pe())
            .left(ws1())
            .left(char_pe(')')))
}

fn override_pe<'a>() -> Parser<'a, MacroType> {
    fmake(move |move_symbol| {
        move |num| move |tape_symbol| MacroType::Override(move_symbol, num, tape_symbol)
    })
    .ap(string_p("override")
        .right(ws1())
        .right(char_pe('('))
        .right(ws1())
        .right(move_pe())
        .left(ws1()))
    .ap(char_pe(',').right(ws1()).right(number_pe()).left(ws1()))
    .ap(char_pe(',')
        .right(ws1())
        .right(char_pe('\''))
        .right(symbol_pe())
        .left(char_pe('\''))
        .left(ws1())
        .left(char_pe(')')))
}

fn place_pe<'a>() -> Parser<'a, MacroType> {
    string_p("place")
        .right(ws1())
        .right(char_pe('('))
        .right(ws1())
        .right(char_pe('\"'))
        .right(tape_pe())
        .left(char_pe('\"'))
        .left(ws1())
        .left(char_pe(')'))
        .fmap(move |content: &'a str| MacroType::Place(content.to_string()))
}

fn shift<'a>() -> Parser<'a, MacroType> {
    fmake(move |move_symbol| move |num| MacroType::Shift(move_symbol, num))
        .ap(string_p("shift")
            .right(ws1())
            .right(char_pe('('))
            .right(ws1())
            .right(move_pe())
            .left(ws1()))
        .ap(char_pe(',')
            .right(ws1())
            .right(number_pe())
            .left(ws1())
            .left(char_pe(')')))
}

// macro type parser
fn macro_pe<'a>() -> Parser<'a, AutomatonType> {
    fmake(move |name: &'a str| move |macro_type| AutomatonType::Macro(name.to_string(), macro_type))
        .ap(string_p("automaton")
            .right(ws2())
            .right(word_pe())
            .not_null_e("Expected automaton name"))
        .left(ws1())
        .left(char_p('='))
        .left(ws1())
        .ap(complement_pe()
            .or(intersect_pe())
            .or(reunion_pe())
            .or(chain_pe())
            .or(repeat_pe())
            .or(move_mpe())
            .or(override_pe())
            .or(place_pe())
            .or(shift())
            .raise_literal("", "Expected macro keyword found ", ""))
        .left(ws0())
        .left(char_pe(';'))
}

// parser for automaton
fn automaton_pe<'a>() -> Parser<'a, AutomatonType> {
    macro_pe().or(machine_pe())
}

// make sure there's no more input left to consume
fn done_p<'a>() -> Parser<'a, ()> {
    Parser {
        parse: Box::new(move |input| {
            if input.input == "" {
                Some((input, Ok(())))
            } else {
                None
            }
        }),
    }
}

// parser for program
fn program_pe<'a>() -> Parser<'a, Program> {
    ws3()
        .right(automaton_pe())
        .many()
        .left(ws3())
        .left(done_p().raise_literal("", "Expected automaton found unexpected keyword ", ""))
        .fmap(move |automata| Program {
            automata: Box::new(automata),
        })
}

// Exposed functions for parsing code

pub fn parse_move(input: &str) -> Result<Move, String> {
    match (move_pe().parse)(Leftover {
        input,
        row: 0,
        col: 0,
    }) {
        None => panic!("move parser should never return none"),
        Some((_, res)) => res,
    }
}

pub fn parse_transition(input: &str) -> Result<Transition, String> {
    match (transition_pe().parse)(Leftover {
        input,
        row: 0,
        col: 0,
    }) {
        None => panic!("transition parser should never return none"),
        Some((_, res)) => res,
    }
}

pub fn parse_state(input: &str) -> Result<StateType, String> {
    match (state_pe().parse)(Leftover {
        input,
        row: 0,
        col: 0,
    }) {
        None => panic!("state parser should never return none"),
        Some((_, res)) => res,
    }
}

pub fn parse_automaton(input: &str) -> Result<AutomatonType, String> {
    match (automaton_pe().parse)(Leftover {
        input,
        row: 0,
        col: 0,
    }) {
        None => panic!("automaton parser should never return none"),
        Some((_, res)) => res,
    }
}

pub fn parse_program(input: &str) -> Result<Program, (Leftover, String)> {
    match (program_pe().parse)(Leftover {
        input,
        row: 0,
        col: 0,
    }) {
        None => panic!("program parser should never return none"),
        Some((leftover, res)) => match res {
            Ok(prog) => Ok(prog),
            Err(err) => Err((leftover, err)),
        },
    }
}