Skip to main content

TokenStream

Struct TokenStream 

Source
pub struct TokenStream { /* private fields */ }
Expand description

An iterator over a sequence of tokens.

Supports peeking, consuming, and backtracking (via snapshots).

Implementations§

Source§

impl TokenStream

Source

pub fn new(tokens: Vec<Token>) -> Self

Create a new token stream from a vector of tokens.

Source

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

Peek at the current token without consuming it.

Source

pub fn peek_ahead(&self, n: usize) -> Option<&Token>

Peek at the token n positions ahead (0 = current).

Source

pub fn current_kind(&self) -> Option<&TokenKind>

Get the current token kind without consuming.

Source

pub fn at_keyword(&self, kw: &TokenKind) -> bool

Check if the current token matches a given kind (by tag, ignoring data).

Source

pub fn at_ident(&self, name: &str) -> bool

Check if the current token is an identifier with a specific name.

Source

pub fn is_eof(&self) -> bool

Check if we are at the end of the stream.

Source

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

Consume and return the current token.

Source

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

Consume the current token if it matches the given kind.

Returns true if consumed, false otherwise.

Source

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

Expect a specific token kind; return an error string if it doesn’t match.

Source

pub fn expect_ident(&mut self) -> Result<String, String>

Expect an identifier and return its name.

Source

pub fn snapshot(&self) -> usize

Take a snapshot of the current position (for backtracking).

Source

pub fn restore(&mut self, snap: usize)

Restore to a previous snapshot position.

Source

pub fn remaining(&self) -> usize

Get the remaining token count.

Source

pub fn current_span(&self) -> Span

Get the current span (or a dummy span if at EOF).

Source

pub fn skip_until(&mut self, kinds: &[TokenKind])

Skip tokens until we hit one of the given token kinds (for error recovery).

Source

pub fn drain_remaining(&mut self) -> Vec<Token>

Collect all tokens from the current position to the end.

Source

pub fn total_len(&self) -> usize

Get the total number of tokens (including EOF).

Auto Trait Implementations§

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.