Skip to content

Commit

Permalink
Fix string representations of parsers (#114)
Browse files Browse the repository at this point in the history
* TransformationParser
* ConversionParser
* RegexParser
  • Loading branch information
drhagen authored Oct 31, 2024
1 parent 1c3e000 commit 6bb1ce8
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 10 deletions.
6 changes: 5 additions & 1 deletion src/parsita/parsers/_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]):
Expand All @@ -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
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
4 changes: 2 additions & 2 deletions tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 > <lambda>"
assert str(TestParsers.twentyone) == "twentyone = two & one > make_twentyone"


def test_recursion():
Expand Down
11 changes: 6 additions & 5 deletions tests/test_regex.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand All @@ -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():
Expand Down 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+')"
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+')"
assert str(TestParsers.digits) == r"digits = reg('\\d+') > float"
assert str(TestParsers.pair) == "pair = digits & digits"


Expand Down Expand Up @@ -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():
Expand Down

0 comments on commit 6bb1ce8

Please sign in to comment.