From 6bb1ce87ef3591693d9b9396f586e3ef4dd503ae Mon Sep 17 00:00:00 2001 From: David Hagen Date: Wed, 30 Oct 2024 21:33:37 -0400 Subject: [PATCH] Fix string representations of parsers (#114) * TransformationParser * ConversionParser * RegexParser --- src/parsita/parsers/_conversion.py | 6 +++++- src/parsita/parsers/_regex.py | 4 ++-- tests/test_basic.py | 4 ++-- tests/test_regex.py | 11 ++++++----- 4 files changed, 15 insertions(+), 10 deletions(-) diff --git a/src/parsita/parsers/_conversion.py b/src/parsita/parsers/_conversion.py index 681021a..d3ee8b2 100644 --- a/src/parsita/parsers/_conversion.py +++ b/src/parsita/parsers/_conversion.py @@ -25,7 +25,7 @@ def _consume( return None def __repr__(self): - return self.name_or_nothing() + repr(self.parser) + return self.name_or_nothing() + f"{self.parser!r} > {self.converter.__name__}" class TransformationParser(Generic[Input, Output, Convert], Parser[Input, Convert]): @@ -47,3 +47,7 @@ def _consume( return self.transformer(status.value).consume(state, status.remainder) else: return status + + def __repr__(self) -> str: + string = f"{self.parser.name_or_repr()} >= {self.transformer.__name__}" + return self.name_or_nothing() + string diff --git a/src/parsita/parsers/_regex.py b/src/parsita/parsers/_regex.py index b446b38..4af22cc 100644 --- a/src/parsita/parsers/_regex.py +++ b/src/parsita/parsers/_regex.py @@ -36,8 +36,8 @@ def _consume(self, state: State[StringType], reader: Reader[StringType]): return Continue(reader, value) - def __repr__(self): - return self.name_or_nothing() + f"reg(r'{self.pattern.pattern}')" + def __repr__(self) -> str: + return self.name_or_nothing() + f"reg({self.pattern.pattern!r})" def reg(pattern: Union[re.Pattern, StringType]) -> RegexParser[StringType]: diff --git a/tests/test_basic.py b/tests/test_basic.py index 041f7a8..80d2edb 100644 --- a/tests/test_basic.py +++ b/tests/test_basic.py @@ -437,8 +437,8 @@ def make_twentyone(x): assert TestParsers.one.parse("1") == Success(1) assert TestParsers.twelve.parse("12") == Success(12) assert TestParsers.twentyone.parse("21") == Success(21) - assert str(TestParsers.twelve) == "twelve = one & two" - assert str(TestParsers.twentyone) == "twentyone = two & one" + assert str(TestParsers.twelve) == "twelve = one & two > " + assert str(TestParsers.twentyone) == "twentyone = two & one > make_twentyone" def test_recursion(): diff --git a/tests/test_regex.py b/tests/test_regex.py index 9927922..947c714 100644 --- a/tests/test_regex.py +++ b/tests/test_regex.py @@ -34,7 +34,7 @@ class TestParsers(ParserContext, whitespace="[ ]*"): assert TestParsers.hundred.parse(" 100") == Success(100) assert TestParsers.hundred.parse("100 ") == Success(100) assert TestParsers.hundred.parse(" 100 ") == Success(100) - assert str(TestParsers.hundred) == "hundred = '100'" + assert str(TestParsers.hundred) == "hundred = '100' > float" def test_literal_no_whitespace(): @@ -48,7 +48,7 @@ class TestParsers(ParserContext): assert TestParsers.hundred.parse("100 ") == Failure( ParseError(StringReader("100 ", 3), ["end of source"]) ) - assert str(TestParsers.hundred) == "hundred = '100'" + assert str(TestParsers.hundred) == "hundred = '100' > float" def test_literal_multiple(): @@ -108,7 +108,7 @@ class TestParsers(ParserContext, whitespace="[ ]*"): assert TestParsers.digits.parse(" 100") == Success("100") assert TestParsers.digits.parse("100 ") == Success("100") assert TestParsers.digits.parse(" 100 ") == Success("100") - assert str(TestParsers.digits) == r"digits = reg(r'\d+')" + assert str(TestParsers.digits) == r"digits = reg('\\d+')" def test_regex_no_whitespace(): @@ -122,7 +122,7 @@ class TestParsers(ParserContext): assert TestParsers.digits.parse("100 ") == Failure( ParseError(StringReader("100 ", 3), ["end of source"]) ) - assert str(TestParsers.digits) == r"digits = reg(r'\d+')" + assert str(TestParsers.digits) == r"digits = reg('\\d+') > float" def test_regex_custom_whitespace(): @@ -142,7 +142,7 @@ class TestParsers(ParserContext, whitespace="[ ]*"): assert TestParsers.pair.parse("100\n100") == Failure( ParseError(StringReader("100\n100", 3), [r"r'\d+'"]) ) - assert str(TestParsers.digits) == r"digits = reg(r'\d+')" + assert str(TestParsers.digits) == r"digits = reg('\\d+') > float" assert str(TestParsers.pair) == "pair = digits & digits" @@ -342,6 +342,7 @@ def select_parser(type: str): assert NumberParsers.number.parse("decimal 5") == Failure( ParseError(StringReader("decimal 5", 8), [r"r'[0-9]+\.[0-9]+'"]) ) + assert str(NumberParsers.number) == "number = type >= select_parser" def test_transformation_error_propogation():