Struct TokenStream

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

Token stream.

§Examples

use php_parser_rs::lexer::token::Token;
use php_parser_rs::lexer::token::TokenKind;
use php_parser_rs::lexer::stream::TokenStream;

let tokens = vec![
    Token { kind: TokenKind::SingleLineComment("// some class".into()), span: (1, 1) },
    Token { kind: TokenKind::Readonly, span: (2, 1) },
    Token { kind: TokenKind::Class, span: (2, 10) },
    Token { kind: TokenKind::Enum, span: (2, 16) },
    Token { kind: TokenKind::LeftBrace, span: (2, 21) },
    Token { kind: TokenKind::SingleLineComment("// empty body!".into()), span: (3, 1) },
    Token { kind: TokenKind::RightBrace, span: (4, 1) },
    Token { kind: TokenKind::Eof, span: (0, 0) },
];

let mut stream = TokenStream::new(tokens);

assert!(matches!(stream.current().kind, TokenKind::Readonly));
assert!(matches!(stream.peek().kind, TokenKind::Class));
assert!(matches!(stream.lookahead(1).kind, TokenKind::Enum));
assert!(matches!(stream.lookahead(2).kind, TokenKind::LeftBrace));
assert!(matches!(stream.lookahead(3).kind, TokenKind::RightBrace));
assert!(matches!(stream.lookahead(4).kind, TokenKind::Eof));
assert!(matches!(stream.lookahead(5).kind, TokenKind::Eof));

stream.next();

assert!(matches!(stream.current().kind, TokenKind::Class));

stream.next();
stream.next();
stream.next();

assert!(matches!(stream.current().kind, TokenKind::RightBrace));

stream.next();

assert!(matches!(stream.current().kind, TokenKind::Eof));
assert!(stream.is_eof());

assert_eq!(stream.comments(), vec![
    Token { kind: TokenKind::SingleLineComment("// some class".into()), span: (1, 1) },
    Token { kind: TokenKind::SingleLineComment("// empty body!".into()), span: (3, 1) },
]);

Implementations§

Source§

impl<'a> TokenStream<'a>

Token stream.

Source

pub fn new(tokens: &'a [Token]) -> TokenStream<'_>

Source

pub fn next(&mut self)

Move cursor to next token.

Comments are collected.

Source

pub const fn current(&self) -> &'a Token

Get current token.

Source

pub const fn previous(&self) -> &'a Token

Get previous token.

Source

pub const fn peek(&self) -> &'a Token

Peek next token.

All comments are skipped.

Source

pub const fn lookahead(&self, n: usize) -> &'a Token

Peek nth+1 token.

All comments are skipped.

Source

pub fn is_eof(&self) -> bool

Check if current token is EOF.

Source

pub fn comments(&mut self) -> CommentGroup

Get all comments.

Trait Implementations§

Source§

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

Source§

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

Returns a copy 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> Default for TokenStream<'a>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<'a> From<&'a Vec<Token>> for TokenStream<'a>

Source§

fn from(tokens: &'a Vec<Token>) -> Self

Converts to this type from the input type.
Source§

impl<'a> PartialEq for TokenStream<'a>

Source§

fn eq(&self, other: &TokenStream<'a>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<'a> Eq for TokenStream<'a>

Source§

impl<'a> StructuralPartialEq 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> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

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> 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.