Skip to main content

oak_zig/parser/
element_type.rs

1use oak_core::{ElementType, UniversalElementRole};
2
3/// Zig element types.
4#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
5#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
6pub enum ZigElementType {
7    /// Root element.
8    Root,
9    /// Whitespace characters.
10    Whitespace,
11    /// Newline character.
12    Newline,
13    /// Comment.
14    Comment,
15    /// Documentation comment.
16    DocComment,
17    /// Error token.
18    Error,
19    /// End of file.
20    Eof,
21
22    /// Identifier.
23    Identifier,
24    /// String literal.
25    StringLiteral,
26    /// Character literal.
27    CharLiteral,
28    /// Integer literal.
29    IntegerLiteral,
30    /// Floating-point literal.
31    FloatLiteral,
32    /// Boolean literal.
33    BooleanLiteral,
34    /// Generic literal.
35    Literal,
36
37    /// `const` keyword.
38    Const,
39    /// `var` keyword.
40    Var,
41    /// `fn` keyword.
42    Fn,
43    /// `struct` keyword.
44    Struct,
45    /// `union` keyword.
46    Union,
47    /// `enum` keyword.
48    Enum,
49    /// `opaque` keyword.
50    Opaque,
51    /// `type` keyword.
52    Type,
53    /// `comptime` keyword.
54    Comptime,
55    /// `inline` keyword.
56    Inline,
57    /// `noinline` keyword.
58    NoInline,
59    /// `pub` keyword.
60    Pub,
61    /// `export` keyword.
62    Export,
63    /// `extern` keyword.
64    Extern,
65    /// `packed` keyword.
66    Packed,
67    /// `align` keyword.
68    Align,
69    /// `callconv` keyword.
70    CallConv,
71    /// `linksection` keyword.
72    LinkSection,
73
74    /// `if` keyword.
75    If,
76    /// `else` keyword.
77    Else,
78    /// `switch` keyword.
79    Switch,
80    /// `while` keyword.
81    While,
82    /// `for` keyword.
83    For,
84    /// `break` keyword.
85    Break,
86    /// `continue` keyword.
87    Continue,
88    /// `return` keyword.
89    Return,
90    /// `defer` keyword.
91    Defer,
92    /// `errdefer` keyword.
93    ErrDefer,
94    /// `unreachable` keyword.
95    Unreachable,
96    /// `noreturn` keyword.
97    NoReturn,
98
99    /// `error` keyword.
100    ErrorKeyword,
101
102    /// `test` keyword.
103    Test,
104    /// `async` keyword.
105    Async,
106    /// `await` keyword.
107    Await,
108    /// `suspend` keyword.
109    Suspend,
110    /// `resume` keyword.
111    Resume,
112    /// `cancel` keyword.
113    Cancel,
114
115    /// `undefined` keyword.
116    Undefined,
117    /// `null` keyword.
118    Null,
119    /// `volatile` keyword.
120    Volatile,
121    /// `allowzero` keyword.
122    AllowZero,
123    /// `noalias` keyword.
124    NoAlias,
125
126    /// `and` keyword.
127    And,
128    /// `or` keyword.
129    Or,
130    /// `anyframe` keyword.
131    AnyFrame,
132    /// `anytype` keyword.
133    AnyType,
134    /// `threadlocal` keyword.
135    ThreadLocal,
136
137    /// `bool` type.
138    Bool,
139    /// `i8` type.
140    I8,
141    /// `i16` type.
142    I16,
143    /// `i32` type.
144    I32,
145    /// `i64` type.
146    I64,
147    /// `i128` type.
148    I128,
149    /// `isize` type.
150    Isize,
151    /// `u8` type.
152    U8,
153    /// `u16` type.
154    U16,
155    /// `u32` type.
156    U32,
157    /// `u64` type.
158    U64,
159    /// `u128` type.
160    U128,
161    /// `usize` type.
162    Usize,
163    /// `f16` type.
164    F16,
165    /// `f32` type.
166    F32,
167    /// `f64` type.
168    F64,
169    /// `f80` type.
170    F80,
171    /// `f128` type.
172    F128,
173    /// `c_short` type.
174    CShort,
175    /// `c_ushort` type.
176    CUshort,
177    /// `c_int` type.
178    CInt,
179    /// `c_uint` type.
180    CUint,
181    /// `c_long` type.
182    CLong,
183    /// `c_ulong` type.
184    CUlong,
185    /// `c_longlong` type.
186    CLongLong,
187    /// `c_ulonglong` type.
188    CUlongLong,
189    /// `c_longdouble` type.
190    CLongDouble,
191    /// `c_void` type.
192    CVoid,
193    /// `void` type.
194    Void,
195    /// `comptime_int` type.
196    ComptimeInt,
197    /// `comptime_float` type.
198    ComptimeFloat,
199
200    /// `+` operator.
201    Plus,
202    /// `-` operator.
203    Minus,
204    /// `*` operator.
205    Star,
206    /// `/` operator.
207    Slash,
208    /// `%` operator.
209    Percent,
210    /// `**` operator.
211    StarStar,
212    /// `+%` operator.
213    PlusPercent,
214    /// `-%` operator.
215    MinusPercent,
216    /// `*%` operator.
217    StarPercent,
218    /// `++` operator.
219    PlusPlus,
220    /// `--` operator.
221    MinusMinus,
222
223    /// `&` operator.
224    Ampersand,
225    /// `|` operator.
226    Pipe,
227    /// `^` operator.
228    Caret,
229    /// `~` operator.
230    Tilde,
231    /// `<<` operator.
232    LessLess,
233    /// `>>` operator.
234    GreaterGreater,
235
236    /// `==` operator.
237    Equal,
238    /// `!=` operator.
239    NotEqual,
240    /// `<` operator.
241    Less,
242    /// `>` operator.
243    Greater,
244    /// `<=` operator.
245    LessEqual,
246    /// `>=` operator.
247    GreaterEqual,
248
249    /// `and` logical operator.
250    AndAnd,
251    /// `or` logical operator.
252    OrOr,
253
254    /// `=` operator.
255    Assign,
256    /// `+=` operator.
257    PlusAssign,
258    /// `-=` operator.
259    MinusAssign,
260    /// `*=` operator.
261    StarAssign,
262    /// `/=` operator.
263    SlashAssign,
264    /// `%=` operator.
265    PercentAssign,
266    /// `&=` operator.
267    AmpersandAssign,
268    /// `|=` operator.
269    PipeAssign,
270    /// `^=` operator.
271    CaretAssign,
272    /// `<<=` operator.
273    LessLessAssign,
274    /// `>>=` operator.
275    GreaterGreaterAssign,
276
277    /// `(` symbol.
278    LeftParen,
279    /// `)` symbol.
280    RightParen,
281    /// `{` symbol.
282    LeftBrace,
283    /// `}` symbol.
284    RightBrace,
285    /// `[` symbol.
286    LeftBracket,
287    /// `]` symbol.
288    RightBracket,
289    /// `;` symbol.
290    Semicolon,
291    /// `,` symbol.
292    Comma,
293    /// `.` symbol.
294    Dot,
295    /// `..` symbol.
296    DotDot,
297    /// `...` symbol.
298    DotDotDot,
299    /// `.?` operator.
300    DotQuestion,
301    /// `.*` operator.
302    DotStar,
303    /// `:` symbol.
304    Colon,
305    /// `?` symbol.
306    Question,
307    /// `!` symbol.
308    Exclamation,
309    /// `->` operator.
310    Arrow,
311    /// `=>` operator.
312    FatArrow,
313
314    /// `orelse` operator.
315    OrElse,
316    /// `catch` operator.
317    CatchKeyword,
318    /// `try` operator.
319    TryKeyword,
320    /// `await` operator.
321    AwaitKeyword,
322
323    /// `@` symbol.
324    At,
325    /// Built-in identifier.
326    BuiltinIdentifier,
327
328    /// Start of a string literal.
329    StringStart,
330    /// End of a string literal.
331    StringEnd,
332    /// Content of a string literal.
333    StringContent,
334    /// Start of string interpolation.
335    InterpolationStart,
336    /// End of string interpolation.
337    InterpolationEnd,
338
339    /// Start of a multiline string.
340    MultilineStringStart,
341    /// End of a multiline string.
342    MultilineStringEnd,
343    /// Content of a multiline string.
344    MultilineStringContent,
345
346    /// Compile-time directive.
347    CompileDirective,
348
349    /// Text content.
350    Text,
351
352    /// Function declaration.
353    FnDeclaration,
354    /// Variable declaration.
355    VarDeclaration,
356    /// Struct declaration.
357    StructDeclaration,
358    /// Enum declaration.
359    EnumDeclaration,
360    /// Union declaration.
361    UnionDeclaration,
362    /// If statement.
363    IfStatement,
364    /// While statement.
365    WhileStatement,
366    /// For statement.
367    ForStatement,
368    /// Return statement.
369    ReturnStatement,
370    /// Block of code.
371    Block,
372    /// Binary expression.
373    BinaryExpr,
374    /// Unary expression.
375    UnaryExpr,
376
377    /// Container field.
378    ContainerField,
379    /// Break statement.
380    BreakStatement,
381    /// Continue statement.
382    ContinueStatement,
383    /// Defer statement.
384    DeferStatement,
385}
386
387impl ElementType for ZigElementType {
388    type Role = UniversalElementRole;
389
390    fn role(&self) -> Self::Role {
391        match self {
392            _ => UniversalElementRole::None,
393        }
394    }
395}
396
397impl From<crate::lexer::token_type::ZigTokenType> for ZigElementType {
398    fn from(token: crate::lexer::token_type::ZigTokenType) -> Self {
399        use crate::lexer::token_type::ZigTokenType as T;
400        match token {
401            T::Root => ZigElementType::Root,
402            T::Whitespace => ZigElementType::Whitespace,
403            T::Newline => ZigElementType::Newline,
404            T::Comment => ZigElementType::Comment,
405            T::DocComment => ZigElementType::DocComment,
406            T::Error => ZigElementType::Error,
407            T::Eof => ZigElementType::Eof,
408            T::Identifier => ZigElementType::Identifier,
409            T::StringLiteral => ZigElementType::StringLiteral,
410            T::CharLiteral => ZigElementType::CharLiteral,
411            T::IntegerLiteral => ZigElementType::IntegerLiteral,
412            T::FloatLiteral => ZigElementType::FloatLiteral,
413            T::BooleanLiteral => ZigElementType::BooleanLiteral,
414            T::Const => ZigElementType::Const,
415            T::Var => ZigElementType::Var,
416            T::Fn => ZigElementType::Fn,
417            T::Struct => ZigElementType::Struct,
418            T::Union => ZigElementType::Union,
419            T::Enum => ZigElementType::Enum,
420            T::Opaque => ZigElementType::Opaque,
421            T::Type => ZigElementType::Type,
422            T::Comptime => ZigElementType::Comptime,
423            T::Inline => ZigElementType::Inline,
424            T::NoInline => ZigElementType::NoInline,
425            T::Pub => ZigElementType::Pub,
426            T::Export => ZigElementType::Export,
427            T::Extern => ZigElementType::Extern,
428            T::Packed => ZigElementType::Packed,
429            T::Align => ZigElementType::Align,
430            T::CallConv => ZigElementType::CallConv,
431            T::LinkSection => ZigElementType::LinkSection,
432            T::If => ZigElementType::If,
433            T::Else => ZigElementType::Else,
434            T::Switch => ZigElementType::Switch,
435            T::While => ZigElementType::While,
436            T::For => ZigElementType::For,
437            T::Break => ZigElementType::Break,
438            T::Continue => ZigElementType::Continue,
439            T::Return => ZigElementType::Return,
440            T::Defer => ZigElementType::Defer,
441            T::ErrDefer => ZigElementType::ErrDefer,
442            T::Unreachable => ZigElementType::Unreachable,
443            T::NoReturn => ZigElementType::NoReturn,
444            T::ErrorKeyword => ZigElementType::ErrorKeyword,
445            T::Test => ZigElementType::Test,
446            T::Async => ZigElementType::Async,
447            T::Await => ZigElementType::Await,
448            T::Suspend => ZigElementType::Suspend,
449            T::Resume => ZigElementType::Resume,
450            T::Cancel => ZigElementType::Cancel,
451            T::Undefined => ZigElementType::Undefined,
452            T::Null => ZigElementType::Null,
453            T::Volatile => ZigElementType::Volatile,
454            T::AllowZero => ZigElementType::AllowZero,
455            T::NoAlias => ZigElementType::NoAlias,
456            T::And => ZigElementType::And,
457            T::Or => ZigElementType::Or,
458            T::AnyFrame => ZigElementType::AnyFrame,
459            T::AnyType => ZigElementType::AnyType,
460            T::ThreadLocal => ZigElementType::ThreadLocal,
461            T::Bool => ZigElementType::Bool,
462            T::I8 => ZigElementType::I8,
463            T::I16 => ZigElementType::I16,
464            T::I32 => ZigElementType::I32,
465            T::I64 => ZigElementType::I64,
466            T::I128 => ZigElementType::I128,
467            T::Isize => ZigElementType::Isize,
468            T::U8 => ZigElementType::U8,
469            T::U16 => ZigElementType::U16,
470            T::U32 => ZigElementType::U32,
471            T::U64 => ZigElementType::U64,
472            T::U128 => ZigElementType::U128,
473            T::Usize => ZigElementType::Usize,
474            T::F16 => ZigElementType::F16,
475            T::F32 => ZigElementType::F32,
476            T::F64 => ZigElementType::F64,
477            T::F80 => ZigElementType::F80,
478            T::F128 => ZigElementType::F128,
479            T::CShort => ZigElementType::CShort,
480            T::CUshort => ZigElementType::CUshort,
481            T::CInt => ZigElementType::CInt,
482            T::CUint => ZigElementType::CUint,
483            T::CLong => ZigElementType::CLong,
484            T::CUlong => ZigElementType::CUlong,
485            T::CLongLong => ZigElementType::CLongLong,
486            T::CUlongLong => ZigElementType::CUlongLong,
487            T::CLongDouble => ZigElementType::CLongDouble,
488            T::CVoid => ZigElementType::CVoid,
489            T::Void => ZigElementType::Void,
490            T::ComptimeInt => ZigElementType::ComptimeInt,
491            T::ComptimeFloat => ZigElementType::ComptimeFloat,
492            T::Plus => ZigElementType::Plus,
493            T::Minus => ZigElementType::Minus,
494            T::Star => ZigElementType::Star,
495            T::Slash => ZigElementType::Slash,
496            T::Percent => ZigElementType::Percent,
497            T::StarStar => ZigElementType::StarStar,
498            T::PlusPercent => ZigElementType::PlusPercent,
499            T::MinusPercent => ZigElementType::MinusPercent,
500            T::StarPercent => ZigElementType::StarPercent,
501            T::PlusPlus => ZigElementType::PlusPlus,
502            T::MinusMinus => ZigElementType::MinusMinus,
503            T::Ampersand => ZigElementType::Ampersand,
504            T::Pipe => ZigElementType::Pipe,
505            T::Caret => ZigElementType::Caret,
506            T::Tilde => ZigElementType::Tilde,
507            T::LessLess => ZigElementType::LessLess,
508            T::GreaterGreater => ZigElementType::GreaterGreater,
509            T::Equal => ZigElementType::Equal,
510            T::NotEqual => ZigElementType::NotEqual,
511            T::Less => ZigElementType::Less,
512            T::Greater => ZigElementType::Greater,
513            T::LessEqual => ZigElementType::LessEqual,
514            T::GreaterEqual => ZigElementType::GreaterEqual,
515            T::AndAnd => ZigElementType::AndAnd,
516            T::OrOr => ZigElementType::OrOr,
517            T::Assign => ZigElementType::Assign,
518            T::PlusAssign => ZigElementType::PlusAssign,
519            T::MinusAssign => ZigElementType::MinusAssign,
520            T::StarAssign => ZigElementType::StarAssign,
521            T::SlashAssign => ZigElementType::SlashAssign,
522            T::PercentAssign => ZigElementType::PercentAssign,
523            T::AmpersandAssign => ZigElementType::AmpersandAssign,
524            T::PipeAssign => ZigElementType::PipeAssign,
525            T::CaretAssign => ZigElementType::CaretAssign,
526            T::LessLessAssign => ZigElementType::LessLessAssign,
527            T::GreaterGreaterAssign => ZigElementType::GreaterGreaterAssign,
528            T::LeftParen => ZigElementType::LeftParen,
529            T::RightParen => ZigElementType::RightParen,
530            T::LeftBrace => ZigElementType::LeftBrace,
531            T::RightBrace => ZigElementType::RightBrace,
532            T::LeftBracket => ZigElementType::LeftBracket,
533            T::RightBracket => ZigElementType::RightBracket,
534            T::Semicolon => ZigElementType::Semicolon,
535            T::Comma => ZigElementType::Comma,
536            T::Dot => ZigElementType::Dot,
537            T::DotDot => ZigElementType::DotDot,
538            T::DotDotDot => ZigElementType::DotDotDot,
539            T::DotQuestion => ZigElementType::DotQuestion,
540            T::DotStar => ZigElementType::DotStar,
541            T::Colon => ZigElementType::Colon,
542            T::Question => ZigElementType::Question,
543            T::Exclamation => ZigElementType::Exclamation,
544            T::Arrow => ZigElementType::Arrow,
545            T::FatArrow => ZigElementType::FatArrow,
546            T::OrElse => ZigElementType::OrElse,
547            T::CatchKeyword => ZigElementType::CatchKeyword,
548            T::TryKeyword => ZigElementType::TryKeyword,
549            T::AwaitKeyword => ZigElementType::AwaitKeyword,
550            T::At => ZigElementType::At,
551            T::BuiltinIdentifier => ZigElementType::BuiltinIdentifier,
552            T::StringStart => ZigElementType::StringStart,
553            T::StringEnd => ZigElementType::StringEnd,
554            T::StringContent => ZigElementType::StringContent,
555            T::InterpolationStart => ZigElementType::InterpolationStart,
556            T::InterpolationEnd => ZigElementType::InterpolationEnd,
557            T::MultilineStringStart => ZigElementType::MultilineStringStart,
558            T::MultilineStringEnd => ZigElementType::MultilineStringEnd,
559            T::MultilineStringContent => ZigElementType::MultilineStringContent,
560            T::CompileDirective => ZigElementType::CompileDirective,
561            T::Text => ZigElementType::Text,
562            T::FnDeclaration => ZigElementType::FnDeclaration,
563            T::VarDeclaration => ZigElementType::VarDeclaration,
564            T::StructDeclaration => ZigElementType::StructDeclaration,
565            T::EnumDeclaration => ZigElementType::EnumDeclaration,
566            T::UnionDeclaration => ZigElementType::UnionDeclaration,
567            T::Block => ZigElementType::Block,
568            T::IfStatement => ZigElementType::IfStatement,
569            T::WhileStatement => ZigElementType::WhileStatement,
570            T::ForStatement => ZigElementType::ForStatement,
571            T::ReturnStatement => ZigElementType::ReturnStatement,
572        }
573    }
574}