Skip to content

Commit

Permalink
refactor(ast_codegen): move formatting regex definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
overlookmotel committed Aug 9, 2024
1 parent d787af5 commit b2c289c
Showing 1 changed file with 16 additions and 16 deletions.
32 changes: 16 additions & 16 deletions tasks/ast_codegen/src/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,17 @@ use syn::parse_file;

static INSERT_MACRO_IDENT: &str = "insert";
static ENDL_MACRO_IDENT: &str = "endl";
static WHITE_SPACES: &str = " ";
static WHITE_SPACE: &str = " ";

lazy_static! {
static ref INSERT_REGEX: Regex = Regex::new(
format!(
r#"(?m)^{WHITE_SPACE}*{INSERT_MACRO_IDENT}!\([\n\s\S]*?\"([\n\s\S]*?)\"[\n\s\S]*?\);$"#
)
.as_str()
)
.unwrap();
}

struct InsertReplacer;

Expand All @@ -21,6 +31,11 @@ impl Replacer for InsertReplacer {
}
}

lazy_static! {
static ref ENDL_REGEX: Regex =
Regex::new(format!(r"{WHITE_SPACE}*{ENDL_MACRO_IDENT}!\(\);").as_str()).unwrap();
}

struct EndlReplacer;

impl Replacer for EndlReplacer {
Expand All @@ -29,21 +44,6 @@ impl Replacer for EndlReplacer {

/// Pretty Print
pub fn pprint(input: &TokenStream) -> String {
lazy_static! {
static ref INSERT_REGEX: Regex = Regex::new(
format!(
r#"(?m)^[{WHITE_SPACES}]*{INSERT_MACRO_IDENT}!\([\n\s\S]*?\"([\n\s\S]*?)\"[\n\s\S]*?\);$"#
)
.as_str()
)
.unwrap();
};

lazy_static! {
static ref ENDL_REGEX: Regex =
Regex::new(format!(r"[{WHITE_SPACES}]*{ENDL_MACRO_IDENT}!\(\);").as_str()).unwrap();
};

let result = prettyplease::unparse(&parse_file(input.to_string().as_str()).unwrap());
let result = ENDL_REGEX.replace_all(&result, EndlReplacer);
let result = INSERT_REGEX.replace_all(&result, InsertReplacer).to_string();
Expand Down

0 comments on commit b2c289c

Please sign in to comment.