ere_core/
lib.rs

1//! This crate provides the core functionality to the `ere` crate.
2
3use proc_macro::TokenStream;
4use quote::quote;
5extern crate proc_macro;
6
7pub mod nfa_static;
8pub mod parse_tree;
9pub mod simplified_tree;
10pub mod working_nfa;
11
12enum RegexEngines {
13    NFA(nfa_static::NFAStatic),
14}
15
16/// A regular expression (specifically, a [POSIX ERE](https://en.wikibooks.org/wiki/Regular_Expressions/POSIX-Extended_Regular_Expressions)).
17///
18/// Internally, this may contain one of several engines depending on the expression.
19pub struct Regex(RegexEngines);
20impl Regex {
21    /// Returns whether or not the text is matched by the regular expression.
22    pub fn test(&self, text: &str) -> bool {
23        return match &self.0 {
24            RegexEngines::NFA(nfa) => nfa.test(text),
25        };
26    }
27}
28
29pub const fn __construct_nfa_regex(nfa: nfa_static::NFAStatic) -> Regex {
30    return Regex(RegexEngines::NFA(nfa));
31}
32
33pub fn __compile_regex(stream: TokenStream) -> TokenStream {
34    let ere: parse_tree::ERE = syn::parse_macro_input!(stream);
35    let tree = simplified_tree::SimplifiedTreeNode::from(ere);
36    let nfa = working_nfa::WorkingNFA::new(&tree);
37
38    let serialized_nfa = nfa_static::NFAStatic::serialize_as_token_stream(&nfa);
39
40    return quote! {
41        ::ere_core::__construct_nfa_regex(#serialized_nfa)
42    }
43    .into();
44}