Skip to content

Commit

Permalink
fix: swallow syntax errors when checkpoint is not committed yet
Browse files Browse the repository at this point in the history
  • Loading branch information
vberlier committed Jun 24, 2021
1 parent bcee95a commit 54e77ef
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
25 changes: 25 additions & 0 deletions tests/test_stream.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import Tuple, Union

import pytest

from tokenstream import TokenStream, UnexpectedToken
Expand Down Expand Up @@ -138,3 +140,26 @@ def test_checkpoint_commit():
assert next(stream).value == "world"
commit()
assert [token.value for token in stream] == []


def test_checkpoint_error():
stream = TokenStream("hello world 1 2 3 thing")

def argument(stream: TokenStream) -> Union[str, Tuple[int, int, int]]:
with stream.checkpoint() as commit:
triplet = (
int(stream.expect("number").value),
int(stream.expect("number").value),
int(stream.expect("number").value),
)
commit()
return triplet
return stream.expect("word").value # type: ignore

with stream.syntax(number=r"\d+", word=r"\w+"):
assert [argument(stream) for _ in stream.peek_until()] == [
"hello",
"world",
(1, 2, 3),
"thing",
]
8 changes: 7 additions & 1 deletion tokenstream/stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -838,7 +838,8 @@ def checkpoint(self) -> Iterator[CheckpointCommit]:
'hello'
'hello'
You can use the returned ``commit()`` function to keep the state of the stream at the end of the ``with`` statement.
You can use the returned ``commit()`` function to keep the state of the stream
at the end of the ``with`` statement.
>>> stream = TokenStream("hello world")
>>> with stream.syntax(word=r"[a-z]+"):
Expand All @@ -848,11 +849,16 @@ def checkpoint(self) -> Iterator[CheckpointCommit]:
... stream.expect("word").value
'hello'
'world'
Checkpoints will swallow syntax errors until the ``commit()`` function is called.
"""
previous_index = [self.index]

try:
yield lambda: previous_index.clear()
except InvalidSyntax:
if not previous_index:
raise
finally:
if previous_index:
self.index = previous_index[0]

0 comments on commit 54e77ef

Please sign in to comment.