Trait lip::Parser

source ·
pub trait Parser<'a> {
    type Output;
    type State: Clone;

Show 19 methods // Required method fn parse( &mut self, input: &'a str, location: Location, state: Self::State ) -> ParseResult<'a, Self::Output, Self::State>; // Provided methods fn run( &mut self, input: &'a str, state: Self::State ) -> ParseResult<'a, Self::Output, Self::State> { ... } fn map<F, NewOutput: 'a>(self, map_fn: F) -> Map<Self, F> where Self: Sized + 'a, F: Fn(Self::Output) -> NewOutput + 'a { ... } fn map_with_state<F, NewOutput: 'a>( self, map_fn: F ) -> MapWithState<Self, F> where Self: Sized + 'a, F: Fn(Self::Output, Self::State) -> NewOutput + 'a { ... } fn map_err<F>(self, map_fn: F) -> MapErr<Self, F> where Self: Sized + 'a, F: Fn(String) -> String { ... } fn and_then<F, P2, B>(self, f: F) -> AndThen<Self, F> where Self: Sized + 'a, P2: Parser<'a, Output = B>, F: Fn(Self::Output) -> P2 { ... } fn pred<F>(self, predicate: F, expecting: &'a str) -> Pred<'_, Self, F> where Self: Sized + 'a, F: Fn(&Self::Output) -> bool { ... } fn ignore(self) -> Ignore<Self> where Self: Sized + 'a { ... } fn update_state<F>(self, f: F) -> UpdateState<Self, F> where Self: Sized + 'a, F: Fn(Self::Output, Self::State) -> Self::State { ... } fn update<B, F>(self, f: F) -> Update<Self, F> where Self: Sized + 'a, F: FnOnce(&'a str, Self::Output, Location, Self::State) -> ParseResult<'a, B, Self::State> + Clone { ... } fn end(self) -> End<Self> where Self: Sized + 'a { ... } fn keep<A, B, P2>(self, arg_parser: P2) -> Keep<Self, P2> where Self: Sized + 'a, Self::Output: FnOnce(A) -> B + Clone, P2: Parser<'a, Output = A> { ... } fn skip<P2>(self, ignored_parser: P2) -> Skip<Self, P2> where Self: Sized + 'a, P2: Parser<'a> { ... } fn backtrackable(self) -> Backtrackable<Self> where Self: Sized + 'a { ... } fn first_of_two<T2>(self) -> OneOfTwo<Self, T2> where Self: Sized + 'a, T2: Parser<'a, Output = Self::Output, State = Self::State> { ... } fn second_of_two<T1>(self) -> OneOfTwo<T1, Self> where Self: Sized + 'a, T1: Parser<'a, Output = Self::Output, State = Self::State> { ... } fn first_of_three<T2, T3>(self) -> OneOfThree<Self, T2, T3> where Self: Sized + 'a, T2: Parser<'a, Output = Self::Output, State = Self::State>, T3: Parser<'a, Output = Self::Output, State = Self::State> { ... } fn second_of_three<T1, T3>(self) -> OneOfThree<T1, Self, T3> where Self: Sized + 'a, T1: Parser<'a, Output = Self::Output, State = Self::State>, T3: Parser<'a, Output = Self::Output, State = Self::State> { ... } fn third_of_three<T1, T2>(self) -> OneOfThree<T1, T2, Self> where Self: Sized + 'a, T1: Parser<'a, Output = Self::Output, State = Self::State>, T2: Parser<'a, Output = Self::Output, State = Self::State> { ... }
}

Required Associated Types§

Required Methods§

source

