Parser

Struct Parser 

Source
pub struct Parser<'a, T, E, F = ()> { /* private fields */ }
Expand description

A parser is a function from a State to a ParseResult. The T type is the type of the value produced by the parser, the E is the type of the errors it can produce. F is the type of failure - this is like an error, but made to be used for backtracking. It is optional. The 'a lifetime is the lifetime of the parser.

Implementations§

Source§

impl<'a, T, E, F> Parser<'a, T, E, F>

Source

pub fn ret(value: T) -> Self
where T: Clone + 'a,

Source

pub fn ret_with(value: impl Fn() -> T + 'a) -> Self

Source

pub fn fail(value: F) -> Self
where F: Clone + 'a,

Source

pub fn fail_with(value: impl Fn() -> F + 'a) -> Self

Source

pub fn err(value: E) -> Self
where E: Clone + 'a,

Source

pub fn err_with(value: impl Fn() -> E + 'a) -> Self

Source

pub fn from_fn<Func>(func: Func) -> Self
where Func: Fn(State<'a>) -> ParseResult<T, E, F> + 'a,

Source

pub fn parse(&self, state: State<'a>) -> ParseResult<T, E, F>

Source

pub fn with_name(self, name: impl Into<String>) -> Self

This is useful for debugging parsers.

Source

pub fn map<U>(self, f: impl Fn(T) -> U + 'a) -> Parser<'a, U, E, F>
where F: 'a, E: 'a, T: 'a,

Source

pub fn map_fail<G>(self, f: impl Fn(F) -> G + 'a) -> Parser<'a, T, E, G>
where F: 'a, E: 'a, T: 'a,

Source

pub fn map_err<E2>(self, f: impl Fn(E) -> E2 + 'a) -> Parser<'a, T, E2, F>
where F: 'a, E: 'a, T: 'a,

Source

pub fn and_then<U, Func>(self, func: Func) -> Parser<'a, U, E, F>
where Func: Fn(T) -> Parser<'a, U, E, F> + 'a, F: 'a, E: 'a, T: 'a,

Source

pub fn and_then_fail<G, Func>(self, func: Func) -> Parser<'a, T, E, G>
where Func: Fn(F) -> Parser<'a, T, E, G> + 'a, F: 'a, E: 'a, T: 'a,

This is like and_then, but the next parser is called when the first parser fails. Unlike and_then, the next parser is started at the same position as the first parser (not at the position that it stopped).

Source

pub fn and_then_err<E2, Func>(self, func: Func) -> Parser<'a, T, E2, F>
where Func: Fn(E) -> Parser<'a, T, E2, F> + 'a, F: 'a, E: 'a, T: 'a,

This is like and_then, but the next parser is called when the first parser returns an error (Notice - this is different from failure). Unlike and_then, the next parser is started at the same position as the first parser (not at the position that it stopped).

Source

pub fn or<G, H>(self, other: Parser<'a, T, E, G>) -> Parser<'a, T, E, H>
where T: 'a, E: 'a, F: 'a + CombineFail<'a, G, H>, G: 'a,

Source

pub fn or_ret<G>(self, x: T) -> Parser<'a, T, E, G>
where T: Clone + 'a, E: 'a, F: 'a, G: 'a,

Source

pub fn or_fail<G>(self, f: G) -> Parser<'a, T, E, G>
where T: 'a, E: 'a, F: 'a, G: Clone + 'a,

Source

pub fn or_err<G>(self, e: E) -> Parser<'a, T, E, G>
where T: 'a, E: Clone + 'a, F: 'a, G: 'a,

Source

pub fn one_of<G>( parsers: impl IntoIterator<Item = Parser<'a, T, E, F>> + 'a, ) -> Parser<'a, T, E, G>
where T: 'a, E: 'a, F: 'a + CombineManyFail<'a, G>, G: 'a,

Source

pub fn filter(self, pred: impl Fn(&T) -> bool + 'a) -> Self
where T: Clone + 'a, E: 'a, F: Clone + Default + 'a,

Source§

impl<'a, E, F> Parser<'a, State<'a>, E, F>

Source

pub fn state() -> Self

A parser that just returns the current state.

Source§

impl<'a, T: 'a, F: 'a, E: 'a> Parser<'a, T, E, F>

Source

pub fn of_bool(value: bool) -> Parser<'a, T, E, F>
where T: Default + Clone, F: Default + Clone,

Source§

impl<'a, E: 'a> Parser<'a, char, E, EofFailure>

Source

pub fn char() -> Parser<'a, char, E, EofFailure>

Source§

impl<'a, F: Clone + Default + 'a, E: 'a> Parser<'a, char, E, F>

Source

pub fn char_eq(ch: char) -> Parser<'a, char, E, F>

Source§

impl<'a, E: 'a> Parser<'a, char, E, NotWhitespace>

Source

pub fn whitespace() -> Parser<'a, char, E, NotWhitespace>

Source§

impl<'a, F: 'a, E: 'a> Parser<'a, (), E, F>

Source

pub fn skip_whitespace() -> Parser<'a, (), E, F>

Source§

impl<'a, E: 'a> Parser<'a, char, E, NotALetter>

Source

pub fn letter() -> Parser<'a, char, E, NotALetter>

Source§

impl<'a, E: 'a> Parser<'a, char, E, NotADigit>

Source

pub fn digit() -> Parser<'a, char, E, NotADigit>

Source§

impl<'a, E: 'a> Parser<'a, char, E, NotFound>

Source

pub fn eof() -> Parser<'a, (), E, NotFound>

Source

pub fn expect_string(expected: &'static str) -> Parser<'a, (), E, NotFound>

Source§

impl<'a, T, F, E> Parser<'a, T, E, F>

Source

pub fn maybe<G>(self) -> Parser<'a, Option<T>, E, G>
where T: 'a, E: 'a, F: 'a,

Repeats this parser zero or one times.

Source

pub fn repeat_0<G>(self) -> Parser<'a, Vec<T>, E, G>
where T: Clone + 'a, E: 'a, F: 'a, G: 'a,

Repeats this parser zero or more times.

Source

pub fn repeat_1(self) -> Parser<'a, Vec<T>, E, F>
where T: Clone + 'a, E: 'a, F: 'a,

Repeats this parser one or more times.

Source§

impl<'a, T, F, E> Parser<'a, T, E, F>

Source

pub fn not<G>(self) -> Parser<'a, (), E, G>
where T: 'a, E: 'a, F: 'a, G: Default + 'a,

Trait Implementations§

Source§

impl<'a, T, E, F> Clone for Parser<'a, T, E, F>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T, E, F> Debug for Parser<'_, T, E, F>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a, T, E, F> Freeze for Parser<'a, T, E, F>

§

impl<'a, T, E, F = ()> !RefUnwindSafe for Parser<'a, T, E, F>

§

impl<'a, T, E, F = ()> !Send for Parser<'a, T, E, F>

§

impl<'a, T, E, F = ()> !Sync for Parser<'a, T, E, F>

§

impl<'a, T, E, F> Unpin for Parser<'a, T, E, F>

§

impl<'a, T, E, F = ()> !UnwindSafe for Parser<'a, T, E, F>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<F1, F2> CombineFail<'_, F2, ()> for F1

Source§

fn combine_fail(self, _: State<'_>, _: F2, _: State<'_>)

Source§

impl<F1, F2> CombineFail<'_, F2, (F1, F2)> for F1

Source§

fn combine_fail(self, _: State<'_>, other: F2, _: State<'_>) -> (F1, F2)

Source§

impl<'a, F1, F2> CombineFail<'a, F2, (F1, State<'a>, F2, State<'a>)> for F1

Source§

fn combine_fail( self, s1: State<'a>, other: F2, s2: State<'a>, ) -> (F1, State<'a>, F2, State<'a>)

Source§

impl<F> CombineManyFail<'_, ()> for F

Source§

impl<F> CombineManyFail<'_, Vec<F>> for F

Source§

fn combine_many_fail(fails: Vec<(F, State<'_>)>) -> Vec<F>

Source§

impl<'a, F> CombineManyFail<'a, Vec<(F, State<'a>)>> for F

Source§

fn combine_many_fail(fails: Vec<(F, State<'_>)>) -> Vec<(F, State<'_>)>

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Mutate for T

Source§

fn mutate<F>(self, f: F) -> Self
where Self: Sized, F: FnOnce(&mut Self),

Source§

impl<X> Pipe for X

Source§

fn pipe<Return, Function>(self, f: Function) -> Return
where Self: Sized, Function: FnOnce(Self) -> Return,

Apply f to self. Read more
Source§

fn pipe_ref<'a, Return, Function>(&'a self, f: Function) -> Return
where Function: FnOnce(&'a Self) -> Return,

Apply f to &self. Read more
Source§

fn pipe_mut<'a, Return, Function>(&'a mut self, f: Function) -> Return
where Function: FnOnce(&'a mut Self) -> Return,

Apply f to &mut self. Read more
Source§

fn pipe_as_ref<'a, Param, Return, Function>(&'a self, f: Function) -> Return
where Self: AsRef<Param>, Param: 'a + ?Sized, Function: FnOnce(&'a Param) -> Return,

Apply f to &self where f takes a single parameter of type Param and Self implements trait AsRef<Param>. Read more
Source§

fn pipe_as_mut<'a, Param, Return, Function>(&'a mut self, f: Function) -> Return
where Self: AsMut<Param>, Param: 'a + ?Sized, Function: FnOnce(&'a mut Param) -> Return,

Apply f to &mut self where f takes a single parameter of type Param and Self implements trait AsMut<Param>. Read more
Source§

fn pipe_deref<'a, Param, Return, Function>(&'a self, f: Function) -> Return
where Self: Deref<Target = Param>, Param: 'a + ?Sized, Function: FnOnce(&'a Param) -> Return,

Apply f to &self where f takes a single parameter of type Param and Self implements trait Deref<Target = Param>. Read more
Source§

fn pipe_deref_mut<'a, Param, Return, Function>( &'a mut self, f: Function, ) -> Return
where Self: DerefMut<Target = Param>, Param: 'a + ?Sized, Function: FnOnce(&'a mut Param) -> Return,

Apply f to &mut self where f takes a single parameter of type Param and Self implements trait DerefMut<Target = Param>. Read more
Source§

fn pipe_borrow<'a, Param, Return, Function>(&'a self, f: Function) -> Return
where Self: Borrow<Param>, Param: 'a + ?Sized, Function: FnOnce(&'a Param) -> Return,

Apply f to &self where f takes a single parameter of type Param and Self implements trait Borrow<Param>. Read more
Source§

fn pipe_borrow_mut<'a, Param, Return, Function>( &'a mut self, f: Function, ) -> Return
where Self: BorrowMut<Param>, Param: 'a + ?Sized, Function: FnOnce(&'a mut Param) -> Return,

Apply f to &mut self where f takes a single parameter of type Param and Self implements trait BorrowMut<Param>. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.