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
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
use std::{iter::Enumerate, str::Chars};

use super::matching::{Instruction, Program};
use crate::regex::matching::InstructionPointer;
use unbounded_interval_tree::interval_tree::IntervalTree;

#[cfg(test)]
pub mod tests {
    use super::*;
    use crate::lexer::TerminalId;

    /// Compile a regex into a program executable on the VM.
    pub fn compile(
        regex: &str,
        id: TerminalId,
    ) -> Result<(Program, usize), RegexError> {
        let mut program = Program::new();
        let (regex, nb_groups) = read(regex, 0)?;
        build(regex, &mut program);
        program.push(Instruction::Match(id));
        Ok((program, nb_groups))
    }

    #[test]
    fn read_char() {
        use Regex::*;
        assert_eq!(read("a", 0).unwrap(), (Char('a'), 0));
    }

    #[test]
    fn read_multiline_comment() {
        use std::ops::Bound::Included;
        use Regex::*;
        let a = read(r"/\*([^*]|\*[^/])*\*/", 0).unwrap();
        let mut first_class = IntervalTree::default();
        first_class.insert((Included('*'), Included('*')));
        let mut second_class = IntervalTree::default();
        second_class.insert((Included('/'), Included('/')));
        // let b = Concat(
        //     Box::new(Char('/')),
        //     Box::new(Concat(
        //         Box::new(Char('*')),
        //         Box::new(Concat(
        //             Box::new(KleeneStar(Box::new(Group(
        //                 Box::new(Option(
        //                     Box::new(CharacterClass(first_class, true)),
        //                     Box::new(Concat(
        //                         Box::new(Char('*')),
        //                         Box::new(CharacterClass(second_class, true)),
        //                     )),
        //                 )),
        //                 0,
        //             )))),
        //             Box::new(Concat(Box::new(Char('*')), Box::new(Char('/')))),
        //         )),
        //     )),
        // );
        let b = Concat(
            Box::new(Concat(
                Box::new(Concat(
                    Box::new(Concat(Box::new(Char('/')), Box::new(Char('*')))),
                    Box::new(KleeneStar(Box::new(Group(
                        Box::new(Option(
                            Box::new(CharacterClass(first_class, true)),
                            Box::new(Concat(
                                Box::new(Char('*')),
                                Box::new(CharacterClass(second_class, true)),
                            )),
                        )),
                        0,
                    )))),
                )),
                Box::new(Char('*')),
            )),
            Box::new(Char('/')),
        );
        assert_eq!(a, (b, 1));
    }

    #[test]
    fn read_word_boundary() {
        use Regex::*;
        assert_eq!(read(r"\b", 0).unwrap(), (WordBoundary, 0));
    }

    #[test]
    fn read_eof() {
        use Regex::*;
        assert_eq!(read(r"\Z", 0).unwrap(), (EOF, 0));
        assert_eq!(read(r"\z", 0).unwrap(), (EOF, 0));
    }

    #[test]
    fn read_char_class() {
        use std::ops::Bound::Included;
        use Regex::*;
        let mut tree = IntervalTree::default();
        tree.insert((Included('a'), Included('a')));
        assert_eq!(read("[a]", 0).unwrap(), (CharacterClass(tree, false), 0));

        let mut tree = IntervalTree::default();
        tree.insert((Included('a'), Included('z')));
        tree.insert((Included('A'), Included('Z')));
        tree.insert((Included('0'), Included('9')));
        tree.insert((Included('_'), Included('_')));
        assert_eq!(
            read("[a-zA-Z0-9_]", 0).unwrap(),
            (CharacterClass(tree, false), 0)
        );

        let mut tree = IntervalTree::default();
        tree.insert((Included('a'), Included('z')));
        tree.insert((Included('A'), Included('Z')));
        tree.insert((Included('0'), Included('9')));
        tree.insert((Included('_'), Included('_')));
        assert_eq!(
            read("[^a-zA-Z0-9_]", 0).unwrap(),
            (CharacterClass(tree, true), 0)
        );

        assert_eq!(
            read("[a-zb-z]", 0).unwrap_err(),
            RegexError {
                position: 8,
                message: String::from(
                    "Found 1 overlaps in character class from position 0 to position 8: (a-z,b-z)"
                ),
            }
        );
    }

