Struct pest::ParserState
[−]
[src]
pub struct ParserState<R: RuleType, I: Input> { pub is_atomic: bool, pub stack: Vec<Span<I>>, // some fields omitted }
A struct
which contains the complete state of a Parser
.
Fields
is_atomic: bool
Specifies whether the current state is atomic
stack: Vec<Span<I>>
Stack of Span
s
Methods
impl<R: RuleType, I: Input> ParserState<R, I>
[src]
fn rule<F>(
&mut self,
rule: R,
pos: Position<I>,
f: F
) -> Result<Position<I>, Position<I>> where
F: FnOnce(&mut ParserState<R, I>, Position<I>) -> Result<Position<I>, Position<I>>,
&mut self,
rule: R,
pos: Position<I>,
f: F
) -> Result<Position<I>, Position<I>> where
F: FnOnce(&mut ParserState<R, I>, Position<I>) -> Result<Position<I>, Position<I>>,
Wrapper needed to generate tokens.
Examples
enum Rule { a } let input = Rc::new(StringInput::new("a".to_owned())); let pairs: Vec<_> = pest::state(input, |state, pos| { state.rule(Rule::a, pos, |_, p| Ok(p)) }).unwrap().collect(); assert_eq!(pairs.len(), 1);
fn sequence<F>(&mut self, f: F) -> Result<Position<I>, Position<I>> where
F: FnOnce(&mut ParserState<R, I>) -> Result<Position<I>, Position<I>>,
F: FnOnce(&mut ParserState<R, I>) -> Result<Position<I>, Position<I>>,
Wrapper which removes Tokens
in case of a sequence's failure.
Usually used in conjunction with
Position::sequence
.
Examples
enum Rule { a } let input = Rc::new(StringInput::new("a".to_owned())); let pairs: Vec<_> = pest::state(input, |state, pos| { state.sequence(move |state| { pos.sequence(|p| { state.rule(Rule::a, p, |_, p| Ok(p)).and_then(|p| { p.match_string("b") }) }) }).or_else(|p| { Ok(p) }) }).unwrap().collect(); assert_eq!(pairs.len(), 0);
fn lookahead<F>(
&mut self,
is_positive: bool,
f: F
) -> Result<Position<I>, Position<I>> where
F: FnOnce(&mut ParserState<R, I>) -> Result<Position<I>, Position<I>>,
&mut self,
is_positive: bool,
f: F
) -> Result<Position<I>, Position<I>> where
F: FnOnce(&mut ParserState<R, I>) -> Result<Position<I>, Position<I>>,
Wrapper which stops Token
s from being generated.
Usually used in conjunction with
Position::lookahead
.
Examples
enum Rule { a } let input = Rc::new(StringInput::new("a".to_owned())); let pairs: Vec<_> = pest::state(input, |state, pos| { state.lookahead(true, move |state| { state.rule(Rule::a, pos, |_, p| Ok(p)) }) }).unwrap().collect(); assert_eq!(pairs.len(), 0);
fn atomic<F>(
&mut self,
is_atomic: bool,
f: F
) -> Result<Position<I>, Position<I>> where
F: FnOnce(&mut ParserState<R, I>) -> Result<Position<I>, Position<I>>,
&mut self,
is_atomic: bool,
f: F
) -> Result<Position<I>, Position<I>> where
F: FnOnce(&mut ParserState<R, I>) -> Result<Position<I>, Position<I>>,
Wrapper which stops Token
s from being generated according to is_atomic
.
Examples
enum Rule { a } let input = Rc::new(StringInput::new("a".to_owned())); let pairs: Vec<_> = pest::state(input, |state, pos| { state.atomic(true, move |state| { state.rule(Rule::a, pos, |_, p| Ok(p)) }) }).unwrap().collect(); assert_eq!(pairs.len(), 0);
fn is_atomic(&self) -> bool
Returns whether the ParserState
is in an atomic state.
Examples
enum Rule { a } let input = Rc::new(StringInput::new("a".to_owned())); let pairs: Vec<_> = pest::state::<(), _, _>(input, |state, pos| { assert!(!state.is_atomic()); state.atomic(true, move |state| { assert!(state.is_atomic()); Ok(pos) }) }).unwrap().collect(); assert_eq!(pairs.len(), 0);