pub trait Parser {
type Input: Stream;
type Output;
// Provided methods
fn parse(
&mut self,
input: Self::Input,
) -> Result<(Self::Output, Self::Input), ParseError<<Self::Input as Stream>::Item>> { ... }
fn parse_state(
&mut self,
input: State<Self::Input>,
) -> ParseResult<Self::Output, Self::Input, <Self::Input as Stream>::Item> { ... }
fn parse_lazy(
&mut self,
input: State<Self::Input>,
) -> ParseResult<Self::Output, Self::Input, <Self::Input as Stream>::Item> { ... }
fn add_error(
&mut self,
_error: &mut ParseError<<Self::Input as Stream>::Item>,
) { ... }
}
Expand description
By implementing the Parser
trait a type says that it can be used to parse an input stream into
the type Output
.
All methods have a default implementation but there needs to be at least an implementation of
parse_state
orparse_lazy
. If parse_ok
is implemented an implementation of add_error
is
also recommended to improve error reporting.
Required Associated Types§
Provided Methods§
Sourcefn parse(
&mut self,
input: Self::Input,
) -> Result<(Self::Output, Self::Input), ParseError<<Self::Input as Stream>::Item>>
fn parse( &mut self, input: Self::Input, ) -> Result<(Self::Output, Self::Input), ParseError<<Self::Input as Stream>::Item>>
Entrypoint of the parser
Takes some input and tries to parse it returning a ParseResult
Sourcefn parse_state(
&mut self,
input: State<Self::Input>,
) -> ParseResult<Self::Output, Self::Input, <Self::Input as Stream>::Item>
fn parse_state( &mut self, input: State<Self::Input>, ) -> ParseResult<Self::Output, Self::Input, <Self::Input as Stream>::Item>
Parses using the state input
by calling Stream::uncons one or more times
On success returns Ok((value, new_state))
on failure it returns Err(error)
Sourcefn parse_lazy(
&mut self,
input: State<Self::Input>,
) -> ParseResult<Self::Output, Self::Input, <Self::Input as Stream>::Item>
fn parse_lazy( &mut self, input: State<Self::Input>, ) -> ParseResult<Self::Output, Self::Input, <Self::Input as Stream>::Item>
Specialized version of parse_state where the parser does not need to add an error to the
ParseError
when it does not consume any input before encountering the error.
Instead the error can be added later through the add_error
method