Skip to main content

oak_c/lexer/
token_type.rs

1use oak_core::{TokenType, UniversalTokenRole};
2#[cfg(feature = "serde")]
3use serde::{Deserialize, Serialize};
4
5#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
6#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
7#[repr(u16)]
8pub enum CTokenType {
9    /// Left parenthesis: `(`
10    LeftParen,
11    /// Right parenthesis: `)`
12    RightParen,
13    /// Left bracket: `[`
14    LeftBracket,
15    /// Right bracket: `]`
16    RightBracket,
17    /// Left brace: `{`
18    LeftBrace,
19    /// Right brace: `}`
20    RightBrace,
21    /// Comma: `,`
22    Comma,
23    /// Semicolon: `;`
24    Semicolon,
25    /// Colon: `:`
26    Colon,
27    /// Dot: `.`
28    Dot,
29    /// Question mark: `?`
30    Question,
31    /// Addition operator: `+`
32    Plus,
33    /// Subtraction operator: `-`
34    Minus,
35    /// Multiplication operator: `*`
36    Star,
37    /// Division operator: `/`
38    Slash,
39    /// Modulo operator: `%`
40    Percent,
41    /// Assignment operator: `=`
42    Assign,
43    /// Addition assignment: `+=`
44    PlusAssign,
45    /// Subtraction assignment: `-=`
46    MinusAssign,
47    /// Multiplication assignment: `*=`
48    StarAssign,
49    /// Division assignment: `/=`
50    SlashAssign,
51    /// Modulo assignment: `%=`
52    PercentAssign,
53    /// Equality comparison: `==`
54    Equal,
55    /// Inequality comparison: `!=`
56    NotEqual,
57    /// Less than: `<`
58    Less,
59    /// Greater than: `>`
60    Greater,
61    /// Less than or equal: `<=`
62    LessEqual,
63    /// Greater than or equal: `>=`
64    GreaterEqual,
65    /// Logical AND: `&&`
66    LogicalAnd,
67    /// Logical OR: `||`
68    LogicalOr,
69    /// Logical NOT: `!`
70    LogicalNot,
71    /// Bitwise AND: `&`
72    BitAnd,
73    /// Bitwise OR: `|`
74    BitOr,
75    /// Bitwise XOR: `^`
76    BitXor,
77    /// Bitwise NOT: `~`
78    BitNot,
79    /// Left shift: `<<`
80    LeftShift,
81    /// Right shift: `>>`
82    RightShift,
83    /// Bitwise AND assignment: `&=`
84    AndAssign,
85    /// Bitwise OR assignment: `|=`
86    OrAssign,
87    /// Bitwise XOR assignment: `^=`
88    XorAssign,
89    /// Left shift assignment: `<<=`
90    LeftShiftAssign,
91    /// Right shift assignment: `>>=`
92    RightShiftAssign,
93    /// Increment: `++`
94    Increment,
95    /// Decrement: `--`
96    Decrement,
97    /// Arrow operator: `->`
98    Arrow,
99    /// `auto` keyword
100    Auto,
101    /// `register` keyword
102    Register,
103    /// `static` keyword
104    Static,
105    /// `extern` keyword
106    Extern,
107    /// `typedef` keyword
108    Typedef,
109    /// `void` keyword
110    Void,
111    /// `char` keyword
112    Char,
113    /// `short` keyword
114    Short,
115    /// `int` keyword
116    Int,
117    /// `long` keyword
118    Long,
119    /// `float` keyword
120    Float,
121    /// `double` keyword
122    Double,
123    /// `signed` keyword
124    Signed,
125    /// `unsigned` keyword
126    Unsigned,
127    /// `struct` keyword
128    Struct,
129    /// `union` keyword
130    Union,
131    /// `enum` keyword
132    Enum,
133    /// `const` keyword
134    Const,
135    /// `volatile` keyword
136    Volatile,
137    /// `restrict` keyword
138    Restrict,
139    /// `if` keyword
140    If,
141    /// `else` keyword
142    Else,
143    /// `switch` keyword
144    Switch,
145    /// `case` keyword
146    Case,
147    /// `default` keyword
148    Default,
149    /// `for` keyword
150    For,
151    /// `while` keyword
152    While,
153    /// `do` keyword
154    Do,
155    /// `break` keyword
156    Break,
157    /// `continue` keyword
158    Continue,
159    /// `goto` keyword
160    Goto,
161    /// `return` keyword
162    Return,
163    /// `sizeof` keyword
164    Sizeof,
165    /// `inline` keyword
166    Inline,
167    /// `_Bool` keyword
168    Bool,
169    /// `_Complex` keyword
170    Complex,
171    /// `_Imaginary` keyword
172    Imaginary,
173    /// `_Alignas` keyword
174    Alignas,
175    /// `_Alignof` keyword
176    Alignof,
177    /// `_Atomic` keyword
178    Atomic,
179    /// `_Static_assert` keyword
180    StaticAssert,
181    /// `_Thread_local` keyword
182    ThreadLocal,
183    /// `_Generic` keyword
184    Generic,
185    /// `_Noreturn` keyword
186    Noreturn,
187    /// Integer literal
188    IntegerLiteral,
189    /// Floating-point literal
190    FloatLiteral,
191    /// Character literal
192    CharLiteral,
193    /// String literal
194    StringLiteral,
195    /// Identifier
196    Identifier,
197    /// Whitespace
198    Whitespace,
199    /// Comments
200    Comment,
201    /// Preprocessor directives
202    PreprocessorDirective,
203    /// Text
204    Text,
205    /// Error token
206    #[default]
207    Error,
208    /// End of file marker
209    Eof,
210}
211
212impl CTokenType {
213    pub fn is_keyword(&self) -> bool {
214        matches!(
215            self,
216            Self::Auto
217                | Self::Register
218                | Self::Static
219                | Self::Extern
220                | Self::Typedef
221                | Self::Void
222                | Self::Char
223                | Self::Short
224                | Self::Int
225                | Self::Long
226                | Self::Float
227                | Self::Double
228                | Self::Signed
229                | Self::Unsigned
230                | Self::Struct
231                | Self::Union
232                | Self::Enum
233                | Self::Const
234                | Self::Volatile
235                | Self::Restrict
236                | Self::If
237                | Self::Else
238                | Self::Switch
239                | Self::Case
240                | Self::Default
241                | Self::For
242                | Self::While
243                | Self::Do
244                | Self::Break
245                | Self::Continue
246                | Self::Goto
247                | Self::Return
248                | Self::Sizeof
249                | Self::Inline
250                | Self::Bool
251                | Self::Complex
252                | Self::Imaginary
253                | Self::Alignas
254                | Self::Alignof
255                | Self::Atomic
256                | Self::StaticAssert
257                | Self::ThreadLocal
258                | Self::Generic
259                | Self::Noreturn
260        )
261    }
262}
263
264impl TokenType for CTokenType {
265    const END_OF_STREAM: Self = Self::Eof;
266    type Role = UniversalTokenRole;
267
268    fn is_ignored(&self) -> bool {
269        matches!(self, Self::Whitespace | Self::Comment)
270    }
271
272    fn is_comment(&self) -> bool {
273        matches!(self, Self::Comment)
274    }
275
276    fn is_whitespace(&self) -> bool {
277        matches!(self, Self::Whitespace)
278    }
279
280    fn role(&self) -> Self::Role {
281        use UniversalTokenRole::*;
282        match self {
283            _ if self.is_keyword() => Keyword,
284            Self::Identifier => Name,
285            Self::IntegerLiteral | Self::FloatLiteral | Self::CharLiteral | Self::StringLiteral => Literal,
286            Self::LeftParen | Self::RightParen | Self::LeftBracket | Self::RightBracket | Self::LeftBrace | Self::RightBrace | Self::Comma | Self::Semicolon | Self::Colon | Self::Dot | Self::Question => Punctuation,
287            Self::Plus
288            | Self::Minus
289            | Self::Star
290            | Self::Slash
291            | Self::Percent
292            | Self::Assign
293            | Self::PlusAssign
294            | Self::MinusAssign
295            | Self::StarAssign
296            | Self::SlashAssign
297            | Self::PercentAssign
298            | Self::Equal
299            | Self::NotEqual
300            | Self::Less
301            | Self::Greater
302            | Self::LessEqual
303            | Self::GreaterEqual
304            | Self::LogicalAnd
305            | Self::LogicalOr
306            | Self::LogicalNot
307            | Self::BitAnd
308            | Self::BitOr
309            | Self::BitXor
310            | Self::BitNot
311            | Self::LeftShift
312            | Self::RightShift
313            | Self::AndAssign
314            | Self::OrAssign
315            | Self::XorAssign
316            | Self::LeftShiftAssign
317            | Self::RightShiftAssign
318            | Self::Increment
319            | Self::Decrement
320            | Self::Arrow => Operator,
321            Self::Comment => Comment,
322            Self::Whitespace => Whitespace,
323            Self::Error => Error,
324            Self::Eof => Eof,
325            _ => None,
326        }
327    }
328}