Skip to content

Commit

Permalink
fix regex matcher
Browse files Browse the repository at this point in the history
  • Loading branch information
jdonszelmann committed Sep 30, 2024
1 parent 453dd02 commit 6ea4f64
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
8 changes: 6 additions & 2 deletions scopegraphs-regular-expressions/src/regex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,8 +299,12 @@ fn normalize_or(l: &Rc<Regex>, r: &Rc<Regex>, ab: &AlphabetOrder) -> Rc<Regex> {
// ~0 | a => ~0
(Regex::Complement(c), _) if c.deref() == &Regex::EmptySet => l,
// (a | (b | c)) => (b | (a | c)) if b < a
(_, Regex::Or(il, ir)) if l.compare(il, ab) > 0 => {
normalize_or(il, &normalize_or(&l, ir, ab), ab)
(_, Regex::Or(il, ir)) => {
if l.compare(il, ab) > 0 {
normalize_or(il, &normalize_or(&l, ir, ab), ab)
} else {
Regex::Or(l, r).into()
}
}
// a | b => b | a if b < a
_ if l.compare(&r, ab) > 0 => normalize_or(&r, &l, ab),
Expand Down
23 changes: 23 additions & 0 deletions scopegraphs/tests/test_regex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,29 @@ fn test_or_3() {
assert!(Machine::new().accepts([C]));
}

#[test]
fn test_repeat_or_2() {
use Alphabet::*;

compile_regex!(type Machine<Alphabet> = A* (A | B));

assert!(!Machine::new().accepts([A; 0]));
assert!(Machine::new().accepts([A]));
assert!(Machine::new().accepts([B]));
}

#[test]
fn test_repeat_or_3() {
use Alphabet::*;

compile_regex!(type Machine<Alphabet> = A* (A | B | C));

assert!(!Machine::new().accepts([A; 0]));
assert!(Machine::new().accepts([A]));
assert!(Machine::new().accepts([B]));
assert!(Machine::new().accepts([C]));
}

#[test]
fn test_negate() {
use Alphabet::*;
Expand Down

0 comments on commit 6ea4f64

Please sign in to comment.