    #[test]
    fn read_escaped() {
        use Regex::*;
        assert_eq!(read(r"\w", 0).unwrap(), (WordChar, 0));
    }

    #[test]
    #[should_panic]
    fn read_wrong_escaped() {
        read(r"\l", 0).unwrap();
    }

    #[test]
    fn read_group() {
        use Regex::*;
        assert_eq!(read("(a)", 0).unwrap(), (Group(Box::new(Char('a')), 0), 1));
    }

    #[test]
    fn read_repetition() {
        use Regex::*;
        assert_eq!(
            read("a+b+", 0).unwrap(),
            (
                Concat(
                    Box::new(Repetition(Box::new(Char('a')))),
                    Box::new(Repetition(Box::new(Char('b'))))
                ),
                0
            )
        );

        assert_eq!(
            read("(a+)(b+)", 0).unwrap(),
            (
                Concat(
                    Box::new(Group(
                        Box::new(Repetition(Box::new(Char('a')))),
                        0
                    )),
                    Box::new(Group(
                        Box::new(Repetition(Box::new(Char('b')))),
                        1
                    ))
                ),
                2
            )
        );
    }

    #[test]
    fn read_kleene_star() {
        use Regex::*;

        assert_eq!(
            read("a*b*", 0).unwrap(),
            (
                Concat(
                    Box::new(KleeneStar(Box::new(Char('a')))),
                    Box::new(KleeneStar(Box::new(Char('b'))))
                ),
                0
            )
        );

        assert_eq!(
            read("(a*)(b*)", 0).unwrap(),
            (
                Concat(
                    Box::new(Group(
                        Box::new(KleeneStar(Box::new(Char('a')))),
                        0
                    )),
                    Box::new(Group(
                        Box::new(KleeneStar(Box::new(Char('b')))),
                        1
                    ))
                ),
                2
            )
        );
    }

    #[test]
    fn read_option() {
        use Regex::*;

        assert_eq!(
            read("abc|bcd", 0).unwrap(),
            (
                Option(
                    Box::new(Concat(
                        Box::new(Concat(
                            Box::new(Char('a')),
                            Box::new(Char('b')),
                        )),
                        Box::new(Char('c'))
                    )),
                    Box::new(Concat(
                        Box::new(Concat(
                            Box::new(Char('b')),
                            Box::new(Char('c')),
                        )),
                        Box::new(Char('d'))
                    )),
                ),
                0
            )
        );

        assert_eq!(
            read("ab|cd|ef", 0).unwrap(),
            (
                Option(
                    Box::new(Option(
                        Box::new(Concat(
                            Box::new(Char('a')),
                            Box::new(Char('b'))
                        )),
                        Box::new(Concat(
                            Box::new(Char('c')),
                            Box::new(Char('d'))
                        ))
                    )),
                    Box::new(Concat(Box::new(Char('e')), Box::new(Char('f'))))
                ),
                0
            )
        );
    }

    #[test]
    fn read_any() {
        use Regex::*;
        assert_eq!(read(".", 0).unwrap(), (Any, 0));
        assert_eq!(read(".*", 0).unwrap(), (KleeneStar(Box::new(Any)), 0));
    }

    #[test]
    fn read_malformed() {
        let wrong_regex = [
            ("a**", 2),
            ("a*?", 2),
            ("a?*", 2),
            ("a*+", 2),
            ("a+*", 2),
            ("a??", 2),
            ("a?+", 2),
            ("a+?", 2),
            ("a++", 2),
            ("*", 0),
            ("?", 0),
            ("+", 0),
        ];
        for &(regex, p) in wrong_regex.iter() {
            if let Err(RegexError { position: pos, .. }) = read(regex, 0) {
                assert_eq!(pos, p);
            } else {
                panic!("(/{}/ shouldn't succeed.", regex);
            }
        }
    }

