Skip to content

Commit a475321

Browse files
committed
allow calling .parse() with legacy streams
1 parent b81c83b commit a475321

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
@@ -117,20 +117,25 @@ def __init__(self, wrapped_fn: Callable[[Stream, int], Result]):
117117
def __call__(self, stream: Stream, index: int):
118118
return self.wrapped_fn(stream, index)
119119

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

125-
def parse_partial(self, stream: Stream) -> tuple[Any, Stream]:
125+
def parse_partial(self, stream: Stream | str | bytes | list) -> tuple[Any, Stream]:
126126
"""
127127
Parses the longest possible prefix of a given string.
128128
Returns a tuple of the result and the unparsed remainder,
129129
or raises ParseError
130130
"""
131-
result = self(stream, 0)
131+
result = self(
132+
stream if isinstance(stream, Stream) else Stream(stream),
133+
0,
134+
)
132135

133136
if result.status:
137+
# The type of the returned remaining stream matches the type of the
138+
# input stream.
134139
return (result.value, stream[result.index :])
135140
else:
136141
raise ParseError(result.expected, stream, result.furthest)

0 commit comments

Comments
 (0)