1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
use crate::{cursor::{MutCursor, Cursor}, flext::Flext};

/// Lexer context for tokenising
pub struct Lext {
    pub cursor: MutCursor,
    pub current: Option<char>,
}

impl Lext {
    #[inline]
    pub fn new(file_name: String, contents: &str) -> Self {
        let cursor = MutCursor::new(Cursor::new(file_name, contents));
        let current = cursor.pos_end.get_char();
        Self {
            cursor,
            current,
        }
    }

    /// Gets the current position of the cursor (-1 idx)
    #[inline]
    pub fn rposition(&self) -> crate::cursor::Position {
        let mut clone = self.cursor.clone();
        clone.revance();
        clone.position()
    }
}

impl Flext for Lext {
    /// Advances to the next token
    #[inline]
    fn advance(&mut self) {
        self.cursor.advance();
        self.current = self.cursor.current_char;
    }

    /// Un-Advances
    #[inline]
    fn revance(&mut self) {
        self.cursor.revance();
        self.current = self.cursor.current_char;
    }

    /// Spawns a child flext
    #[inline]
    fn spawn(&self) -> Self {
        Self {
            cursor: self.cursor.spawn(),
            current: self.current,
        }
    }

    /// Gets the current position of the cursor
    #[inline]
    fn position(&self) -> crate::cursor::Position {
        self.cursor.position()
    }
}