Trait Parser

Source
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§

Source

type Err

The error type this parser can return

Required Methods§

Source

fn parse(&self, s: &mut ParserString) -> Result<T, Self::Err>

Run this parser, using a ParserString.

Provided Methods§

Source

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.

Source

fn chain<U, P2: Parser<U, Err = E>, E: Into<Self::Err>>( self, other: P2, ) -> Chain<T, U, Self, P2>

Constructs a Chain combinator.

Source

fn or<P2: Parser<T, Err = E>, E: Into<Self::Err>>( self, other: P2, ) -> Or<T, E, Self, P2>

Constructs a Or combinator.

Source

fn many(self) -> Many<T, Self>

Constructs a Many combinator.

Source

fn many1(self) -> Many1<T, Self>

Constructs a Many1 combinator.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl<T, E, F: Fn(&mut ParserString) -> Result<T, E>> Parser<T> for F

Source§

type Err = E

Source§

impl<T, E, P1, P2> Parser<T> for Or<T, E, P1, P2>
where P1: Parser<T>, E: Into<P1::Err>, P2: Parser<T, Err = E>,

Source§

type Err = <P1 as Parser<T>>::Err

Source§

impl<T, P> Parser<Vec<T>> for Many1<T, P>
where P: Parser<T>,

Source§

type Err = <P as Parser<T>>::Err

Source§

impl<T, P> Parser<Vec<T>> for Many<T, P>
where P: Parser<T>,

Source§

impl<T, U, P1, P2, E> Parser<(T, U)> for Chain<T, U, P1, P2>
where P1: Parser<T>, E: Into<P1::Err>, P2: Parser<U, Err = E>,

Source§

type Err = <P1 as Parser<T>>::Err