Skip to main content

TokenStream

Struct TokenStream 

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

A peekable stream of tokens with span tracking.

Implementations§

Source§

impl TokenStream

Source

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

Create a stream from a token list.

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 without consuming.

Source

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

Consume and return the next token.

Source

pub fn eat(&mut self, expected: &TokenKind) -> Option<Token>

Consume the next token only if its kind matches expected.

Source

pub fn eat_while<F>(&mut self, pred: F) -> Vec<Token>
where F: FnMut(&Token) -> bool,

Consume tokens while the predicate holds.

Source

pub fn position(&self) -> usize

Return the current position in the stream (number consumed).

Source

pub fn is_empty(&self) -> bool

true if all tokens have been consumed.

Source

pub fn remaining(&self) -> usize

Remaining token count.

Source

pub fn rewind(&mut self, saved: usize)

Rewind to a previously saved position.

Source

pub fn save(&self) -> usize

Save the current position so it can be restored later.

Source

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

Consume the next token and return an error if the kind is wrong.

Source

pub fn collect_remaining(self) -> Vec<Token>

Collect all remaining tokens as a Vec.

Source§

impl TokenStream

Source

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

Peek at the n-th token from the current position (0 = next).

Source

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

Consume and discard tokens while pred holds.

Source

pub fn skip_to_inclusive(&mut self, kind: &TokenKind)

Consume tokens up to and including the next occurrence of kind.

Source

pub fn skip_to(&mut self, kind: &TokenKind)

Consume tokens up to (but not including) the next occurrence of kind.

Source

pub fn peek_slice(&self, n: usize) -> &[Token]

Return a slice of the next n tokens without consuming them.

Source

pub fn matches_sequence(&self, kinds: &[&TokenKind]) -> bool

Check if the next n tokens match the given kind sequence.

Source

pub fn consume_n(&mut self, n: usize) -> Vec<Token>

Consume exactly n tokens and return them.

Source

pub fn peek_all(&self) -> &[Token]

Peek at all remaining tokens.

Source

pub fn inject(&mut self, tokens: Vec<Token>)

Insert tokens at the current position (for synthetic token injection).

Source

pub fn len(&self) -> usize

Return the total token count (including consumed).

Trait Implementations§

Source§

impl Clone for TokenStream

Source§

fn clone(&self) -> TokenStream

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for TokenStream

Source§

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

Formats the value using the given formatter. Read more

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.