TokenStream

Struct TokenStream 

Source
pub struct TokenStream<'a> { /* private fields */ }
Expand description

Representation of the input as a sequence of tokens

The TokenStrem holds the reference of the input stream and is responsible for tokenizing it into a sequence of Tokens. TokenStream is immutable, all operations that extract tokens from the stream must return another instance of a TokenStream representing tokens remaining in the stream.

Opening and closing parenthesis have special meaning for the token streams. They are intended to enclose tokens related to the same value to avoid ambiguity when parsing nested structures with unknown number of required arguments, such as nested vectors. TokenStream aims to make handling parenthesis as transparent as possible:

  • attempt to take a token from a stream fails with an UnbalancedParenthesis error if the non-consumed portion of the input starts with an opening parenthesis;
  • the token stream is considered to be empty if the non-consumed portion of the input start with a closing parenthesis;
  • to handle nested structures, use either with_nested or complete_nested. These methods take a closure that returns a result and a TokenStream representing the remainder of the stream that wasn’t consumed during parsing. Token stream checks that all tokens consumed by the inner closure have been processed and return an appropriate error otherwise.

Implementations§

Source§

impl<'a> TokenStream<'a>

Source

pub fn new(input: &'a str) -> Self

Creates a new TokenStream from an input string slice.

Source

pub fn is_empty(&self) -> bool

Returns true if the last token of the stream has been consumed.

More specifically, it returns true if the non-consumed portion of the input string is empty or contains an arbitrary number of whitespace characters followed by an octothorp (#), a closing parenthesis, or the end of a string.

Source

pub fn is_all_consumed(&self) -> bool

Returns true if the entirety of the input string was consumed and the last token is not followed by any whitespace characters or a comment.

This is useful when performing completion: suggestions should only be generated for the last token of the stream if it isn’t followed by any other character.

If is_all_consumed returns true, then is_empty must return true, while reverse is not true.

Source

pub fn peek(&self) -> Option<Result<Token<'a>, UnbalancedParenthesis>>

Returns the next token in the token stream, if any. This is an efficient operation that does not cause any lexing operations and should be preferred when the token will not be consumed.

It returns:

  • Some(Ok(token)) if the stream is not empty;
  • Some(Err(UnbalancedParenthesis)) if the remaining part of the string starts with a closing parenthesis;
  • None if the stream is empty.
Source

pub fn take( &self, ) -> Option<Result<(Token<'a>, TokenStream<'a>), UnbalancedParenthesis>>

Returns the next token in the stream and an instance of the TokenStream representing the remaining tokens. The behavior is similar to peek with the same returned value with addition of a TokenStream instance.

Source

pub fn with_nested<R, F: FnOnce(TokenStream<'a>) -> ParseResult<'a, R>>( &self, callback: F, ) -> ParseResult<'a, R>

Executes a closure on an inner portion of the token stream. The closure passed to this function returns either a parsed value along with the remaining TokenSream, or a parsing failure.

This method behaves differently depending on whether the non-consumed portion of the input string starts with an opening parenthesis. If not, the value returned by the closure as is.

Otherwise, this functions consumes the parenthesis, and calls the closure. with_nested then returns a parse failure if the closure call failed or if there are any not consumed tokens remaining. Note, that in case the closure fails due to an [UnrecognizedToken] it’s converted into an error.

Source

pub fn complete_nested<F: FnOnce(TokenStream<'a>) -> CompletionResult<'a>>( &self, callback: F, ) -> CompletionResult<'a>

Executes a closure on an inner portion of the token stream. If the non-consumed portion of the input stream starts with an opening parenthesis, this function attempts to skip all tokens until the corresponding closing parenthesis. If no such parenthesis is found or if there is no opening parenthesis, the closure is called and its execution result is returned.

Trait Implementations§

Source§

impl<'a> Clone for TokenStream<'a>

Source§

fn clone(&self) -> TokenStream<'a>

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<'a> Debug for TokenStream<'a>

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<'a> Copy for TokenStream<'a>

Auto Trait Implementations§

§

impl<'a> Freeze for TokenStream<'a>

§

impl<'a> RefUnwindSafe for TokenStream<'a>

§

impl<'a> Send for TokenStream<'a>

§

impl<'a> Sync for TokenStream<'a>

§

impl<'a> Unpin for TokenStream<'a>

§

impl<'a> UnwindSafe for TokenStream<'a>

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> ParsableTransformation<Box<T>> for T

Source§

type Input = T

The type that will be transformed by the implementation if this trait (Self::Input -> O).
Source§

fn transform( input: <T as ParsableTransformation<Box<T>>>::Input, ) -> Result<Box<T>, ParseError<'static>>

Performs the transformation.
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.