diff --git a/src/main.rs b/src/main.rs --- a/src/main.rs +++ b/src/main.rs @@ -18,6 +18,10 @@ struct Asterisk { content: Box } +struct Plus { + content: Box +} + struct Chain { content: Vec> } @@ -60,6 +64,28 @@ impl Token for Asterisk { } } +impl Token for Plus { + fn list_first(&self) -> Vec { + return self.content.list_first(); + } + + fn list_last(&self) -> Vec { + return self.content.list_last(); + } + + fn list_neighbours(&self) -> Vec<(usize, usize)> { + let mut res = self.content.list_neighbours(); + + for x in self.list_last() { + for y in self.list_first() { + res.push((x, y)); + } + } + + return res; + } +} + impl Token for Chain { fn list_first(&self) -> Vec { let mut res = Vec::new(); @@ -136,6 +162,11 @@ fn parse(pattern: &String, offset: usize res.push(Box::new(Asterisk{content: token})); i += 1; } + '+' => { + let token = res.pop().unwrap(); + res.push(Box::new(Plus{content: token})); + i += 1; + } c => { res.push(Box::new(Symbol{position: i+offset, value: c})); i += 1; @@ -202,7 +233,7 @@ impl Regexp { fn main() { let tests = ["a", "ab", "aabb", "abab", "abcd", "abcbcdbcd"]; - for pattern in ["a*b*", "(ab)*", "a((bc)*d)*"] { + for pattern in ["a*b*", "a+b+", "(ab)*", "(ab)+", "a((bc)*d)*"] { println!("# {pattern}"); let r = Regexp::new(&pattern.to_string()); for &t in tests.iter() {