Struct Compiler
struct Compiler { ... }
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: &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: &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: &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: &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(self: &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(self: &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::
impl Clone for Compiler
fn clone(self: &Self) -> Compiler
impl Debug for Compiler
fn fmt(self: &Self, f: &mut Formatter<'_>) -> Result
impl Freeze for Compiler
impl RefUnwindSafe for Compiler
impl Send for Compiler
impl Sync for Compiler
impl Unpin for Compiler
impl UnsafeUnpin for Compiler
impl UnwindSafe for Compiler
impl<T> Any for Compiler
fn type_id(self: &Self) -> TypeId
impl<T> Borrow for Compiler
fn borrow(self: &Self) -> &T
impl<T> BorrowMut for Compiler
fn borrow_mut(self: &mut Self) -> &mut T
impl<T> CloneToUninit for Compiler
unsafe fn clone_to_uninit(self: &Self, dest: *mut u8)
impl<T> From for Compiler
fn from(t: T) -> TReturns the argument unchanged.
impl<T> ToOwned for Compiler
fn to_owned(self: &Self) -> Tfn clone_into(self: &Self, target: &mut T)
impl<T, U> Into for Compiler
fn into(self: Self) -> UCalls
U::from(self).That is, this conversion is whatever the implementation of
[From]<T> for Uchooses to do.
impl<T, U> TryFrom for Compiler
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
impl<T, U> TryInto for Compiler
fn try_into(self: Self) -> Result<U, <U as TryFrom<T>>::Error>