Struct Compiler
pub struct Compiler { /* private fields */ }
A builder for compiling an NFA from a regex's high-level intermediate representation (HIR).
This compiler provides a way to translate a parsed regex pattern into an NFA state graph. The NFA state graph can either be used directly to execute a search (e.g., with a Pike VM), or it can be further used to build a DFA.
This compiler provides APIs both for compiling regex patterns directly from
their concrete syntax, or via a regex_syntax::hir::Hir.
This compiler has various options that may be configured via
thompson::Config.
Note that a compiler is not the same as a thompson::Builder.
A Builder provides a lower level API that is uncoupled from a regex
pattern's concrete syntax or even its HIR. Instead, it permits stitching
together an NFA by hand. See its docs for examples.
Example: compilation from concrete syntax
This shows how to compile an NFA from a pattern string while setting a size limit on how big the NFA is allowed to be (in terms of bytes of heap used).
use ;
let config = NFAconfig.nfa_size_limit;
let nfa = NFAcompiler.configure.build?;
let re = new_from_nfa?;
let mut cache = re.create_cache;
let mut caps = re.create_captures;
let expected = Some;
re.captures;
assert_eq!;
# Ok::
Example: compilation from HIR
This shows how to hand assemble a regular expression via its HIR, and then compile an NFA directly from it.
use ;
use ;
let hir = class;
let config = NFAconfig.nfa_size_limit;
let nfa = NFAcompiler.configure.build_from_hir?;
let re = new_from_nfa?;
let mut cache = re.create_cache;
let mut caps = re.create_captures;
let expected = Some;
re.captures;
assert_eq!;
# Ok::
Implementations
impl Compiler
fn new() -> CompilerCreate a new NFA builder with its default configuration.
fn build(&self, pattern: &str) -> Result<NFA, BuildError>Compile the given regular expression pattern into an NFA.
If there was a problem parsing the regex, then that error is returned.
Otherwise, if there was a problem building the NFA, then an error is returned. The only error that can occur is if the compiled regex would exceed the size limits configured on this builder, or if any part of the NFA would exceed the integer representations used. (For example, too many states might plausibly occur on a 16-bit target.)
Example
use ; let config = NFAconfig.nfa_size_limit; let nfa = NFAcompiler.configure.build?; let re = new_from_nfa?; let mut cache = re.create_cache; let mut caps = re.create_captures; let expected = Some; re.captures; assert_eq!; # Ok::fn build_many<P: AsRef<str>>(&self, patterns: &[P]) -> Result<NFA, BuildError>Compile the given regular expression patterns into a single NFA.
When matches are returned, the pattern ID corresponds to the index of the pattern in the slice given.
Example
use ; let config = NFAconfig.nfa_size_limit; let nfa = NFAcompiler.configure.build_many?; let re = new_from_nfa?; let mut cache = re.create_cache; let mut caps = re.create_captures; let expected = Some; re.captures; assert_eq!; # Ok::fn build_from_hir(&self, expr: &Hir) -> Result<NFA, BuildError>Compile the given high level intermediate representation of a regular expression into an NFA.
If there was a problem building the NFA, then an error is returned. The only error that can occur is if the compiled regex would exceed the size limits configured on this builder, or if any part of the NFA would exceed the integer representations used. (For example, too many states might plausibly occur on a 16-bit target.)
Example
use ; use ; let hir = class; let config = NFAconfig.nfa_size_limit; let nfa = NFAcompiler.configure.build_from_hir?; let re = new_from_nfa?; let mut cache = re.create_cache; let mut caps = re.create_captures; let expected = Some; re.captures; assert_eq!; # Ok::fn build_many_from_hir<H: Borrow<Hir>>(&self, exprs: &[H]) -> Result<NFA, BuildError>Compile the given high level intermediate representations of regular expressions into a single NFA.
When matches are returned, the pattern ID corresponds to the index of the pattern in the slice given.
Example
use ; use ; let hirs = &; let config = NFAconfig.nfa_size_limit; let nfa = NFAcompiler.configure.build_many_from_hir?; let re = new_from_nfa?; let mut cache = re.create_cache; let mut caps = re.create_captures; let expected = Some; re.captures; assert_eq!; # Ok::fn configure(&mut self, config: Config) -> &mut CompilerApply the given NFA configuration options to this builder.
Example
use NFA; let config = NFAconfig.nfa_size_limit; let nfa = NFAcompiler.configure.build?; assert_eq!; # Ok::fn syntax(&mut self, config: Config) -> &mut CompilerSet the syntax configuration for this builder using
syntax::Config.This permits setting things like case insensitivity, Unicode and multi line mode.
This syntax configuration only applies when an NFA is built directly from a pattern string. If an NFA is built from an HIR, then all syntax settings are ignored.
Example
use ; let syntax_config = new.unicode; let nfa = NFAcompiler.syntax.build?; // If Unicode were enabled, the number of states would be much bigger. assert!; # Ok::
Trait Implementations
impl Clone for Compiler
fn clone(&self) -> Self
impl Debug for Compiler
fn fmt(&self, f: &mut Formatter<'_>) -> Result
Auto Trait Implementations
impl !Freeze for Compiler
impl !RefUnwindSafe for Compiler
impl !Sync for Compiler
impl Send for Compiler
impl Unpin for Compiler
impl UnsafeUnpin for Compiler
impl UnwindSafe for Compiler
Blanket Implementations
impl<T> Any for Compiler
where
T: 'static + ?Sized,
fn type_id(&self) -> TypeId
impl<T> Borrow<T> for Compiler
where
T: ?Sized,
fn borrow(&self) -> &T
impl<T> BorrowMut<T> for Compiler
where
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
impl<T> CloneToUninit for Compiler
where
T: Clone,
unsafe fn clone_to_uninit(&self, dest: *mut u8)
impl<T> From<T> for Compiler
fn from(t: T) -> TReturns the argument unchanged.
impl<T> ToOwned for Compiler
where
T: Clone,
type Owned = T;fn to_owned(&self) -> Tfn clone_into(&self, target: &mut T)
impl<T, U> Into<U> for Compiler
where
U: From<T>,
fn into(self) -> UCalls
U::from(self).That is, this conversion is whatever the implementation of
[From]<T> for Uchooses to do.
impl<T, U> TryFrom<U> for Compiler
where
U: Into<T>,
type Error = never;fn try_from(value: U) -> Result<T, never>
impl<T, U> TryInto<U> for Compiler
where
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error;fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>