From 9c426f09533c88667cbc3cec2cd6df3a357a703b Mon Sep 17 00:00:00 2001 From: Melody Horn Date: Sun, 4 Apr 2021 16:05:39 -0600 Subject: improve replacement in macro tokens --- src/makefile/macro.rs | 40 ++++++++++++++++++++++++++++++++-------- 1 file changed, 32 insertions(+), 8 deletions(-) (limited to 'src/makefile') diff --git a/src/makefile/macro.rs b/src/makefile/macro.rs index 0ab1b4b..4d58e07 100644 --- a/src/makefile/macro.rs +++ b/src/makefile/macro.rs @@ -121,10 +121,14 @@ impl<'parent, 'lookup> Set<'parent, 'lookup> { Some((subst1, subst2)) => { let subst1 = self.expand(subst1)?; let subst1_suffix = regex::escape(&subst1); - let subst1_suffix = Regex::new(&format!(r"{}\b", subst1_suffix)) + let subst1_suffix = Regex::new(&format!(r"{}(\s|$)", subst1_suffix)) .context("formed invalid regex somehow")?; let subst2 = self.expand(subst2)?; - subst1_suffix.replace_all(¯o_value, subst2).to_string() + subst1_suffix + .replace_all(¯o_value, |c: ®ex::Captures| { + format!("{}{}", subst2, c.get(1).unwrap().as_str()) + }) + .to_string() } None => macro_value, }; @@ -132,12 +136,10 @@ impl<'parent, 'lookup> Set<'parent, 'lookup> { } #[cfg(feature = "full")] Token::FunctionCall { name, args } => { - result.push_str(&functions::expand_call( - name, - args, - self, - Some(Rc::clone(&self.to_eval)), - )?); + let fn_result = + functions::expand_call(name, args, self, Some(Rc::clone(&self.to_eval)))?; + log::trace!("expanded {} into \"{}\"", token, &fn_result); + result.push_str(&fn_result); } } } @@ -240,3 +242,25 @@ fn builtins() -> Vec<(&'static str, TokenString)> { FFLAGS="" ] } + +#[cfg(test)] +mod test { + use super::*; + + type R = Result<()>; + + #[test] + fn subst() -> R { + let mut macros = Set::new(); + macros.set( + "oof".to_owned(), + Source::File, + TokenString::text("bruh; swag; yeet;"), + ); + assert_eq!( + macros.expand(&"$(oof:;=?)".parse().unwrap())?, + "bruh? swag? yeet?" + ); + Ok(()) + } +} -- cgit v1.2.3