Skip to content

Commit

Permalink
add fancy_regex support
Browse files Browse the repository at this point in the history
  • Loading branch information
stevefan1999-personal committed Jan 27, 2024
1 parent 5a4199a commit 1358f20
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
27 changes: 24 additions & 3 deletions rustemo-compiler/src/generator/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,17 @@ impl<'g, 's> PartGenerator<'g, 's> for BasePartGenerator {
let mut imports: Vec<syn::Stmt> = 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::<Vec<syn::Stmt>>(parse_quote! {
use regex::Regex;
#regex
use once_cell::sync::Lazy;
use rustemo::StringLexer;
});
Expand Down Expand Up @@ -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> {
Expand All @@ -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
}
Expand Down
9 changes: 9 additions & 0 deletions rustemo-compiler/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ pub struct Settings {

pub(crate) force: bool,
pub(crate) dot: bool,
pub(crate) fancy_regex: bool,
}

impl Default for Settings {
Expand Down Expand Up @@ -136,6 +137,7 @@ impl Default for Settings {
force: false,
exclude: vec![],
dot: false,
fancy_regex: false,
}
}
}
Expand Down Expand Up @@ -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<()> {
Expand Down

0 comments on commit 1358f20

Please sign in to comment.