luau_lexer/
state.rs

1//! The [`State`] struct.
2
3use lsp_types::Position;
4
5use crate::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 byte position.
15    pub(crate) byte_position: usize,
16
17    /// The current [`position`](Position) in the file.
18    pub(crate) lexer_position: Position,
19
20    /// The spaces after the last parsed token.
21    pub(crate) last_trivia: Vec<Trivia>,
22}
23
24impl State {
25    /// Move the state by the passed character.
26    pub fn increment_position_by_char(&mut self, character: char) {
27        self.position += 1;
28        self.byte_position += character.len_utf8();
29
30        match character {
31            '\n' => {
32                self.lexer_position.character = 0;
33                self.lexer_position.line += 1;
34            }
35            _ => self.lexer_position.character += 1,
36        }
37    }
38
39    /// Move th state ahead by the passed amount of characters.
40    pub fn increment_position(&mut self, amount: u32) {
41        self.position += amount as usize;
42        self.lexer_position.character += amount;
43    }
44
45    /// Get the current file [`position`](Position).
46    #[inline]
47    pub fn lexer_position(&self) -> Position {
48        self.lexer_position
49    }
50}