Skip to main content

Lexer

Struct Lexer 

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

A standalone lexer that produces tokens on demand without heap allocation.

The lexer processes input bytes and yields Token values one at a time. It handles whitespace/comment skipping, number parsing, string escape sequences, character literals, and symbol case-folding.

§No-std Compatible

The lexer uses fixed-size stack buffers for string and symbol content, avoiding any heap allocation. String literals are limited to 1024 characters and symbols to 64 characters.

§Example

use grift_parser::Lexer;

let mut lexer = Lexer::new("(+ 1 2)");
while let Some(result) = lexer.next_token() {
    let spanned = result.unwrap();
    // Process token...
}

Implementations§

Source§

impl<'a> Lexer<'a>

Source

pub fn new(input: &'a str) -> Lexer<'a>

Create a new lexer from a string slice

Source

pub fn from_bytes(input: &'a [u8]) -> Lexer<'a>

Create a new lexer from a byte slice

Source

pub fn loc(&self) -> SourceLoc

Get the current source location

Source

pub fn symbol_bytes(&self, len: usize) -> &[u8]

Get the lowercased symbol bytes from the last Token::Symbol produced.

The len field from Token::Symbol { len } indicates how many bytes are valid.

Source

pub fn string_chars(&self, len: usize) -> &[char]

Get the string characters from the last Token::String produced.

The len field from Token::String { len } indicates how many characters are valid.

Source

pub fn position(&self) -> (usize, usize)

Get current position in the input

Source

pub fn has_more(&mut self) -> bool

Check if there’s more input (after skipping whitespace/comments)

Source

pub fn next_token(&mut self) -> Option<Result<SpannedToken, LexError>>

Produce the next token, or None if input is exhausted.

Returns None when there is no more input (after whitespace). Returns Some(Err(...)) for lexer errors. Returns Some(Ok(...)) for successfully lexed tokens.

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.