Skip to main content

oak_voml/parser/
element_type.rs

1//! Voml element types.
2
3use oak_core::{ElementType, UniversalElementRole};
4
5/// Enum representing all possible element types in Voml.
6#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
7#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
8#[repr(u16)]
9pub enum VomlElementType {
10    /// Root node of the AST.
11    Root,
12    /// A source file containing VOML content.
13    SourceFile,
14    /// A module definition.
15    Module,
16    /// A function definition.
17    Function,
18    /// A memory allocation.
19    Memory,
20    /// An export declaration.
21    Export,
22    /// An import declaration.
23    Import,
24    /// A function parameter.
25    Param,
26    /// A function result.
27    Result,
28    /// A local variable.
29    Local,
30    /// A single instruction.
31    Instruction,
32
33    /// Integer type (int).
34    Int,
35    /// Unsigned integer type (uint).
36    Uint,
37    /// 32-bit floating point type (f32).
38    F32,
39    /// 64-bit floating point type (f64).
40    F64,
41    /// String type.
42    String,
43    /// Rune (Unicode character) type.
44    Rune,
45    /// Byte type.
46    Byte,
47    /// Void pointer type.
48    Voidptr,
49    /// Character type.
50    Char,
51    /// Boolean type.
52    Bool,
53
54    /// An identifier.
55    Identifier,
56    /// A numeric literal.
57    Number,
58    /// A boolean literal (true/false).
59    Boolean,
60
61    /// Left parenthesis (().
62    LeftParen,
63    /// Right parenthesis ()).
64    RightParen,
65    /// Left bracket ([).
66    LeftBracket,
67    /// Right bracket (]).
68    RightBracket,
69    /// Left brace ({).
70    LeftBrace,
71    /// Right brace (}).
72    RightBrace,
73    /// Dot separator (.).
74    Dot,
75    /// Comma separator (,).
76    Comma,
77    /// Colon separator (:).
78    Colon,
79    /// Semicolon separator (;).
80    Semicolon,
81
82    /// Whitespace characters.
83    Whitespace,
84    /// A comment.
85    Comment,
86    /// An error node representing a syntax error.
87    Error,
88    /// End of file marker.
89    Eof,
90}
91
92impl ElementType for VomlElementType {
93    type Role = UniversalElementRole;
94
95    fn role(&self) -> Self::Role {
96        match self {
97            Self::Root | Self::SourceFile => UniversalElementRole::Root,
98            Self::Module | Self::Function | Self::Memory | Self::Export | Self::Import => UniversalElementRole::Statement,
99            Self::Int | Self::Uint | Self::F32 | Self::F64 | Self::String | Self::Rune | Self::Byte | Self::Voidptr | Self::Char | Self::Bool => UniversalElementRole::Typing,
100            Self::Identifier => UniversalElementRole::Reference,
101            Self::Number | Self::Boolean => UniversalElementRole::Value,
102            Self::Error => UniversalElementRole::Error,
103            _ => UniversalElementRole::None,
104        }
105    }
106}
107
108impl From<crate::lexer::token_type::VomlTokenType> for VomlElementType {
109    fn from(token: crate::lexer::token_type::VomlTokenType) -> Self {
110        unsafe { std::mem::transmute(token) }
111    }
112}