oak_go/lexer/token_type.rs
1use core::fmt;
2use oak_core::{Source, Token, TokenType, UniversalElementRole, UniversalTokenRole};
3
4/// Go language token type.
5pub type GoToken = Token<GoTokenType>;
6
7/// Token types for the Go language.
8#[derive(Clone, Copy, PartialEq, Eq, Hash)]
9#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
10pub enum GoTokenType {
11 // Non-terminal nodes
12 /// A source file node.
13 SourceFile,
14 /// A package clause node.
15 PackageClause,
16 /// An import declaration node.
17 ImportDeclaration,
18 /// An import spec node.
19 ImportSpec,
20 /// A function declaration node.
21 FunctionDeclaration,
22 /// A parameter list node.
23 ParameterList,
24 /// A parameter declaration node.
25 ParameterDecl,
26 /// A code block node.
27 Block,
28 /// A variable declaration node.
29 VariableDeclaration,
30 /// A variable spec node.
31 VariableSpec,
32 /// A constant declaration node.
33 ConstDeclaration,
34 /// A constant spec node.
35 ConstSpec,
36 /// A type declaration node.
37 TypeDeclaration,
38 /// A type spec node.
39 TypeSpec,
40 /// A struct type node.
41 StructType,
42 /// A field declaration list node.
43 FieldDeclList,
44 /// A field declaration node.
45 FieldDecl,
46 /// An interface type node.
47 InterfaceType,
48 /// A method spec list node.
49 MethodSpecList,
50 /// A method spec node.
51 MethodSpec,
52 /// An expression list node.
53 ExpressionList,
54 /// An assignment statement node.
55 AssignmentStatement,
56 /// A short variable declaration node.
57 ShortVarDecl,
58 /// A return statement node.
59 ReturnStatement,
60 /// An if statement node.
61 IfStatement,
62 /// A for statement node.
63 ForStatement,
64 /// A switch statement node.
65 SwitchStatement,
66 /// An expression case clause node.
67 ExprCaseClause,
68 /// A type switch statement node.
69 TypeSwitchStatement,
70 /// A type case clause node.
71 TypeCaseClause,
72 /// A select statement node.
73 SelectStatement,
74 /// A communication clause node.
75 CommClause,
76 /// A go statement node.
77 GoStatement,
78 /// A defer statement node.
79 DeferStatement,
80 /// A function call expression node.
81 CallExpression,
82 /// An index expression node.
83 IndexExpression,
84 /// A selector expression node.
85 SelectorExpression,
86 /// A slice expression node.
87 SliceExpression,
88 /// A type assertion expression node.
89 TypeAssertion,
90 /// A unary expression node.
91 UnaryExpression,
92 /// A binary expression node.
93 BinaryExpression,
94 /// A literal value node.
95 LiteralValue,
96 /// An element list node.
97 ElementList,
98 /// A keyed element node.
99 KeyedElement,
100
101 // Literals
102 /// An integer literal.
103 IntLiteral,
104 /// A floating-point literal.
105 FloatLiteral,
106 /// A string literal.
107 StringLiteral,
108 /// A rune literal.
109 RuneLiteral,
110 /// A boolean literal.
111 BoolLiteral,
112
113 // Identifiers
114 /// An identifier.
115 Identifier,
116
117 // Keywords
118 /// `break` keyword.
119 Break,
120 /// `case` keyword.
121 Case,
122 /// `chan` keyword.
123 Chan,
124 /// `const` keyword.
125 Const,
126 /// `continue` keyword.
127 Continue,
128 /// `default` keyword.
129 Default,
130 /// `defer` keyword.
131 Defer,
132 /// `else` keyword.
133 Else,
134 /// `fallthrough` keyword.
135 Fallthrough,
136 /// `for` keyword.
137 For,
138 /// `func` keyword.
139 Func,
140 /// `go` keyword.
141 Go,
142 /// `goto` keyword.
143 Goto,
144 /// `if` keyword.
145 If,
146 /// `import` keyword.
147 Import,
148 /// `interface` keyword.
149 Interface,
150 /// `map` keyword.
151 Map,
152 /// `package` keyword.
153 Package,
154 /// `range` keyword.
155 Range,
156 /// `return` keyword.
157 Return,
158 /// `select` keyword.
159 Select,
160 /// `struct` keyword.
161 Struct,
162 /// `switch` keyword.
163 Switch,
164 /// `type` keyword.
165 Type,
166 /// `var` keyword.
167 Var,
168
169 // Built-in types
170 /// `bool` type.
171 Bool,
172 /// `byte` type.
173 Byte,
174 /// `complex64` type.
175 Complex64,
176 /// `complex128` type.
177 Complex128,
178 /// `error` type.
179 ErrorType,
180 /// `float32` type.
181 Float32,
182 /// `float64` type.
183 Float64,
184 /// `int` type.
185 Int,
186 /// `int8` type.
187 Int8,
188 /// `int16` type.
189 Int16,
190 /// `int32` type.
191 Int32,
192 /// `int64` type.
193 Int64,
194 /// `rune` type.
195 Rune,
196 /// `string` type.
197 String,
198 /// `uint` type.
199 Uint,
200 /// `uint8` type.
201 Uint8,
202 /// `uint16` type.
203 Uint16,
204 /// `uint32` type.
205 Uint32,
206 /// `uint64` type.
207 Uint64,
208 /// `uintptr` type.
209 Uintptr,
210
211 // Special literals
212 /// `nil` literal.
213 NilLiteral,
214 /// A number literal.
215 NumberLiteral,
216 /// A character literal.
217 CharLiteral,
218
219 // Operators
220 /// `+`.
221 Plus,
222 /// `-`.
223 Minus,
224 /// `*`.
225 Star,
226 /// `/`.
227 Slash,
228 /// `%`.
229 Percent,
230 /// `&`.
231 Ampersand,
232 /// `|`.
233 Pipe,
234 /// `^`.
235 Caret,
236 /// `<<`.
237 LeftShift,
238 /// `>>`.
239 RightShift,
240 /// `&^`.
241 AmpersandCaret,
242
243 /// `+=`.
244 PlusAssign,
245 /// `-=`.
246 MinusAssign,
247 /// `*=`.
248 StarAssign,
249 /// `/=`.
250 SlashAssign,
251 /// `%=`.
252 PercentAssign,
253 /// `&=`.
254 AmpersandAssign,
255 /// `|=`.
256 PipeAssign,
257 /// `^=`.
258 CaretAssign,
259 /// `^=` (alias).
260 XorAssign,
261 /// `<<=`.
262 LeftShiftAssign,
263 /// `>>=`.
264 RightShiftAssign,
265 /// `&^=`.
266 AmpersandCaretAssign,
267 /// `&=` (alias).
268 AndAssign,
269 /// `|=` (alias).
270 OrAssign,
271 /// `&^=` (alias).
272 AndNotAssign,
273 /// `&^` (alias).
274 AndNot,
275
276 /// `&&`.
277 LogicalAnd,
278 /// `||`.
279 LogicalOr,
280 /// `&&` (alias).
281 And,
282 /// `||` (alias).
283 Or,
284 /// `<-`.
285 Arrow,
286 /// `<-` (alias).
287 LeftArrow,
288 /// `++`.
289 Increment,
290 /// `--`.
291 Decrement,
292
293 /// `==`.
294 Equal,
295 /// `<`.
296 Less,
297 /// `>`.
298 Greater,
299 /// `=`.
300 Assign,
301 /// `!`.
302 LogicalNot,
303 /// `!` (alias).
304 Not,
305
306 /// `!=`.
307 NotEqual,
308 /// `<=`.
309 LessEqual,
310 /// `>=`.
311 GreaterEqual,
312 /// `:=`.
313 ColonAssign,
314 /// `:=` (alias).
315 Define,
316 /// `...`.
317 Ellipsis,
318
319 // Delimiters
320 /// `(`.
321 LeftParen,
322 /// `)`.
323 RightParen,
324 /// `[`.
325 LeftBracket,
326 /// `]`.
327 RightBracket,
328 /// `{`.
329 LeftBrace,
330 /// `}`.
331 RightBrace,
332 /// `,`.
333 Comma,
334 /// `.`.
335 Period,
336 /// `.` (alias).
337 Dot,
338 /// `;`.
339 Semicolon,
340 /// `:`.
341 Colon,
342
343 // Whitespace and comments
344 /// Whitespace.
345 Whitespace,
346 /// Comment.
347 Comment,
348
349 // Special
350 /// End of stream marker.
351 Eof,
352 /// Error element.
353 Error,
354}
355
356impl GoTokenType {
357 /// Returns true if this token type represents ignored trivia (whitespace or comments).
358 pub fn is_ignored(&self) -> bool {
359 matches!(self, Self::Whitespace | Self::Comment)
360 }
361
362 /// Returns true if this token type represents a Go keyword.
363 pub fn is_keyword(&self) -> bool {
364 matches!(
365 self,
366 Self::Break
367 | Self::Case
368 | Self::Chan
369 | Self::Const
370 | Self::Continue
371 | Self::Default
372 | Self::Defer
373 | Self::Else
374 | Self::Fallthrough
375 | Self::For
376 | Self::Func
377 | Self::Go
378 | Self::Goto
379 | Self::If
380 | Self::Import
381 | Self::Interface
382 | Self::Map
383 | Self::Package
384 | Self::Range
385 | Self::Return
386 | Self::Select
387 | Self::Struct
388 | Self::Switch
389 | Self::Type
390 | Self::Var
391 )
392 }
393}
394
395impl fmt::Debug for GoTokenType {
396 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
397 write!(f, "{:?}", self)
398 }
399}
400
401impl TokenType for GoTokenType {
402 type Role = UniversalTokenRole;
403 const END_OF_STREAM: Self = Self::Eof;
404
405 fn is_ignored(&self) -> bool {
406 matches!(self, Self::Whitespace | Self::Comment)
407 }
408
409 fn role(&self) -> Self::Role {
410 match self {
411 Self::Eof => UniversalTokenRole::Eof,
412 Self::Identifier => UniversalTokenRole::Name,
413 Self::StringLiteral | Self::IntLiteral | Self::FloatLiteral | Self::RuneLiteral | Self::BoolLiteral | Self::NilLiteral | Self::NumberLiteral | Self::CharLiteral => UniversalTokenRole::Literal,
414 Self::Break
415 | Self::Case
416 | Self::Chan
417 | Self::Const
418 | Self::Continue
419 | Self::Default
420 | Self::Defer
421 | Self::Else
422 | Self::Fallthrough
423 | Self::For
424 | Self::Func
425 | Self::Go
426 | Self::Goto
427 | Self::If
428 | Self::Import
429 | Self::Interface
430 | Self::Map
431 | Self::Package
432 | Self::Range
433 | Self::Return
434 | Self::Select
435 | Self::Struct
436 | Self::Switch
437 | Self::Type
438 | Self::Var => UniversalTokenRole::Keyword,
439 Self::Whitespace => UniversalTokenRole::Whitespace,
440 Self::Comment => UniversalTokenRole::Comment,
441 Self::Error => UniversalTokenRole::Error,
442 _ => UniversalTokenRole::None,
443 }
444 }
445}