pub struct Parser { /* private fields */ }
Expand description
The main parser struct, which holds the main logic for the parser.
Implementations§
Source§impl Parser
impl Parser
Sourcepub fn new(stream: &str) -> Result<Self, ParseErr>
pub fn new(stream: &str) -> Result<Self, ParseErr>
Creates a new parser from a given string.
In the instantiation process, this function will attempt to tokenize the string into tokens, raising an error if it fails.
Sourcepub fn cursor(&self) -> Span
pub fn cursor(&self) -> Span
Gets the range of the next token to read (or an EOL range if there are no more tokens to read).
Sourcepub fn parse<P: Parse>(&mut self) -> Result<P, ParseErr>
pub fn parse<P: Parse>(&mut self) -> Result<P, ParseErr>
Parses the current token stream into a component, erroring if not possible.
If parsing fails, there are no guarantees about what happens to the input, and the parser likely should not be used after an error is raised during parsing.
Sourcepub fn match_<P: TokenParse>(&mut self) -> Result<Option<P>, ParseErr>
pub fn match_<P: TokenParse>(&mut self) -> Result<Option<P>, ParseErr>
Check if the next token matches the given component and consume it if so.
This function can error if the next token does match the given component, but an error occurs in trying to convert it to that component.
Sourcepub fn advance_if<T>(
&mut self,
pred: impl FnOnce(Option<&Token>, Span) -> Result<T, ParseErr>,
) -> Result<T, ParseErr>
pub fn advance_if<T>( &mut self, pred: impl FnOnce(Option<&Token>, Span) -> Result<T, ParseErr>, ) -> Result<T, ParseErr>
Applies the provided predicate to the next token in the input.
If an error is raised from the predicate, the parser does not advance its input.