Skip to main content

oak_go/parser/
element_type.rs

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