fn parse( &mut self, input: &'a str, location: Location, state: Self::State ) -> ParseResult<'a, Self::Output, Self::State>

Parse a given input, starting at a given location and state.

Provided Methods§

source

fn run( &mut self, input: &'a str, state: Self::State ) -> ParseResult<'a, Self::Output, Self::State>

Run the parser on a given input, starting at the first character.

Example:

Parse a (x, y) position pair.

// Parse an (x, y) position pair
let position = succeed!(|x, y| (x, y))
  .keep(int())
  .skip(token(","))
  .keep(int())
  .run("2, 3", ()); // position == (2, 3)
source

fn map<F, NewOutput: 'a>(self, map_fn: F) -> Map<Self, F>where Self: Sized + 'a, F: Fn(Self::Output) -> NewOutput + 'a,

Map the output to a new output if parse succeeds. Otherwise, return error as usual.

The only difference from the map on ParseResult is that this map turns one Parser into another while the map on ParseResult turns one ParseResult into another.

source

fn map_with_state<F, NewOutput: 'a>(self, map_fn: F) -> MapWithState<Self, F>where Self: Sized + 'a, F: Fn(Self::Output, Self::State) -> NewOutput + 'a,

The map function is supplied both the output and the state of the parser. Otherwise, return error as usual.

The only difference from the map_with_state on ParseResult is that this map_with_state turns one Parser into another while the map_with_state on ParseResult turns one ParseResult into another.

source

fn map_err<F>(self, map_fn: F) -> MapErr<Self, F>where Self: Sized + 'a, F: Fn(String) -> String,

Map the error message to a new message if parse fails. Otherwise, return output as usual.

The only difference from the map_err on ParseResult is that this map_err turns one Parser into another while the map_err on ParseResult turns one ParseResult into another.

source

fn and_then<F, P2, B>(self, f: F) -> AndThen<Self, F>where Self: Sized + 'a, P2: Parser<'a, Output = B>, F: Fn(Self::Output) -> P2,

Returns a new parser which is given the current output if parse succeeds. Otherwise, return error as usual.

The only difference from the and_then on ParseResult is that this and_then turns one Parser into another while the and_then on ParseResult turns one ParseResult into another.

source

fn pred<F>(self, predicate: F, expecting: &'a str) -> Pred<'_, Self, F>where Self: Sized + 'a, F: Fn(&Self::Output) -> bool,

Judge if the output meets the requirement using a predicate function if the parse succeeds. Otherwise, return error as usual.

source

fn ignore(self) -> Ignore<Self>where Self: Sized + 'a,

Ignore the parse output and return () (emtpy tuple)

source

fn update_state<F>(self, f: F) -> UpdateState<Self, F>where Self: Sized + 'a, F: Fn(Self::Output, Self::State) -> Self::State,

Update the state given the new output and state of the parse if parse succeeds. Otherwise, return error as usual.

source

fn update<B, F>(self, f: F) -> Update<Self, F>where Self: Sized + 'a, F: FnOnce(&'a str, Self::Output, Location, Self::State) -> ParseResult<'a, B, Self::State> + Clone,

Update the result of the parser if parse succeeds. Otherwise, return error as usual.

This is the most general and powerful method of the parser. Think about using simpler methods like map and map_err before choosing update.

source

fn end(self) -> End<Self>where Self: Sized + 'a,

Check if you have reached the end of the input you are parsing.

If you want to parse an input containing only “abc”:

assert_succeed(
 token("abc").end(),
 "abc", "abc",
);
assert_fail(
  token("abc").end(),
  "abcd", "I'm expecting the end of input.",
);
source

fn keep<A, B, P2>(self, arg_parser: P2) -> Keep<Self, P2>where Self: Sized + 'a, Self::Output: FnOnce(A) -> B + Clone, P2: Parser<'a, Output = A>,

Keep values in a parser pipeline.

Exammple:

Parse a (x, y) position pair.

// Parse an (x, y) position pair
let position = succeed!(|x, y| (x, y))
  .keep(int())
  .skip(token(","))
  .keep(int())
  .run("2, 3", ()); // position == (2, 3)
source

fn skip<P2>(self, ignored_parser: P2) -> Skip<Self, P2>where Self: Sized + 'a, P2: Parser<'a>,

Skip values in a parser pipeline.

Exammple:

Parse a (x, y) position pair.

// Parse an (x, y) position pair
let position = succeed!(|x, y| (x, y))
  .keep(int())
  .skip(token(","))
  .keep(int())
  .run("2, 3", ()); // position == (2, 3)
source

fn backtrackable(self) -> Backtrackable<Self>where Self: Sized + 'a,

source

fn first_of_two<T2>(self) -> OneOfTwo<Self, T2>where Self: Sized + 'a, T2: Parser<'a, Output = Self::Output, State = Self::State>,

source

fn second_of_two<T1>(self) -> OneOfTwo<T1, Self>where Self: Sized + 'a, T1: Parser<'a, Output = Self::Output, State = Self::State>,

source

fn first_of_three<T2, T3>(self) -> OneOfThree<Self, T2, T3>where Self: Sized + 'a, T2: Parser<'a, Output = Self::Output, State = Self::State>, T3: Parser<'a, Output = Self::Output, State = Self::State>,

source

fn second_of_three<T1, T3>(self) -> OneOfThree<T1, Self, T3>where Self: Sized + 'a, T1: Parser<'a, Output = Self::Output, State = Self::State>, T3: Parser<'a, Output = Self::Output, State = Self::State>,

source

fn third_of_three<T1, T2>(self) -> OneOfThree<T1, T2, Self>where Self: Sized + 'a, T1: Parser<'a, Output = Self::Output, State = Self::State>, T2: Parser<'a, Output = Self::Output, State = Self::State>,

Implementors§

source§

impl<'a, A, B, P, F> Parser<'a> for MapWithState<P, F>where P: Parser<'a, Output = A>, F: FnOnce(A, P::State) -> B + Clone,

§

type Output = B

§

type State = <P as Parser<'a>>::State

source§

impl<'a, A, S: Clone + 'a> Parser<'a> for BoxedParser<'a, A, S>

§

type Output = A

§

type State = S

source§

impl<'a, A, S: Clone, F> Parser<'a> for FnParser<F, A, S>where F: FnMut(&'a str, Location, S) -> ParseResult<'a, A, S>,

§

type Output = A

§

type State = S

source§

impl<'a, A: 'a, B: 'a, P, F> Parser<'a> for Map<P, F>where P: Parser<'a, Output = A> + 'a, F: FnOnce(A) -> B + Clone,

§

type Output = B

§

type State = <P as Parser<'a>>::State

source§

impl<'a, P1, P2, A, B, F, S: Clone + 'a> Parser<'a> for Keep<P1, P2>where F: FnOnce(A) -> B, P1: Parser<'a, Output = F, State = S>, P2: Parser<'a, Output = A, State = S>,

§

type Output = B

§

type State = <P1 as Parser<'a>>::State

source§

impl<'a, P1, P2, F, S: Clone + 'a> Parser<'a> for AndThen<P1, F>where P1: Parser<'a, State = S>, P2: Parser<'a, State = S>, F: FnOnce(P1::Output) -> P2 + Clone,

§

type Output = <P2 as Parser<'a>>::Output

§

type State = S

source§

impl<'a, P1, P2, P3, S: Clone + 'a> Parser<'a> for OneOfThree<P1, P2, P3>where P1: Parser<'a, State = S>, P2: Parser<'a, Output = P1::Output, State = S>, P3: Parser<'a, Output = P1::Output, State = S>,

§

type Output = <P1 as Parser<'a>>::Output

§

type State = S

source§

impl<'a, P1, P2, S: Clone + 'a> Parser<'a> for OneOfTwo<P1, P2>where P1: Parser<'a, State = S>, P2: Parser<'a, Output = P1::Output, State = S>,

§

type Output = <P1 as Parser<'a>>::Output

§

type State = S

source§

impl<'a, P1, P2, S: Clone + 'a> Parser<'a> for Skip<P1, P2>where P1: Parser<'a, State = S>, P2: Parser<'a, State = S>,

§

type Output = <P1 as Parser<'a>>::Output

§

type State = <P1 as Parser<'a>>::State

source§

impl<'a, P> Parser<'a> for Backtrackable<P>where P: Parser<'a> + 'a,

§

type Output = <P as Parser<'a>>::Output

§

type State = <P as Parser<'a>>::State

source§

impl<'a, P> Parser<'a> for End<P>where P: Parser<'a>,

§

type Output = <P as Parser<'a>>::Output

§

type State = <P as Parser<'a>>::State

source§

impl<'a, P> Parser<'a> for Ignore<P>where P: Parser<'a>,

§

type Output = ()

§

type State = <P as Parser<'a>>::State

source§

impl<'a, P, A, B, F> Parser<'a> for Update<P, F>where P: Parser<'a, Output = A> + 'a, F: FnOnce(&'a str, A, Location, P::State) -> ParseResult<'a, B, P::State> + Clone,

§

type Output = B

§

type State = <P as Parser<'a>>::State

source§

impl<'a, P, F> Parser<'a> for MapErr<P, F>where P: Parser<'a> + 'a, F: FnOnce(String) -> String + Clone,

§

type Output = <P as Parser<'a>>::Output

§

type State = <P as Parser<'a>>::State

source§

impl<'a, P, F> Parser<'a> for UpdateState<P, F>where P: Parser<'a> + 'a, F: Fn(&P::Output, P::State) -> P::State,

§

type Output = <P as Parser<'a>>::Output

§

type State = <P as Parser<'a>>::State

source§

impl<'a, P, F, Output: Display> Parser<'a> for Pred<'a, P, F>where P: Parser<'a, Output = Output> + 'a, F: Fn(&P::Output) -> bool + 'a,

§

type Output = <P as Parser<'a>>::Output

§

type State = <P as Parser<'a>>::State