From 6ea4f64e564e901ae78b3d40d3dd01fa01810c3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonathan=20D=C3=B6nszelmann?= Date: Mon, 30 Sep 2024 12:17:43 +0200 Subject: [PATCH] fix regex matcher --- scopegraphs-regular-expressions/src/regex.rs | 8 +++++-- scopegraphs/tests/test_regex.rs | 23 ++++++++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/scopegraphs-regular-expressions/src/regex.rs b/scopegraphs-regular-expressions/src/regex.rs index a400f7c..0db0aa9 100644 --- a/scopegraphs-regular-expressions/src/regex.rs +++ b/scopegraphs-regular-expressions/src/regex.rs @@ -299,8 +299,12 @@ fn normalize_or(l: &Rc, r: &Rc, ab: &AlphabetOrder) -> Rc { // ~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), diff --git a/scopegraphs/tests/test_regex.rs b/scopegraphs/tests/test_regex.rs index 4844e39..45a0087 100644 --- a/scopegraphs/tests/test_regex.rs +++ b/scopegraphs/tests/test_regex.rs @@ -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 = 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 = 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::*;