Skip to main content

Parser

Struct Parser 

Source
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: usize

Next 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>

Source

pub fn new( db: &'db dyn Db, anchor: Anchor<'db>, tokens: &'token [Token<'token, 'db>], ) -> Self

Source

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.

Source

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.

Source

pub fn push_diagnostic(&mut self, diagnostic: Diagnostic)

Record a diagnostic, indicating that parsing recovered from an error.

Source

pub fn take_diagnostics(&mut self, parser: Parser<'_, 'db>)

Take all diagnostics from another parser (e.g., one parsing a delimited set of tokens).

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn last_span(&self) -> Span<'db>

Span of the last consumed token.

Source

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)

Source

pub fn illformed(&mut self, expected: Expected) -> ParseFail<'db>

Create a parse error because the next token is not what we expected.

Source

pub fn eat_keyword(&mut self, kw: Keyword) -> Result<Span<'db>, ParseFail<'db>>

Source

pub fn eat_id(&mut self) -> Result<SpannedIdentifier<'db>, ParseFail<'db>>

Source

pub fn eat_op(&mut self, op: Op) -> Result<Span<'db>, ParseFail<'db>>

Source

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.

Source

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.

Source

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
§

impl<T> Lookup<T> for T

§

fn into_owned(self) -> T

§

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

impl<T> ErasedDestructor for T
where T: 'static,

§

impl<T> MaybeSendSync for T