aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMelody Horn <melody@boringcactus.com>2021-03-25 19:28:07 -0600
committerMelody Horn <melody@boringcactus.com>2021-03-25 19:28:07 -0600
commitb83e916ec888b38b15a242c673c89da39b52a8e7 (patch)
tree7620b5c0d8c5ab497230dbe48f5f971021469ba4
parentfa31fa96b8088e5157cbb9022f3e77664cf7e5d8 (diff)
downloadyapymake-b83e916ec888b38b15a242c673c89da39b52a8e7.tar.gz
yapymake-b83e916ec888b38b15a242c673c89da39b52a8e7.zip
handle iterator end more gracefully in peek
-rw-r--r--yapymake/makefile/__init__.py5
-rw-r--r--yapymake/util/peekable_iterator.py7
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