RegexOptions

Struct RegexOptions 

Source
pub struct RegexOptions { /* private fields */ }
Expand description

Regular expression parsing options.

§Examples

Initializing a regex with the “ignore whitespace” flag enabled:

use compile_regex::{ast::{Node, Syntax}, RegexOptions};

const SYNTAX: Syntax = RegexOptions::DEFAULT
    .ignore_whitespace(true)
    .parse(r"(?<digits> # This is a comment
        [ \d - ]+ # since `-` is a last non-whitespace char in a set,
                  # it's a literal '-'
    )");

let comment_count = SYNTAX
    .iter()
    .filter(|spanned| matches!(spanned.node, Node::Comment))
    .count();
// Sequential comments are glued together, so the number of comment nodes is 2
// rather than 3.
assert_eq!(comment_count, 2);

Implementations§

Source§

impl RegexOptions

Source

pub const DEFAULT: Self

Default options.

Source

pub const fn ignore_whitespace(self, yes: bool) -> Self

Sets whether whitespace should be ignored when parsing. This is equivalent to setting (?x) flag at the start of the regex.

Source

pub const fn try_validate(&self, regex: &str) -> Result<ValidationOutput, Error>

Tries to validate the provided regular expression.

§Errors

Returns an error if the provided regex is not a valid regular expression.

Source

pub const fn validate(&self, regex: &str) -> ValidationOutput

Validates the provided regular expression, panicking on errors. This is a shortcut for Self::try_validate().unwrap().

§Panics

Panics if the provided regex is not a valid regular expression.

Source

pub const fn try_parse<const CAP: usize>( &self, regex: &str, ) -> Result<Syntax<CAP>, Error>

Tries to parse the provided regular expression.

The CAP constant specifies the capacity of the produced Syntax. If it is exceeded, the method will return AstOverflow. Use the parse! macro to determine the capacity automatically.

§Errors
  • Returns an error if the provided regex is not a valid regular expression.
  • Errors if one of internal limits is hit (e.g., the number of syntax spans or the number of named captures).
Source

pub const fn parse<const CAP: usize>(&self, regex: &str) -> Syntax<CAP>

Parses the provided regular expression, panicking on errors. This is a shortcut for Self::try_parse().unwrap().

§Panics

Panics in the same situations in which Self::try_parse() returns an error.

Source

pub fn try_parse_to_vec(&self, regex: &str) -> Result<Vec<Spanned>, Error>

Available on crate feature alloc only.

Parses regex without restricting the number of produced spans.

§Errors

Returns an error if the provided regex is not a valid regular expression.

Trait Implementations§

Source§

impl Debug for RegexOptions

Source§

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

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

impl Default for RegexOptions

Source§

fn default() -> RegexOptions

Returns the “default value” for a type. Read more

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> 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, 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.