Skip to main content

xidl_parser/typed_ast/
mod.rs

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