mylang_lexer/state/
symbol.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
use std::str::FromStr;

use mylang_token::{range, KeywordKind, Pos, Token};

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

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

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

    pub fn tokenize(&self) -> Token {
        if let Ok(keyword_kind) = KeywordKind::from_str(&self.acc) {
            Token::Keyword(range!(self.start.clone(), self.end.clone()), keyword_kind)
        } else {
            Token::Ident(
                range!(self.start.clone(), self.end.clone()),
                self.acc.clone(),
            )
        }
    }
}