From 1ca7b5e1ab6982805503766eff6c1f723caa8b27 Mon Sep 17 00:00:00 2001 From: Philippe Proulx Date: Wed, 27 Sep 2023 13:59:58 -0400 Subject: [PATCH] Fix: parse a 0-item repetition Change-Id: I89d3d01db51a78a0ff543650d95486208eb91d26 Signed-off-by: Philippe Proulx --- normand/normand.py | 33 +++++++++++++++------------------ pyproject.toml | 2 +- 2 files changed, 16 insertions(+), 19 deletions(-) diff --git a/normand/normand.py b/normand/normand.py index 31c3aff..fd308ed 100644 --- a/normand/normand.py +++ b/normand/normand.py @@ -22,7 +22,7 @@ # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. __author__ = "Philippe Proulx" -__version__ = "0.1.0" +__version__ = "0.1.1" __all__ = [ "ByteOrder", "parse", @@ -863,9 +863,11 @@ class _Parser: ) return int(m.group(0), 0) - # Tries to parse a repeatable item followed or not by a repetition, - # returning an item on success. - def _try_parse_item(self): + # Tries to parse an item, possibly followed by a repetition, + # returning `True` on success. + # + # Appends any parsed item to `items`. + def _try_append_item(self, items: List[_Item]): self._skip_ws_and_comments() # Parse a base item @@ -873,20 +875,21 @@ class _Parser: if item is None: # No item - return + return False # Parse repetition if the base item is repeatable if isinstance(item, _RepableItem): rep = self._try_parse_rep() if rep == 0: - # No item - return + # No item, but that's okay + return True elif rep > 1: # Convert to repetition item item = _Rep(item, rep, self._text_loc) - return item + items.append(item) + return True # Parses and returns items, skipping whitespaces, insignificant # symbols, and comments when allowed, and stopping at the first @@ -895,16 +898,10 @@ class _Parser: items = [] # type: List[_Item] while self._isnt_done(): - # Try to parse item - item = self._try_parse_item() - - if item is not None: - # Append new item - items.append(item) - continue - - # Unknown at this point - break + # Try to append item + if not self._try_append_item(items): + # Unknown at this point + break return items diff --git a/pyproject.toml b/pyproject.toml index ae8c1b3..bab57d8 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -23,7 +23,7 @@ [tool.poetry] name = 'normand' -version = '0.1.0' +version = '0.1.1' description = 'Text-to-binary processor with its own language' license = 'MIT' authors = ['Philippe Proulx '] -- 2.34.1