Skip to main content

TokenCursor

Struct TokenCursor 

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

A cursor for reading tokens with position tracking and backtracking.

Implementations§

Source§

impl<'a> TokenCursor<'a>

Source

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

Create a cursor over a slice of tokens.

Source

pub fn peek(&self) -> Option<&'a Token>

Peek at the current token.

Source

pub fn peek_n(&self, n: usize) -> Option<&'a Token>

Peek n positions ahead.

Source

pub fn advance(&mut self) -> Option<&'a Token>

Consume the current token.

Source

pub fn position(&self) -> usize

Current position.

Source

pub fn remaining(&self) -> usize

Remaining tokens.

Source

pub fn is_eof(&self) -> bool

Whether we are at end of input.

Source

pub fn save(&mut self)

Save current position as a checkpoint.

Source

pub fn restore(&mut self)

Restore the most recent checkpoint.

Source

pub fn commit(&mut self)

Discard the most recent checkpoint (commit the advance).

Source

pub fn try_consume(&mut self, kind: &TokenKind) -> bool

Consume if the current token kind matches.

Source

pub fn expect_kind(&mut self, kind: &TokenKind) -> Result<&'a Token, String>

Expect the current kind matches, else return an error string.

Source

pub fn remaining_slice(&self) -> &'a [Token]

Get a slice of the remaining tokens.

Source

pub fn collect_until<F: Fn(&Token) -> bool>( &mut self, pred: F, ) -> Vec<&'a Token>

Collect tokens until pred returns true.

Source

pub fn skip_while<F: Fn(&Token) -> bool>(&mut self, pred: F)

Skip tokens as long as pred returns true.

Auto Trait Implementations§

§

impl<'a> Freeze for TokenCursor<'a>

§

impl<'a> RefUnwindSafe for TokenCursor<'a>

§

impl<'a> Send for TokenCursor<'a>

§

impl<'a> Sync for TokenCursor<'a>

§

impl<'a> Unpin for TokenCursor<'a>

§

impl<'a> UnsafeUnpin for TokenCursor<'a>

§

impl<'a> UnwindSafe for TokenCursor<'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.