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    /// A field member.
79    Field,
80    /// A parameter.
81    Param,
82
83    /// A module declaration.
84    ModuleDeclaration,
85    /// An interface declaration.
86    InterfaceDeclaration,
87    /// A struct declaration.
88    StructDeclaration,
89    /// A union declaration.
90    UnionDeclaration,
91    /// An enum declaration.
92    EnumDeclaration,
93    /// A typedef declaration.
94    TypedefDeclaration,
95    /// A constant declaration.
96    ConstDeclaration,
97    /// An exception declaration.
98    ExceptionDeclaration,
99
100    /// The entire source file.
101    SourceFile,
102    /// An include directive.
103    Include,
104    /// A pragma directive.
105    Pragma,
106    /// An error element.
107    Error,
108    /// End of file.
109    Eof,
110    
111    /// An identifier.
112    Identifier,
113    /// A string literal.
114    StringLiteral,
115    /// A numeric literal.
116    NumberLiteral,
117    /// A boolean literal.
118    BooleanLiteral,
119    /// The `readonly` keyword.
120    Readonly,
121    /// The `in` keyword.
122    In,
123    /// The `out` keyword.
124    Out,
125    /// The `inout` keyword.
126    Inout,
127}
128
129impl IdlElementType {
130    /// Returns true if this is a basic type.
131    pub fn is_basic_type(&self) -> bool {
132        matches!(self, 
133            Self::Void | Self::Boolean | Self::Byte | Self::Octet | 
134            Self::Short | Self::UnsignedShort | Self::Long | Self::UnsignedLong |
135            Self::LongLong | Self::UnsignedLongLong | Self::Float | Self::Double |
136            Self::LongDouble | Self::Char | Self::WChar | Self::String | 
137            Self::WString | Self::Any | Self::Object | Self::ValueBase
138        )
139    }
140}
141
142impl ElementType for IdlElementType {
143    type Role = UniversalElementRole;
144
145    fn role(&self) -> Self::Role {
146        match self {
147            Self::SourceFile => UniversalElementRole::Root,
148            Self::ModuleDeclaration | Self::InterfaceDeclaration | Self::StructDeclaration | Self::UnionDeclaration | Self::EnumDeclaration | Self::TypedefDeclaration | Self::ConstDeclaration | Self::ExceptionDeclaration => {
149                UniversalElementRole::Definition
150            }
151            Self::Module | Self::Interface | Self::Struct | Self::Union | Self::Enum => UniversalElementRole::Container,
152            _ => UniversalElementRole::None,
153        }
154    }
155}
156
157impl From<crate::lexer::token_type::IdlTokenType> for IdlElementType {
158    fn from(token: crate::lexer::token_type::IdlTokenType) -> Self {
159        match token {
160            crate::lexer::token_type::IdlTokenType::Void => Self::Void,
161            crate::lexer::token_type::IdlTokenType::Boolean => Self::Boolean,
162            crate::lexer::token_type::IdlTokenType::Byte => Self::Byte,
163            crate::lexer::token_type::IdlTokenType::Octet => Self::Octet,
164            crate::lexer::token_type::IdlTokenType::Short => Self::Short,
165            crate::lexer::token_type::IdlTokenType::UnsignedShort => Self::UnsignedShort,
166            crate::lexer::token_type::IdlTokenType::Long => Self::Long,
167            crate::lexer::token_type::IdlTokenType::UnsignedLong => Self::UnsignedLong,
168            crate::lexer::token_type::IdlTokenType::LongLong => Self::LongLong,
169            crate::lexer::token_type::IdlTokenType::UnsignedLongLong => Self::UnsignedLongLong,
170            crate::lexer::token_type::IdlTokenType::Float => Self::Float,
171            crate::lexer::token_type::IdlTokenType::Double => Self::Double,
172            crate::lexer::token_type::IdlTokenType::LongDouble => Self::LongDouble,
173            crate::lexer::token_type::IdlTokenType::Char => Self::Char,
174            crate::lexer::token_type::IdlTokenType::WChar => Self::WChar,
175            crate::lexer::token_type::IdlTokenType::String => Self::String,
176            crate::lexer::token_type::IdlTokenType::WString => Self::WString,
177            crate::lexer::token_type::IdlTokenType::Any => Self::Any,
178            crate::lexer::token_type::IdlTokenType::Object => Self::Object,
179            crate::lexer::token_type::IdlTokenType::ValueBase => Self::ValueBase,
180            crate::lexer::token_type::IdlTokenType::Struct => Self::Struct,
181            crate::lexer::token_type::IdlTokenType::Union => Self::Union,
182            crate::lexer::token_type::IdlTokenType::Enum => Self::Enum,
183            crate::lexer::token_type::IdlTokenType::Interface => Self::Interface,
184            crate::lexer::token_type::IdlTokenType::Module => Self::Module,
185            crate::lexer::token_type::IdlTokenType::Exception => Self::Exception,
186            crate::lexer::token_type::IdlTokenType::Typedef => Self::Typedef,
187            crate::lexer::token_type::IdlTokenType::Sequence => Self::Sequence,
188            crate::lexer::token_type::IdlTokenType::Array => Self::Array,
189            crate::lexer::token_type::IdlTokenType::Fixed => Self::Fixed,
190            crate::lexer::token_type::IdlTokenType::Const => Self::Const,
191            crate::lexer::token_type::IdlTokenType::Attribute => Self::Attribute,
192            crate::lexer::token_type::IdlTokenType::Readonly => Self::Readonly,
193            crate::lexer::token_type::IdlTokenType::In => Self::In,
194            crate::lexer::token_type::IdlTokenType::Out => Self::Out,
195            crate::lexer::token_type::IdlTokenType::Inout => Self::Inout,
196            crate::lexer::token_type::IdlTokenType::Identifier => Self::Identifier,
197            crate::lexer::token_type::IdlTokenType::StringLiteral => Self::StringLiteral,
198            crate::lexer::token_type::IdlTokenType::NumberLiteral => Self::NumberLiteral,
199            crate::lexer::token_type::IdlTokenType::BooleanLiteral => Self::BooleanLiteral,
200            crate::lexer::token_type::IdlTokenType::Error => Self::Error,
201            crate::lexer::token_type::IdlTokenType::Eof => Self::Eof,
202            _ => Self::Error,
203        }
204    }
205}