From 41ad330695cb844ee0c6846ac307f33cbd2068a7 Mon Sep 17 00:00:00 2001 From: Melody Horn Date: Fri, 26 Mar 2021 21:15:27 -0600 Subject: keep a line number so errors are easier to find --- src/makefile/mod.rs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/makefile/mod.rs b/src/makefile/mod.rs index bd8838a..4c39160 100644 --- a/src/makefile/mod.rs +++ b/src/makefile/mod.rs @@ -270,9 +270,9 @@ impl Makefile { } pub fn and_read(&mut self, source: impl BufRead) -> &mut Makefile { - let mut lines_iter = source.lines().peekable(); + let mut lines_iter = source.lines().enumerate().peekable(); while lines_iter.peek().is_some() { - let line = match lines_iter.next() { + let (line_number, line) = match lines_iter.next() { Some(x) => x, // fancy Rust trick: break-with-an-argument to return a value from a // `loop` expression @@ -284,7 +284,7 @@ impl Makefile { // handle escaped newlines while line.ends_with(r"\") { let next_line = match lines_iter.next() { - Some(x) => x, + Some((_, x)) => x, None => Ok("".into()), }; let next_line = next_line.expect("failed to read line of makefile!"); @@ -362,7 +362,7 @@ impl Makefile { Some((prerequisites, mut command)) => { while command.ends_with(r"\") && lines_iter.peek().is_some() { command.strip_suffix(r"\"); - command.extend(tokenize(&lines_iter.next().unwrap().unwrap())); + command.extend(tokenize(&lines_iter.next().unwrap().1.unwrap())); } (prerequisites, vec![command]) } @@ -371,12 +371,12 @@ impl Makefile { let prerequisites = self.expand_macros(&prerequisites, None); let prerequisites = prerequisites.split_whitespace().map(|x| x.into()).collect::>(); - while lines_iter.peek().and_then(|x| x.as_ref().ok()).map_or(false, |line| line.starts_with('\t')) { - let line = lines_iter.next().unwrap().unwrap(); + while lines_iter.peek().and_then(|(_, x)| x.as_ref().ok()).map_or(false, |line| line.starts_with('\t')) { + let line = lines_iter.next().unwrap().1.unwrap(); let mut line: String = line.strip_prefix("\t").unwrap().into(); while line.ends_with('\\') { match lines_iter.next() { - Some(Ok(next_line)) => { + Some((_, Ok(next_line))) => { let next_line = next_line.strip_prefix("\t").unwrap_or(&next_line); line.push('\n'); line.push_str(next_line); @@ -497,7 +497,7 @@ impl Makefile { self.macros.insert(name.into(), (MacroSource::File, value)); } LineType::Unknown => { - panic!("Unknown line {:?}", line_tokens); + panic!("error: line {}: unknown line {:?}", line_number, line_tokens); } } } -- cgit v1.2.3