From 1358f20f19d859c8ca971f1c61423e183484cf05 Mon Sep 17 00:00:00 2001 From: Steve Fan <29133953+stevefan1999-personal@users.noreply.github.com> Date: Sat, 27 Jan 2024 23:09:06 +0800 Subject: [PATCH] add fancy_regex support --- rustemo-compiler/src/generator/base.rs | 27 +++++++++++++++++++++++--- rustemo-compiler/src/settings.rs | 9 +++++++++ 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/rustemo-compiler/src/generator/base.rs b/rustemo-compiler/src/generator/base.rs index 067b5ec8..0c28f6d0 100644 --- a/rustemo-compiler/src/generator/base.rs +++ b/rustemo-compiler/src/generator/base.rs @@ -32,8 +32,17 @@ impl<'g, 's> PartGenerator<'g, 's> for BasePartGenerator { let mut imports: Vec = vec![]; if let LexerType::Default = generator.settings.lexer_type { + let regex: syn::Stmt = if generator.settings.fancy_regex { + parse_quote! { + use fancy_regex::Regex; + } + } else { + parse_quote! { + use regex::Regex; + } + }; imports.extend::>(parse_quote! { - use regex::Regex; + #regex use once_cell::sync::Lazy; use rustemo::StringLexer; }); @@ -552,6 +561,18 @@ impl<'g, 's> PartGenerator<'g, 's> for BasePartGenerator { #[derive(Debug)] pub struct TokenRecognizer(TokenKind, Recognizer); }); + + let regex: syn::Expr = if generator.settings.fancy_regex { + parse_quote!{ + Ok(Some(x)) + } + + } else { + parse_quote!{ + Some(x) + } + }; + ast.push(parse_quote!{ impl<'i> TokenRecognizerT<'i> for TokenRecognizer { fn recognize(&self, input: &'i str) -> Option<&'i str> { @@ -572,12 +593,12 @@ impl<'g, 's> PartGenerator<'g, 's> for BasePartGenerator { logn!("{} {:?} -- ", " Recognizing".green(), token_kind); let match_str = r.find(input); match match_str { - Some(x) => { + #regex => { let x_str = x.as_str(); log!("{} '{}'", "recognized".bold().green(), x_str); Some(x_str) }, - None => { + _ => { log!("{}", "not recognized".red()); None } diff --git a/rustemo-compiler/src/settings.rs b/rustemo-compiler/src/settings.rs index 03aed20e..848ff8dc 100644 --- a/rustemo-compiler/src/settings.rs +++ b/rustemo-compiler/src/settings.rs @@ -101,6 +101,7 @@ pub struct Settings { pub(crate) force: bool, pub(crate) dot: bool, + pub(crate) fancy_regex: bool, } impl Default for Settings { @@ -136,6 +137,7 @@ impl Default for Settings { force: false, exclude: vec![], dot: false, + fancy_regex: false, } } } @@ -325,6 +327,13 @@ impl Settings { self } + /// Set whether or not we use [`fancy_regex`](https://docs.rs/fancy-regex/latest/fancy_regex/) + /// instead of [`regex`](https://docs.rs/regex/latest/regex/) + pub fn fancy_regex(mut self, fancy_regex: bool) -> Self { + self.fancy_regex = fancy_regex; + self + } + /// Recursively traverse the root dir and process each Rustemo grammar found. /// Used as the last call to the configured [Settings] value. pub fn process_dir(&self) -> Result<()> {