Skip to content

Commit f3991ff

Browse files
committed
allow calling .parse() with legacy streams
1 parent 64bfd2f commit f3991ff

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

src/parsy/__init__.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,20 +114,25 @@ def __init__(self, wrapped_fn: Callable[[Stream, int], Result]):
114114
def __call__(self, stream: Stream, index: int):
115115
return self.wrapped_fn(stream, index)
116116

117-
def parse(self, stream: Stream) -> Any:
117+
def parse(self, stream: Stream | str | bytes | list) -> Any:
118118
"""Parses a string or list of tokens and returns the result or raise a ParseError."""
119119
(result, _) = (self << eof).parse_partial(stream)
120120
return result
121121

122-
def parse_partial(self, stream: Stream) -> tuple[Any, Stream]:
122+
def parse_partial(self, stream: Stream | str | bytes | list) -> tuple[Any, Stream]:
123123
"""
124124
Parses the longest possible prefix of a given string.
125125
Returns a tuple of the result and the unparsed remainder,
126126
or raises ParseError
127127
"""
128-
result = self(stream, 0)
128+
result = self(
129+
stream if isinstance(stream, Stream) else Stream(stream),
130+
0,
131+
)
129132

130133
if result.status:
134+
# The type of the returned remaining stream matches the type of the
135+
# input stream.
131136
return (result.value, stream[result.index :])
132137
else:
133138
raise ParseError(result.expected, stream, result.furthest)

0 commit comments

Comments
 (0)