    #[test]
    fn build_basic() {
        use Instruction::*;
        let program = compile("a+b+", TerminalId(0)).unwrap();
        assert_eq!(
            program,
            (
                Program::from(vec![
                    Char('a'),
                    Split(InstructionPointer(0), InstructionPointer(2)),
                    Char('b'),
                    Split(InstructionPointer(2), InstructionPointer(4)),
                    Match(TerminalId(0))
                ]),
                0
            )
        );
    }

    #[test]
    fn build_char() {
        use Instruction::*;
        let program = compile("a", TerminalId(0)).unwrap();
        assert_eq!(
            program,
            (Program::from(vec![Char('a'), Match(TerminalId(0))]), 0)
        );
    }

    #[test]
    fn build_char_class() {
        use std::ops::Bound::Included;
        use Instruction::*;
        let mut tree = IntervalTree::default();
        tree.insert((Included('a'), Included('z')));
        tree.insert((Included('A'), Included('Z')));
        tree.insert((Included('0'), Included('9')));
        tree.insert((Included('_'), Included('_')));
        let program = compile("[a-zA-Z0-9_]", TerminalId(0)).unwrap();
        assert_eq!(
            program,
            (
                Program::from(vec![
                    CharacterClass(tree, false),
                    Match(TerminalId(0))
                ]),
                0
            )
        );
    }

    #[test]
    fn build_word_boundary() {
        use Instruction::*;
        let program = compile(r"\b", TerminalId(0)).unwrap();
        assert_eq!(
            program,
            (Program::from(vec![WordBoundary, Match(TerminalId(0))]), 0)
        );
    }

    #[test]
    fn build_eof() {
        use Instruction::*;
        let program = compile(r"\z", TerminalId(0)).unwrap();
        assert_eq!(
            program,
            (Program::from(vec![EOF, Match(TerminalId(0))]), 0)
        );
        let program = compile(r"\Z", TerminalId(0)).unwrap();
        assert_eq!(
            program,
            (Program::from(vec![EOF, Match(TerminalId(0))]), 0)
        );
    }

    #[test]
    fn build_escaped() {
        use Instruction::*;
        let program = compile(r"\w", TerminalId(0)).unwrap();
        assert_eq!(
            program,
            (Program::from(vec![WordChar, Match(TerminalId(0))]), 0)
        );
    }

    #[test]
    fn build_concat() {
        use Instruction::*;
        let program = compile("ab", TerminalId(0)).unwrap();
        assert_eq!(
            program,
            (
                Program::from(vec![Char('a'), Char('b'), Match(TerminalId(0))]),
                0
            )
        );
    }

    #[test]
    fn build_option() {
        use Instruction::*;
        let program = compile("a|b", TerminalId(0)).unwrap();
        assert_eq!(
            program,
            (
                Program::from(vec![
                    Split(InstructionPointer(1), InstructionPointer(3)),
                    Char('a'),
                    Jump(InstructionPointer(4)),
                    Char('b'),
                    Match(TerminalId(0))
                ]),
                0
            )
        );
    }

    #[test]
    fn build_optional() {
        use Instruction::*;
        let program = compile("a?", TerminalId(0)).unwrap();
        assert_eq!(
            program,
            (
                Program::from(vec![
                    Split(InstructionPointer(1), InstructionPointer(2)),
                    Char('a'),
                    Match(TerminalId(0))
                ]),
                0
            )
        );
    }

    #[test]
    fn build_kleene_star() {
        use Instruction::*;
        let program = compile("a*", TerminalId(0)).unwrap();
        assert_eq!(
            program,
            (
                Program::from(vec![
                    Split(InstructionPointer(1), InstructionPointer(3)),
                    Char('a'),
                    Jump(InstructionPointer(0)),
                    Match(TerminalId(0))
                ]),
                0
            )
        );
    }

