pub enum FinalNode {
    Byte(u8),
    AnyByte,
    Seq(Vec<FinalNode>),
    Class(boolVec<ClassItem>),
    Group(Box<FinalNode>),
    NonCapturingGroup(Box<FinalNode>),
    Alt(Vec<FinalNode>),
    Repeat(Box<FinalNode>, usizeOption<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(boolVec<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>, usizeOption<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

Assumes this is a FinalNode::Alt(_) and returns its contents. Panics if this is a different enum variant.

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method returns an ordering between self and other values if one exists. Read more

This method tests less than (for self and other) and is used by the < operator. Read more

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

This method tests greater than (for self and other) and is used by the > operator. Read more

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The resulting type after obtaining ownership.

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

🔬 This is a nightly-only experimental API. (toowned_clone_into)

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

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.