Skip to main content

xidl_parser/typed_ast/
mod.rs

1mod base_types;
2pub use base_types::*;
3use serde::Serialize;
4
5mod expr;
6pub use expr::*;
7use xidl_parser_derive::Parser;
8
9mod annotation;
10pub use annotation::*;
11
12mod annotation_builtin;
13pub use annotation_builtin::*;
14
15mod preproc;
16pub use preproc::*;
17
18mod import_dcl;
19pub use import_dcl::*;
20
21mod bitmask;
22pub use bitmask::*;
23
24mod interface;
25pub use interface::*;
26
27mod union;
28pub use union::*;
29
30mod typedef_dcl_imp;
31pub use typedef_dcl_imp::*;
32
33mod module_dcl;
34pub use module_dcl::*;
35
36mod exception_dcl;
37pub use exception_dcl::*;
38
39mod template_module;
40pub use template_module::*;
41
42#[derive(Debug, Parser, Serialize)]
43pub struct Specification(pub Vec<Definition>);
44
45#[derive(Debug, Parser, Serialize)]
46pub enum Definition {
47    ModuleDcl(ModuleDcl),
48    TypeDcl(TypeDcl),
49    ConstDcl(ConstDcl),
50    ExceptDcl(ExceptDcl),
51    InterfaceDcl(InterfaceDcl),
52    TemplateModuleDcl(TemplateModuleDcl),
53    TemplateModuleInst(TemplateModuleInst),
54    ImportDcl(ImportDcl),
55    PreprocInclude(PreprocInclude),
56    PreprocCall(PreprocCall),
57    PreprocDefine(PreprocDefine),
58}
59
60#[derive(Debug, Serialize)]
61pub struct TypeDcl {
62    pub annotations: Vec<AnnotationAppl>,
63    pub decl: TypeDclInner,
64}
65
66#[derive(Debug, Parser, Serialize)]
67#[ts(transparent)]
68#[allow(clippy::large_enum_variant)]
69pub enum TypeDclInner {
70    ConstrTypeDcl(ConstrTypeDcl),
71    NativeDcl(NativeDcl),
72    TypedefDcl(TypedefDcl),
73}
74
75impl<'a> crate::parser::FromTreeSitter<'a> for TypeDcl {
76    fn from_node(
77        node: tree_sitter::Node<'a>,
78        ctx: &mut crate::parser::ParseContext<'a>,
79    ) -> crate::error::ParserResult<Self> {
80        assert_eq!(node.kind_id(), xidl_parser_derive::node_id!("type_dcl"));
81        let mut annotations = Vec::new();
82        let mut decl = None;
83        for ch in node.children(&mut node.walk()) {
84            match ch.kind_id() {
85                xidl_parser_derive::node_id!("annotation_appl")
86                | xidl_parser_derive::node_id!("extend_annotation_appl") => {
87                    annotations.push(AnnotationAppl::from_node(ch, ctx)?);
88                }
89                xidl_parser_derive::node_id!("constr_type_dcl")
90                | xidl_parser_derive::node_id!("native_dcl")
91                | xidl_parser_derive::node_id!("typedef_dcl") => {
92                    decl = Some(TypeDclInner::from_node(ch, ctx)?);
93                }
94                _ => {}
95            }
96        }
97        if let Some(doc) = ctx.take_doc_comment(&node) {
98            annotations.insert(0, AnnotationAppl::doc(doc));
99        }
100        Ok(Self {
101            annotations,
102            decl: decl.ok_or_else(|| {
103                crate::error::ParseError::UnexpectedNode(format!(
104                    "parent: {}, got: missing type decl",
105                    node.kind()
106                ))
107            })?,
108        })
109    }
110}
111
112#[derive(Debug, Parser, Serialize)]
113pub struct NativeDcl {
114    pub decl: SimpleDeclarator,
115}
116
117#[derive(Debug, Parser, Serialize)]
118pub enum ConstrTypeDcl {
119    StructDcl(StructDcl),
120    UnionDcl(UnionDcl),
121    EnumDcl(EnumDcl),
122    BitsetDcl(BitsetDcl),
123    BitmaskDcl(BitmaskDcl),
124}
125
126#[derive(Debug, Parser, Serialize)]
127pub enum StructDcl {
128    StructForwardDcl(StructForwardDcl),
129    StructDef(StructDef),
130}
131
132#[derive(Debug, Parser, Serialize)]
133pub struct StructForwardDcl {
134    pub ident: Identifier,
135}
136
137#[derive(Debug, Parser, Serialize)]
138pub struct StructDef {
139    pub ident: Identifier,
140    pub parent: Vec<ScopedName>,
141    pub member: Vec<Member>,
142}
143
144#[derive(Debug, Serialize)]
145pub struct Member {
146    pub annotations: Vec<AnnotationAppl>,
147    pub ty: TypeSpec,
148    pub ident: Declarators,
149    pub default: Option<Default>,
150}
151
152impl<'a> crate::parser::FromTreeSitter<'a> for Member {
153    fn from_node(
154        node: tree_sitter::Node<'a>,
155        ctx: &mut crate::parser::ParseContext<'a>,
156    ) -> crate::error::ParserResult<Self> {
157        assert_eq!(node.kind_id(), xidl_parser_derive::node_id!("member"));
158        let mut annotations = Vec::new();
159        let mut ty = None;
160        let mut ident = None;
161        let mut default = None;
162        for ch in node.children(&mut node.walk()) {
163            match ch.kind_id() {
164                xidl_parser_derive::node_id!("annotation_appl")
165                | xidl_parser_derive::node_id!("extend_annotation_appl") => {
166                    annotations.push(AnnotationAppl::from_node(ch, ctx)?);
167                }
168                xidl_parser_derive::node_id!("type_spec") => {
169                    ty = Some(TypeSpec::from_node(ch, ctx)?);
170                }
171                xidl_parser_derive::node_id!("declarators") => {
172                    ident = Some(Declarators::from_node(ch, ctx)?);
173                }
174                xidl_parser_derive::node_id!("default") => {
175                    default = Some(Default::from_node(ch, ctx)?);
176                }
177                _ => {}
178            }
179        }
180        if let Some(doc) = ctx.take_doc_comment(&node) {
181            annotations.insert(0, AnnotationAppl::doc(doc));
182        }
183        Ok(Self {
184            annotations,
185            ty: ty.ok_or_else(|| {
186                crate::error::ParseError::UnexpectedNode(format!(
187                    "parent: {}, got: missing type",
188                    node.kind()
189                ))
190            })?,
191            ident: ident.ok_or_else(|| {
192                crate::error::ParseError::UnexpectedNode(format!(
193                    "parent: {}, got: missing declarators",
194                    node.kind()
195                ))
196            })?,
197            default,
198        })
199    }
200}
201
202#[derive(Debug, Parser, Serialize)]
203pub struct Default(pub ConstExpr);
204
205#[derive(Debug, Serialize)]
206pub struct ConstDcl {
207    pub annotations: Vec<AnnotationAppl>,
208    pub ty: ConstType,
209    pub ident: Identifier,
210    pub value: ConstExpr,
211}
212
213impl<'a> crate::parser::FromTreeSitter<'a> for ConstDcl {
214    fn from_node(
215        node: tree_sitter::Node<'a>,
216        ctx: &mut crate::parser::ParseContext<'a>,
217    ) -> crate::error::ParserResult<Self> {
218        assert_eq!(node.kind_id(), xidl_parser_derive::node_id!("const_dcl"));
219        let mut annotations = Vec::new();
220        let mut ty = None;
221        let mut ident = None;
222        let mut value = None;
223        for ch in node.children(&mut node.walk()) {
224            match ch.kind_id() {
225                xidl_parser_derive::node_id!("annotation_appl")
226                | xidl_parser_derive::node_id!("extend_annotation_appl") => {
227                    annotations.push(AnnotationAppl::from_node(ch, ctx)?);
228                }
229                xidl_parser_derive::node_id!("const_type") => {
230                    ty = Some(ConstType::from_node(ch, ctx)?);
231                }
232                xidl_parser_derive::node_id!("identifier") => {
233                    ident = Some(Identifier::from_node(ch, ctx)?);
234                }
235                xidl_parser_derive::node_id!("const_expr") => {
236                    value = Some(ConstExpr::from_node(ch, ctx)?);
237                }
238                _ => {}
239            }
240        }
241        if let Some(doc) = ctx.take_doc_comment(&node) {
242            annotations.insert(0, AnnotationAppl::doc(doc));
243        }
244        Ok(Self {
245            annotations,
246            ty: ty.ok_or_else(|| {
247                crate::error::ParseError::UnexpectedNode(format!(
248                    "parent: {}, got: missing const type",
249                    node.kind()
250                ))
251            })?,
252            ident: ident.ok_or_else(|| {
253                crate::error::ParseError::UnexpectedNode(format!(
254                    "parent: {}, got: missing identifier",
255                    node.kind()
256                ))
257            })?,
258            value: value.ok_or_else(|| {
259                crate::error::ParseError::UnexpectedNode(format!(
260                    "parent: {}, got: missing const value",
261                    node.kind()
262                ))
263            })?,
264        })
265    }
266}
267
268#[derive(Debug, Parser, Serialize)]
269pub enum ConstType {
270    IntegerType(IntegerType),
271    FloatingPtType(FloatingPtType),
272    FixedPtConstType(FixedPtConstType),
273    CharType(CharType),
274    WideCharType(WideCharType),
275    BooleanType(BooleanType),
276    OctetType(OctetType),
277    StringType(StringType),
278    WideStringType(WideStringType),
279    ScopedName(ScopedName),
280    SequenceType(SequenceType),
281}
282
283#[derive(Debug, Clone, PartialEq, Parser, Serialize)]
284#[ts(transparent)]
285pub struct Identifier(pub String);
286
287#[derive(Debug, Clone, Parser, Serialize)]
288pub struct PositiveIntConst(pub ConstExpr);