Enum safe_regex_compiler::parser::FinalNode
source · [−]pub enum FinalNode {
Byte(u8),
AnyByte,
Seq(Vec<FinalNode>),
Class(bool, Vec<ClassItem>),
Group(Box<FinalNode>),
NonCapturingGroup(Box<FinalNode>),
Alt(Vec<FinalNode>),
Repeat(Box<FinalNode>, usize, Option<usize>),
}
Expand description
A completely parsed element of a regular expression abstract syntax tree (AST).
A FinalNode
could represent an entire regular expression (it is the root)
or a small part of one.
Since a regular expression ultimately processes bytes, the leaves of the AST are nodes with bytes:
All other variants are edges of the AST:
Variants
Byte(u8)
Byte(u8)
A byte of input. This may be a non-printable byte that was escaped in the source regular expression.
Examples
use safe_regex_compiler::parser::parse;
use safe_regex_compiler::parser::FinalNode;
assert_eq!(
Ok(FinalNode::Byte(b'a')),
parse(br"a"),
);
assert_eq!(
Ok(FinalNode::Byte(b'\n')),
parse(br"\n"),
);
assert_eq!(
Ok(FinalNode::Byte(b'\\')),
parse(br"\\"),
);
assert_eq!(
Ok(FinalNode::Byte(0x12)),
parse(br"\x12"),
);
AnyByte
AnyByte
Matches any byte of input. This is the .
operator.
Example
use safe_regex_compiler::parser::parse;
use safe_regex_compiler::parser::FinalNode;
assert_eq!(
Ok(FinalNode::AnyByte),
parse(br"."),
);
Seq(Vec<FinalNode>)
Seq(Vec<FinalNode>)
A sequence of nodes.
Example
use safe_regex_compiler::parser::parse;
use safe_regex_compiler::parser::FinalNode;
assert_eq!(
Ok(FinalNode::Seq(vec![
FinalNode::Byte(b'a'),
FinalNode::Byte(b'b')],
)),
parse(br"ab"),
);
Class(bool, Vec<ClassItem>)
Class(inclusive: bool, Vec<ClassItem>)
A character class. Matches any byte in the class.
See ClassItem
Examples
use safe_regex_compiler::parser::parse;
use safe_regex_compiler::parser::FinalNode;
use safe_regex_compiler::parser::ClassItem;
assert_eq!(
Ok(FinalNode::Class(true, vec![
ClassItem::Byte(b'a'),
ClassItem::Byte(b'b'),
])),
parse(br"[ab]"),
);
assert_eq!(
Ok(FinalNode::Class(false, vec![
ClassItem::Byte(b'a'),
ClassItem::Byte(b'b'),
])),
parse(br"[^ab]"),
);
assert_eq!(
Ok(FinalNode::Class(true, vec![
ClassItem::ByteRange(b'0', b'9'),
])),
parse(br"[0-9]"),
);
Group(Box<FinalNode>)
Group(Box<FinalNode>)
A capturing group of nodes. Regular expression authors use it to override operator precedence rules and to capture sub-slices of input.
Examples
use safe_regex_compiler::parser::parse;
use safe_regex_compiler::parser::FinalNode;
assert_eq!(
Ok(FinalNode::Seq(vec![
FinalNode::Byte(b'a'),
FinalNode::Group(Box::new(
FinalNode::Alt(vec![
FinalNode::Byte(b'b'),
FinalNode::Byte(b'c'),
])
)),
])),
parse(br"a(b|c)"),
);
assert_eq!(
Ok(FinalNode::Repeat(
Box::new(FinalNode::Group(Box::new(
FinalNode::Seq(vec![
FinalNode::Byte(b'a'),
FinalNode::Byte(b'b'),
])
))),
0, // min
None, // max
)),
parse(br"(ab)*"),
);
NonCapturingGroup(Box<FinalNode>)
NonCapturingGroup(Box<FinalNode>)
A non-capturing group of nodes. Regular expression authors use it to override operator precedence rules.
Examples
use safe_regex_compiler::parser::parse;
use safe_regex_compiler::parser::FinalNode;
assert_eq!(
Ok(FinalNode::Seq(vec![
FinalNode::Byte(b'a'),
FinalNode::NonCapturingGroup(Box::new(
FinalNode::Alt(vec![
FinalNode::Byte(b'b'),
FinalNode::Byte(b'c'),
])
)),
])),
parse(br"a(?:b|c)"),
);
assert_eq!(
Ok(FinalNode::Repeat(
Box::new(FinalNode::NonCapturingGroup(Box::new(
FinalNode::Seq(vec![
FinalNode::Byte(b'a'),
FinalNode::Byte(b'b'),
])
))),
0, // min
None, // max
)),
parse(br"(?:ab)*"),
);
Alt(Vec<FinalNode>)
Alt(Vec<FinalNode>)
A list of alternate nodes. The input can match any of them.
Examples
use safe_regex_compiler::parser::parse;
use safe_regex_compiler::parser::FinalNode;
assert_eq!(
Ok(FinalNode::Alt(vec![
FinalNode::Byte(b'a'),
FinalNode::Byte(b'b'),
])),
parse(br"a|b"),
);
assert_eq!(
Ok(FinalNode::Alt(vec![
FinalNode::Byte(b'a'),
FinalNode::Byte(b'b'),
FinalNode::Seq(vec![
FinalNode::AnyByte,
FinalNode::Byte(b'c'),
]),
])),
parse(br"a|b|.c"),
);
Repeat(Box<FinalNode>, usize, Option<usize>)
Repeat(Box<FinalNode>, min: usize, max: Option<usize>)
A repetition of a node. It contains the minimum number of repetitions and an optional inclusive maximum number.
Examples
use safe_regex_compiler::parser::parse;
use safe_regex_compiler::parser::FinalNode;
assert_eq!(
Ok(FinalNode::Repeat(
Box::new(FinalNode::Byte(b'a')),
0, // min
Some(1), // max
)),
parse(br"a?"),
);
assert_eq!(
Ok(FinalNode::Repeat(
Box::new(FinalNode::Byte(b'a')),
0, // min
None, // max
)),
parse(br"a*"),
);
assert_eq!(
Ok(FinalNode::Repeat(
Box::new(FinalNode::Byte(b'a')),
1, // min
None, // max
)),
parse(br"a+"),
);
assert_eq!(
Ok(FinalNode::Repeat(
Box::new(FinalNode::Byte(b'a')),
5, // min
Some(5), // max
)),
parse(br"a{5}"),
);
assert_eq!(
Ok(FinalNode::Repeat(
Box::new(FinalNode::Byte(b'a')),
5, // min
None, // max
)),
parse(br"a{5,}"),
);
assert_eq!(
Ok(FinalNode::Repeat(
Box::new(FinalNode::Byte(b'a')),
0, // min
Some(7), // max
)),
parse(br"a{,7}"),
);
assert_eq!(
Ok(FinalNode::Repeat(
Box::new(FinalNode::Byte(b'a')),
5, // min
Some(7), // max
)),
parse(br"a{5,7}"),
);
assert_eq!(
Ok(FinalNode::Repeat(
Box::new(FinalNode::Byte(b'a')),
0, // min
None, // max
)),
parse(br"a{,}"),
);
Implementations
sourceimpl FinalNode
impl FinalNode
sourcepub fn unwrap_alt(self) -> Vec<FinalNode>
pub fn unwrap_alt(self) -> Vec<FinalNode>
Assumes this is a FinalNode::Alt(_)
and returns its contents.
Panics if this is a different enum variant.
Trait Implementations
sourceimpl PartialOrd<FinalNode> for FinalNode
impl PartialOrd<FinalNode> for FinalNode
sourcefn partial_cmp(&self, other: &FinalNode) -> Option<Ordering>
fn partial_cmp(&self, other: &FinalNode) -> Option<Ordering>
This method returns an ordering between self
and other
values if one exists. Read more
1.0.0 · sourcefn lt(&self, other: &Rhs) -> bool
fn lt(&self, other: &Rhs) -> bool
This method tests less than (for self
and other
) and is used by the <
operator. Read more
1.0.0 · sourcefn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
impl StructuralPartialEq for FinalNode
Auto Trait Implementations
impl RefUnwindSafe for FinalNode
impl Send for FinalNode
impl Sync for FinalNode
impl Unpin for FinalNode
impl UnwindSafe for FinalNode
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcepub fn borrow_mut(&mut self) -> &mut T
pub fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcepub fn to_owned(&self) -> T
pub fn to_owned(&self) -> T
Creates owned data from borrowed data, usually by cloning. Read more
sourcepub fn clone_into(&self, target: &mut T)
pub fn clone_into(&self, target: &mut T)
toowned_clone_into
)Uses borrowed data to replace owned data, usually by cloning. Read more