Struct Lexer

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

A lexer (tokenizer) for a mathematical expression.

The Lexer programatically tokenizes one or more bytes of a program, with safety checks to enable notification that the program has concluded (EOF). Each Token is individually useless unless further processed; for instance, by the Parser.

As an example, an abstract representation of a program’s tokenization is shown:

in:
    123 + 281 ./ 3
    ^~~ ^ ^~~ ^~ ^
out (over 5 iterations):
    1. INTEGER(123)
    2. PLUS
    3. INTEGER(281)
    4. DIVIDEINT
    5. INTEGER(3)

We expect the Parser to be agnostic to the significance and relationship of each byte in the program, instead concerned only with the creation and typing of bytes. As such, the Lexer is built to refrain from analyzing anything but the values of bytes in a program.

Implementations§

Source§

impl<'a> Lexer<'a>

Source

pub fn from(text: &str) -> Lexer<'_>

Creates a Lexer from a text program.

Source

pub fn skip(&mut self, amt: usize) -> &mut Lexer<'a>

Skips amt Tokens and returns the current Lexer. Primarily intended for use in method chaining:

let mut lexer = Lexer::from("2 + 3");
let result = lexer.skip(1).next_token().unwrap(); // => Token::PLUS
Source

pub fn next_token(&mut self) -> Result<Token, ProgramError>

Tokenizes the next one or more characters in the program. If no valid Token can be made, the Lexer panics.

let mut lexer = Lexer::from("2 + 3");
let result = lexer.next_token().unwrap(); // => Token::NUMBER(2)

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.