diff --git a/src/main.rs b/src/main.rs --- a/src/main.rs +++ b/src/main.rs @@ -87,6 +87,10 @@ impl Token for Plus { } impl Token for Chain { + fn is_skippable(&self) -> bool { + return self.content.iter().all(|x| x.is_skippable()); + } + fn list_first(&self) -> Vec { let mut res = Vec::new(); for token in self.content.iter() { @@ -206,7 +210,10 @@ impl Regexp { }; } - let end_states = HashSet::from_iter(r.list_last().into_iter()); + let mut end_states = HashSet::from_iter(r.list_last().into_iter()); + if r.is_skippable() { + end_states.insert(START); + } return Regexp{rules, end_states}; } @@ -232,7 +239,7 @@ impl Regexp { } fn main() { - let tests = ["a", "ab", "aabb", "abab", "abcd", "abcbcdbcd"]; + let tests = ["", "a", "ab", "aabb", "abab", "abcd", "abcbcdbcd"]; for pattern in ["a*b*", "a+b+", "(ab)*", "(ab)+", "a((bc)*d)*"] { println!("# {pattern}"); let r = Regexp::new(&pattern.to_string());