    #[test]
    fn build_repetition() {
        use Instruction::*;
        let program = compile("a+", TerminalId(0)).unwrap();
        assert_eq!(
            program,
            (
                Program::from(vec![
                    Char('a'),
                    Split(InstructionPointer(0), InstructionPointer(2)),
                    Match(TerminalId(0))
                ]),
                0
            )
        );
    }

    #[test]
    fn build_any() {
        use Instruction::*;
        let program = compile(".", TerminalId(0)).unwrap();
        assert_eq!(
            program,
            (Program::from(vec![Any, Match(TerminalId(0))]), 0)
        );
    }

    #[test]
    fn build_groups() {
        use Instruction::*;
        let program = compile("(a+)(b+)", TerminalId(0)).unwrap();
        assert_eq!(
            program,
            (
                Program::from(vec![
                    Save(0),
                    Char('a'),
                    Split(InstructionPointer(1), InstructionPointer(3)),
                    Save(1),
                    Save(2),
                    Char('b'),
                    Split(InstructionPointer(5), InstructionPointer(7)),
                    Save(3),
                    Match(TerminalId(0))
                ]),
                2
            )
        )
    }

    #[test]
    fn build_string() {
        use std::collections::Bound::Included;
        use Instruction::*;
        let program =
            compile(r"'(([^'\\]|\\[^\\]|\\\\)*)'", TerminalId(0)).unwrap();
        let mut first_char_class = IntervalTree::default();
        first_char_class.insert((Included('\''), Included('\'')));
        first_char_class.insert((Included('\\'), Included('\\')));
        let mut second_char_class = IntervalTree::default();
        second_char_class.insert((Included('\\'), Included('\\')));
        let correct = Program::from(vec![
            Char('\''),
            Save(0),
            Split(InstructionPointer(3), InstructionPointer(15)),
            Save(2),
            Split(InstructionPointer(5), InstructionPointer(11)),
            Split(InstructionPointer(6), InstructionPointer(8)),
            CharacterClass(first_char_class, true),
            Jump(InstructionPointer(10)),
            Char('\\'),
            CharacterClass(second_char_class, true),
            Jump(InstructionPointer(13)),
            Char('\\'),
            Char('\\'),
            Save(3),
            Jump(InstructionPointer(2)),
            Save(1),
            Char('\''),
            Match(TerminalId(0)),
        ]);
        assert_eq!(program, (correct, 2));
    }
}

/// # Summary
///
/// `Regex` represents any successfully parsed regex.
#[cfg_attr(test, derive(PartialEq))]
#[derive(Debug)]
pub enum Regex {
    Char(char),
    Option(Box<Regex>, Box<Regex>),
    Optional(Box<Regex>),
    Repetition(Box<Regex>),
    KleeneStar(Box<Regex>),
    Concat(Box<Regex>, Box<Regex>),
    Group(Box<Regex>, usize),
    CharacterClass(IntervalTree<char>, bool),
    WordChar,
    Digit,
    Whitespace,
    WordBoundary,
    EOF,
    Any,
    Empty,
}

/// # Summary
///
/// `RegexError` is an alias of a couple (usize, String).
/// The first element is the position at which the error occured,
/// and the second one is a description of the said error.
#[derive(Debug, PartialEq, Eq)]
pub struct RegexError {
    pub position: usize,
    pub message: String,
}

impl From<(Regex, Option<Regex>)> for Regex {
    fn from(t: (Regex, Option<Regex>)) -> Self {
        match t {
            (r, Some(r2)) => Regex::Option(Box::new(r2), Box::new(r)),
            (r, None) => r,
        }
    }
}

