luau_lexer/
state.rs

1//! The [`State`] struct.
2
3use smol_str::SmolStr;
4
5use crate::{
6    position::{Position, PositionComponent},
7    token::Trivia,
8};
9
10/// A struct representing the state of a lexer at a specific time.
11#[derive(Clone, Debug, Default, Hash, PartialEq, Eq, PartialOrd, Ord)]
12#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
13pub struct State {
14    /// The current character position.
15    pub(crate) position: usize,
16
17    /// The current byte position.
18    pub(crate) byte_position: usize,
19
20    /// The current [`position`](Position) in the file.
21    pub(crate) lexer_position: Position,
22
23    /// The spaces after the last parsed token.
24    pub(crate) last_trivia: Vec<Trivia>,
25}
26
27impl State {
28    /// Move the state by the passed character.
29    pub fn increment_position_by_char(&mut self, character: char) {
30        self.position += 1;
31        self.byte_position += character.len_utf8();
32
33        match character {
34            '\n' => {
35                self.lexer_position.character = 0;
36                self.lexer_position.line += 1;
37            }
38            _ => self.lexer_position.character += 1,
39        }
40    }
41
42    /// Move th state ahead by the passed amount of characters.
43    pub fn increment_position(&mut self, amount: PositionComponent) {
44        self.position += amount as usize;
45        self.lexer_position.character += amount;
46    }
47
48    /// Get the current file [`position`](Position).
49    #[inline]
50    pub fn lexer_position(&self) -> Position {
51        self.lexer_position
52    }
53}