pub enum Parsed<T, E> {
Res(Result<T, E>),
Fallthrough,
}
Expand description
Return type for recursive descent parser that can fall through to another parser on unexpected input.
If you have a parser that only fails irrecoverably, by returning a Result
, you can use
into
to convert it to a Parsed
value. Conversely, Parsed
values can be
converted into plain Result
using or_give_up
, making the Parsed
value fall through to an unrecoverable error, or using optional
making the
parsed value optional.
Note that some of the methods of Parsed
already have equivalent methods of the Result
type
in std
. Some more methods are also implemented for Result
by this crate, using the
ResultExt
trait. Finally some of the methods are only intended for parser that can fall
through, and as such, they are not implemented for Result
.
Variants§
Res(Result<T, E>)
A successfully parsed value or an irrecoverable error.
When Res(Ok(v))
is returned, the value was parsed successfully and the input stream should
be advanced past the parsed input.
When Res(Err(err))
is returned, either the initial input matched, but continued in an
unexpected way or some other kind of runtime error occured. In this case the input stream
points at the position of the unexpected input or the input causing an error.
Fallthrough
The input does not match the expected input of the parser.
This leaves the input stream unchanged, so that another parser can be tried.
Implementations§
source§impl<T, E> Parsed<T, E>
impl<T, E> Parsed<T, E>
sourcepub fn or_give_up(self, err: impl FnOnce() -> E) -> Result<T, E>
pub fn or_give_up(self, err: impl FnOnce() -> E) -> Result<T, E>
Makes a parser fail irrecoverably when the current input did not match the expected input of the returning parser.
This makes the expected input of the parser returning this Parsed
value a mandatory part
of the input.
sourcepub fn optional(self) -> Result<Option<T>, E>
pub fn optional(self) -> Result<Option<T>, E>
Returns None
when the current input did not match the expected input of the returning
parser.
This makes the expected input of the parser returning this Parsed
value optional.
sourcepub fn matches(self) -> Result<bool, E>
pub fn matches(self) -> Result<bool, E>
Returns whether the current input did match the expected input of the returning parser.
This makes the expected input of the parser returning this Parsed
value optional.
sourcepub fn or_parse(self, parse: impl FnOnce() -> Parsed<T, E>) -> Parsed<T, E>
pub fn or_parse(self, parse: impl FnOnce() -> Parsed<T, E>) -> Parsed<T, E>
Tries a different parser when the current input did not match the expected input of the returning parser.
This adds another parser to the expected choices for the current input.
This is intentionally not called or_else
as calling
Result::or_else
on an irrecoverably failing parser cannot
be used to implement choice, as such a parser can leave the input stream in a modified state
when it returns an Err
value.
sourcepub fn or_always_parse(
self,
parse: impl FnOnce() -> Result<T, E>
) -> Result<T, E>
pub fn or_always_parse( self, parse: impl FnOnce() -> Result<T, E> ) -> Result<T, E>
Variant of or_parse
for when the alternative parser is guaranteed to not fall through.
sourcepub fn and_then<U>(self, parse: impl FnOnce(T) -> Result<U, E>) -> Parsed<U, E>
pub fn and_then<U>(self, parse: impl FnOnce(T) -> Result<U, E>) -> Parsed<U, E>
Locks in the choice when the input matches the expected input of the returning parser.
When the returning parser succeeds, but the passed parser fails, further alternatives will not be tried.
sourcepub fn and_also(
self,
parse: impl FnOnce(&mut T) -> Result<(), E>
) -> Parsed<T, E>
pub fn and_also( self, parse: impl FnOnce(&mut T) -> Result<(), E> ) -> Parsed<T, E>
Variant of and_then
which always returns the (possibly modified)
original value on success.
This is useful to parse delimiters which do not contain any parsed information.
sourcepub fn and_do(self, action: impl FnOnce(&mut T)) -> Parsed<T, E>
pub fn and_do(self, action: impl FnOnce(&mut T)) -> Parsed<T, E>
Variant of and_also
where the action cannot fail.
This is useful to modify a value or to perform side-effects on successful parses.