Enum 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§

Source§

impl FinalNode

Source

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§

Source§

impl Clone for FinalNode

Source§

fn clone(&self) -> FinalNode

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for FinalNode

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl PartialEq for FinalNode

Source§

fn eq(&self, other: &FinalNode) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for FinalNode

Source§

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 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl StructuralPartialEq for FinalNode

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.