diff options
Diffstat (limited to 'src/makefile/macro.rs')
-rw-r--r-- | src/makefile/macro.rs | 31 |
1 files changed, 17 insertions, 14 deletions
diff --git a/src/makefile/macro.rs b/src/makefile/macro.rs index 79df339..88397c5 100644 --- a/src/makefile/macro.rs +++ b/src/makefile/macro.rs @@ -2,6 +2,7 @@ use std::collections::HashMap; use std::env; use std::fmt; +use anyhow::Context; use regex::Regex; use super::functions; @@ -15,9 +16,9 @@ pub(crate) enum MacroSource { Builtin, } -pub(crate) trait LookupInternal: for<'a> Fn(&'a str) -> String {} +pub(crate) trait LookupInternal: for<'a> Fn(&'a str) -> anyhow::Result<String> {} -impl<F: for<'a> Fn(&'a str) -> String> LookupInternal for F {} +impl<F: for<'a> Fn(&'a str) -> anyhow::Result<String>> LookupInternal for F {} #[derive(Clone)] pub(crate) struct MacroSet<'parent, 'lookup> { @@ -50,13 +51,13 @@ impl<'parent, 'lookup> MacroSet<'parent, 'lookup> { } } - fn lookup_internal(&self, name: &str) -> String { + fn lookup_internal(&self, name: &str) -> anyhow::Result<String> { if let Some(lookup) = self.lookup_internal { lookup(name) } else if let Some(parent) = self.parent { parent.lookup_internal(name) } else { - panic!("no lookup possible"); + anyhow::bail!("no lookup possible") } } @@ -79,7 +80,7 @@ impl<'parent, 'lookup> MacroSet<'parent, 'lookup> { self.data.remove(name) } - pub(crate) fn expand(&self, text: &TokenString) -> String { + pub(crate) fn expand(&self, text: &TokenString) -> anyhow::Result<String> { let mut result = String::new(); for token in text.tokens() { match token { @@ -92,18 +93,20 @@ impl<'parent, 'lookup> MacroSet<'parent, 'lookup> { && name.starts_with(internal_macro_names) && name.ends_with(internal_macro_suffices); let macro_value = if just_internal || suffixed_internal { - self.lookup_internal(name) + self.lookup_internal(name)? } else { - self.get(name) - .map_or_else(String::new, |(_, macro_value)| self.expand(macro_value)) + self.get(name).map_or_else( + || Ok(String::new()), + |(_, macro_value)| self.expand(macro_value), + )? }; let macro_value = match replacement { Some((subst1, subst2)) => { - let subst1 = self.expand(subst1); + let subst1 = self.expand(subst1)?; let subst1_suffix = regex::escape(&subst1); - let subst1_suffix = - Regex::new(&format!(r"{}\b", subst1_suffix)).unwrap(); - let subst2 = self.expand(subst2); + let subst1_suffix = Regex::new(&format!(r"{}\b", subst1_suffix)) + .context("formed invalid regex somehow")?; + let subst2 = self.expand(subst2)?; subst1_suffix.replace_all(¯o_value, subst2).to_string() } None => macro_value, @@ -111,11 +114,11 @@ impl<'parent, 'lookup> MacroSet<'parent, 'lookup> { result.push_str(¯o_value); } Token::FunctionCall { name, args } => { - result.push_str(&functions::expand_call(name, args, self)); + result.push_str(&functions::expand_call(name, args, self)?); } } } - result + Ok(result) } pub(crate) fn with_lookup<'l, 's: 'l>( |