TokenStream

Trait TokenStream 

Source
pub trait TokenStream: Sized {
    type Token: Clone;
    type Span: SpanLike;
    type Spanned<T: Clone>: SpannedLike<T, Span = Self::Span>;

Show 17 methods // Required methods fn peek_token_raw(&self) -> Option<&Self::Spanned<Self::Token>>; fn next_raw(&mut self) -> Option<Self::Spanned<Self::Token>>; fn cursor(&self) -> usize; fn rewind(&mut self, pos: usize); fn fork(&self) -> Self; fn cursor_span(&self) -> Option<Self::Span>; fn last_span(&self) -> Option<Self::Span>; fn span_at(&self, pos: usize) -> Option<Self::Span>; // Provided methods fn peek_token(&self) -> Option<&Self::Spanned<Self::Token>> { ... } fn next(&mut self) -> Option<Self::Spanned<Self::Token>> { ... } fn peek<T>(&self) -> bool where T: Peek<Token = Self::Token> { ... } fn parse<T>(&mut self) -> Result<T, <T as Parse>::Error> where T: Parse<Token = Self::Token> { ... } fn parse_spanned<T>( &mut self, ) -> Result<Self::Spanned<T>, <T as Parse>::Error> where T: Parse<Token = Self::Token> + Clone { ... } fn is_empty(&self) -> bool { ... } fn remaining(&self) -> usize { ... } fn ensure_consumed(&self) -> Result<(), Error> { ... } fn span_range(&self, range: Range<usize>) -> Self::Span { ... }
}
Expand description

A stream of tokens for parsing.

Provides the core interface for lexer output consumption. Token streams support peeking, consumption, forking for lookahead, and rewinding.

Required Associated Types§

Source

type Token: Clone

The token type produced by the lexer.

Source

type Span: SpanLike

The span type for tracking source locations.

Source

type Spanned<T: Clone>: SpannedLike<T, Span = Self::Span>

A spanned wrapper type for associating values with spans.

Required Methods§

Source

fn peek_token_raw(&self) -> Option<&Self::Spanned<Self::Token>>

Peeks at the next token without consuming (includes whitespace).

Source

fn next_raw(&mut self) -> Option<Self::Spanned<Self::Token>>

Consumes and returns the next token (includes whitespace).

Source

fn cursor(&self) -> usize

Returns the current cursor position.

Source

fn rewind(&mut self, pos: usize)

Rewinds to a previous cursor position.

Source

fn fork(&self) -> Self

Creates a fork for lookahead without consuming tokens.

Source

fn cursor_span(&self) -> Option<Self::Span>

Returns the span at the current cursor position.

Source

fn last_span(&self) -> Option<Self::Span>

Returns the span of the last consumed token.

Source

fn span_at(&self, pos: usize) -> Option<Self::Span>

Get the span of a token at a specific cursor position.

Returns None if the position is out of bounds.

Provided Methods§

Source

fn peek_token(&self) -> Option<&Self::Spanned<Self::Token>>

Peeks at the next significant token (skips whitespace by default).

Source

fn next(&mut self) -> Option<Self::Spanned<Self::Token>>

Consumes and returns the next significant token.

Source

fn peek<T>(&self) -> bool
where T: Peek<Token = Self::Token>,

Checks if the next token matches type T without consuming.

Source

fn parse<T>(&mut self) -> Result<T, <T as Parse>::Error>
where T: Parse<Token = Self::Token>,

Parses a value of type T from the stream.

Source

fn parse_spanned<T>(&mut self) -> Result<Self::Spanned<T>, <T as Parse>::Error>
where T: Parse<Token = Self::Token> + Clone,

Parses a value and wraps it with its source span.

Source

fn is_empty(&self) -> bool

Source

fn remaining(&self) -> usize

Returns the number of remaining significant tokens (excluding whitespace).

The default implementation counts tokens via peek_token() and next(), which may be inefficient. Implementations may override this for better performance.

Source

fn ensure_consumed(&self) -> Result<(), Error>

Ensures the stream has been fully consumed.

Returns Ok(()) if no significant tokens remain (whitespace is ignored). Returns Err(Error::StreamNotConsumed) if tokens remain.

§Example
let doc: Document = stream.parse()?;
stream.ensure_consumed()?; // Error if trailing garbage
Source

fn span_range(&self, range: Range<usize>) -> Self::Span

Create a span covering a range of cursor positions.

This is useful for tracking the span of a parsed AST node that spans multiple tokens.

§Example
let start = stream.cursor();
// ... parse multiple tokens ...
let end = stream.cursor();
let span = stream.span_range(start..end);

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§