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
#[derive(Debug, PartialEq)]
pub enum Ast {
    Byte(u8),
    Digit,
    Seq(Vec<Ast>),
    Or(Box<Ast>, Box<Ast>),
    ZeroOrOne(Box<Ast>),
    CaseSensitive(Box<Ast>),
}

impl Ast {
    pub fn byte(b: u8) -> Self {
        Self::Byte(b)
    }

    pub fn digit() -> Self {
        Self::Digit
    }

    pub fn case_sensitive(inner: Ast) -> Self {
        Self::CaseSensitive(Box::new(inner))
    }

    pub fn then(lhs: Ast, rhs: Ast) -> Self {
        if let Self::Seq(mut inner) = lhs {
            inner.push(rhs);
            Self::Seq(inner)
        } else {
            Self::Seq(vec![lhs, rhs])
        }
    }

    pub fn or(lhs: Ast, rhs: Ast) -> Self {
        Self::Or(Box::new(lhs), Box::new(rhs))
    }

    pub fn zero_or_one(inner: Ast) -> Self {
        Self::ZeroOrOne(Box::new(inner))
    }
}