Trait lip::Parser [−][src]
pub trait Parser<'a, Output, State: Clone> {
Show 13 methods
fn parse(
&self,
input: &'a str,
location: Location,
state: State
) -> ParseResult<'a, Output, State>;
fn run(
&self,
input: &'a str,
state: State
) -> ParseResult<'a, Output, State>;
fn map<F, NewOutput>(self, map_fn: F) -> BoxedParser<'a, NewOutput, State>
where
Self: Sized + 'a,
Output: 'a,
NewOutput: 'a,
State: 'a,
F: Fn(Output) -> NewOutput + 'a,
{ ... }
fn map_with_state<F, NewOutput>(
self,
map_fn: F
) -> BoxedParser<'a, NewOutput, State>
where
Self: Sized + 'a,
Output: 'a,
NewOutput: 'a,
State: 'a,
F: Fn(Output, State) -> NewOutput + 'a,
{ ... }
fn map_err<F>(self, map_fn: F) -> BoxedParser<'a, Output, State>
where
Self: Sized + 'a,
Output: 'a,
State: 'a,
F: Fn(String) -> String + 'a,
{ ... }
fn and_then<F, NextParser, NewOutput>(
self,
f: F
) -> BoxedParser<'a, NewOutput, State>
where
Self: Sized + 'a,
Output: 'a,
NewOutput: 'a,
State: 'a,
NextParser: Parser<'a, NewOutput, State> + 'a,
F: Fn(Output) -> NextParser + 'a,
{ ... }
fn pred<F>(
self,
predicate: F,
expecting: &'a str
) -> BoxedParser<'a, Output, State>
where
Self: Sized + 'a,
Output: Display + 'a,
State: 'a,
F: Fn(&Output) -> bool + 'a,
{ ... }
fn ignore(self) -> BoxedParser<'a, (), State>
where
Self: Sized + 'a,
Output: 'a,
State: 'a,
{ ... }
fn update_state<F>(self, f: F) -> BoxedParser<'a, Output, State>
where
Self: Sized + 'a,
State: 'a,
Output: Clone + 'a,
F: Fn(Output, State) -> State + 'a,
{ ... }
fn update<F, NewOutput>(self, f: F) -> BoxedParser<'a, NewOutput, State>
where
Self: Sized + 'a,
State: 'a,
Output: Clone + 'a,
NewOutput: Clone + 'a,
F: Fn(&'a str, Output, Location, State) -> ParseResult<'a, NewOutput, State> + 'a,
{ ... }
fn end(self) -> BoxedParser<'a, Output, State>
where
Self: Sized + 'a,
Output: Clone + 'a,
State: 'a,
{ ... }
fn keep<A, B, ArgParser>(
self,
arg_parser: ArgParser
) -> BoxedParser<'a, B, State>
where
Self: Sized + 'a,
A: 'a,
B: 'a,
State: 'a,
Output: FnOnce(A) -> B + 'a,
ArgParser: Parser<'a, A, State> + 'a,
{ ... }
fn skip<P: 'a, A>(self, ignored_parser: P) -> BoxedParser<'a, Output, State>
where
Self: Sized + 'a,
A: 'a,
State: 'a,
Output: 'a,
P: Parser<'a, A, State>,
{ ... }
}
Required methods
fn parse(
&self,
input: &'a str,
location: Location,
state: State
) -> ParseResult<'a, Output, State>
fn parse(
&self,
input: &'a str,
location: Location,
state: State
) -> ParseResult<'a, Output, State>
Parse a given input, starting at a given location and state.
fn run(&self, input: &'a str, state: State) -> ParseResult<'a, Output, State>
fn run(&self, input: &'a str, state: State) -> ParseResult<'a, Output, State>
Run the parser on a given input, starting at the first character.
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)
Provided methods
fn map<F, NewOutput>(self, map_fn: F) -> BoxedParser<'a, NewOutput, State> where
Self: Sized + 'a,
Output: 'a,
NewOutput: 'a,
State: 'a,
F: Fn(Output) -> NewOutput + 'a,
fn map<F, NewOutput>(self, map_fn: F) -> BoxedParser<'a, NewOutput, State> where
Self: Sized + 'a,
Output: 'a,
NewOutput: 'a,
State: 'a,
F: Fn(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.
fn map_with_state<F, NewOutput>(
self,
map_fn: F
) -> BoxedParser<'a, NewOutput, State> where
Self: Sized + 'a,
Output: 'a,
NewOutput: 'a,
State: 'a,
F: Fn(Output, State) -> NewOutput + 'a,
fn map_with_state<F, NewOutput>(
self,
map_fn: F
) -> BoxedParser<'a, NewOutput, State> where
Self: Sized + 'a,
Output: 'a,
NewOutput: 'a,
State: 'a,
F: Fn(Output, 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.
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.
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.
Judge if the output meets the requirement using a predicate function if the parse succeeds. Otherwise, return error as usual.
fn ignore(self) -> BoxedParser<'a, (), State> where
Self: Sized + 'a,
Output: 'a,
State: 'a,
fn ignore(self) -> BoxedParser<'a, (), State> where
Self: Sized + 'a,
Output: 'a,
State: 'a,
Ignore the parse output and return ()
(emtpy tuple)
fn update_state<F>(self, f: F) -> BoxedParser<'a, Output, State> where
Self: Sized + 'a,
State: 'a,
Output: Clone + 'a,
F: Fn(Output, State) -> State + 'a,
fn update_state<F>(self, f: F) -> BoxedParser<'a, Output, State> where
Self: Sized + 'a,
State: 'a,
Output: Clone + 'a,
F: Fn(Output, State) -> State + 'a,
Update the state given the new output and state of the parse if parse succeeds. Otherwise, return error as usual.
fn update<F, NewOutput>(self, f: F) -> BoxedParser<'a, NewOutput, State> where
Self: Sized + 'a,
State: 'a,
Output: Clone + 'a,
NewOutput: Clone + 'a,
F: Fn(&'a str, Output, Location, State) -> ParseResult<'a, NewOutput, State> + 'a,
fn update<F, NewOutput>(self, f: F) -> BoxedParser<'a, NewOutput, State> where
Self: Sized + 'a,
State: 'a,
Output: Clone + 'a,
NewOutput: Clone + 'a,
F: Fn(&'a str, Output, Location, State) -> ParseResult<'a, NewOutput, State> + 'a,
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
.
fn end(self) -> BoxedParser<'a, Output, State> where
Self: Sized + 'a,
Output: Clone + 'a,
State: 'a,
fn end(self) -> BoxedParser<'a, Output, State> where
Self: Sized + 'a,
Output: Clone + 'a,
State: '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.",
);
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)
fn skip<P: 'a, A>(self, ignored_parser: P) -> BoxedParser<'a, Output, State> where
Self: Sized + 'a,
A: 'a,
State: 'a,
Output: 'a,
P: Parser<'a, A, State>,
fn skip<P: 'a, A>(self, ignored_parser: P) -> BoxedParser<'a, Output, State> where
Self: Sized + 'a,
A: 'a,
State: 'a,
Output: 'a,
P: Parser<'a, A, State>,
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)