Skip to main content

Lexer

Struct Lexer 

Source
pub struct Lexer<'t> {
    pub(crate) path: Symbol,
    pub(crate) text: &'t str,
    pub(crate) iter: Peekable<CharIndices<'t>>,
    pub(crate) head: u32,
    pub(crate) tail: u32,
}

Fields§

§path: Symbol§text: &'t str

The source text

§iter: Peekable<CharIndices<'t>>

A peekable iterator over the source text

§head: u32

The start of the current token

§tail: u32

The end of the current token

Implementations§

Source§

impl<'t> Lexer<'t>

Source

pub fn new(path: Symbol, text: &'t str) -> Self

Constructs a new Lexer from some text

Source

pub const fn span(&self) -> Span

Gets the [struct@Span] of the current token.

When called from outside Lexer::scan, this will return a zero-sized span marking the current lexer location.

Source

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

Peeks the next character without advancing the lexer

Source

pub(crate) fn advance_tail(&mut self)

Advances the tail to the current character index

Source

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

Takes the last character

Source

pub(crate) fn next_if(&mut self, expected: char) -> Option<char>

Source

pub(crate) fn consume(&mut self) -> &mut Self

Consumes the last-peeked character, advancing the tail

Source

pub(crate) const fn error(&self, res: LexFailure) -> LexError

Produces a LexError at the start of the current token

Source

pub(crate) fn as_str(&self) -> (&'t str, Span)

Gets the Lexer’s current &str lexeme and [struct@Span]

Source

pub(crate) fn produce(&mut self, kind: TKind) -> Token

Produces a Token

Source

pub(crate) fn produce_with_lexeme( &mut self, kind: TKind, lexeme: Lexeme, ) -> Token

Source

pub(crate) fn skip_whitespace(&mut self) -> &mut Self

Consumes 0 or more whitespace

Source

pub(crate) const fn start_token(&mut self) -> &mut Self

Source

pub fn scan(&mut self) -> Result<Token, LexError>

Scans forward until it finds the next Token in the input

Source

pub fn trailing(&mut self, kind: TKind) -> Result<Token, LexError>

Elides the trailing Token kind when it comes before a list terminator.

Source

pub fn line_comment(&mut self) -> Result<Token, LexError>

Consumes characters until the lexer reaches a newline '\n'

Source

pub fn block_comment(&mut self) -> Result<&mut Self, LexError>

Consumes characters until the lexer reaches the end of a nested block comment. This allows you to arbitrarily comment out code, even if that code has a block comment.

Source

pub fn identifier(&mut self) -> Result<Token, LexError>

Consumes characters until it reaches a character not in is_xid_continue.

Always consumes the first character.

Maps the result to either a TKind::Identifier or a TKind keyword.

Source

pub fn character(&mut self) -> Result<Token, LexError>

Eagerly parses a character literal starting at the current lexer position.

Source

pub fn string(&mut self) -> Result<Token, LexError>

Source

pub fn escape(&mut self) -> Result<char, LexError>

Parses a single escape sequence into its resulting char value.

Source

pub fn hex_escape(&mut self) -> Result<char, LexError>

Parses two hex-digits and constructs a char out of them.

Source

pub fn unicode_escape(&mut self) -> Result<char, LexError>

Parses a sequence of {}-bracketed hex-digits and constructs a char out of them.

Source

pub fn digits<const BASE: u32>(&mut self) -> Result<Token, LexError>

Parses a sequence of digits (and underscores) in base BASE, where 2 <= BASE <= 36.

If the sequence of digits exceeds the bounds of a u128, the resulting number will wrap around 2^128.

Source

pub fn digit<const BASE: u32>(&mut self) -> Result<u32, LexError>

Parses a single digit in base BASE as a u32, where 2 <= BASE <= 36.

Trait Implementations§

Source§

impl<'t> Clone for Lexer<'t>

Source§

fn clone(&self) -> Lexer<'t>

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<'t> Debug for Lexer<'t>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'t> Freeze for Lexer<'t>

§

impl<'t> RefUnwindSafe for Lexer<'t>

§

impl<'t> Send for Lexer<'t>

§

impl<'t> Sync for Lexer<'t>

§

impl<'t> Unpin for Lexer<'t>

§

impl<'t> UnwindSafe for Lexer<'t>

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 = 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.