Skip to content

Commit

Permalink
Make regex repr more accurate
Browse files Browse the repository at this point in the history
  • Loading branch information
drhagen committed Oct 31, 2024
1 parent ba78e77 commit 0cb38b7
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/parsita/parsers/_regex.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Expand Down
6 changes: 3 additions & 3 deletions tests/test_regex.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand All @@ -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():
Expand All @@ -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"


Expand Down

0 comments on commit 0cb38b7

Please sign in to comment.