From 383ef92e0c7196991ca8bd061b4a89d45a963c00 Mon Sep 17 00:00:00 2001 From: David Hagen Date: Wed, 30 Oct 2024 21:23:37 -0400 Subject: [PATCH] Make regex repr more accurate --- src/parsita/parsers/_regex.py | 4 ++-- tests/test_regex.py | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) 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_regex.py b/tests/test_regex.py index a3d8274..947c714 100644 --- a/tests/test_regex.py +++ b/tests/test_regex.py @@ -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+') > float" + 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+') > float" + assert str(TestParsers.digits) == r"digits = reg('\\d+') > float" assert str(TestParsers.pair) == "pair = digits & digits"