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        Ok(Self {
91            annotations,
92            decl: decl.ok_or_else(|| {
93                crate::error::ParseError::UnexpectedNode(format!(
94                    "parent: {}, got: missing type decl",
95                    node.kind()
96                ))
97            })?,
98        })
99    }
100}
101
102#[derive(Debug, Parser, Serialize, Deserialize)]
103pub struct NativeDcl {
104    pub decl: SimpleDeclarator,
105}
106
107#[derive(Debug, Parser, Serialize, Deserialize)]
108pub enum ConstrTypeDcl {
109    StructDcl(StructDcl),
110    UnionDcl(UnionDcl),
111    EnumDcl(EnumDcl),
112    BitsetDcl(BitsetDcl),
113    BitmaskDcl(BitmaskDcl),
114}
115
116#[derive(Debug, Parser, Serialize, Deserialize)]
117pub enum StructDcl {
118    StructForwardDcl(StructForwardDcl),
119    StructDef(StructDef),
120}
121
122#[derive(Debug, Parser, Serialize, Deserialize)]
123pub struct StructForwardDcl {
124    pub ident: Identifier,
125}
126
127#[derive(Debug, Parser, Serialize, Deserialize)]
128pub struct StructDef {
129    pub ident: Identifier,
130    pub parent: Vec<ScopedName>,
131    pub member: Vec<Member>,
132}
133
134#[derive(Debug, Serialize, Deserialize)]
135pub struct Member {
136    pub annotations: Vec<AnnotationAppl>,
137    pub ty: TypeSpec,
138    pub ident: Declarators,
139    pub default: Option<Default>,
140}
141
142impl<'a> crate::parser::FromTreeSitter<'a> for Member {
143    fn from_node(
144        node: tree_sitter::Node<'a>,
145        ctx: &mut crate::parser::ParseContext<'a>,
146    ) -> crate::error::ParserResult<Self> {
147        assert_eq!(node.kind_id(), xidl_parser_derive::node_id!("member"));
148        let mut annotations = Vec::new();
149        let mut ty = None;
150        let mut ident = None;
151        let mut default = None;
152        for ch in node.children(&mut node.walk()) {
153            match ch.kind_id() {
154                xidl_parser_derive::node_id!("annotation_appl")
155                | xidl_parser_derive::node_id!("extend_annotation_appl") => {
156                    annotations.push(AnnotationAppl::from_node(ch, ctx)?);
157                }
158                xidl_parser_derive::node_id!("type_spec") => {
159                    ty = Some(TypeSpec::from_node(ch, ctx)?);
160                }
161                xidl_parser_derive::node_id!("declarators") => {
162                    ident = Some(Declarators::from_node(ch, ctx)?);
163                }
164                xidl_parser_derive::node_id!("default") => {
165                    default = Some(Default::from_node(ch, ctx)?);
166                }
167                _ => {}
168            }
169        }
170        Ok(Self {
171            annotations,
172            ty: ty.ok_or_else(|| {
173                crate::error::ParseError::UnexpectedNode(format!(
174                    "parent: {}, got: missing type",
175                    node.kind()
176                ))
177            })?,
178            ident: ident.ok_or_else(|| {
179                crate::error::ParseError::UnexpectedNode(format!(
180                    "parent: {}, got: missing declarators",
181                    node.kind()
182                ))
183            })?,
184            default,
185        })
186    }
187}
188
189#[derive(Debug, Parser, Serialize, Deserialize)]
190pub struct Default(pub ConstExpr);
191
192#[derive(Debug, Parser, Serialize, Deserialize)]
193pub struct ConstDcl {
194    pub ty: ConstType,
195    pub ident: Identifier,
196    pub value: ConstExpr,
197}
198
199#[derive(Debug, Parser, Serialize, Deserialize)]
200pub enum ConstType {
201    IntegerType(IntegerType),
202    FloatingPtType(FloatingPtType),
203    FixedPtConstType(FixedPtConstType),
204    CharType(CharType),
205    WideCharType(WideCharType),
206    BooleanType(BooleanType),
207    OctetType(OctetType),
208    StringType(StringType),
209    WideStringType(WideStringType),
210    ScopedName(ScopedName),
211    SequenceType(SequenceType),
212}
213
214#[derive(Debug, Clone, PartialEq, Parser, Serialize, Deserialize)]
215#[ts(transparent)]
216pub struct Identifier(pub String);
217
218#[derive(Debug, Clone, Parser, Serialize, Deserialize)]
219pub struct PositiveIntConst(pub ConstExpr);