aboutsummaryrefslogtreecommitdiff
path: root/src/main.rs
blob: 01a41058ce446a367e7f849dd51fe21c77615398 (plain)
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
#![allow(dead_code, unused_variables, unused_mut)]

type ReToken = Box<RegexToken>;

#[derive(Debug, PartialEq, Clone)]
enum RegexToken {
    Token(ReToken),
    Symbol(char),
    Number(usize),
    Concat((ReToken, ReToken)),
    Union((ReToken, ReToken)),
    Star(ReToken),
    Dot,
    None,
}

macro_rules! Sym {
    ($c:expr) => {
        RegexToken::Symbol($c)
    };
}

macro_rules! Star {
    ($c:expr) => {
        RegexToken::Star(Box::new($c))
    };
}

macro_rules! Concat {
    ($a:expr, $b:expr) => {
        RegexToken::Concat((Box::new($a), Box::new($b)))
    };
}

macro_rules! Union {
    ($a:expr, $b:expr) => {
        RegexToken::Union((Box::new($a), Box::new($b)))
    };
}

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

    #[test]
    fn test_concat() {
        assert_eq!(
            Regex::new(String::from("ab")),
            Concat!(Sym!('a'), Sym!('b'))
        )
    }

    #[test]
    fn test_union() {
        assert_eq!(
            Regex::new(String::from("(a|b)")),
            Concat!(Union!(Sym!('a'), Sym!('b')), RegexToken::None)
        )
    }

    #[test]
    fn test_none() {
        assert_eq!(Regex::new(String::from("")), RegexToken::None)
    }

    #[test]
    fn test_star() {
        assert_eq!(
            Regex::new(String::from("a*b")),
            Concat!(Star!(Sym!('a')), Sym!('b'))
        )
    }
}

#[derive(Debug, PartialEq)]
struct Regex {}

impl Regex {
    fn new(input: String) -> RegexToken {
        Regex::parse(input)
    }

    fn parse(input: String) -> RegexToken {
        if input.is_empty() {
            return RegexToken::None;
        }

        let mut chars = input.chars().peekable();
        let mut parsed_token = Self::parse_token(&mut RegexToken::None, &mut chars);

        Self::parse_expression(&mut parsed_token, &mut chars)
    }

    fn parse_expression(
        left: &mut RegexToken,
        chars: &mut std::iter::Peekable<std::str::Chars>,
    ) -> RegexToken {
        while let Some(&next) = chars.peek() {
            match next {
                '|' => {
                    chars.next(); // Consume '|'
                    let right = Self::parse_token(left, chars);
                    *left = RegexToken::Union((Box::new(left.clone()), Box::new(right)));
                }
                '*' => {
                    chars.next(); // Consume '|'
                    let right = Self::parse_token(left, chars);
                    *left = RegexToken::Star(Box::new(left.clone()));
                }
                _ => {
                    let right = Self::parse_token(left, chars);
                    *left = RegexToken::Concat((Box::new(left.clone()), Box::new(right)));
                }
            }
        }
        left.clone()
    }

    fn parse_token(
        left: &mut RegexToken,
        chars: &mut std::iter::Peekable<std::str::Chars>,
    ) -> RegexToken {
        match chars.next() {
            Some('(') => {
                let token = Self::parse(chars.collect());
                chars.next(); // Skip ')'
                token
            }
            Some('.') => RegexToken::Dot,
            Some(c) if c.is_ascii_alphanumeric() => Sym!(c),
            Some('*') => {
                let token = Self::parse_token(left, chars);
                Star!(left.clone())
            }
            _ => RegexToken::None, // Handle other cases accordingly
        }
    }
}

fn main() {
    let input = "a*b";
    let token = Regex::new(String::from(input));
    println!("{input}\n{:#?}", token)
}