/// Take a `Regex`, and a reference to a `Program` and
/// appends the implementation of the `Regex` at the
/// end of the `Program`.
///
/// **Warning**: this function is recursive, and a malicious input
/// may lead to a stack overflow. This is because this library
/// is really meant to be used in a context in which the end user
/// (the one providing the input) is also the one using the library.
pub fn build(regex: Regex, program: &mut Program) {
    match regex {
        Regex::Char(c) => {
            program.push(Instruction::Char(c));
        }
        Regex::Option(r1, r2) => {
            let split_pos = InstructionPointer(program.len());
            program.push(Instruction::Split(
                InstructionPointer(0),
                InstructionPointer(0),
            ));
            build(*r1, program);
            let jmp_pos = program.len_ip();
            program.push(Instruction::Jump(InstructionPointer(0)));
            build(*r2, program);
            program[split_pos] =
                Instruction::Split(split_pos.incr(), jmp_pos.incr());
            program[jmp_pos] = Instruction::Jump(program.len_ip());
        }
        Regex::Concat(r1, r2) => {
            build(*r1, program);
            build(*r2, program);
        }
        Regex::Optional(r) => {
            let split_pos = program.len_ip();
            program.push(Instruction::Split(
                InstructionPointer(0),
                InstructionPointer(0),
            ));
            build(*r, program);
            program[split_pos] =
                Instruction::Split(split_pos.incr(), program.len_ip());
        }
        Regex::KleeneStar(r) => {
            let split_pos = program.len_ip();
            program.push(Instruction::Split(
                InstructionPointer(0),
                InstructionPointer(0),
            ));
            build(*r, program);
            program.push(Instruction::Jump(split_pos));
            program[split_pos] =
                Instruction::Split(split_pos.incr(), program.len_ip());
        }
        Regex::Repetition(r) => {
            let init_pos = program.len_ip();
            build(*r, program);
            program.push(Instruction::Split(init_pos, program.len_ip().incr()));
        }
        Regex::Group(r, i) => {
            program.push(Instruction::Save(2 * i));
            build(*r, program);
            program.push(Instruction::Save(2 * i + 1));
        }
        Regex::Any => program.push(Instruction::Any),
        Regex::WordChar => program.push(Instruction::WordChar),
        Regex::Digit => program.push(Instruction::Digit),
        Regex::WordBoundary => program.push(Instruction::WordBoundary),
        Regex::CharacterClass(class, negated) => {
            program.push(Instruction::CharacterClass(class, negated))
        }
        Regex::EOF => program.push(Instruction::EOF),
        Regex::Whitespace => program.push(Instruction::Whitespace),
        Regex::Empty => {}
    };
}

