pub trait Parser<T, I>: Sized {
    // Required method
    fn parse(&mut self, input: I) -> Option<(T, I)>;

    // Provided methods
    fn and<U, P: Parser<U, I>>(self, parser: P) -> And<Self, P> { ... }
    fn or<P: Parser<T, I>>(self, parser: P) -> Or<Self, P> { ... }
    fn map<F: Fn(T) -> U, U>(self, func: F) -> Map<Self, F, T> { ... }
    fn and_left<P: Parser<U, I>, U>(self, parser: P) -> AndLeft<Self, P, U> { ... }
    fn and_right<P: Parser<U, I>, U>(self, parser: P) -> AndRight<Self, P, T> { ... }
}
Expand description

Defines a parser.

It is basically a function that takes an input and returns a parsed result along with the rest of input (which can be parsed further).

Required Methods§

source

fn parse(&mut self, input: I) -> Option<(T, I)>

Applies the parser on some input.

It returns the parsed value and the rest of the unparsed input as Some, if successful.
Failing that, it returns None.

Provided Methods§

source

fn and<U, P: Parser<U, I>>(self, parser: P) -> And<Self, P>

Sequences two parsers, one after the other, collecting both results.

source

fn or<P: Parser<T, I>>(self, parser: P) -> Or<Self, P>

Tries to apply the first parser, if it fails, it tries to apply the second parser.

source

fn map<F: Fn(T) -> U, U>(self, func: F) -> Map<Self, F, T>

Maps a function over the output value of the parser.

source

fn and_left<P: Parser<U, I>, U>(self, parser: P) -> AndLeft<Self, P, U>

Sequences two parsers, one after the other, but discards the output of the second one.

source

fn and_right<P: Parser<U, I>, U>(self, parser: P) -> AndRight<Self, P, T>

Sequences two parsers, one after the other, but discards the output of the first one.

Object Safety§

This trait is not object safe.

Implementors§

source§

impl<A, B, T, U, I> Parser<T, I> for AndLeft<A, B, U>
where A: Parser<T, I>, B: Parser<U, I>,

source§

impl<A, B, T, U, I> Parser<U, I> for AndRight<A, B, T>
where A: Parser<T, I>, B: Parser<U, I>,

source§

impl<P, T, F, U, I> Parser<U, I> for Map<P, F, T>
where P: Parser<T, I>, F: Fn(T) -> U,

source§

impl<T1, T2, A, B, I> Parser<(T1, T2), I> for And<A, B>
where A: Parser<T1, I>, B: Parser<T2, I>,

source§

impl<T, A, B, I> Parser<T, I> for Or<A, B>
where I: Clone, A: Parser<T, I>, B: Parser<T, I>,

source§

impl<T, F, I> Parser<T, I> for F
where F: FnMut(I) -> Option<(T, I)>,

Because a Parser is basically a function of the following signature.

(I) -> (T, I)

We can implement it for any bare Fn(I) -> (T, I).