Skip to main content

Parser

Struct Parser 

Source
pub struct Parser<'t> {
    pub lexer: Lexer<'t>,
    pub next_tok: Option<PResult<Token>>,
    pub last_loc: Span,
    pub elide_do: bool,
}
Expand description

Handles stateful extraction from a Lexer, with single-Token lookahead.

Fields§

§lexer: Lexer<'t>§next_tok: Option<PResult<Token>>§last_loc: Span§elide_do: bool

Implementations§

Source§

impl<'t> Parser<'t>

Source

pub fn new(lexer: Lexer<'t>) -> Self

Constructs a new Parser

Source

pub const fn then<T>(&self, t: T) -> T

The identity function. This exists to make production chaining easier.

Source

pub const fn span(&self) -> Span

Gets the Span of the last-consumed Token

Source

pub fn parse<T: Parse<'t>>(&mut self, level: T::Prec) -> PResult<T>

Parses a value that implements the Parse trait.

Source

pub fn parse_entire<T: Parse<'t>>(&mut self, level: T::Prec) -> PResult<T>

Parses a value that implements the Parse trait, and asserts the entire input has been consumed.

Source

pub fn peek(&mut self) -> PResult<&Token>

Peeks the next Token. Returns ParseError::FromLexer on lexer error.

Source

pub fn peek_if(&mut self, expected: TKind) -> PResult<Option<&Token>>

Peeks the next token if it matches the expected TKind

Source

pub fn take(&mut self) -> PResult<Token>

Consumes and returns the currently-peeked Token.

Source

pub fn take_lexeme(&mut self) -> PResult<Lexeme>

Consumes the currently-peeked Token, returning its lexeme without cloning.

Source

pub fn next(&mut self) -> PResult<Token>

Source

pub fn next_if(&mut self, expected: TKind) -> PResult<Result<Token, TKind>>

Consumes and returns the next Token if it matches the expected TKind

Source

pub fn list<P: Parse<'t>>( &mut self, elems: Vec<P>, level: P::Prec, sep: TKind, end: TKind, ) -> PResult<Vec<P>>

Parses a list of P separated by sep tokens, ending in an end token.

List<T> = (T sep)* T? end ;
Source

pub fn list_bare<P: Parse<'t>>( &mut self, elems: Vec<P>, level: P::Prec, sep: TKind, ) -> PResult<Vec<P>>

Parses a list of one or more P at level level, separated by sep tokens

UnterminatedList<P> = P (sep P)*
Source

pub fn opt_if<P: Parse<'t>>( &mut self, level: P::Prec, next: TKind, ) -> PResult<Option<P>>

Parses into an Option<P> if the next token is next

Source

pub fn opt<P: Parse<'t>>( &mut self, level: P::Prec, end: TKind, ) -> PResult<Option<P>>

Parses a P unless the next Token’s TKind is end

Source

pub fn expect(&mut self, next: TKind) -> PResult<&mut Self>

Ensures the next Token’s TKind is next

Source

pub fn consume(&mut self) -> &mut Self

Consumes the currently peeked token without returning it.

Source

pub fn split(&mut self) -> PResult<Token>

Consumes the next token, and attempts to split it into multiple.

If the next token cannot be split, it will be returned.

Trait Implementations§

Source§

impl<'t> Debug for Parser<'t>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'t> Freeze for Parser<'t>

§

impl<'t> RefUnwindSafe for Parser<'t>

§

impl<'t> Send for Parser<'t>

§

impl<'t> Sync for Parser<'t>

§

impl<'t> Unpin for Parser<'t>

§

impl<'t> UnwindSafe for Parser<'t>

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.