pipeline_script/lexer/
token.rs

1use std::fmt::{Display, Formatter};
2
3#[allow(unused)]
4#[derive(Debug, Clone, PartialEq)]
5pub enum Token {
6    String(String),
7    FormatString(String),
8    Int(i64),
9    Float(f32),
10    Identifier(String),
11    Boolean(bool),
12    /// 关键字
13    Keyword(String),
14    /// (
15    BraceLeft,
16    /// )
17    BraceRight,
18    /// [
19    BracketLeft,
20    /// ]
21    BracketRight,
22    /// {
23    ParenLeft,
24    /// }
25    ParenRight,
26    /// .
27    Dot,
28    /// :
29    Colon,
30    //::
31    ScopeSymbol,
32    /// =
33    Assign,
34    /// ,
35    Comma,
36    /// +
37    Plus,
38    ///-
39    Minus,
40    /// *
41    Star,
42    /// /
43    Slash,
44    /// %
45    Mod,
46    /// >
47    Greater,
48    /// <
49    Less,
50    /// <=
51    LessEqual,
52    /// >=
53    GreaterEqual,
54    /// ==
55    Equal,
56    /// !=
57    NotEqual,
58    ///->
59    Arrow,
60    /// !
61    Not,
62    /// &&
63    And,
64    /// ||
65    Or,
66    /// |
67    Vertical,
68    /// @
69    Annotation,
70    /// &
71    BitAnd,
72    Eof,
73}
74impl Display for Token {
75    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
76        match self {
77            Token::String(s) => write!(f, "String({s})"),
78            Token::Int(i) => write!(f, "Int({i})"),
79            Token::Float(f0) => write!(f, "Float({f0})"),
80            Token::Identifier(i) => write!(f, "Identifier({i})"),
81            Token::Keyword(kw) => write!(f, "Keyword({kw})"),
82            Token::BraceLeft => write!(f, "Symbol(()"),
83            Token::BraceRight => write!(f, "Symbol())"),
84            Token::BracketLeft => write!(f, "Symbol([)"),
85            Token::BracketRight => write!(f, "Symbol(])"),
86            Token::ParenLeft => write!(f, "Symbol({{)"),
87            Token::ParenRight => write!(f, "Symbol(}})"),
88            Token::Dot => write!(f, "Symbol(.)"),
89            Token::Colon => write!(f, "Symbol(:)"),
90            Token::ScopeSymbol => write!(f, "Symbol(::)"),
91            Token::Assign => write!(f, "Symbol(=)"),
92            Token::Comma => write!(f, "Symbol(,)"),
93            Token::Plus => write!(f, "Symbol(+)"),
94            Token::Minus => write!(f, "Symbol(-)"),
95            Token::Star => write!(f, "Symbol(*)"),
96            Token::Slash => write!(f, "Symbol(/)"),
97            Token::Mod => write!(f, "Symbol(%)"),
98            Token::Greater => write!(f, "Symbol(>)"),
99            Token::Less => write!(f, "Symbol(<)"),
100            Token::Equal => write!(f, "Symbol(==)"),
101            Token::NotEqual => write!(f, "Symbol(!=)"),
102            Token::Arrow => write!(f, "Symbol(->)"),
103            Token::Eof => write!(f, "EOF"),
104            Token::Not => write!(f, "Symbol(!)"),
105            Token::And => write!(f, "Symbol(&&)"),
106            Token::Vertical => write!(f, "Symbol(|)"),
107            Token::Annotation => write!(f, "Symbol(@)"),
108            Token::BitAnd => write!(f, "Symbol(&)"),
109            Token::FormatString(s) => write!(f, "FormatString({s})"),
110            Token::LessEqual => write!(f, "Symbol(<=)"),
111            Token::GreaterEqual => write!(f, "Symbol(>=)"),
112            Token::Or => write!(f, "Symbol(||)"),
113            _ => todo!(),
114        }
115    }
116}
117impl Token {
118    #[allow(unused)]
119    pub fn token_id(&self) -> i8 {
120        match self {
121            Token::String(_) => 0,
122            Token::Int(_) => 1,
123            Token::Float(_) => 2,
124            Token::Identifier(_) => 3,
125            Token::Keyword(_) => 4,
126            Token::BraceLeft => 5,
127            Token::BraceRight => 6,
128            Token::BracketLeft => 7,
129            Token::BracketRight => 8,
130            Token::ParenLeft => 9,
131            Token::ParenRight => 10,
132            Token::Dot => 11,
133            Token::Comma => 12,
134            Token::Eof => 13,
135            Token::Colon => 14,
136            Token::Assign => 15,
137            Token::Plus => 16,
138            Token::Star => 17,
139            Token::Greater => 18,
140            Token::Less => 19,
141            Token::Equal => 20,
142            Token::Minus => 21,
143            Token::Slash => 22,
144            Token::Mod => 23,
145            Token::ScopeSymbol => 24,
146            Token::NotEqual => 25,
147            Token::Arrow => 26,
148            Token::Not => 27,
149            Token::And => 28,
150            Token::Vertical => 29,
151            Token::Annotation => 30,
152            Token::Or => 31,
153            _ => todo!(),
154        }
155    }
156    pub fn is_colon(&self) -> bool {
157        matches!(self, Token::Colon)
158    }
159    pub fn is_parenthesis_left(&self) -> bool {
160        matches!(self, Token::ParenLeft)
161    }
162    pub fn is_keyword(&self, keyword: &str) -> bool {
163        match self {
164            Token::Keyword(k) => {
165                if k == keyword {
166                    return true;
167                }
168                false
169            }
170            _ => false,
171        }
172    }
173    pub fn is_assign(&self) -> bool {
174        matches!(self, Token::Assign)
175    }
176    pub fn get_identifier_value(&self) -> &str {
177        match self {
178            Token::Identifier(s) => s.as_str(),
179            _ => "",
180        }
181    }
182}