Skip to main content

oak_nim/parser/
element_type.rs

1use oak_core::{ElementType, UniversalElementRole};
2
3/// Element types for the Nim language.
4#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
5#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
6#[repr(u8)]
7pub enum NimElementType {
8    // Whitespace and comments
9    /// Whitespace characters.
10    Whitespace,
11    /// A newline character.
12    Newline,
13    /// A comment token.
14    CommentToken,
15
16    // Keywords
17    /// The `addr` keyword.
18    AddrKeyword,
19    /// The `and` keyword.
20    AndKeyword,
21    /// The `as` keyword.
22    AsKeyword,
23    /// The `asm` keyword.
24    AsmKeyword,
25    /// The `bind` keyword.
26    BindKeyword,
27    /// The `block` keyword.
28    BlockKeyword,
29    /// The `break` keyword.
30    BreakKeyword,
31    /// The `case` keyword.
32    CaseKeyword,
33    /// The `cast` keyword.
34    CastKeyword,
35    /// The `concept` keyword.
36    ConceptKeyword,
37    /// The `const` keyword.
38    ConstKeyword,
39    /// The `continue` keyword.
40    ContinueKeyword,
41    /// The `converter` keyword.
42    ConverterKeyword,
43    /// The `defer` keyword.
44    DeferKeyword,
45    /// The `discard` keyword.
46    DiscardKeyword,
47    /// The `distinct` keyword.
48    DistinctKeyword,
49    /// The `div` keyword.
50    DivKeyword,
51    /// The `do` keyword.
52    DoKeyword,
53    /// The `elif` keyword.
54    ElifKeyword,
55    /// The `else` keyword.
56    ElseKeyword,
57    /// The `end` keyword.
58    EndKeyword,
59    /// The `enum` keyword.
60    EnumKeyword,
61    /// The `except` keyword.
62    ExceptKeyword,
63    /// The `export` keyword.
64    ExportKeyword,
65    /// The `finally` keyword.
66    FinallyKeyword,
67    /// The `for` keyword.
68    ForKeyword,
69    /// The `from` keyword.
70    FromKeyword,
71    /// The `func` keyword.
72    FuncKeyword,
73    /// The `if` keyword.
74    IfKeyword,
75    /// The `import` keyword.
76    ImportKeyword,
77    /// The `in` keyword.
78    InKeyword,
79    /// The `include` keyword.
80    IncludeKeyword,
81    /// The `interface` keyword.
82    InterfaceKeyword,
83    /// The `is` keyword.
84    IsKeyword,
85    /// The `iterator` keyword.
86    IteratorKeyword,
87    /// The `let` keyword.
88    LetKeyword,
89    /// The `macro` keyword.
90    MacroKeyword,
91    /// The `method` keyword.
92    MethodKeyword,
93    /// The `mixin` keyword.
94    MixinKeyword,
95    /// The `mod` keyword.
96    ModKeyword,
97    /// The `nil` keyword.
98    NilKeyword,
99    /// The `not` keyword.
100    NotKeyword,
101    /// The `notnil` keyword.
102    NotnilKeyword,
103    /// The `object` keyword.
104    ObjectKeyword,
105    /// The `of` keyword.
106    OfKeyword,
107    /// The `or` keyword.
108    OrKeyword,
109    /// The `out` keyword.
110    OutKeyword,
111    /// The `proc` keyword.
112    ProcKeyword,
113    /// The `ptr` keyword.
114    PtrKeyword,
115    /// The `raise` keyword.
116    RaiseKeyword,
117    /// The `ref` keyword.
118    RefKeyword,
119    /// The `return` keyword.
120    ReturnKeyword,
121    /// The `shl` keyword.
122    ShlKeyword,
123    /// The `shr` keyword.
124    ShrKeyword,
125    /// The `static` keyword.
126    StaticKeyword,
127    /// The `template` keyword.
128    TemplateKeyword,
129    /// The `try` keyword.
130    TryKeyword,
131    /// The `tuple` keyword.
132    TupleKeyword,
133    /// The `type` keyword.
134    TypeKeyword,
135    /// The `using` keyword.
136    UsingKeyword,
137    /// The `var` keyword.
138    VarKeyword,
139    /// The `when` keyword.
140    WhenKeyword,
141    /// The `while` keyword.
142    WhileKeyword,
143    /// The `xor` keyword.
144    XorKeyword,
145    /// The `yield` keyword.
146    YieldKeyword,
147
148    // Operators
149    /// The `+` operator.
150    Plus,
151    /// The `-` operator.
152    Minus,
153    /// The `*` operator.
154    Star,
155    /// The `/` operator.
156    Slash,
157    /// The `%` operator.
158    Percent,
159    /// The `=` operator.
160    Equal,
161    /// The `==` operator.
162    EqualEqual,
163    /// The `!=` operator.
164    NotEqual,
165    /// The `<` operator.
166    Less,
167    /// The `<=` operator.
168    LessEqual,
169    /// The `>` operator.
170    Greater,
171    /// The `>=` operator.
172    GreaterEqual,
173    /// The `&` operator.
174    Ampersand,
175    /// The `|` operator.
176    Pipe,
177    /// The `^` operator.
178    Caret,
179    /// The `~` operator.
180    Tilde,
181    /// The `<<` operator.
182    LeftShift,
183    /// The `>>` operator.
184    RightShift,
185    /// The `..` operator.
186    DotDot,
187    /// The `->` operator.
188    Arrow,
189    /// The `@` operator.
190    At,
191
192    // Punctuation
193    /// An opening parenthesis `(`.
194    LeftParen,
195    /// A closing parenthesis `)`.
196    RightParen,
197    /// An opening bracket `[`.
198    LeftBracket,
199    /// A closing bracket `]`.
200    RightBracket,
201    /// An opening brace `{`.
202    LeftBrace,
203    /// A closing brace `}`.
204    RightBrace,
205    /// A comma `,`.
206    Comma,
207    /// A semicolon `;`.
208    Semicolon,
209    /// A colon `:`.
210    Colon,
211    /// A dot `.`.
212    Dot,
213    /// A question mark `?`.
214    Question,
215    /// An exclamation mark `!`.
216    Exclamation,
217    /// A dollar sign `$`.
218    Dollar,
219    /// A backtick `` ` ``.
220    Backtick,
221
222    // Literals
223    /// An integer literal.
224    IntLiteral,
225    /// A floating-point literal.
226    FloatLiteral,
227    /// A string literal.
228    StringLiteral,
229    /// A character literal.
230    CharLiteral,
231    /// A boolean literal.
232    BoolLiteral,
233
234    // Identifiers
235    /// An identifier.
236    Identifier,
237
238    // Special
239    /// Root node of the AST.
240    Root,
241    /// Procedure declaration.
242    ProcDecl,
243    /// Variable declaration.
244    VarDecl,
245    /// Let declaration.
246    LetDecl,
247    /// Constant declaration.
248    ConstDecl,
249    /// Type declaration.
250    TypeDecl,
251    /// If statement.
252    IfStmt,
253    /// While statement.
254    WhileStmt,
255    /// For statement.
256    ForStmt,
257    /// Case statement.
258    CaseStmt,
259    /// When statement (compile-time).
260    WhenStmt,
261    /// Static statement (compile-time).
262    StaticStmt,
263    /// Block statement.
264    BlockStmt,
265    /// An expression.
266    Expression,
267    /// A literal.
268    Literal,
269    /// A comment.
270    Comment,
271    /// Import declaration.
272    ImportDecl,
273    /// An error node in the AST.
274    ErrorNode,
275    /// An error element.
276    Error,
277    /// End of stream.
278    Eof,
279}
280
281impl NimElementType {
282    /// Returns true if the element type is a token.
283    pub fn is_token(&self) -> bool {
284        !self.is_element()
285    }
286
287    /// Returns true if the element type is a non-terminal element.
288    pub fn is_element(&self) -> bool {
289        matches!(self, Self::Root | Self::ProcDecl | Self::TypeDecl | Self::VarDecl | Self::ConstDecl | Self::LetDecl | Self::ImportDecl | Self::Comment | Self::ErrorNode)
290    }
291}
292
293impl ElementType for NimElementType {
294    type Role = UniversalElementRole;
295
296    fn role(&self) -> Self::Role {
297        match self {
298            Self::Root => UniversalElementRole::Root,
299            Self::ProcDecl | Self::VarDecl | Self::LetDecl | Self::ConstDecl | Self::TypeDecl => UniversalElementRole::Definition,
300            Self::ImportDecl => UniversalElementRole::Metadata,
301            Self::IfStmt | Self::WhileStmt | Self::ForStmt | Self::CaseStmt | Self::BlockStmt => UniversalElementRole::Statement,
302            Self::Expression => UniversalElementRole::Expression,
303            Self::Comment => UniversalElementRole::Documentation,
304            Self::ErrorNode | Self::Error => UniversalElementRole::Error,
305            _ => UniversalElementRole::None,
306        }
307    }
308}
309
310impl From<crate::lexer::token_type::NimTokenType> for NimElementType {
311    fn from(token: crate::lexer::token_type::NimTokenType) -> Self {
312        unsafe { std::mem::transmute(token) }
313    }
314}