Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add fancy_regex support #8

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading