Struct fe_parser::Parser

source ·
pub struct Parser<'a> {
    pub file_id: SourceFileId,
    pub diagnostics: Vec<Diagnostic>,
    /* private fields */
}
Expand description

Parser maintains the parsing state, such as the token stream, “enclosure” (paren, brace, ..) stack, diagnostics, etc. Syntax parsing logic is in the crate::grammar module.

See [BTParser] if you need backtrackable parser.

Fields§

§file_id: SourceFileId§diagnostics: Vec<Diagnostic>

The diagnostics (errors and warnings) emitted during parsing.

Implementations§

source§

impl<'a> Parser<'a>

source

pub fn new(file_id: SourceFileId, content: &'a str) -> Self

Create a new parser for a source code string and associated file id.

source

pub fn as_bt_parser<'b>(&'b mut self) -> BTParser<'a, 'b>

Returns back tracking parser.

source

pub fn next(&mut self) -> ParseResult<Token<'a>>

Return the next token, or an error if we’ve reached the end of the file.

source

pub fn peek_or_err(&mut self) -> ParseResult<TokenKind>

Take a peek at the next token kind without consuming it, or return an error if we’ve reached the end of the file.

source

pub fn peek(&mut self) -> Option<TokenKind>

Take a peek at the next token kind. Returns None if we’ve reached the end of the file.

source

pub fn split_next(&mut self) -> ParseResult<Token<'a>>

Split the next token into two tokens, returning the first. Only supports splitting the >> token into two > tokens, specifically for parsing the closing angle bracket of a generic type argument list (Map<x, Map<y, z>>).

Panics

Panics if the next token isn’t >>

source

pub fn done(&mut self) -> bool

Returns true if the parser has reached the end of the file.

source

pub fn eat_newlines(&mut self)

source

pub fn assert(&mut self, tk: TokenKind) -> Token<'a>

Assert that the next token kind it matches the expected token kind, and return it. This should be used in cases where the next token kind is expected to have been checked already.

Panics

Panics if the next token kind isn’t tk.

source

pub fn expect<S: Into<String>>( &mut self, expected: TokenKind, message: S ) -> ParseResult<Token<'a>>

If the next token matches the expected kind, return it. Otherwise emit an error diagnostic with the given message and return an error.

source

pub fn expect_with_notes<Str, NotesFn>( &mut self, expected: TokenKind, message: Str, notes_fn: NotesFn ) -> ParseResult<Token<'a>>where Str: Into<String>, NotesFn: FnOnce(&Token<'_>) -> Vec<String>,

Like Parser::expect, but with additional notes to be appended to the bottom of the diagnostic message. The notes are provided by a function that returns a Vec<String>, to avoid allocations in the case where the token is as expected.

source

pub fn optional(&mut self, kind: TokenKind) -> Option<Token<'a>>

If the next token matches the expected kind, return it. Otherwise return None.

source

pub fn unexpected_token_error<S: Into<String>>( &mut self, tok: &Token<'_>, message: S, notes: Vec<String> )

Emit an “unexpected token” error diagnostic with the given message.

source

pub fn enter_block( &mut self, context_span: Span, context_name: &str ) -> ParseResult<()>

Enter a “block”, which is a brace-enclosed list of statements, separated by newlines and/or semicolons. This checks for and consumes the { that precedes the block.

source

pub fn expect_stmt_end(&mut self, context_name: &str) -> ParseResult<()>

Consumes newlines and semicolons. Returns Ok if one or more newlines or semicolons are consumed, or if the next token is a }.

source

pub fn error<S: Into<String>>(&mut self, span: Span, message: S)

Emit an error diagnostic, but don’t stop parsing

source

pub fn fancy_error<S: Into<String>>( &mut self, message: S, labels: Vec<Label>, notes: Vec<String> )

Emit a “fancy” error diagnostic with any number of labels and notes, but don’t stop parsing.

Auto Trait Implementations§

§

impl<'a> RefUnwindSafe for Parser<'a>

§

impl<'a> Send for Parser<'a>

§

impl<'a> Sync for Parser<'a>

§

impl<'a> Unpin for Parser<'a>

§

impl<'a> UnwindSafe for Parser<'a>

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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.

source§

impl<T, U> Into<U> for Twhere 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, U> TryFrom<U> for Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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.