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