/// Parse a regex. The parsing technique is quite efficient,
/// essentially linear time.
/// **This is a private function, please use the API instead.**
pub fn read(
    regex: &str,
    mut groups: usize,
) -> Result<(Regex, usize), RegexError> {
    /// Parse a character class.
    fn read_char_class(
        input: &mut std::iter::Enumerate<std::str::Chars<'_>>,
        size: usize,
        actual: usize,
    ) -> Result<Regex, RegexError> {
        use std::ops::{Bound, Bound::Included};
        fn insert(
            interval: (Bound<char>, Bound<char>),
            tree: &mut IntervalTree<char>,
            overlaps: &mut Vec<Vec<(char, char)>>,
        ) {
            let over = tree.get_interval_overlaps(&interval);
            if !over.is_empty() {
                overlaps.push(
                    over.into_iter()
                        .chain(std::iter::once(&interval))
                        .map(|(start, end)| {
                            let s = match start {
                                Included(s) => *s,
                                _ => panic!(),
                            };
                            let e = match end {
                                Included(e) => *e,
                                _ => panic!(),
                            };
                            (s, e)
                        })
                        .collect(),
                );
            }
            tree.insert(interval);
        }
        let mut tree = IntervalTree::default();
        let mut last = None;
        let mut overlaps = Vec::new();
        let mut negated = false;

        fn read_escaped_char(
            input: &mut Enumerate<Chars<'_>>,
            pos: usize,
        ) -> Result<char, RegexError> {
            if let Some((pos, c)) = input.next() {
                match c {
                    ']' => Ok(c),
                    't' => Ok('\t'),
                    'n' => Ok('\n'),
                    '^' => Ok('^'),
                    '-' => Ok('-'),
                    '\\' => Ok('\\'),
                    _ => {
                        Err(RegexError {
                            position: pos,
                            message: format!(
                                "Cannot escape character inside character class {}, try replacing /\\{}/ with /{}/",
                                c, c, c
                            ),
                        })
                    }
                }
            } else {
                Err(RegexError {
                    position: pos,
                    message: String::from(
                        "Expected something after '\', but found EOF instead",
                    ),
                })
            }
        }

        while let Some((pos, chr)) = input.next() {
            match chr {
                '\\' => {
                    if let Some(cr) = last {
                        insert(
                            (Included(cr), Included(cr)),
                            &mut tree,
                            &mut overlaps,
                        );
                    }
                    last = Some(read_escaped_char(input, pos)?);
                }
                '^' if pos == actual + 1 => {
                    negated = true;
                }
                '-' => {
                    if let Some(c1) = last {
                        if let Some((_, c2)) = input.next() {
                            let chr = if c2 == '\\' {
                                read_escaped_char(input, pos)?
                            } else {
                                c2
                            };
                            insert(
                                (Included(c1), Included(chr)),
                                &mut tree,
                                &mut overlaps,
                            );
                        } else {
                            return Err(RegexError {
                                position: pos,
                                message: String::from(
                                    "Expected something after '-', but found EOF instead",
                                ),
                            });
                        }
                        last = None;
                    } else {
                        insert(
                            (Included('-'), Included('-')),
                            &mut tree,
                            &mut overlaps,
                        );
                    }
                }
                ']' => {
                    if let Some(c) = last {
                        insert(
                            (Included(c), Included(c)),
                            &mut tree,
                            &mut overlaps,
                        );
                    }
                    if !overlaps.is_empty() {
                        let mut err_str = format!("Found {} overlaps in character class from position {} to position {}: ", overlaps.len(), actual, pos+1);
                        for overlap in overlaps {
                            err_str.push('(');
                            for (s, e) in overlap {
                                err_str.push(s);
                                err_str.push('-');
                                err_str.push(e);
                                err_str.push(',');
                            }
                            err_str.pop();
                            err_str.push_str("), ");
                        }
                        err_str.pop();
                        err_str.pop();
                        return Err(RegexError {
                            position: pos + 1,
                            message: err_str,
                        });
                    } else {
                        return Ok(Regex::CharacterClass(tree, negated));
                    }
                }
                c => {
                    if let Some(cr) = last {
                        insert(
                            (Included(cr), Included(cr)),
                            &mut tree,
                            &mut overlaps,
                        );
                    }
                    last = Some(c);
                }
            }
        }
        Err(RegexError {position: size, message: format!("Expected end of character class, but found EOF. Try adding ']' if you really meant to have a character class, or adding '\\' at position {} if you didn't want a character class", actual)})
    }

    fn add(regex: Regex, stack: &mut Vec<(Regex, Option<Regex>, usize)>) {
        let (last, remainder, group) = stack.pop().unwrap();
        stack.push((concat(last, regex), remainder, group));
    }

    fn concat(left: Regex, right: Regex) -> Regex {
        if let Regex::Empty = left {
            right
        } else {
            Regex::Concat(Box::new(left), Box::new(right))
        }
    }

    fn kleene_star(exp: Regex, pos: usize) -> Result<Regex, RegexError> {
        match exp {
            Regex::Concat(r1, r2) => {
                Ok(Regex::Concat(r1, Box::new(kleene_star(*r2, pos)?)))
            }
            Regex::Option(r1, r2) => {
                Ok(Regex::Option(r1, Box::new(kleene_star(*r2, pos)?)))
            }
            Regex::Empty => Err(RegexError {
                position: pos,
                message: String::from(
                    "Cannot apply kleene star to empty regex.",
                ),
            }),
            Regex::KleeneStar(..) => Err(RegexError {
                position: pos,
                message: String::from("Cannot apply kleene star twice."),
            }),
            Regex::Optional(..) => Err(RegexError {
                position: pos,
                message: String::from(
                    "Cannot apply kleene star on an optional group.",
                ),
            }),
            Regex::Repetition(..) => Err(RegexError {
                position: pos,
                message: String::from(
                    "Cannot apply kleene star and repetition.",
                ),
            }),
            r => Ok(Regex::KleeneStar(Box::new(r))),
        }
    }

    fn repetition(exp: Regex, pos: usize) -> Result<Regex, RegexError> {
        match exp {
            Regex::Concat(r1, r2) => {
                Ok(Regex::Concat(r1, Box::new(repetition(*r2, pos)?)))
            }
            Regex::Option(r1, r2) => {
                Ok(Regex::Option(r1, Box::new(repetition(*r2, pos)?)))
            }
            Regex::Empty => Err(RegexError {
                position: pos,
                message: String::from(
                    "Cannot apply repetition to empty regex.",
                ),
            }),
            Regex::KleeneStar(..) => Err(RegexError {
                position: pos,
                message: String::from("Cannot apply repetition twice."),
            }),
            Regex::Optional(..) => Err(RegexError {
                position: pos,
                message: String::from(
                    "Cannot apply repetition on an optional group.",
                ),
            }),
            Regex::Repetition(..) => Err(RegexError {
                position: pos,
                message: String::from(
                    "Cannot apply repetition and kleene star.",
                ),
            }),
            r => Ok(Regex::Repetition(Box::new(r))),
        }
    }

    fn optional(exp: Regex, pos: usize) -> Result<Regex, RegexError> {
        match exp {
            Regex::Concat(r1, r2) => {
                Ok(Regex::Concat(r1, Box::new(optional(*r2, pos)?)))
            }
            Regex::Option(r1, r2) => {
                Ok(Regex::Option(r1, Box::new(optional(*r2, pos)?)))
            }
            Regex::Empty => Err(RegexError {
                position: pos,
                message: String::from("Cannot apply optional to empty regex."),
            }),
            Regex::KleeneStar(..) => Err(RegexError {
                position: pos,
                message: String::from(
                    "Non-greedy kleene star is not supported.",
                ),
            }),
            Regex::Optional(..) => Err(RegexError {
                position: pos,
                message: String::from("Non-greedy optional is not supported."),
            }),
            Regex::Repetition(..) => Err(RegexError {
                position: pos,
                message: String::from(
                    "Non-greedy repetition is not supported.",
                ),
            }),
            r => Ok(Regex::Optional(Box::new(r))),
        }
    }

    let mut stack = vec![(Regex::Empty, None, 0)];
    let mut chrs = regex.chars().enumerate();
    let size = regex.chars().count();
    while let Some((pos, chr)) = chrs.next() {
        match chr {
            '(' => {
                stack.push((Regex::Empty, None, groups));
		groups += 1;
            }
            ')' => {
                if stack.len() <= 1 {
                    return Err(RegexError {
                        position: pos,
                        message: String::from("Closing parenthesis doesn't match any previously opened."),
                    });
                }
                {
		    let (last, remainder, nb_group) = stack.pop().unwrap();
                    let group = Regex::Group(Box::new((last, remainder).into()), nb_group);
                    let (last, remainder, nb_group) = stack.pop().unwrap();
                    stack.push((concat(last, group), remainder, nb_group));
                }
            }
            '?' => {
                let (last, remainder, nb_group) = stack.pop().unwrap();
                stack.push((optional(last, pos)?, remainder, nb_group));
            }
            '*' => {
                let (last, remainder, nb_group) = stack.pop().unwrap();
                stack.push((kleene_star(last, pos)?, remainder, nb_group));
            }
            '+' => {
                let (last, remainder, nb_group) = stack.pop().unwrap();
                stack.push((repetition(last, pos)?, remainder, nb_group));
            }
            '|' => {
		let (l, remainder, group) = stack.pop().unwrap();
                let last = (l, remainder).into();
                stack.push((Regex::Empty, Some(last), group));
            }
	    '$' => return Err(RegexError {
		position: pos,
		message: String::from("End-of-line /$/ is not supported. Matches are anchored anyways. Try /\\n/ instead.")
	    }),
	    '^' => return Err(RegexError {
		position: pos,
		message: String::from("Start-of-line /^/ is not supported. Matches are anchored anyways.")
	    }),
            '.' => add(Regex::Any, &mut stack),
            '\\' => {
                if let Some((pos, chr)) = chrs.next() {
                    match chr {
			'\\' | '.' | '(' | ')' | '?' | '+' | '*' | '|' | '$' | '^' | '[' => add(Regex::Char(chr), &mut stack),
			'A' => return Err(RegexError {
			    position: pos,
			    message: String::from("Start of the string anchor /\\A/ is not supported.")
			}),
			'N' => return Err(RegexError {
			    position: pos,
			    message: String::from("Not a line break shorthand /\\N/ is not supported. Try /[^\\n]/ instead.")
			}),
			'Z' => add(Regex::EOF, &mut stack),
			'b' => add(Regex::WordBoundary, &mut stack),
			'd' => add(Regex::Digit, &mut stack),
			'h' => return Err(RegexError {
			    position: pos,
			    message: String::from("Horizontal whitespace / hexadecimal digit shorthand /\\h/ is not supported. Try [0-9a-f] instead if you wanted hexadecimal digit.")
			}),
			'l' => return Err(RegexError {
			    position: pos,
			    message: String::from("Lowercase shorthand /\\l/ is not supported. Try /[a-z]/ instead.")
			}),
			'n' => add(Regex::Char('\n'), &mut stack),
			'r' => return Err(RegexError {
			    position: pos,
			    message: String::from("Nonsense EOL /\\r/ is not supported. Try /\\n/ instead.")
			}),
			's' => add(Regex::Whitespace, &mut stack),
			't' => add(Regex::Char('\t'), &mut stack),
			'u' => return Err(RegexError {
			    position: pos,
			    message: String::from("Uppercase shorthand /\\u/ is not supported. Try /[A-Z]/ instead.")
			}),
			'v' => return Err(RegexError {
			    position: pos,
			    message: String::from("Vertical whitespace shorthand /\\v/ is not supported.")
			}),
                        'w' => add(Regex::WordChar, &mut stack),
			'x' => return Err(RegexError {
			    position: pos,
			    message: String::from("Hexadecimal escape /\\xFF/ is not supported.")
			}),
			'z' => add(Regex::EOF, &mut stack),
                        _ => {
                            return Err(RegexError {
                                position: pos,
                                message: format!(
                                    "Cannot escape character {}, try replacing /\\{}/ with /{}/",
                                    chr, chr, chr
                                ),
                            })
                        }
                    }
                } else {
                    return Err(RegexError {
                        position: pos,
                        message: String::from(
                            "Expected something after character '\', but found EOF instead",
                        ),
                    });
                }
            }
            '[' => {
                add(read_char_class(&mut chrs, size, pos)?, &mut stack);
            }
            c => add(Regex::Char(c), &mut stack),
        }
    }
    if stack.len() != 1 {
        Err(RegexError {
            position: size,
            message: format!(
                "Expected {} closing parenthesis, found EOF",
                stack.len() - 1
            ),
        })
    } else {
        Ok((
            {
                let (last, remainder, _) = stack.pop().unwrap();
                (last, remainder).into()
            },
            groups,
        ))
    }
}