Skip to main content

ParseStream

Struct ParseStream 

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

The parsing cursor, similar to syn’s ParseBuffer.

This is the primary interface for consuming tokens during parsing.

Implementations§

Source§

impl<'a> ParseStream<'a>

Source

pub fn is_empty(&self) -> bool

Returns true if there are no more tokens to parse (except EOF).

Source

pub fn is_empty_raw(&self) -> bool

Returns true if there are no more tokens (including comments) except EOF.

Source

pub fn peek(&self) -> &Token

Peek at the current token without consuming it. Comment tokens are skipped transparently.

Source

pub fn peek_raw(&self) -> &Token

Peek at the raw token at the cursor without skipping comments.

Source

pub fn cursor(&self) -> usize

Get the current cursor position.

Source

pub fn set_cursor(&self, pos: usize)

Set the current cursor position (used for backtracking).

Source

pub fn save_state(&self) -> ParserState

Save the full parser state (cursor + pending_gts) for speculative parsing.

Source

pub fn restore_state(&self, state: ParserState)

Restore the full parser state (cursor + pending_gts) after speculative parsing.

Source

pub fn error<T: Display>(&self, span: Span, msg: T) -> Error

Push an error to the error list.

Source

pub fn split_gt(&self)

Split a >> token into two > tokens for nested generic parsing.

Source

pub fn next(&self) -> &Token

Consume and return the current token regardless of its kind.

Source

pub fn is(&self, kind: &TokenKind) -> bool

Check if the current token matches the given kind.

Source

pub fn is_ident(&self, name: &str) -> bool

Check if the current token is an identifier with the given name.

Source

pub fn is_any_ident(&self) -> bool

Check if the current token is any identifier (including contextual keywords).

Source

pub fn is_type_ident(&self) -> bool

Check if the current token can be used as a type name.

Source

pub fn is_keyword(&self, kind: TokenKind) -> bool

Check if the current token is a given keyword.

Source

pub fn eat(&self, expected: &TokenKind) -> bool

Consume the next token if it matches the expected kind.

Source

pub fn expect(&self, kind: TokenKind) -> Result<()>

If the current token matches the given kind, consume it. Otherwise, report an error.

Source

pub fn look_ahead(&self, n: usize) -> &Token

Look ahead n tokens (skipping comments).

Source

pub fn parse_terminated<T, F>(&self, parse_item: F) -> Result<Vec<T>>
where F: FnMut(&ParseStream<'_>) -> Result<T>,

Parse a comma-separated list of items terminated by some token.

Source

pub fn parse_separated<T, F>( &self, can_start_fn: fn(&TokenKind) -> bool, parse_item: F, ) -> Result<Vec<T>>
where F: FnMut(&ParseStream<'_>) -> Result<T>,

Parse zero or more items separated by commas, not requiring a terminator.

Source

pub fn try_parse<T, F>(&self, f: F) -> Option<T>
where F: FnOnce(&ParseStream<'_>) -> Result<T>,

Try to parse something. If parsing fails, revert the cursor.

Source

pub fn parse_parenthesized<T, F>(&self, f: F) -> Result<T>
where F: FnMut(&ParseStream<'_>) -> Result<T>,

Parse something inside parentheses.

Source

pub fn parse<T: Parse>(&self) -> Result<T>

Parse any type that implements Parse.

Source

pub fn parse_braced<T, F>(&self, f: F) -> Result<T>
where F: FnMut(&ParseStream<'_>) -> Result<T>,

Parse something inside braces.

Source

pub fn parse_bracketed<T, F>(&self, f: F) -> Result<T>
where F: FnMut(&ParseStream<'_>) -> Result<T>,

Parse something inside brackets.

Source

pub fn expect_then_raw_span(&self, kind: TokenKind) -> Result<Span>

Consume the expected token, then return the span of the raw token at the cursor. Unlike expect(kind); peek().span, this does NOT skip comments after consuming.

Source

pub fn parse_ident(&self) -> Result<Ident>

Also accepts contextual keywords (record, sealed, var, yield, open, etc.)

Source

pub fn take_errors(&self) -> Vec<Error>

Take any errors accumulated during parsing.

Source

pub fn span_since(&self, start: Span) -> Span

Create a span covering from the given start to the current position.

Source

pub fn skip_comments_to_peek(&self)

Skip past any comment tokens at the current cursor position. This is the public version for explicit comment skipping.

Source

pub fn collect_pending_doc_comments(&self) -> Vec<Comment>

Collect pending doc comments (skipped by peek/advance). Returns only doc comments (/// and /** */), discards regular comments.

Source

pub fn collect_pending_comments(&self) -> Vec<Comment>

Collect all pending comments (skipped by peek/advance).

Source

pub fn collect_leading_doc_comments(&self) -> Vec<Comment>

Collect and consume leading doc comments (/// and /** /). Regular comments (// and / */) are skipped.

Source

pub fn collect_leading_comments(&self) -> Vec<Comment>

Collect and consume all leading comments (both doc and regular).

Auto Trait Implementations§

§

impl<'a> !Freeze for ParseStream<'a>

§

impl<'a> !RefUnwindSafe for ParseStream<'a>

§

impl<'a> !Sync for ParseStream<'a>

§

impl<'a> Send for ParseStream<'a>

§

impl<'a> Unpin for ParseStream<'a>

§

impl<'a> UnsafeUnpin for ParseStream<'a>

§

impl<'a> UnwindSafe for ParseStream<'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> 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.