pub trait TokenReader<T: TokenTrait, TData> {
    // Required methods
    fn peek(&mut self) -> Option<&Token<T, TData>>;
    fn peek_n(&mut self, n: usize) -> Option<&Token<T, TData>>;
    fn peek_mut(&mut self) -> Option<&mut Token<T, TData>>;
    fn next(&mut self) -> Option<Token<T, TData>>;
    fn scan(
        &mut self,
        cb: impl FnMut(&T, &TData) -> bool
    ) -> Option<&Token<T, TData>>;

    // Provided methods
    fn conditional_next(
        &mut self,
        cb: impl FnOnce(&T) -> bool
    ) -> Option<Token<T, TData>> { ... }
    fn expect_next(
        &mut self,
        expected_type: T
    ) -> Result<TData, Option<(T, Token<T, TData>)>> { ... }
}
Expand description

A reader over a sequence of tokens

Required Methods§

source

fn peek(&mut self) -> Option<&Token<T, TData>>

Returns a reference to next token but does not advance current position

source

fn peek_n(&mut self, n: usize) -> Option<&Token<T, TData>>

Returns a reference to nth (zero based) upcoming token without advancing

source

fn peek_mut(&mut self) -> Option<&mut Token<T, TData>>

Use with caution

source

fn next(&mut self) -> Option<Token<T, TData>>

Returns the next token and advances

source

fn scan( &mut self, cb: impl FnMut(&T, &TData) -> bool ) -> Option<&Token<T, TData>>

Runs the closure (cb) over upcoming tokens. Passes the value behind the Token to the closure. Will stop and return a reference to the next Token from when the closure returns true. Returns None if scanning finishes before closure returns true. Does not advance the reader.

Used for lookahead and then branching based on return value during parsing

Provided Methods§

source

fn conditional_next( &mut self, cb: impl FnOnce(&T) -> bool ) -> Option<Token<T, TData>>

Returns next if cb returns true for the upcoming token (the token from TokenReader::peek)

source

fn expect_next( &mut self, expected_type: T ) -> Result<TData, Option<(T, Token<T, TData>)>>

Tests that next token matches an expected type. Will return error if does not match. The Ok value contains the data of the valid token. Else it will return the Err with the expected token type and the token that did not match

Is the token is skippable (using TokenTrait::is_skippable)

Implementors§

source§

impl<T, TData, TGeneratorState, TGenerator> TokenReader<T, TData> for GeneratorTokenQueue<T, TData, TGeneratorState, TGenerator>where T: TokenTrait, TData: Debug, for<'a> TGenerator: FnMut(&mut TGeneratorState, &mut GeneratorTokenQueueBuffer<'a, T, TData>),

source§

impl<T: TokenTrait, TData> TokenReader<T, TData> for BufferedTokenQueue<T, TData>