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
mod compiler;
pub(crate) mod dfa;
mod nfa;

use std::rc::Rc;

use crate::CompiledRegex;

use self::compiler::compile_regex;

pub enum Regex<T> {
    Satisfy(Rc<dyn Fn(&T) -> bool>),
    Concat(Box<Regex<T>>, Box<Regex<T>>),
    Group(Box<Regex<T>>),
    Or(Box<Regex<T>>, Box<Regex<T>>),
    ZeroOrOne(Box<Regex<T>>),
    Repeat0(Box<Regex<T>>),
    Repeat1(Box<Regex<T>>),
}

impl<T> std::fmt::Debug for Regex<T> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Regex::Satisfy(_) => write!(f, "{{#<fn>}}"),
            Regex::Concat(l, r) => write!(f, "{:?}{:?}", l, r),
            Regex::Group(r) => write!(f, "({:?})", r),
            Regex::Or(l, r) => write!(f, "{:?}|{:?}", l, r),
            Regex::Repeat0(r) => write!(f, "{:?}*", r),
            Regex::ZeroOrOne(r) => write!(f, "{:?}?", r),
            Regex::Repeat1(r) => write!(f, "{:?}+", r),
        }
    }
}

impl<T: 'static> Regex<T> {
    pub fn satisfy(f: impl Fn(&T) -> bool + 'static) -> Self {
        Regex::Satisfy(Rc::new(f))
    }

    pub fn any() -> Self {
        Regex::Satisfy(Rc::new(|_| true))
    }

    pub fn zero_or_one(reg: Self) -> Self {
        Regex::ZeroOrOne(reg.into())
    }

    pub fn repeat1(reg: Self) -> Self {
        Regex::Repeat1(reg.into())
    }

    pub fn repeat0(reg: Self) -> Self {
        Regex::Repeat0(reg.into())
    }

    pub fn concat(r: Self, s: Self) -> Self {
        Regex::Concat(r.into(), s.into())
    }

    pub fn or(left: Self, right: Self) -> Self {
        Regex::Or(left.into(), right.into())
    }

    pub fn group(reg: Self) -> Self {
        Regex::Group(reg.into())
    }

    pub fn is(value: T) -> Self
    where
        T: PartialEq + 'static,
    {
        Regex::Satisfy(Rc::new(move |v| *v == value))
    }

    pub fn seq(values: &[T]) -> Self
    where
        T: PartialEq + Clone + 'static,
    {
        if values.len() == 1 {
            Regex::is(values[0].clone())
        } else {
            let mut reg = Regex::is(values[0].clone());
            for v in values.iter().skip(1) {
                reg = Regex::concat(reg, Regex::is(v.clone()));
            }
            reg
        }
    }

    pub fn compile(&self) -> CompiledRegex<T> {
        compile_regex(self)
    }
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn match_is() {
        let reg = Regex::is(1).compile();
        assert!(reg.is_match(&[1]));
    }

    #[test]
    fn match_or() {
        let reg = Regex::or(Regex::is(1), Regex::is(2)).compile();
        assert!(reg.is_match(&[1]));
        assert!(reg.is_match(&[2]));
    }

    #[test]
    fn match_concat() {
        let reg = Regex::concat(Regex::is(1), Regex::is(2)).compile();
        assert!(reg.is_match(&[1, 2]));
    }

    #[test]
    fn test_group() {
        let reg = Regex::group(Regex::concat(Regex::is(1), Regex::is(2))).compile();
        assert!(reg.is_match(&[1, 2]));
    }

    #[test]
    fn match_seq() {
        let reg = Regex::seq(&[1, 2]).compile();
        assert!(reg.is_match(&[1, 2]));
    }

    #[test]
    fn match_star() {
        let reg = Regex::repeat0(Regex::is(1)).compile();
        assert!(reg.is_match(&[]));
        assert!(reg.is_match(&[1]));
        assert!(reg.is_match(&[1, 1]));
    }

    #[test]
    fn match_some() {
        let reg = Regex::repeat1(Regex::is(1)).compile();
        assert!(!reg.is_match(&[]));
        assert!(reg.is_match(&[1]));
        assert!(reg.is_match(&[1, 1]));
        assert!(!reg.is_match(&[1, 2]));
    }

    #[test]
    fn match_zero_or_one() {
        let reg = Regex::zero_or_one(Regex::is(1)).compile();
        assert!(reg.is_match(&[]));
        assert!(reg.is_match(&[1]));
        assert!(!reg.is_match(&[1, 1]));
    }

    #[test]
    fn match_statisfy() {
        let reg = Regex::satisfy(|v| v % 2 == 0).compile();
        assert!(reg.is_match(&[0]));
        assert!(!reg.is_match(&[1]));
        assert!(reg.is_match(&[2]));
        assert!(!reg.is_match(&[3]));
    }

    #[test]
    fn match_any() {
        let reg = Regex::any().compile();
        assert!(reg.is_match(&[1]));
        assert!(reg.is_match(&[42]));
    }

    #[test]
    fn match_complex() {
        let is_fizz = |x: &i32| x % 3 == 0;
        let is_buzz = |x: &i32| x % 5 == 0;
        let is_fizz_buzz = |x: &i32| x % 15 == 0;
        let reg = Regex::concat(
            Regex::satisfy(is_fizz),
            Regex::repeat1(Regex::concat(
                Regex::satisfy(is_buzz),
                Regex::satisfy(is_fizz_buzz),
            )),
        )
        .compile();
        assert!(!reg.is_match(&[1, 2, 3]));
        assert!(reg.is_match(&[3, 5, 15]));
        assert!(reg.is_match(&[6, 10, 15, 10, 30]));
    }
}