#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u8)]
pub enum ElementType {
End = 0,
Alt = 1,
RuleRef = 2,
Char = 3,
CharNot = 4,
CharRngUpper = 5,
CharAlt = 6,
CharAny = 7,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Element {
pub etype: ElementType,
pub value: u32,
}
impl Element {
pub fn new(etype: ElementType, value: u32) -> Self {
Self { etype, value }
}
pub fn end() -> Self {
Self::new(ElementType::End, 0)
}
pub fn alt() -> Self {
Self::new(ElementType::Alt, 0)
}
pub fn rule_ref(rule_id: u32) -> Self {
Self::new(ElementType::RuleRef, rule_id)
}
pub fn char_(cp: u32) -> Self {
Self::new(ElementType::Char, cp)
}
pub fn char_not(cp: u32) -> Self {
Self::new(ElementType::CharNot, cp)
}
pub fn char_rng_upper(cp: u32) -> Self {
Self::new(ElementType::CharRngUpper, cp)
}
pub fn char_alt(cp: u32) -> Self {
Self::new(ElementType::CharAlt, cp)
}
pub fn char_any() -> Self {
Self::new(ElementType::CharAny, 0)
}
pub fn is_char_element(&self) -> bool {
matches!(
self.etype,
ElementType::Char
| ElementType::CharNot
| ElementType::CharAlt
| ElementType::CharRngUpper
| ElementType::CharAny
)
}
pub fn is_end_of_sequence(&self) -> bool {
matches!(self.etype, ElementType::End | ElementType::Alt)
}
}
pub type Rule = Vec<Element>;
pub type Rules = Vec<Rule>;
pub type Pos = (usize, usize);
pub type Stack = Vec<Pos>;
pub type Stacks = Vec<Stack>;