pub fn state<'i, R: RuleType, F>(
input: &'i str,
f: F
) -> Result<Pairs<'i, R>, Error<R>>where
F: FnOnce(Box<ParserState<'i, R>>) -> ParseResult<Box<ParserState<'i, R>>>,
Expand description
Creates a ParserState
from a &str
, supplying it to a closure f
.
Examples
let input = "";
pest::state::<(), _>(input, |s| Ok(s)).unwrap();
Examples found in repository?
examples/parens.rs (lines 42-46)
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
fn parse(rule: Rule, input: &str) -> Result<Pairs<Rule>, Error<Rule>> {
fn expr(state: Box<ParserState<'_, Rule>>) -> ParseResult<Box<ParserState<'_, Rule>>> {
state.sequence(|s| s.repeat(paren).and_then(|s| s.end_of_input()))
}
fn paren(state: Box<ParserState<'_, Rule>>) -> ParseResult<Box<ParserState<'_, Rule>>> {
state.rule(Rule::paren, |s| {
s.sequence(|s| {
s.match_string("(")
.and_then(|s| {
s.optional(|s| {
s.sequence(|s| {
s.lookahead(true, |s| s.match_string("("))
.and_then(|s| s.repeat(paren))
})
})
})
.and_then(|s| s.rule(Rule::paren_end, |s| s.match_string(")")))
})
})
}
state(input, |state| match rule {
Rule::expr => expr(state),
Rule::paren => paren(state),
_ => unreachable!(),
})
}