LexerState

Struct LexerState 

Source
pub struct LexerState<S, L: Language> { /* private fields */ }
Expand description

State information for incremental lexical analysis.

This struct maintains the current position and context during tokenization, enabling incremental and resumable lexing operations.

Implementations§

Source§

impl<S: Source, L: Language> LexerState<S, L>

Source

pub fn new(source: S) -> Self

Creates a new lexer state with the given source text.

§Arguments
  • source - The source text to lex
§Returns

A new LexerState initialized at the beginning of the source

Source

pub fn new_with_cache( source: S, changed: usize, cache: IncrementalCache<'_, L>, ) -> Self

Creates a new lexer state with the given source text and incremental cache.

§Arguments
  • source - The source text to lex
  • changed - The number of bytes that have changed since the last lex
  • cache - The incremental cache containing previous lexing results
§Returns

A new LexerState initialized at the beginning of the source with cache support

Source

pub fn rest(&self) -> &str

Gets the remaining text from the current position to the end of the source.

§Returns

A string slice containing the remaining text

Source

pub fn get_position(&self) -> usize

Gets the current byte offset position in the source text.

§Returns

The current byte offset from the start of the source text

Source

pub fn set_position(&mut self, offset: usize) -> usize

Sets the current position to the specified byte offset.

§Arguments
  • offset - The new byte offset position
§Returns

The previous byte offset position

Source

pub fn get_length(&self) -> usize

Gets the total length of the source text in bytes.

§Returns

The total number of bytes in the source text

Source

pub fn add_error(&mut self, error: impl Into<OakError>)

Adds an error to the lexer state.

§Arguments
  • error - The error to add to the diagnostics
Source

pub fn add_token(&mut self, kind: L::SyntaxKind, start: usize, end: usize)

Adds a token to the lexer state.

§Arguments
  • kind - The kind of the token
  • start - The starting byte offset of the token
  • end - The ending byte offset of the token
Source

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

Gets the current character at the current position.

§Returns

The current character, or None if at the end of the source

Source

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

Peeks at the next character without advancing the position.

§Returns

The next character, or None if at the end of the source

Source

pub fn peek_next_n(&self, n: usize) -> Option<char>

Peeks at the character n positions ahead without advancing the position.

§Arguments
  • n - The number of characters to peek ahead
§Returns

The character n positions ahead, or None if beyond the end of the source

Source

pub fn advance(&mut self, length: usize) -> usize

Advances the position by the specified number of bytes.

§Arguments
  • length - The number of bytes to advance
§Returns

The new byte offset position

Source

pub fn advance_with(&mut self, token: Token<L::SyntaxKind>) -> usize

Advances the position by the specified number of bytes and adds a token.

§Arguments
  • length - The number of bytes to advance
  • token - The kind of token to add
§Returns

The new byte offset position

§Note

The caller must ensure that the advance is at character boundaries.

Source

pub fn take_while(&mut self, pred: impl FnMut(char) -> bool) -> Range<usize>

Consumes characters while the predicate returns true, returning the consumed range.

§Arguments
  • pred - The predicate function that determines whether to consume a character
§Returns

The byte range of consumed characters

Source

pub fn not_at_end(&self) -> bool

Checks if the lexer has not reached the end of the source text.

§Returns

true if not at the end of the source, false otherwise

Source

pub fn safe_check(&mut self, safe_point: usize)

Performs a safety check to prevent infinite loops during lexing.

This method ensures that the lexer always makes progress by forcing advancement when stuck at the same position. It’s used as a safeguard against infinite loops in lexer implementations.

§Arguments
  • safe_point - The position to check against for potential deadlock
Source

pub fn finish(self, result: Result<(), OakError>) -> LexOutput<L>

Finishes lexing and returns the final output with tokens and diagnostics.

§Returns

A LexOutput containing the collected tokens and any errors encountered

Trait Implementations§

Source§

impl<S: Debug, L: Debug + Language> Debug for LexerState<S, L>
where L::SyntaxKind: Debug,

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<S: Source, L: Language> Deref for LexerState<S, L>

Source§

type Target = S

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.

Auto Trait Implementations§

§

impl<S, L> Freeze for LexerState<S, L>
where S: Freeze,

§

impl<S, L> !RefUnwindSafe for LexerState<S, L>

§

impl<S, L> Send for LexerState<S, L>
where S: Send,

§

impl<S, L> Sync for LexerState<S, L>
where S: Sync, <L as Language>::SyntaxKind: Sync,

§

impl<S, L> Unpin for LexerState<S, L>
where S: Unpin, <L as Language>::SyntaxKind: Unpin,

§

impl<S, L> !UnwindSafe for LexerState<S, L>

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<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
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.
Source§

impl<T> ErasedDestructor for T
where T: 'static,