Struct miden_parsing::Scanner

source ·
pub struct Scanner<S> { /* private fields */ }
Expand description

Scanner handles the low-level details of reading characters from a raw input stream of bytes. It decodes those bytes into UTF-8 characters, and associates each character with the SourceIndex at which it occurs.

The Scanner is intended to be consumed by a lexer, which handles converting the stream of characters into a token stream for use by the parser.

Scanner Lifecycle

The following illustrates how content flows from the raw input stream through the scanner.

lexer <- (peek) <- pending <- source
      <- (pop) <- current <- pending <- source

As shown above, the lexer is “pulling” characters from the scanner.

When “peeking” a character, we return the character currently in the pending field, but if pending is empty, we read enough bytes from the source to construct a UTF-8 character, and store it as pending, as well as returning it to the lexer.

When “popping” a character (i.e. we are advancing the scanner in the input), we are returing the character in the current field, and then moving the character in pending into current. Accordingly, if any of those fields is empty, we must pull from the next field in the chain, reading bytes from the input as we go.

Implementations§

source§

impl<S> Scanner<S>where S: Source,

source

pub fn new(source: S) -> Self

Construct a new Scanner for the given source

source

pub fn start(&self) -> SourceIndex

Returns a SourceIndex representing the start of the source

source

pub fn advance(&mut self)

Advance scanner pipeline by a single character.

pending becomes current, and bytes are read from the input to repopulate pending.

source

pub fn pop(&mut self) -> (SourceIndex, char)

Return the current character and advance our position in the source

source

pub fn peek(&self) -> (SourceIndex, char)

Return the next character in the input, but do not advance.

source

pub fn peek_next(&mut self) -> (SourceIndex, char)

Return the character after the next character in the input, but do not advance.

source

pub fn read(&self) -> (SourceIndex, char)

Get current character in the input.

source

pub fn slice(&self, span: impl Into<Range<usize>>) -> &str

Get a string slice representing the given range in the underlying source

Auto Trait Implementations§

§

impl<S> RefUnwindSafe for Scanner<S>where S: RefUnwindSafe,

§

impl<S> Send for Scanner<S>where S: Send,

§

impl<S> Sync for Scanner<S>where S: Sync,

§

impl<S> Unpin for Scanner<S>where S: Unpin,

§

impl<S> UnwindSafe for Scanner<S>where S: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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 Twhere 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 Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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.