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>
impl<S: Source, L: Language> LexerState<S, L>
Sourcepub fn new_with_cache(
source: S,
changed: usize,
cache: IncrementalCache<'_, L>,
) -> Self
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 lexchanged- The number of bytes that have changed since the last lexcache- The incremental cache containing previous lexing results
§Returns
A new LexerState initialized at the beginning of the source with cache support
Sourcepub fn rest(&self) -> &str
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
Sourcepub fn get_position(&self) -> usize
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
Sourcepub fn set_position(&mut self, offset: usize) -> usize
pub fn set_position(&mut self, offset: usize) -> usize
Sourcepub fn get_length(&self) -> usize
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
Sourcepub fn add_token(&mut self, kind: L::SyntaxKind, start: usize, end: usize)
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 tokenstart- The starting byte offset of the tokenend- The ending byte offset of the token
Sourcepub fn current(&self) -> Option<char>
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
Sourcepub fn peek(&self) -> Option<char>
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
Sourcepub fn peek_next_n(&self, n: usize) -> Option<char>
pub fn peek_next_n(&self, n: usize) -> Option<char>
Sourcepub fn advance_with(&mut self, token: Token<L::SyntaxKind>) -> usize
pub fn advance_with(&mut self, token: Token<L::SyntaxKind>) -> usize
Sourcepub fn not_at_end(&self) -> bool
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
Sourcepub fn safe_check(&mut self, safe_point: usize)
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