pub struct Input<'a> { /* private fields */ }Expand description
A struct representing the input code being lexed.
The Input struct provides methods to read, peek, consume, and skip characters
from the bytes input code while keeping track of the current position (line, column, offset).
Implementations§
Source§impl<'a> Input<'a>
impl<'a> Input<'a>
Sourcepub fn from_file_in(arena: &'a Bump, file: &File) -> Self
pub fn from_file_in(arena: &'a Bump, file: &File) -> Self
Sourcepub fn anchored_at(
file_id: FileId,
bytes: &'a [u8],
anchor_position: Position,
) -> Self
pub fn anchored_at( file_id: FileId, bytes: &'a [u8], anchor_position: Position, ) -> Self
Creates a new Input instance representing a byte slice that is
“anchored” at a specific absolute position within a larger source file.
This is useful when lexing a subset (slice) of a source file, as it allows generated tokens to retain accurate absolute positions and spans relative to the original file.
The internal cursor (offset) starts at 0 relative to the bytes slice,
but the absolute position is calculated relative to the anchor_position.
§Arguments
file_id- The unique identifier for the source file this input belongs to.bytes- A byte slice representing the input code subset to be lexed.anchor_position- The absolutePositionin the original source file where the providedbytesslice begins.
§Returns
A new Input instance ready to lex the bytes, maintaining positions
relative to anchor_position.
Sourcepub const fn current_position(&self) -> Position
pub const fn current_position(&self) -> Position
Returns the absolute current Position of the lexer within the original source file.
It calculates this by adding the internal offset (progress within the current byte slice)
to the starting_position the Input was initialized with.
Sourcepub const fn current_offset(&self) -> usize
pub const fn current_offset(&self) -> usize
Returns the current internal byte offset relative to the start of the input slice.
This indicates how many bytes have been consumed from the current bytes slice.
To get the absolute position in the original source file, use current_position().
Sourcepub const fn is_empty(&self) -> bool
pub const fn is_empty(&self) -> bool
Returns true if the input slice is empty (length is zero).
Sourcepub const fn len(&self) -> usize
pub const fn len(&self) -> usize
Returns the total length in bytes of the input slice being processed.
Sourcepub const fn has_reached_eof(&self) -> bool
pub const fn has_reached_eof(&self) -> bool
Checks if the current position is at the end of the input.
§Returns
true if the current offset is greater than or equal to the input length; false otherwise.
Sourcepub fn slice_in_range(&self, from: u32, to: u32) -> &'a [u8] ⓘ
pub fn slice_in_range(&self, from: u32, to: u32) -> &'a [u8] ⓘ
Returns a byte slice within a specified absolute range.
The from and to arguments are absolute byte offsets from the beginning
of the original source file. The method calculates the correct slice
relative to the starting_position of this Input.
This is useful for retrieving the raw text of a Span or Token whose
positions are absolute, even when the Input only contains a subsection
of the source file.
The returned slice is defensively clamped to the bounds of the current
Input’s byte slice to prevent panics.
§Arguments
from- The absolute starting byte offset.to- The absolute ending byte offset (exclusive).
§Returns
A byte slice &[u8] corresponding to the requested range.
Sourcepub fn next(&mut self)
pub fn next(&mut self)
Advances the current position by one character, updating line and column numbers.
Handles different line endings (\n, \r, \r\n) and updates line and column counters accordingly.
If the end of input is reached, no action is taken.
Sourcepub fn skip(&mut self, count: usize)
pub fn skip(&mut self, count: usize)
Skips the next count characters, advancing the position accordingly.
Updates line and column numbers as it advances.
§Arguments
count- The number of characters to skip.
Sourcepub fn consume_remaining(&mut self) -> &'a [u8] ⓘ
pub fn consume_remaining(&mut self) -> &'a [u8] ⓘ
Consumes all remaining characters from the current position to the end of input.
Advances the position to EOF.
§Returns
A byte slice containing the remaining characters.
Sourcepub fn consume_until(
&mut self,
search: &[u8],
ignore_ascii_case: bool,
) -> &'a [u8] ⓘ
pub fn consume_until( &mut self, search: &[u8], ignore_ascii_case: bool, ) -> &'a [u8] ⓘ
Consumes characters until the given byte slice is found.
Advances the position to the start of the search slice if found, or to EOF if not found.
§Arguments
search- The byte slice to search for.ignore_ascii_case- Whether to ignore ASCII case when comparing characters.
§Returns
A byte slice containing the consumed characters.
pub fn consume_through(&mut self, search: u8) -> &'a [u8] ⓘ
Sourcepub fn consume_whitespaces(&mut self) -> &'a [u8] ⓘ
pub fn consume_whitespaces(&mut self) -> &'a [u8] ⓘ
Consumes whitespaces until a non-whitespace character is found.
§Returns
A byte slice containing the consumed whitespaces.
Sourcepub fn read_at(&self, at: usize) -> &'a u8
pub fn read_at(&self, at: usize) -> &'a u8
Reads a single byte at a specific byte offset within the input slice, without advancing the internal cursor.
This provides direct, low-level access to the underlying byte data.
§Arguments
at- The zero-based byte offset within the input slice (self.bytes) from which to read the byte.
§Returns
A reference to the byte located at the specified offset at.
§Panics
This method panics if the provided at offset is out of bounds
for the input byte slice (i.e., if at >= self.bytes.len()).
Sourcepub const fn match_sequence_ignore_whitespace(
&self,
search: &[u8],
ignore_ascii_case: bool,
) -> Option<usize>
pub const fn match_sequence_ignore_whitespace( &self, search: &[u8], ignore_ascii_case: bool, ) -> Option<usize>
Attempts to match the given byte sequence at the current position, ignoring whitespace in the input.
This method tries to match the provided byte slice search against the input starting
from the current position, possibly ignoring ASCII case. Whitespace characters in the input
are skipped during matching, but their length is included in the returned length.
Importantly, the method does not include any trailing whitespace after the matched sequence in the returned length.
For example, to match the sequence (string), the input could be (string), ( string ), ( string ), etc.,
and this method would return the total length of the input consumed to match (string),
including any whitespace within the matched sequence, but excluding any whitespace after it.
§Arguments
search- The byte slice to match against the input.ignore_ascii_case- Iftrue, ASCII case is ignored during comparison.
§Returns
Some(length)- If the input matchessearch(ignoring whitespace within the sequence), returns the total length of the input consumed to matchsearch, including any skipped whitespace within the matched sequence.None- If the input does not matchsearch.
Trait Implementations§
Source§impl<'a> Ord for Input<'a>
impl<'a> Ord for Input<'a>
Source§impl<'a> PartialOrd for Input<'a>
impl<'a> PartialOrd for Input<'a>
impl<'a> Copy for Input<'a>
impl<'a> Eq for Input<'a>
impl<'a> StructuralPartialEq for Input<'a>
Auto Trait Implementations§
impl<'a> Freeze for Input<'a>
impl<'a> RefUnwindSafe for Input<'a>
impl<'a> Send for Input<'a>
impl<'a> Sync for Input<'a>
impl<'a> Unpin for Input<'a>
impl<'a> UnwindSafe for Input<'a>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more