From ab249a1f95e898b2d81e480b32a5b6ad9e4a7087 Mon Sep 17 00:00:00 2001 From: Melody Horn Date: Sun, 11 Apr 2021 18:40:46 -0600 Subject: add more error context --- src/makefile/input.rs | 24 +++++++++++++++++++++--- src/makefile/macro.rs | 12 +++++++++--- 2 files changed, 30 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/makefile/input.rs b/src/makefile/input.rs index beb517a..e8e2e95 100644 --- a/src/makefile/input.rs +++ b/src/makefile/input.rs @@ -327,8 +327,24 @@ impl<'a, 'parent, R: BufRead> MakefileReader<'a, 'parent, R> { }; match line_type { - LineType::Rule => self.read_rule(&line_tokens, line_number)?, - LineType::Macro => self.read_macro(line_tokens, line_number)?, + LineType::Rule => { + self.read_rule(&line_tokens, line_number) + .wrap_err_with(|| { + format!( + "while parsing rule definition starting on line {}", + line_number + ) + })? + } + LineType::Macro => { + self.read_macro(line_tokens, line_number) + .wrap_err_with(|| { + format!( + "while parsing macro definition starting on line {}", + line_number + ) + })? + } LineType::Unknown => { if !line_tokens.is_empty() { // TODO handle assignments here @@ -772,7 +788,9 @@ impl<'a, 'parent, R: BufRead> MakefileReader<'a, 'parent, R> { } fn expand_macros(&self, text: &TokenString) -> Result { - self.macros.expand(text) + self.macros + .expand(text) + .wrap_err_with(|| format!("while expanding \"{}\"", text)) } pub fn finish(self) -> FinishedMakefileReader { diff --git a/src/makefile/macro.rs b/src/makefile/macro.rs index f00fdff..3444e2c 100644 --- a/src/makefile/macro.rs +++ b/src/makefile/macro.rs @@ -218,7 +218,9 @@ impl<'parent, 'lookup> Set<'parent, 'lookup> { match token { Token::Text(t) => result.push_str(t), Token::MacroExpansion { name, replacement } => { - let name = self.expand(name)?; + let name = self + .expand(name) + .wrap_err_with(|| format!("while expanding \"{}\"", name))?; let internal_macro_names = &['@', '?', '<', '*'][..]; let internal_macro_suffices = &['D', 'F'][..]; let just_internal = name.len() == 1 && name.starts_with(internal_macro_names); @@ -226,14 +228,18 @@ impl<'parent, 'lookup> Set<'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) + .wrap_err_with(|| format!("while expanding $\"{}\"", name))? } else { self.get(&name).map_or_else( || { self.warn(format!("undefined macro {}", name)); Ok(String::new()) }, - |x| self.expand(&x.text), + |x| { + self.expand(&x.text) + .wrap_err_with(|| format!("while expanding \"{}\"", &x.text)) + }, )? }; let macro_value = match replacement { -- cgit v1.2.3