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