luau_lexer/
state.rs

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