Skip to content

Commit

Permalink
Add a bconcat helper to the Parser class
Browse files Browse the repository at this point in the history
  • Loading branch information
jap committed Feb 26, 2024
1 parent 294590f commit e7b9aca
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/parsy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,13 @@ def concat(self) -> Parser:
"""
return self.map("".join)

def bconcat(self) -> Parser:
"""
Returns a parser that concatenates together (as a bytestring) the previously
produced values.
"""
return self.map(b"".join)

def then(self, other: Parser) -> Parser:
"""
Returns a parser which, if the initial parser succeeds, will
Expand Down
5 changes: 5 additions & 0 deletions tests/test_parsy.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,11 @@ def test_concat(self):
self.assertEqual(parser.parse(""), "")
self.assertEqual(parser.parse("abc"), "abc")

def test_bconcat(self):
parser = any_char.many().bconcat()
self.assertEqual(parser.parse(b""), b"")
self.assertEqual(parser.parse(b"abc"), b"abc")

def test_generate(self):
x = y = None

Expand Down

0 comments on commit e7b9aca

Please sign in to comment.