Skip to main content

TokenStream

Struct TokenStream 

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

A generic TokenStream struct that manages a stream of tokens with cursor and bookmark functionality.

§Examples

use slk_tokenstream::TokenStream;
use slk_tokenstream::Mark;
 
let tokens = &[1, 2, 3];
let mut token_stream = TokenStream::new(tokens);

assert_eq!(token_stream.consume(), Some(&1));
assert_eq!(token_stream.peek(), Some(&2));
assert_eq!(token_stream.tokens_remaining(), 2);

Implementations§

Source§

impl<'a, T> TokenStream<'a, T>

Source

pub fn new(data: &'a [T]) -> Self

Creates a new TokenStream from a vector of tokens. Sets cursor to 0.

§Examples
use slk_tokenstream::TokenStream;
 
let tokens = &[1, 2, 3];
let token_stream = TokenStream::new(tokens);
 
assert_eq!(token_stream.peek(), Some(&1));
assert_eq!(token_stream.peek_offset(1), Some(&2));
assert_eq!(token_stream.peek_offset(2), Some(&3));
Source

pub fn consume(&mut self) -> Option<&T>

Advances the cursor and returns the next token if available, otherwise returns None.

§Examples
use slk_tokenstream::TokenStream;
 
let tokens = &[1, 2, 3];
let mut token_stream = TokenStream::new(tokens);
 
assert_eq!(token_stream.consume(), Some(&1));
assert_eq!(token_stream.consume(), Some(&2));
assert_eq!(token_stream.consume(), Some(&3));
assert_eq!(token_stream.consume(), None);
Source

pub fn peek(&self) -> Option<&T>

Peeks at the token at the current cursor position without advancing the cursor.

§Examples
use slk_tokenstream::TokenStream;
 
let tokens = &[1, 2, 3];
let token_stream = TokenStream::new(tokens);
 
assert_eq!(token_stream.peek(), Some(&1));
Source

pub fn peek_offset(&self, offset: usize) -> Option<&T>

Peeks at the current cursor position plus an offset without advancing the cursor.

§Examples
use slk_tokenstream::TokenStream;
 
let tokens = &[1, 2, 3];
let token_stream = TokenStream::new(tokens);
 
assert_eq!(token_stream.peek(), Some(&1));
assert_eq!(token_stream.peek_offset(1), Some(&2));
assert_eq!(token_stream.peek_offset(2), Some(&3));
Source

pub fn rewind(&mut self)

Moves the cursor back by one position, saturating at zero.

§Examples
use slk_tokenstream::TokenStream;
 
let tokens = &[1, 2, 3];
let mut token_stream = TokenStream::new(tokens);
 
assert_eq!(token_stream.consume(), Some(&1));
token_stream.rewind();
assert_eq!(token_stream.consume(), Some(&1));
Source

pub fn rewind_offset(&mut self, offset: usize)

Rewinds the cursor a specified amount of times, saturating at 0.

§Examples
use slk_tokenstream::TokenStream;
 
let tokens = &[1, 2, 3];
let mut token_stream = TokenStream::new(tokens);
 
assert_eq!(token_stream.consume(), Some(&1));
assert_eq!(token_stream.consume(), Some(&2));
assert_eq!(token_stream.consume(), Some(&3));
assert_eq!(token_stream.consume(), None);
token_stream.rewind_offset(2);
assert_eq!(token_stream.consume(), Some(&2));
Source

pub fn mark(&self) -> Mark

Returns a mark to the current cursor position.

§Examples
use slk_tokenstream::TokenStream;
 
let tokens = &[1, 2, 3];
let mut token_stream = TokenStream::new(tokens);
let mark = token_stream.mark();
 
token_stream.advance(5);
assert_eq!(token_stream.peek(), None);
token_stream.reset(&mark);
assert_eq!(token_stream.peek(), Some(&1));
Source

pub fn reset(&mut self, bookmark: &Mark) -> usize

Moves the cursor to the position of a previously registered bookmark by handle and returns the previous position

§Examples
use slk_tokenstream::TokenStream;
 
let tokens = &[1, 2, 3];
let mut token_stream = TokenStream::new(tokens);
let mark = token_stream.mark();
 
token_stream.advance(3);
assert_eq!(token_stream.peek(), None);
assert_eq!(token_stream.reset(&mark), 3);
assert_eq!(token_stream.peek(), Some(&1));
Source

pub fn tokens_remaining(&self) -> usize

Returns the amount of tokens remaining, including the current token

§Examples
use slk_tokenstream::TokenStream;
 
let tokens = &[1, 2, 3];
let mut token_stream = TokenStream::new(tokens);
 
assert_eq!(token_stream.tokens_remaining(), 3);
assert_eq!(token_stream.consume(), Some(&1));
assert_eq!(token_stream.tokens_remaining(), 2);
assert_eq!(token_stream.consume(), Some(&2));
assert_eq!(token_stream.tokens_remaining(), 1);
Source

pub fn is_eof(&self) -> bool

Returns if the current token is the end of file

§Examples
use slk_tokenstream::TokenStream;
 
let tokens = &[1, 2, 3];
let mut token_stream = TokenStream::new(tokens);
 
assert!(!token_stream.is_eof());
assert_eq!(token_stream.consume(), Some(&1));
assert_eq!(token_stream.consume(), Some(&2));
assert_eq!(token_stream.consume(), Some(&3));
assert!(token_stream.is_eof());
Source

pub fn slice_from_marks(&self, mark_1: &Mark, mark_2: &Mark) -> &[T]

Returns a slice which starts on the earliest mark and ends on the latest.

§Examples
use slk_tokenstream::TokenStream;
 
let tokens = &[1, 2, 3];
let mut token_stream = TokenStream::new(tokens);
let mark_1 = token_stream.mark();
token_stream.advance(3);
let mark_2 = token_stream.mark();
 
assert_eq!(token_stream.slice_from_marks(&mark_1, &mark_2), &[1, 2, 3]);
assert_eq!(token_stream.slice_from_marks(&mark_2, &mark_1), &[1, 2, 3]);
Source

pub fn advance(&mut self, offset: usize)

Advances the cursor by specified amount

Cursor is clamped to the length of the data

§Examples
use slk_tokenstream::TokenStream;
 
let tokens = &[1, 2, 3];
let mut token_stream = TokenStream::new(tokens);
 
token_stream.advance(2);
assert_eq!(token_stream.peek(), Some(&3));
Source

pub fn peek_if<F: Fn(&T) -> bool>(&self, f: F) -> Option<&T>

Returns the next item if it exists and the closure returns true

§Examples
use slk_tokenstream::TokenStream;
 
let tokens = &[1, 2, 3];
let mut token_stream = TokenStream::new(tokens);
 
assert_eq!(token_stream.peek_if(|token| *token == 1), Some(&1));
assert_eq!(token_stream.peek_if(|token| *token == 2), None);
Source

pub fn expect<F: Fn(&T) -> bool>(&mut self, f: F) -> Option<&T>

Returns the next item and advances the cursor if the item exists and the closure returns true

§Examples
use slk_tokenstream::TokenStream;
 
let tokens = &[1, 2, 3];
let mut token_stream = TokenStream::new(tokens);
 
assert_eq!(token_stream.expect(|token| *token == 1), Some(&1));
assert_eq!(token_stream.expect(|token| *token == 2), Some(&2));
Source

pub fn consume_while<F: Fn(&T) -> bool>(&mut self, f: F) -> &[T]

Returns a slice of items starting from the cursor and ending when the closure returns false. The cursor remains on the first item failing the test

§Examples
use slk_tokenstream::TokenStream;
 
let tokens = &[1, 2, 3];
let mut token_stream = TokenStream::new(tokens);
 
assert_eq!(token_stream.consume_while(|token| *token < 3), &[1, 2]);
assert_eq!(token_stream.peek(), Some(&3));
Source

pub fn peek_while<F: Fn(&T) -> bool>(&self, f: F) -> &[T]

Returns a slice of items starting from the cursor and ending when the closure returns false. The cursor remains in the original position

§Examples
use slk_tokenstream::TokenStream;
 
let tokens = &[1, 2, 3];
let mut token_stream = TokenStream::new(tokens);
 
assert_eq!(token_stream.peek_while(|token| *token < 3), &[1, 2]);
assert_eq!(token_stream.peek(), Some(&1));
Source

pub fn skip(&mut self)

Advances the cursor 1 step

§Examples
use slk_tokenstream::TokenStream;
 
let tokens = &[1, 2, 3];
let mut token_stream = TokenStream::new(tokens);
 
token_stream.skip();
assert_eq!(token_stream.peek(), Some(&2));
token_stream.skip();
assert_eq!(token_stream.peek(), Some(&3));
Source

pub fn skip_if<F: Fn(&T) -> bool>(&mut self, f: F)

Advances the cursor one step if the closure returns true

§Examples
use slk_tokenstream::TokenStream;
 
let tokens = &[1, 2, 3];
let mut token_stream = TokenStream::new(tokens);
 
token_stream.skip_if(|token| *token == 1);
assert_eq!(token_stream.peek(), Some(&2));
token_stream.skip_if(|token| *token == 1);
assert_eq!(token_stream.peek(), Some(&2));
Source

pub fn skip_while<F: Fn(&T) -> bool>(&mut self, f: F)

Advances the cursor until the closure returns false

§Examples
use slk_tokenstream::TokenStream;
 
let tokens = &[1, 2, 3];
let mut token_stream = TokenStream::new(tokens);
 
token_stream.skip_while(|token| *token < 3);
assert_eq!(token_stream.peek(), Some(&3));
Source

pub fn position(&self) -> usize

Returns the current position of the cursor

§Examples
use slk_tokenstream::TokenStream;
let tokens = &[1, 2, 3];
let mut token_stream = TokenStream::new(tokens);
 
assert_eq!(token_stream.position(), 0);
assert_eq!(token_stream.consume(), Some(&1));
assert_eq!(token_stream.position(), 1);
assert_eq!(token_stream.consume(), Some(&2));
assert_eq!(token_stream.position(), 2);
assert_eq!(token_stream.consume(), Some(&3));
assert_eq!(token_stream.position(), 3);

Trait Implementations§

Source§

impl<'a, T: Debug> Debug for TokenStream<'a, T>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

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

§

impl<'a, T> RefUnwindSafe for TokenStream<'a, T>
where T: RefUnwindSafe,

§

impl<'a, T> Send for TokenStream<'a, T>
where T: Sync,

§

impl<'a, T> Sync for TokenStream<'a, T>
where T: Sync,

§

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

§

impl<'a, T> UnsafeUnpin for TokenStream<'a, T>

§

impl<'a, T> UnwindSafe for TokenStream<'a, T>
where T: RefUnwindSafe,

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.