Skip to main content

oak_go/lexer/
token_type.rs

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