mylang_lexer/state/
str.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
use mylang_token::{range, Pos, Token};

use crate::{LexErr, LexResult};

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

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

    pub fn try_append_char(&self, pos: Pos, c: char) -> LexResult<Self> {
        match (self.escape, c) {
            (_, '\n') => Err(LexErr::ForbiddenChar(pos, c)),

            (true, c @ ('n' | '\\' | '"')) => Ok(Self {
                start: self.start.clone(),
                end: pos,
                escape: false,
                acc: format!("{}{c}", self.acc),
            }),

            (true, _) => Err(LexErr::InvalidEscapeSequence(pos, c)),

            (false, '\\') => Ok(Self {
                start: self.start.clone(),
                end: pos,
                escape: true,
                acc: self.acc.clone(),
            }),

            (false, c) => Ok(Self {
                start: self.start.clone(),
                end: pos,
                escape: false,
                acc: format!("{}{c}", self.acc),
            }),
        }
    }

    pub fn tokenize(&self) -> Token {
        if self.escape {
            panic!("Illigal state")
        }
        Token::Str(
            range!(self.start.clone(), self.end.clone()),
            self.acc.clone(),
        )
    }
}

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

    use super::StrState;

    #[test]
    fn newline() {
        let state = StrState::default();
        let state = state.try_append_char(pos!(0;0), '\n').unwrap_err();
        insta::assert_debug_snapshot!(state);
    }

    #[test]
    fn escape() {
        let state = StrState::default();
        let state = state.try_append_char(pos!(0;0), '\\').unwrap();
        insta::assert_debug_snapshot!(state);
    }

    #[test]
    #[should_panic]
    fn illigal_state() {
        let state = StrState::default();
        let state = state.try_append_char(pos!(0;0), '\\').unwrap();
        state.tokenize();
    }
}