Skip to main content

GraphQLTokenStream

Struct GraphQLTokenStream 

Source
pub struct GraphQLTokenStream<'src, TTokenSource: GraphQLTokenSource<'src>> { /* private fields */ }
Expand description

Streaming lexer that produces GraphQLTokens given some GraphQLTokenSource with a bounded lookahead buffer.

This structure accepts any GraphQLTokenSource and provides lookahead capabilities while maintaining efficient streaming behavior. It centralizes buffering, peeking, and lookahead logic.

Since trivia is already attached to tokens by the lexer, the parser can simply call peek() and consume() without worrying about trivia.

§Internal Buffer Management

Tokens are stored in a VecDeque ring buffer. Unconsumed tokens are buffered at the back; consume() pops from the front and returns the owned token via O(1) pop_front().

§Type Parameters

  • 'src - The lifetime of the source text that tokens are lexed from.
  • TTokenSource - The underlying token source, which must implement GraphQLTokenSource (i.e., Iterator<Item = GraphQLToken>).

§Future TODOs

  • Consider adding a GraphQLTokenStreamOptions struct to configure behavior:
    • include_trivia: bool - Whether to include preceding_trivia in tokens (can be disabled for performance when trivia is not needed)
    • max_tokens: Option<usize> - Limit total tokens returned (DoS protection)

Implementations§

Source§

impl<'src, TTokenSource: GraphQLTokenSource<'src>> GraphQLTokenStream<'src, TTokenSource>

Source

pub fn consume(&mut self) -> Option<GraphQLToken<'src>>

Advance to the next token and return it as an owned value.

Returns None if the stream is exhausted.

Source

pub fn current_buffer_len(&self) -> usize

Returns the number of GraphQLTokens currently buffered (unconsumed).

Source

pub fn is_at_end(&mut self) -> bool

Check if we’ve reached the end of the stream.

Returns true if there are no more tokens to consume, or if the next token is Eof.

Source

pub fn new(token_source: TTokenSource) -> Self

Creates a new token stream from a token source.

Source

pub fn peek(&mut self) -> Option<&GraphQLToken<'src>>

Peek at the next token without consuming it.

Returns the front of the buffer (filling it first if empty). Returns None if the stream is exhausted.

Source

pub fn peek_nth(&mut self, n: usize) -> Option<&GraphQLToken<'src>>

Peek at the nth token ahead (0-indexed from next unconsumed token).

peek_nth(0) is equivalent to peek().

Fills the buffer up to n+1 elements if needed. Returns None if the stream ends before reaching position n.

Auto Trait Implementations§

§

impl<'src, TTokenSource> Freeze for GraphQLTokenStream<'src, TTokenSource>
where TTokenSource: Freeze,

§

impl<'src, TTokenSource> RefUnwindSafe for GraphQLTokenStream<'src, TTokenSource>
where TTokenSource: RefUnwindSafe,

§

impl<'src, TTokenSource> Send for GraphQLTokenStream<'src, TTokenSource>
where TTokenSource: Send,

§

impl<'src, TTokenSource> Sync for GraphQLTokenStream<'src, TTokenSource>
where TTokenSource: Sync,

§

impl<'src, TTokenSource> Unpin for GraphQLTokenStream<'src, TTokenSource>
where TTokenSource: Unpin,

§

impl<'src, TTokenSource> UnwindSafe for GraphQLTokenStream<'src, TTokenSource>
where TTokenSource: UnwindSafe,

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.