Function state

Source
pub fn state<'i, R: RuleType, F>(
    input: Arc<str>,
    f: F,
) -> Result<Pairs<R>, Error<R>>
Expand description

Creates a ParserState from a &str, supplying it to a closure f.

ยงExamples

let input: Arc<str> = Arc::from("");
pest::state::<(), _>(input, |s| Ok(s)).unwrap();
Examples found in repository?
examples/parens.rs (lines 45-49)
23    fn parse(rule: Rule, input: Arc<str>) -> Result<Pairs<Rule>, Error<Rule>> {
24        fn expr(state: Box<ParserState<Rule>>) -> ParseResult<Box<ParserState<Rule>>> {
25            state.sequence(|s| s.repeat(|s| paren(s)).and_then(|s| s.end_of_input()))
26        }
27
28        fn paren(state: Box<ParserState<Rule>>) -> ParseResult<Box<ParserState<Rule>>> {
29            state.rule(Rule::paren, |s| {
30                s.sequence(|s| {
31                    s.match_string("(")
32                        .and_then(|s| {
33                            s.optional(|s| {
34                                s.sequence(|s| {
35                                    s.lookahead(true, |s| s.match_string("("))
36                                        .and_then(|s| s.repeat(|s| paren(s)))
37                                })
38                            })
39                        })
40                        .and_then(|s| s.rule(Rule::paren_end, |s| s.match_string(")")))
41                })
42            })
43        }
44
45        state(input, |state| match rule {
46            Rule::expr => expr(state),
47            Rule::paren => paren(state),
48            _ => unreachable!(),
49        })
50    }