From b83e916ec888b38b15a242c673c89da39b52a8e7 Mon Sep 17 00:00:00 2001 From: Melody Horn Date: Thu, 25 Mar 2021 19:28:07 -0600 Subject: handle iterator end more gracefully in peek --- yapymake/makefile/__init__.py | 5 +++-- yapymake/util/peekable_iterator.py | 7 +++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/yapymake/makefile/__init__.py b/yapymake/makefile/__init__.py index ddd049d..a5cb19d 100644 --- a/yapymake/makefile/__init__.py +++ b/yapymake/makefile/__init__.py @@ -130,8 +130,9 @@ class Makefile: prerequisite_tokens, command_tokens = semicolon_split prerequisites = self.expand_macros(prerequisite_tokens).split() command_token_strings = [command_tokens] - while lines_iter.peek().startswith('\t'): - command_token_strings.append(tokenize(next(lines_iter).lstrip('\t'))) + while (peeked := lines_iter.peek()) is not None and peeked.startswith('\t'): + # TODO handle escaped newlines + command_token_strings.append(tokenize(next(lines_iter).lstrip('\t').rstrip('\n'))) commands = [CommandLine(c) for c in command_token_strings] # we don't know yet if it's a target rule or an inference rule diff --git a/yapymake/util/peekable_iterator.py b/yapymake/util/peekable_iterator.py index 67bd59d..cd2fcac 100644 --- a/yapymake/util/peekable_iterator.py +++ b/yapymake/util/peekable_iterator.py @@ -24,7 +24,10 @@ class PeekableIterator(Iterator[T]): return result return next(self._inner) - def peek(self) -> T: + def peek(self) -> Optional[T]: if self._peeked is None: - self._peeked = next(self._inner) + try: + self._peeked = next(self._inner) + except StopIteration: + self._peeked = None return self._peeked -- cgit v1.2.3