pub trait Parser<T>: Sized {
type Err;
// Required method
fn parse(&self, s: &mut ParserString) -> Result<T, Self::Err>;
// Provided methods
fn try_parse(&self, s: &mut ParserString) -> Result<T, Self::Err> { ... }
fn chain<U, P2: Parser<U, Err = E>, E: Into<Self::Err>>(
self,
other: P2
) -> Chain<T, U, Self, P2> { ... }
fn or<P2: Parser<T, Err = E>, E: Into<Self::Err>>(
self,
other: P2
) -> Or<T, E, Self, P2> { ... }
fn many(self) -> Many<T, Self> { ... }
fn many1(self) -> Many1<T, Self> { ... }
fn map<U: 'static>(
self,
f: impl Fn(T) -> U + 'static
) -> impl Parser<U, Err = Self::Err> { ... }
fn map_err<E: 'static>(
self,
f: impl Fn(Self::Err) -> E + 'static
) -> impl Parser<T, Err = E> { ... }
fn and_then<U: 'static, E: Into<Self::Err>>(
self,
f: impl Fn(T) -> Result<U, E> + 'static
) -> impl Parser<U, Err = Self::Err> { ... }
fn after<U, P2: Parser<U, Err = E>, E: Into<Self::Err>>(
self,
other: P2
) -> impl Parser<T, Err = Self::Err> { ... }
fn replace<U, P2: Parser<U, Err = E>, E: Into<Self::Err>>(
self,
other: P2
) -> impl Parser<U, Err = Self::Err> { ... }
fn convert_err<E: From<Self::Err> + 'static>(
self
) -> impl Parser<T, Err = E> { ... }
}
Expand description
All parsers implement this trait. Any function or closure with the signature
Fn(&mut ParserString) -> Result<T, E>
implements Parser.
Required Associated Types§
Required Methods§
sourcefn parse(&self, s: &mut ParserString) -> Result<T, Self::Err>
fn parse(&self, s: &mut ParserString) -> Result<T, Self::Err>
Run this parser, using a ParserString
.
Provided Methods§
sourcefn try_parse(&self, s: &mut ParserString) -> Result<T, Self::Err>
fn try_parse(&self, s: &mut ParserString) -> Result<T, Self::Err>
Run this parser without affecting the string on failure. In other words, the string will be “rewinded” on failure.
sourcefn chain<U, P2: Parser<U, Err = E>, E: Into<Self::Err>>(
self,
other: P2
) -> Chain<T, U, Self, P2>
fn chain<U, P2: Parser<U, Err = E>, E: Into<Self::Err>>( self, other: P2 ) -> Chain<T, U, Self, P2>
Constructs a Chain
combinator.
sourcefn or<P2: Parser<T, Err = E>, E: Into<Self::Err>>(
self,
other: P2
) -> Or<T, E, Self, P2>
fn or<P2: Parser<T, Err = E>, E: Into<Self::Err>>( self, other: P2 ) -> Or<T, E, Self, P2>
Constructs a Or
combinator.
sourcefn map<U: 'static>(
self,
f: impl Fn(T) -> U + 'static
) -> impl Parser<U, Err = Self::Err>
fn map<U: 'static>( self, f: impl Fn(T) -> U + 'static ) -> impl Parser<U, Err = Self::Err>
Apply a function to the output of this parser on success.
sourcefn map_err<E: 'static>(
self,
f: impl Fn(Self::Err) -> E + 'static
) -> impl Parser<T, Err = E>
fn map_err<E: 'static>( self, f: impl Fn(Self::Err) -> E + 'static ) -> impl Parser<T, Err = E>
Apply a function to the Err
output of this parser on failure.
sourcefn and_then<U: 'static, E: Into<Self::Err>>(
self,
f: impl Fn(T) -> Result<U, E> + 'static
) -> impl Parser<U, Err = Self::Err>
fn and_then<U: 'static, E: Into<Self::Err>>( self, f: impl Fn(T) -> Result<U, E> + 'static ) -> impl Parser<U, Err = Self::Err>
Applies a function to the output of this parser on success, using error coercion rules.
sourcefn after<U, P2: Parser<U, Err = E>, E: Into<Self::Err>>(
self,
other: P2
) -> impl Parser<T, Err = Self::Err>
fn after<U, P2: Parser<U, Err = E>, E: Into<Self::Err>>( self, other: P2 ) -> impl Parser<T, Err = Self::Err>
Similar to Chain
, but only keeps the output of the first parser.
sourcefn replace<U, P2: Parser<U, Err = E>, E: Into<Self::Err>>(
self,
other: P2
) -> impl Parser<U, Err = Self::Err>
fn replace<U, P2: Parser<U, Err = E>, E: Into<Self::Err>>( self, other: P2 ) -> impl Parser<U, Err = Self::Err>
Similar to Chain
, but only keeps the output of the second parser.
sourcefn convert_err<E: From<Self::Err> + 'static>(self) -> impl Parser<T, Err = E>
fn convert_err<E: From<Self::Err> + 'static>(self) -> impl Parser<T, Err = E>
Explicitly sets the target error type of this parser. Can help with type inference.