pub(crate) struct Parser<'token, 'db> {
pub(crate) db: &'db dyn Db,
pub(crate) tokens: &'token [Token<'token, 'db>],
pub(crate) next_token: usize,
pub(crate) last_span: Span<'db>,
pub(crate) diagnostics: Vec<Diagnostic>,
}Fields§
§db: &'db dyn Db§tokens: &'token [Token<'token, 'db>]Input tokens
next_token: usizeNext token (if any) in the token list
last_span: Span<'db>Span of the last consumed token; starts as the span of the anchor
diagnostics: Vec<Diagnostic>Additional diagnostics that were reported by parsers. Used when we are able to partially parse something and recover. These need to be reported to the user eventually. They are stored in the parser to support speculative parsing.
Implementations§
Source§impl<'token, 'db> Parser<'token, 'db>
impl<'token, 'db> Parser<'token, 'db>
pub fn new( db: &'db dyn Db, anchor: Anchor<'db>, tokens: &'token [Token<'token, 'db>], ) -> Self
Sourcepub fn deferred<T>(
db: &'db dyn Db,
anchor: impl Into<Anchor<'db>>,
deferred_parse: &'db DeferredParse<'db>,
op: impl FnOnce(Parser<'_, 'db>) -> T,
) -> T
pub fn deferred<T>( db: &'db dyn Db, anchor: impl Into<Anchor<'db>>, deferred_parse: &'db DeferredParse<'db>, op: impl FnOnce(Parser<'_, 'db>) -> T, ) -> T
Parse the contents of deferred_parse using op in the context of the given anchor.
See [DeferredParse][] for an explanation of when/why we use deferred parsing.
Sourcepub fn parse_many_and_report_diagnostics<T>(
self,
db: &'db dyn Db,
) -> SpanVec<'db, T::Output>where
T: Parse<'db>,
pub fn parse_many_and_report_diagnostics<T>(
self,
db: &'db dyn Db,
) -> SpanVec<'db, T::Output>where
T: Parse<'db>,
Top-level parsing function: parses zero or more instances of T and reports any errors.
Sourcepub fn push_diagnostic(&mut self, diagnostic: Diagnostic)
pub fn push_diagnostic(&mut self, diagnostic: Diagnostic)
Record a diagnostic, indicating that parsing recovered from an error.
Sourcepub fn take_diagnostics(&mut self, parser: Parser<'_, 'db>)
pub fn take_diagnostics(&mut self, parser: Parser<'_, 'db>)
Take all diagnostics from another parser (e.g., one parsing a delimited set of tokens).
Sourcepub fn into_diagnostics(self) -> Vec<Diagnostic>
pub fn into_diagnostics(self) -> Vec<Diagnostic>
Complete parsing and convert the parser into the resulting diagnostics (errors).
Reports an error if there are any unconsumed tokens.
Sourcepub(crate) fn fork(&self) -> Self
pub(crate) fn fork(&self) -> Self
Forks this parser into a split parser at the same point with a fresh set of diagnostics. Used for speculation.
Sourcepub(crate) fn eat_errors(&mut self)
pub(crate) fn eat_errors(&mut self)
Eats any pending error tokens and adds them to the diagnostic list.
Does not adjust last_span.
This implements eager error consumption - error tokens from the tokenizer
are immediately converted to diagnostics rather than disrupting normal parsing.
Called automatically after each eat_next_token() to maintain clean token streams.
This pattern allows parsing to continue after tokenizer errors, enabling better error recovery and multiple error reporting in a single pass.
Sourcepub fn eat_next_token(&mut self) -> Result<(), ParseFail<'db>>
pub fn eat_next_token(&mut self) -> Result<(), ParseFail<'db>>
Advance by one token, returning Err if there is no current token.
After advancing, also eagerly eats any error tokens.
Sourcepub fn peek(&mut self) -> Option<&Token<'token, 'db>>
pub fn peek(&mut self) -> Option<&Token<'token, 'db>>
Peek at the next token, returning None if there is none. Implicitly advances past error tokens. Does not consume the token returned.
Sourcepub fn peek_span(&mut self) -> Span<'db>
pub fn peek_span(&mut self) -> Span<'db>
Span of the next token in the input (or the end of the final token, if there are no more tokens)
Sourcepub fn illformed(&mut self, expected: Expected) -> ParseFail<'db>
pub fn illformed(&mut self, expected: Expected) -> ParseFail<'db>
Create a parse error because the next token is not what we expected.
pub fn eat_keyword(&mut self, kw: Keyword) -> Result<Span<'db>, ParseFail<'db>>
pub fn eat_id(&mut self) -> Result<SpannedIdentifier<'db>, ParseFail<'db>>
pub fn eat_op(&mut self, op: Op) -> Result<Span<'db>, ParseFail<'db>>
Sourcepub fn defer_delimited(
&mut self,
delimiter: Delimiter,
) -> Result<DeferredParse<'db>, ParseFail<'db>>
pub fn defer_delimited( &mut self, delimiter: Delimiter, ) -> Result<DeferredParse<'db>, ParseFail<'db>>
Returns a deferred parse of the next delimited token.
If this returns Err, then nothing has been consumed.
Sourcepub fn eat_delimited(
&mut self,
delimiter: Delimiter,
) -> Result<&'token str, ParseFail<'db>>
pub fn eat_delimited( &mut self, delimiter: Delimiter, ) -> Result<&'token str, ParseFail<'db>>
Eats the next token if it is a delimited token with the given delimiter;
returns a &str slice of the token’s contents.
Sourcepub(crate) fn next_token_on_same_line(&mut self) -> bool
pub(crate) fn next_token_on_same_line(&mut self) -> bool
Returns true if the next token is on the same line as the most recently consumed token. Some parts of our grammar are newline sensitive.
Auto Trait Implementations§
impl<'token, 'db> Freeze for Parser<'token, 'db>
impl<'token, 'db> !RefUnwindSafe for Parser<'token, 'db>
impl<'token, 'db> !Send for Parser<'token, 'db>
impl<'token, 'db> !Sync for Parser<'token, 'db>
impl<'token, 'db> Unpin for Parser<'token, 'db>
impl<'token, 'db> UnsafeUnpin for Parser<'token, 'db>
impl<'token, 'db> !UnwindSafe for Parser<'token, 'db>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more