use super::Regex;
use crate::range_set::RangeSet;
type BoxRe<S> = Box<CanonRe<S>>;
#[derive(Debug, Clone, PartialEq)]
pub enum CanonRe<S> {
Cat(BoxRe<S>, BoxRe<S>),
Star(BoxRe<S>),
Con(BoxRe<S>, BoxRe<S>),
Dis(BoxRe<S>, BoxRe<S>),
Not(BoxRe<S>),
Sym(RangeSet<S>),
Nil,
}
impl<S> CanonRe<S> {
pub const BOTTOM: Self = Self::Sym(RangeSet::EMPTY);
#[must_use]
pub fn cat(self, rhs: Self) -> Self { Self::Cat(self.into(), rhs.into()) }
#[must_use]
pub fn star(self) -> Self { Self::Star(self.into()) }
#[must_use]
pub fn con(self, rhs: Self) -> Self { Self::Con(self.into(), rhs.into()) }
#[must_use]
pub fn dis(self, rhs: Self) -> Self { Self::Dis(self.into(), rhs.into()) }
#[must_use]
pub fn not(self) -> Self { Self::Not(self.into()) }
#[must_use]
pub fn matches_empty(&self) -> bool {
match self {
Self::Cat(l, r) | Self::Con(l, r) => l.matches_empty() && r.matches_empty(),
Self::Dis(l, r) => l.matches_empty() || r.matches_empty(),
Self::Not(c) => !c.matches_empty(),
Self::Sym(_) => false,
Self::Star(_) | Self::Nil => true,
}
}
#[must_use]
#[inline]
pub fn delta<T>(&self) -> CanonRe<T> {
if self.matches_empty() {
CanonRe::Nil
} else {
CanonRe::BOTTOM
}
}
}
impl<S: Ord> CanonRe<S> {
#[must_use]
pub fn derivative(self, sym: &S) -> Self
where S: Clone + Eq {
match self {
Self::Cat(l, r) => l
.delta()
.cat(r.clone().derivative(sym))
.dis(l.derivative(sym).cat(*r)),
Self::Star(r) => r.as_ref().clone().derivative(sym).cat(r.star()),
Self::Con(l, r) => l.derivative(sym).con(r.derivative(sym)),
Self::Dis(l, r) => l.derivative(sym).dis(r.derivative(sym)),
Self::Not(r) => r.derivative(sym).not(),
Self::Sym(s) if s.contains(sym) => Self::Nil,
Self::Sym(_) | Self::Nil => Self::BOTTOM,
}
}
}
impl<S: Clone + Ord> CanonRe<S> {
pub fn start_set(&self) -> StartSet<S> {
match self {
CanonRe::Cat(l, r) => {
let set = l.start_set();
if set.nil {
set.union(&r.start_set())
} else {
set
}
},
CanonRe::Star(r) => r.start_set().union(&StartSet::NIL),
CanonRe::Con(l, r) => l.start_set().intersect(&r.start_set()),
CanonRe::Dis(l, r) => l.start_set().union(&r.start_set()),
CanonRe::Not(r) => r.inv_start_set(),
CanonRe::Sym(s) => StartSet::from(s.clone()),
CanonRe::Nil => StartSet::NIL,
}
}
fn inv_start_set(&self) -> StartSet<S> {
match self {
CanonRe::Cat(l, r) => {
let set = l.inv_start_set();
if set.nil {
set.union(&r.inv_start_set())
} else {
set
}
},
CanonRe::Star(r) => r.inv_start_set().intersect(&StartSet::NON_NIL),
CanonRe::Con(l, r) => l.inv_start_set().union(&r.inv_start_set()),
CanonRe::Dis(l, r) => l.inv_start_set().intersect(&r.inv_start_set()),
CanonRe::Not(r) => r.start_set(),
CanonRe::Sym(s) => StartSet::from(s.clone()).invert(),
CanonRe::Nil => StartSet::NON_NIL,
}
}
}
#[derive(Debug, PartialEq, Eq, Hash)]
pub struct StartSet<S> {
nil: bool,
set: RangeSet<S>,
}
impl<S> StartSet<S> {
pub const BOTTOM: Self = Self {
nil: false,
set: RangeSet::EMPTY,
};
pub const NIL: Self = Self {
nil: true,
set: RangeSet::EMPTY,
};
pub const NON_NIL: Self = Self {
nil: false,
set: RangeSet::FULL,
};
pub const TOP: Self = Self {
nil: true,
set: RangeSet::FULL,
};
}
impl<S: Clone + Ord> StartSet<S> {
pub fn union(self, other: &Self) -> Self {
let Self {
nil: l_nil,
set: l_set,
} = self;
let Self {
nil: r_nil,
set: r_set,
} = other;
Self {
nil: l_nil || *r_nil,
set: l_set.unioned(r_set),
}
}
pub fn intersect(self, other: &Self) -> Self {
let Self {
nil: l_nil,
set: l_set,
} = self;
let Self {
nil: r_nil,
set: r_set,
} = other;
Self {
nil: l_nil && *r_nil,
set: l_set.intersected(r_set),
}
}
pub fn invert(self) -> Self {
let Self { nil, set } = self;
Self {
nil: !nil,
set: set.inverted(),
}
}
}
impl<S> From<RangeSet<S>> for StartSet<S> {
fn from(set: RangeSet<S>) -> Self { Self { nil: false, set } }
}
impl<L: IntoIterator, S> From<Regex<L>> for CanonRe<S>
where L::Item: Into<RangeSet<S>>
{
fn from(re: Regex<L>) -> Self {
match re {
Regex::Con(v) => v
.into_iter()
.map(Into::into)
.reduce(CanonRe::con)
.unwrap_or(CanonRe::BOTTOM.not()),
Regex::Dis(v) => v
.into_iter()
.map(Into::into)
.reduce(CanonRe::dis)
.unwrap_or(CanonRe::BOTTOM),
Regex::Not(r) => CanonRe::not((*r).into()),
Regex::Cat(v) => v
.into_iter()
.map(Into::into)
.reduce(CanonRe::cat)
.unwrap_or(CanonRe::Nil),
Regex::Star(r) => CanonRe::star((*r).into()),
Regex::Lit(l) => l
.into_iter()
.map(|l| CanonRe::Sym(l.into()))
.reduce(CanonRe::cat)
.unwrap_or(CanonRe::Nil),
}
}
}