Skip to main content

Parser

Struct Parser 

Source
pub struct Parser<'a> { /* private fields */ }
Expand description

A cursor over tokens. Shared by the expression parser (this task) and the section parser (Task 4).

Implementations§

Source§

impl<'a> Parser<'a>

Source

pub fn new(toks: &'a [Token]) -> Self

A parser positioned at the first token.

§Panics

If toks is empty; lex always appends Tok::Eof, so this cannot happen for tokens it produced.

Source

pub fn peek(&self) -> &Tok

The current token without consuming it.

Source

pub fn peek2(&self) -> &Tok

The token after the current one.

Source

pub fn peek_at(&self, n: usize) -> &Tok

The token n positions ahead of the cursor; peek_at(0) is Parser::peek. Clamped to the trailing Eof, like peek and peek2.

Source

pub fn span(&self) -> Span

The current token’s span.

Source

pub fn prev_span(&self) -> Span

Span of the token just consumed, or of the current one at the start of input.

Expr::span() cannot serve here: a parenthesised expression carries the span of its contents, so !(a | b) ends before the closing parens. This gives the real end of whatever was last read, which is what quoting a construct back to the author needs.

Source

pub fn bump(&mut self) -> Token

Consumes and returns the current token.

Source

pub fn eat(&mut self, want: &Tok) -> bool

Consumes the current token if it matches, reporting whether it did.

Source

pub fn expect( &mut self, want: &Tok, what: &str, diags: &mut Diagnostics, ) -> bool

Consumes the current token if it matches, otherwise records a diagnostic.

Source

pub fn at_eof(&self) -> bool

Whether input is exhausted.

Source

pub fn parse_expr(&mut self, diags: &mut Diagnostics) -> Expr

Parses a formula.

Auto Trait Implementations§

§

impl<'a> Freeze for Parser<'a>

§

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> UnsafeUnpin for Parser<'a>

§

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

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.

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, 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.