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§
Provided Methods§
sourcefn run(
&mut self,
input: &'a str,
state: Self::State
) -> ParseResult<'a, Self::Output, Self::State>
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)
sourcefn map<F, NewOutput: 'a>(self, map_fn: F) -> Map<Self, F>where
Self: Sized + 'a,
F: Fn(Self::Output) -> NewOutput + 'a,
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.
sourcefn 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_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.
sourcefn map_err<F>(self, map_fn: F) -> MapErr<Self, F>where
Self: Sized + 'a,
F: Fn(String) -> String,
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.
sourcefn 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 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.
sourcefn pred<F>(self, predicate: F, expecting: &'a str) -> Pred<'_, Self, F>where
Self: Sized + 'a,
F: Fn(&Self::Output) -> bool,
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.
sourcefn ignore(self) -> Ignore<Self>where
Self: Sized + 'a,
fn ignore(self) -> Ignore<Self>where Self: Sized + 'a,
Ignore the parse output and return ()
(emtpy tuple)
sourcefn update_state<F>(self, f: F) -> UpdateState<Self, F>where
Self: Sized + 'a,
F: Fn(Self::Output, Self::State) -> Self::State,
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.
sourcefn 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 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
.
sourcefn end(self) -> End<Self>where
Self: Sized + 'a,
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.",
);
sourcefn 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 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)
sourcefn skip<P2>(self, ignored_parser: P2) -> Skip<Self, P2>where
Self: Sized + 'a,
P2: Parser<'a>,
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)