Skip to main content

oak_idl/parser/
element_type.rs

1use oak_core::{ElementType, UniversalElementRole};
2
3/// Element types for the IDL parser.
4#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
5#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
6#[repr(u8)]
7pub enum IdlElementType {
8    /// The `void` type.
9    Void,
10    /// The `boolean` type.
11    Boolean,
12    /// The `byte` type.
13    Byte,
14    /// The `octet` type.
15    Octet,
16    /// The `short` type.
17    Short,
18    /// The `unsigned short` type.
19    UnsignedShort,
20    /// The `long` type.
21    Long,
22    /// The `unsigned long` type.
23    UnsignedLong,
24    /// The `long long` type.
25    LongLong,
26    /// The `unsigned long long` type.
27    UnsignedLongLong,
28    /// The `float` type.
29    Float,
30    /// The `double` type.
31    Double,
32    /// The `long double` type.
33    LongDouble,
34    /// The `char` type.
35    Char,
36    /// The `wchar` type.
37    WChar,
38    /// The `string` type.
39    String,
40    /// The `wstring` type.
41    WString,
42    /// The `any` type.
43    Any,
44    /// The `Object` type.
45    Object,
46    /// The `ValueBase` type.
47    ValueBase,
48
49    /// A struct type.
50    Struct,
51    /// A union type.
52    Union,
53    /// An enum type.
54    Enum,
55    /// An interface type.
56    Interface,
57    /// A module type.
58    Module,
59    /// An exception type.
60    Exception,
61    /// A typedef.
62    Typedef,
63    /// A sequence type.
64    Sequence,
65    /// An array type.
66    Array,
67    /// A fixed-point type.
68    Fixed,
69
70    /// An attribute member.
71    Attribute,
72    /// An operation member.
73    Operation,
74    /// A constant member.
75    Const,
76    /// An exception member.
77    ExceptionMember,
78
79    /// A module declaration.
80    ModuleDeclaration,
81    /// An interface declaration.
82    InterfaceDeclaration,
83    /// A struct declaration.
84    StructDeclaration,
85    /// A union declaration.
86    UnionDeclaration,
87    /// An enum declaration.
88    EnumDeclaration,
89    /// A typedef declaration.
90    TypedefDeclaration,
91    /// A constant declaration.
92    ConstDeclaration,
93    /// An exception declaration.
94    ExceptionDeclaration,
95
96    /// The entire source file.
97    SourceFile,
98    /// An include directive.
99    Include,
100    /// A pragma directive.
101    Pragma,
102    /// An error element.
103    Error,
104    /// End of file.
105    Eof,
106}
107
108impl ElementType for IdlElementType {
109    type Role = UniversalElementRole;
110
111    fn role(&self) -> Self::Role {
112        match self {
113            Self::SourceFile => UniversalElementRole::Root,
114            Self::ModuleDeclaration | Self::InterfaceDeclaration | Self::StructDeclaration | Self::UnionDeclaration | Self::EnumDeclaration | Self::TypedefDeclaration | Self::ConstDeclaration | Self::ExceptionDeclaration => {
115                UniversalElementRole::Definition
116            }
117            Self::Module | Self::Interface | Self::Struct | Self::Union | Self::Enum => UniversalElementRole::Container,
118            _ => UniversalElementRole::None,
119        }
120    }
121}
122
123impl From<crate::lexer::token_type::IdlTokenType> for IdlElementType {
124    fn from(token: crate::lexer::token_type::IdlTokenType) -> Self {
125        match token {
126            crate::lexer::token_type::IdlTokenType::Void => Self::Void,
127            crate::lexer::token_type::IdlTokenType::Boolean => Self::Boolean,
128            crate::lexer::token_type::IdlTokenType::Byte => Self::Byte,
129            crate::lexer::token_type::IdlTokenType::Octet => Self::Octet,
130            crate::lexer::token_type::IdlTokenType::Short => Self::Short,
131            crate::lexer::token_type::IdlTokenType::UnsignedShort => Self::UnsignedShort,
132            crate::lexer::token_type::IdlTokenType::Long => Self::Long,
133            crate::lexer::token_type::IdlTokenType::UnsignedLong => Self::UnsignedLong,
134            crate::lexer::token_type::IdlTokenType::LongLong => Self::LongLong,
135            crate::lexer::token_type::IdlTokenType::UnsignedLongLong => Self::UnsignedLongLong,
136            crate::lexer::token_type::IdlTokenType::Float => Self::Float,
137            crate::lexer::token_type::IdlTokenType::Double => Self::Double,
138            crate::lexer::token_type::IdlTokenType::LongDouble => Self::LongDouble,
139            crate::lexer::token_type::IdlTokenType::Char => Self::Char,
140            crate::lexer::token_type::IdlTokenType::WChar => Self::WChar,
141            crate::lexer::token_type::IdlTokenType::String => Self::String,
142            crate::lexer::token_type::IdlTokenType::WString => Self::WString,
143            crate::lexer::token_type::IdlTokenType::Any => Self::Any,
144            crate::lexer::token_type::IdlTokenType::Object => Self::Object,
145            crate::lexer::token_type::IdlTokenType::ValueBase => Self::ValueBase,
146            crate::lexer::token_type::IdlTokenType::Struct => Self::Struct,
147            crate::lexer::token_type::IdlTokenType::Union => Self::Union,
148            crate::lexer::token_type::IdlTokenType::Enum => Self::Enum,
149            crate::lexer::token_type::IdlTokenType::Interface => Self::Interface,
150            crate::lexer::token_type::IdlTokenType::Module => Self::Module,
151            crate::lexer::token_type::IdlTokenType::Exception => Self::Exception,
152            crate::lexer::token_type::IdlTokenType::Typedef => Self::Typedef,
153            crate::lexer::token_type::IdlTokenType::Sequence => Self::Sequence,
154            crate::lexer::token_type::IdlTokenType::Array => Self::Array,
155            crate::lexer::token_type::IdlTokenType::Fixed => Self::Fixed,
156            crate::lexer::token_type::IdlTokenType::Const => Self::Const,
157            crate::lexer::token_type::IdlTokenType::Attribute => Self::Attribute,
158            crate::lexer::token_type::IdlTokenType::Error => Self::Error,
159            crate::lexer::token_type::IdlTokenType::Eof => Self::Eof,
160            _ => Self::Error,
161        }
162    }
163}