Trait texlang::vm::TokenStream

source ·
pub trait TokenStream {
    type S: TexlangState;

    // Required methods
    fn next_or(&mut self) -> Result<Option<Token>>;
    fn back(&mut self, token: Token);
    fn vm(&self) -> &VM<Self::S>;

    // Provided methods
    fn next<E: EndOfInputError>(&mut self, err: E) -> Result<Token> { ... }
    fn peek(&mut self) -> Result<Option<Token>> { ... }
    fn commands_map(&self) -> &Map<Self::S> { ... }
    fn state(&self) -> &Self::S { ... }
}
Expand description

A stream of tokens generated on demand.

This trait describes a general stream of tokens where the front of the stream may retrieved using TokenStream::next or peeked at using TokenStream::peek. In practice, all TokenStreams in Texlang are either ExecutionInput, ExpansionInput or UnexpandedStream. This trait exists to allow a generic function to accept any of these types.

§Note on lazy loading

The simplest example of a stream is a vector of tokens. However, streams are more general than this and can encompass situations in which the full contents cannot be determined in advance. This can be thought of as “lazy loading” for the tokens. The classic example of this kind of stream comes from the following LaTeX snippet:

\makeatletter \do@

Assuming the default TeX catcode map, if we were to parse this input all at once we would get three tokens: the control sequence makeatletter, the control sequence do, and a single character token with value @ and catcode “other”. This is not the correct result, though: the first control sequence changes the tokenization rules such that @ is now an admissible character in the name of a control sequence. The correct input is thus the control sequence makeatletter followed by the control sequence do@.

Required Associated Types§

source

type S: TexlangState

The type of the custom state in the VM.

Required Methods§

source

fn next_or(&mut self) -> Result<Option<Token>>

Gets the next token in the stream or Ok(None) if the stream is exhausted.

source

fn back(&mut self, token: Token)

Returns a token to the front of the token stream.

source

fn vm(&self) -> &VM<Self::S>

Returns a reference to the VM.

Provided Methods§

source

fn next<E: EndOfInputError>(&mut self, err: E) -> Result<Token>

Gets the next token in the stream or error if the stream is exhausted.

This method is almost the same as the next method in Rust’s iterator trait, except a stream can return an error.

source

fn peek(&mut self) -> Result<Option<Token>>

source

fn commands_map(&self) -> &Map<Self::S>

Returns a reference to the commands map.

source

fn state(&self) -> &Self::S

Returns a reference to the custom state.

Object Safety§

This trait is not object safe.

Implementors§