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

use crate::{state::str::StrState, LexErr};

pub enum StrLexResult {
    Continued(StrState),
    Completed(Token),
    Err(StrState, LexErr),
}

pub fn str_lex(str_state: &StrState, (pos, c): (Pos, char)) -> StrLexResult {
    match (str_state.escape, c) {
        (false, '"') => StrLexResult::Completed(str_state.tokenize()),

        (_, c) => match str_state.try_append_char(pos, c) {
            Ok(str_state) => StrLexResult::Continued(str_state),
            Err(e) => {
                let mut str_state = str_state.clone();
                str_state.escape = false;
                StrLexResult::Err(str_state, e)
            }
        },
    }
}