Skip to main content

Cursor

Struct Cursor 

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

A position in the token stream.

Implementations§

Source§

impl<'a> Cursor<'a>

Source

pub fn new(tokens: &'a [Token]) -> Self

A cursor on the first token of tokens.

§Panics

Panics if tokens does not end in TokenKind::Eof. Every method here relies on that token being there: it is what a peek past the end returns and it is what stops every recovery skip, so a stream without one would turn a malformed file into a hang.

Source

pub fn current(&self) -> Token

The token the parser is looking at.

Source

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

The token n places ahead, which is the final TokenKind::Eof once the end is reached rather than an out of range access.

§Panics

Panics if n is greater than MAX_LOOKAHEAD.

Source

pub fn span(&self) -> Span

Where the current token is, which is where a diagnostic about it points.

Source

pub fn prev_end(&self) -> Span

The empty span just after the previous token, which is where something that should have been written and was not belongs.

Pointing a missing semicolon at the token that follows it is a small thing that reads badly, because the token that follows is usually on the next line and is not the problem. Before the first token there is no previous one, so this is the start of the current token instead.

Source

pub fn is_eof(&self) -> bool

Whether the parser has reached the end of the translation unit.

Source

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

Whether the current token is exactly kind.

Source

pub fn at_punct(&self, punct: Punct) -> bool

Whether the current token is the punctuator punct.

Source

pub fn at_keyword(&self, keyword: Keyword) -> bool

Whether the current token is the keyword keyword.

Source

pub fn bump(&mut self) -> Token

Steps over the current token and gives it back.

Stepping over the end is not an error and does not move: the cursor stays on the final TokenKind::Eof, so a loop that forgets to check for the end runs out of tokens instead of reading off the end of the slice. It will still spin, which is what Cursor::index is for.

Source

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

Steps over the current token if it is kind, and reports whether it did.

Source

pub fn eat_punct(&mut self, punct: Punct) -> bool

Steps over the current token if it is the punctuator punct.

Source

pub fn eat_keyword(&mut self, keyword: Keyword) -> bool

Steps over the current token if it is the keyword keyword.

Source

pub fn index(&self) -> usize

How many tokens the cursor has stepped over.

The parser’s loops compare this across an iteration to check that they made progress. A production that returns without consuming anything is the classic way a recursive descent parser hangs on malformed input, and it is a bug in the parser rather than something to recover from, so the check belongs in an assertion and not in a if.

Source

pub fn save(&self) -> Mark

The current position, to be given back to Cursor::restore.

Source

pub fn restore(&mut self, mark: Mark)

Goes back to a saved position.

This is not a general backtrack and it does not undo anything but the position. Between a save and a restore the parser must not report a diagnostic and must not put a node in the tree, because neither is taken back, and a speculative parse that leaves either behind produces an error about a reading of the source that was abandoned. The two constructs that need this are in spec/06-lexer-and-parser.md section 6.4.

§Panics

Panics if mark came from a cursor on a different token stream.

Trait Implementations§

Source§

impl<'a> Clone for Cursor<'a>

Source§

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

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'a> Debug for Cursor<'a>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a> Freeze for Cursor<'a>

§

impl<'a> RefUnwindSafe for Cursor<'a>

§

impl<'a> Send for Cursor<'a>

§

impl<'a> Sync for Cursor<'a>

§

impl<'a> Unpin for Cursor<'a>

§

impl<'a> UnsafeUnpin for Cursor<'a>

§

impl<'a> UnwindSafe for Cursor<'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> 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 = !

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.