Skip to content

Commit

Permalink
Add a bstring Parser to match bytestrings.
Browse files Browse the repository at this point in the history
  • Loading branch information
jap committed Feb 26, 2024
1 parent e7b9aca commit 6ae512f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/parsy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,27 @@ def string_parser(stream, index):
return string_parser


def bstring(expected_bytes: bytes, transform: Callable[[bytes], bytes] = noop) -> Parser:
"""Returns a parser that expects the ``expected_bytes`` and produces
that bytestring value.
Optionally, a transform function can be passed, which will be used on both
the expected bytestring and tested bytestring.
"""

blen = len(expected_bytes)
transformed_b = transform(expected_bytes)

@Parser
def bytes_parser(stream, index):
if transform(stream[index : index + blen]) == transformed_b:
return Result.success(index + blen, transformed_b)
else:
return Result.failure(index, expected_bytes)

return bytes_parser


def regex(exp: str, flags=0, group: int | str | tuple = 0) -> Parser:
"""
Returns a parser that expects the given ``exp``, and produces the
Expand Down
14 changes: 14 additions & 0 deletions tests/test_parsy.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
ParseError,
alt,
any_char,
bstring,
char_from,
decimal_digit,
digit,
Expand Down Expand Up @@ -53,6 +54,19 @@ def test_string_transform_2(self):

self.assertRaises(ParseError, parser.parse, "dog")

def test_bstring(self):
parser = bstring(b"x")
self.assertEqual(parser.parse(b"x"), b"x")

self.assertRaises(ParseError, parser.parse, b"y")

def test_bstring_transform(self):
parser = bstring(b"x", transform=lambda s: s.lower())
self.assertEqual(parser.parse(b"x"), b"x")
self.assertEqual(parser.parse(b"X"), b"x")

self.assertRaises(ParseError, parser.parse, b"y")

def test_regex_str(self):
parser = regex(r"[0-9]")

Expand Down

0 comments on commit 6ae512f

Please sign in to comment.