use alloc::boxed::Box;
use alloc::collections::BTreeMap;
use alloc::string::String;
use alloc::vec::Vec;
use core::fmt;
#[derive(Debug, Clone)]
pub struct PatternSyntaxError {
pub message: String,
pub pattern: String,
pub index: usize,
}
impl PatternSyntaxError {
pub fn new(message: String) -> Self {
Self { message, pattern: String::new(), index: 0 }
}
pub fn with_context(message: String, pattern: String, index: usize) -> Self {
Self { message, pattern, index }
}
}
impl fmt::Display for PatternSyntaxError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if self.pattern.is_empty() {
write!(f, "{}", self.message)
} else {
write!(f, "{} near index {}\n{}\n", self.message, self.index, self.pattern)?;
for _ in 0..self.index { f.write_str(" ")?; }
f.write_str("^")
}
}
}
#[derive(Clone, Copy, Debug, Default)]
pub struct Flags {
pub case_insensitive: bool, pub multiline: bool, pub dotall: bool, pub comments: bool, pub unicode_class: bool, pub unix_lines: bool, pub unicode_case: bool, pub literal: bool, }
#[derive(Clone, Debug)]
pub struct Pattern {
pub branches: Vec<Vec<Node>>,
}
#[derive(Clone, Debug)]
pub enum Node {
Literal(char),
Dot,
Anchor(AnchorKind),
CharClass(CharClass),
Quantified {
inner: Box<Node>,
min: u32,
max: u32,
kind: QuantKind,
},
Group {
index: Option<usize>,
#[allow(dead_code)]
name: Option<String>,
inner: Pattern,
},
Lookahead {
positive: bool,
inner: Pattern,
},
Lookbehind {
positive: bool,
inner: Pattern,
},
AtomicGroup {
inner: Pattern,
},
Backreference(usize),
NamedBackreference(String),
LinebreakMatcher, GraphemeCluster, SetFlags(Flags), FlagGroup { flags: Flags,
inner: Pattern,
},
GroupEnd {
index: usize,
start: usize,
},
RestoreFlags(Flags), #[allow(dead_code)]
PositionCheck(usize), GreedyCont { atom: Box<Node>,
min: u32,
max: u32,
count: u32,
rest: Vec<Node>,
prev_pos: usize,
},
ReluctantCont { atom: Box<Node>,
min: u32,
max: u32,
count: u32,
rest: Vec<Node>,
prev_pos: usize,
},
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum AnchorKind {
StartOfLine, EndOfLine, StartOfInput, EndOfInput, EndOfInputBeforeFinalNewline, WordBoundary, NonWordBoundary, PreviousMatchEnd, }
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum QuantKind {
Greedy,
Reluctant,
Possessive,
}
#[derive(Clone, Debug)]
pub struct CharClass {
pub negated: bool,
pub items: Vec<CharClassItem>,
}
#[derive(Clone, Debug)]
pub enum CharClassItem {
Single(char),
Range(char, char),
Predefined(PredefinedClass),
UnicodeProperty {
name: String,
negated: bool,
},
Nested(CharClass),
Intersection(Vec<CharClassItem>, Vec<CharClassItem>),
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum PredefinedClass {
Digit, NonDigit, Word, NonWord, Whitespace, NonWhitespace, HorizWhitespace, NonHorizWhitespace, VertWhitespace, NonVertWhitespace, }
#[derive(Debug, Clone, PartialEq)]
pub struct MatchInfo {
pub matched_text: String,
pub start: usize,
pub end: usize,
pub groups: Vec<Option<String>>,
pub group_positions: Vec<Option<(usize, usize)>>,
pub named_groups: BTreeMap<String, String>,
}
impl MatchInfo {
pub fn group(&self, n: usize) -> Option<&str> {
if n == 0 {
Some(self.matched_text.as_str())
} else {
self.groups.get(n - 1).and_then(|g| g.as_deref())
}
}
pub fn name(&self, name: &str) -> Option<&str> {
self.named_groups.get(name).map(|s| s.as_str())
}
pub fn group_count(&self) -> usize {
self.groups.len()
}
}