Skip to content

Commit

Permalink
feat: add stream.peek_until
Browse files Browse the repository at this point in the history
  • Loading branch information
vberlier committed Jun 14, 2021
1 parent 02bdabd commit 8b0475c
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions tokenstream/stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,15 @@ def peek(self, n: int = 1) -> Optional[Token]:

return token

def peek_until(self, *patterns: TokenPattern) -> Iterator[Token]:
"""Collect tokens until one of the given patterns matches."""
while token := self.peek():
if token.match(*patterns):
next(self)
return
yield token
raise self.emit_error(UnexpectedEOF(patterns))

@overload
def collect(self) -> Iterator[Token]:
...
Expand Down

0 comments on commit 8b0475c

Please sign in to comment.