pub struct ParserState<'a, L> { /* private fields */ }Expand description
The inner state of a parser.
ParserState is a cursor over the lexer and keeps track of the current position
in the source code. It is used to drive the parsing process.
§Writing a Parser
A parser is a function Fn(&ParserState) -> Result<T, Error>, that takes a
&ParserState as input and returns the parsed result or an error.
The common use case is to call the parse method to
read a token from the lexer and advance the state by one token.
fn parse_abc(state: &mut ParserState<CharLexer>) -> Result<char, Error> {
state.parse_char('a')?;
state.parse_char('b')?;
state.parse_char('c')?;
Ok('c')
}
let mut state = ParserState::new("abc");
parse_abc(&mut state).unwrap();
assert!(state.is_empty());Please note that ParserState uses interior mutability to share its state
between parsers. This means that even if a parser takes a &ParserState,
the state can still be mutated.
§Speculative Parsing
ParserState allows you to create a fork of the current state via the
fork method, and join it back to the original state
later via the advance_to method. This is useful
for speculative parsing.
It’s important to note that ParserState can only move forward and not
backward. When joining a fork back to the original state, it must be
ensured that the fork is at a position beyond or equal to the original
state.
fn parse_option(
state: &mut ParserState<CharLexer>,
parser: impl Fn(&mut ParserState<CharLexer>) -> Result<char, Error>
) -> Result<Option<char>, Error> {
let fork = &mut state.fork();
match parser(fork) {
Ok(c) => {
state.advance_to(fork);
Ok(Some(c))
}
Err(_) => Ok(None),
}
}
let mut state = ParserState::new("aaa");
assert_eq!(parse_option(&mut state, |state| state.parse_char('a')).unwrap(), Some('a'));
assert_eq!(parse_option(&mut state, |state| state.parse_char('b')).unwrap(), None);Implementations§
Source§impl<'a, L: LexIt + Clone> ParserState<'a, L>
impl<'a, L: LexIt + Clone> ParserState<'a, L>
Sourcepub fn parse_with<T>(
&mut self,
matches: impl FnOnce(L::Token<'a>) -> Option<T>,
) -> Result<T, Error>
pub fn parse_with<T>( &mut self, matches: impl FnOnce(L::Token<'a>) -> Option<T>, ) -> Result<T, Error>
Consume the next token if it matches the given token.
Sourcepub fn parse_type<T>(&mut self) -> Result<T, Error>
pub fn parse_type<T>(&mut self) -> Result<T, Error>
Parse a token that can be converted to the given type.
Sourcepub fn parse_char(&mut self, c: char) -> Result<char, Error>
pub fn parse_char(&mut self, c: char) -> Result<char, Error>
Parse a token that exactly matches the given character.
Sourcepub fn parse_str(&mut self, literal: &'a str) -> Result<&str, Error>
pub fn parse_str(&mut self, literal: &'a str) -> Result<&str, Error>
Parse a token that exactly matches the given string.
Sourcepub fn advance_to(&mut self, other: &Self)
pub fn advance_to(&mut self, other: &Self)
Advance the state to the given state.
§Panics
Panics if the given state is before the current state.
Sourcepub fn advance_to_cursor(&mut self, cursor: Cursor)
pub fn advance_to_cursor(&mut self, cursor: Cursor)
Advance the state to the given position.
§Panics
Panics if the given position is before the current position.