pub const INVALID_STATE: u32 = u32::MAX;
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum PatternError {
ParseError {
offset: usize,
message: String,
},
UnsupportedEscape {
offset: usize,
escape: char,
},
RecursionLimit {
limit: usize,
},
StateOverflow {
limit: u32,
},
EmptyPatternSet,
}
impl core::fmt::Display for PatternError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Self::ParseError { offset, message } => {
write!(f, "pattern parse error at byte {offset}: {message}")
}
Self::UnsupportedEscape { offset, escape } => {
write!(
f,
"pattern parse error at byte {offset}: Fix: unsupported escape `\\{escape}`; spell the byte set explicitly with a character class."
)
}
Self::RecursionLimit { limit } => {
write!(
f,
"Fix: regex group nesting exceeded the recursion limit ({limit}); flatten or split the expression."
)
}
Self::StateOverflow { limit } => {
write!(
f,
"Fix: state count exceeded the pipeline budget ({limit}); split the pattern set into smaller batches."
)
}
Self::EmptyPatternSet => write!(
f,
"Fix: pattern set must contain at least one non-empty pattern."
),
}
}
}
impl std::error::Error for PatternError {}
pub type NfaStateId = u32;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct NfaEdge {
pub from: NfaStateId,
pub byte: Option<u8>,
pub to: NfaStateId,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Nfa {
pub state_count: u32,
pub edges: Vec<NfaEdge>,
pub start: NfaStateId,
pub accept: Vec<bool>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Dfa {
pub state_count: u32,
pub transitions: Vec<u32>,
pub start: u32,
pub accept: Vec<bool>,
}
impl Dfa {
#[must_use]
#[inline]
pub fn go(&self, state: u32, byte: u8) -> u32 {
self.transitions[(state as usize) * 256 + byte as usize]
}
#[inline]
pub fn set(&mut self, state: u32, byte: u8, next: u32) {
self.transitions[(state as usize) * 256 + byte as usize] = next;
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum DfaPackFormat {
Dense,
EquivClass,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PackedDfa {
pub format: DfaPackFormat,
pub state_count: u32,
pub start: u32,
pub bytes: Vec<u8>,
}