Struct Lexer

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

The tokenizer you write will be passed a Lexer as an argument, and will call methods on it to figure out what the next lexeme is and, if needed, get its text. The Lexer keeps track of the column range of the lexeme.

Implementations§

Source§

impl<'a> Lexer<'a>

Source

pub fn next(&mut self) -> Option<char>

Returns the next character and adds it to the lexeme.

Source

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

Returns the next character without advancing.

Source

pub fn at(&mut self, c: char) -> bool

If the next character is c (the argument, not the letter), adds c to the lexeme and returns true. Otherwise just returns false.

Source

pub fn take_while(&mut self, f: impl Fn(char) -> bool) -> &mut Self

Keeps adding characters to the lexeme while f(char) is true. Returns self to allow chaining.

Source

pub fn skip_while(&mut self, f: impl Fn(char) -> bool) -> &mut Self

Skips over characters while f(char) is true; the lexeme will start at the char where f(char) is false. Returns self to allow chaining.

Source

pub fn get(&self) -> &'a str

Returns the lexeme as an &str.

Source

pub fn into<T: From<&'a str>>(&mut self) -> T

Returns the lexeme converted to the needed type (commonly a String). This is just a shortcut for .lexeme().into().

Source

pub fn map<T>(&mut self, f: impl Fn(&'a str) -> T) -> T

Returns the result of applying f() to the lexeme.

Source

pub fn column_range(&self) -> Range<usize>

Gets the range of columns associated with the lexeme. You probably won’t need to use this, since the column range is included in the Iterator’s output, but it could be useful if the interpretation of lexemes depends upon the columns in which you find them.

Source

pub fn str_range(&self) -> Range<usize>

Gets the byte range of the line associated with the lexeme. You probably won’t need to use this, since .get() gives you the &str for the lexeme, but it could be useful if the interpretation of lexemes depends on the surrounding text.

Source

pub fn line(&self) -> &str

Returns the line we are currently tokenizing. You probably won’t need to use this, but it could be useful if the interpretation of lexemes depends on the surrounding text.

Auto Trait Implementations§

§

impl<'a> Freeze for Lexer<'a>

§

impl<'a> RefUnwindSafe for Lexer<'a>

§

impl<'a> Send for Lexer<'a>

§

impl<'a> Sync for Lexer<'a>

§

impl<'a> Unpin for Lexer<'a>

§

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