mylang_lexer/state/
i32.rs

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
use mylang_token::{range, Pos, Token};

#[derive(Clone, Debug, Default)]
pub struct I32State {
    start: Pos,
    end: Pos,
    acc: String,
}

impl I32State {
    pub fn new(pos: Pos, acc: String) -> Self {
        Self {
            start: pos.clone(),
            end: pos,
            acc,
        }
    }

    pub fn append_digit_char(&self, pos: Pos, c: char) -> Self {
        Self {
            start: self.start.clone(),
            end: pos,
            acc: format!("{}{c}", self.acc),
        }
    }

    pub fn tokenize(&self) -> Token {
        let i = self.acc.parse::<i32>().unwrap();
        Token::I32(range!(self.start.clone(), self.end.clone()), i)
    }
}

#[cfg(test)]
mod tests {
    use mylang_token::pos;

    use super::I32State;

    #[test]
    fn test_i32_state() {
        let state = I32State::default();
        let state = state.append_digit_char(pos!(0;0), '1');
        let state = state.append_digit_char(pos!(0;1), '2');
        let token = state.tokenize();
        insta::assert_debug_snapshot!((state, token));
    }
}