diff --git a/src/main.rs b/src/regexp.rs copy from src/main.rs copy to src/regexp.rs --- a/src/main.rs +++ b/src/regexp.rs @@ -1,186 +1,10 @@ use std::collections::{HashMap, HashSet}; +mod token; +use token::{Token, parse}; + const START: usize = usize::MAX; -trait Token { - fn is_skippable(&self) -> bool {false} - fn list_first(&self) -> Vec; - fn list_last(&self) -> Vec; - fn list_neighbours(&self) -> Vec<(usize, usize)>; -} - -struct Symbol { - position: usize, - value: char -} - -struct Asterisk { - content: Box -} - -struct Plus { - content: Box -} - -struct Chain { - content: Vec> -} - -impl Token for Symbol { - fn list_first(&self) -> Vec { - return vec![self.position]; - } - - fn list_last(&self) -> Vec { - return vec![self.position]; - } - - fn list_neighbours(&self) -> Vec<(usize, usize)> { - return vec![]; - } -} - -impl Token for Asterisk { - fn is_skippable(&self) -> bool {true} - - 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 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 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() { - res.append(&mut token.list_first()); - if !token.is_skippable() {break;} - } - - return res; - } - - fn list_last(&self) -> Vec { - let mut res = Vec::new(); - for token in self.content.iter().rev() { - res.append(&mut token.list_last()); - if !token.is_skippable() {break;} - } - - return res; - } - - fn list_neighbours(&self) -> Vec<(usize, usize)> { - let mut res = Vec::new(); - let mut previous: Vec<&Box> = Vec::new(); - for token in self.content.iter() { - for t in previous.iter() { - for x in t.list_last() { - for y in token.list_first() { - res.push((x, y)); - } - } - } - res.append(&mut token.list_neighbours()); - - if token.is_skippable() { - previous.push(token); - } else { - previous = vec![token]; - } - } - - return res; - } -} - -fn find_closing_parenthesis(s: &String) -> Option { - let chars: Vec = s.chars().collect(); - let mut counter = 0; - - for (i, c) in chars.iter().enumerate() { - if *c == '(' {counter += 1;} - else if *c == ')' {counter -= 1;} - if counter == 0 {return Some(i);} - } - - return None; -} - -fn parse(pattern: &String, offset: usize) -> Chain { - let chars: Vec = pattern.chars().collect(); - let mut res: Vec> = Vec::new(); - let mut i = 0; - while i < pattern.len() { - let c = chars[i]; - match c { - '(' => { - let j = find_closing_parenthesis(&pattern[i..].to_string()).unwrap() + i; - let inner_content = parse(&pattern[i+1..j].to_string(), offset+i+1); - res.push(Box::new(inner_content)); - i = j+1; - } - '*' => { - let token = res.pop().unwrap(); - 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; - } - } - } - - return Chain{content: res}; -} - fn encode_set(set: &HashSet) -> u64 { let mut res = 0; for x in set.iter() { @@ -204,13 +28,13 @@ fn decode_set(x: u64) ->HashSet { return res; } -struct Regexp { +pub struct Regexp { rules: HashMap<(usize, char), HashSet>, end_states: HashSet } impl Regexp { - fn new(pattern: &String) -> Regexp { + pub fn new(pattern: &String) -> Regexp { let r = parse(pattern, 0); let pattern_chars = Vec::from_iter(pattern.chars()); let mut rules: HashMap<(usize, char), HashSet> = HashMap::new(); @@ -260,7 +84,7 @@ impl Regexp { return multistate.iter().any(|x| self.end_states.contains(x)); } - fn determinize(&self) -> RegexpDFA { + pub fn determinize(&self) -> RegexpDFA { let mut rules: HashMap<(u64, char), u64> = HashMap::new(); let mut end_states: HashSet = HashSet::new(); if self.end_states.contains(&START) {end_states.insert(START as u64);} @@ -299,13 +123,13 @@ impl Regexp { } } -struct RegexpDFA { +pub struct RegexpDFA { rules: HashMap<(u64, char), u64>, end_states: HashSet } impl RegexpDFA { - fn eval(&self, s: String) -> bool { + pub fn eval(&self, s: String) -> bool { let mut state = START as u64; for c in s.chars() { @@ -319,15 +143,3 @@ impl RegexpDFA { return self.end_states.contains(&state); } } - -fn main() { - 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()).determinize(); - for &t in tests.iter() { - println!("{t} {}", r.eval(t.to_string())); - } - println!(); - } -}