From 7c2a09e5c75d87d5dd728623517899ca7cada630 Mon Sep 17 00:00:00 2001 From: Melody Horn Date: Sat, 3 Apr 2021 16:05:15 -0600 Subject: fix next_line_if so it isn't greedy with escaped newlines --- src/makefile/input.rs | 33 ++++++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/src/makefile/input.rs b/src/makefile/input.rs index 2b85376..3abdb8b 100644 --- a/src/makefile/input.rs +++ b/src/makefile/input.rs @@ -140,7 +140,6 @@ pub struct MakefileReader<'a, 'parent, R: BufRead> { pub first_non_special_target: Option, args: &'a Args, lines_iter: Peekable>>, - pending_line: Option<(usize, Result)>, #[cfg(feature = "full")] conditional_stack: Vec, } @@ -170,7 +169,6 @@ impl<'a, 'parent, R: BufRead> MakefileReader<'a, 'parent, R> { lines_iter: source.lines().line_numbered().peekable(), #[cfg(feature = "full")] conditional_stack: Vec::new(), - pending_line: None, }; reader.read_all()?; Ok(reader) @@ -256,9 +254,6 @@ impl<'a, 'parent, R: BufRead> MakefileReader<'a, 'parent, R> { &mut self, escaped_newline_replacement: &'static str, ) -> Option<(usize, Result)> { - if let Some(x) = self.pending_line.take() { - return Some(x); - } while let Some((line_number, line)) = self.lines_iter.next() { let mut line = match line { Ok(x) => x, @@ -320,16 +315,16 @@ impl<'a, 'parent, R: BufRead> MakefileReader<'a, 'parent, R> { None } + /// Only applies the predicate to the next physical line in the file. + /// Doesn't apply the escaped newline replacement unless the predicate passes. fn next_line_if( &mut self, escaped_newline_replacement: &'static str, predicate: impl FnOnce(&(usize, Result)) -> bool, ) -> Option<(usize, Result)> { - let pending_line = self.next_line(escaped_newline_replacement)?; - if (predicate)(&pending_line) { - Some(pending_line) + if predicate(self.lines_iter.peek()?) { + self.next_line(escaped_newline_replacement) } else { - self.pending_line = Some(pending_line); None } } @@ -544,6 +539,26 @@ mod test { type R = Result<()>; + #[test] + fn multi_line_dependencies() -> R { + let file = " +unrelated: example +\tswag +x = 3 4 \\ +\t\t5 +a: $(x) b \\ +\t\tc \\ +\t\td +\tfoo"; + let args = Args::empty(); + let makefile = MakefileReader::read(&args, MacroSet::new(), Cursor::new(file))?.finish(); + assert_eq!( + makefile.targets["a"].prerequisites, + vec!["3", "4", "5", "b", "c", "d"] + ); + Ok(()) + } + #[cfg(feature = "full")] #[test] fn basic_conditionals() -> R { -- cgit v1.2.3