1use crate::typed_ast::ScopedName;
2use serde::{Deserialize, Serialize};
3
4use super::{AnnotationAppl, ConstDcl, ExceptDcl, Identifier, SimpleDeclarator, TypeDcl, TypeSpec};
5use xidl_parser_derive::Parser;
6
7#[derive(Debug, Serialize, Deserialize)]
8pub struct InterfaceDcl {
9 pub annotations: Vec<AnnotationAppl>,
10 pub decl: InterfaceDclInner,
11}
12
13#[derive(Debug, Parser, Serialize, Deserialize)]
14#[ts(transparent)]
15pub enum InterfaceDclInner {
16 InterfaceForwardDcl(InterfaceForwardDcl),
17 InterfaceDef(InterfaceDef),
18}
19
20impl<'a> crate::parser::FromTreeSitter<'a> for InterfaceDcl {
21 fn from_node(
22 node: tree_sitter::Node<'a>,
23 ctx: &mut crate::parser::ParseContext<'a>,
24 ) -> crate::error::ParserResult<Self> {
25 assert_eq!(
26 node.kind_id(),
27 xidl_parser_derive::node_id!("interface_dcl")
28 );
29 let mut annotations = vec![];
30 let mut decl = None;
31 for ch in node.children(&mut node.walk()) {
32 match ch.kind_id() {
33 xidl_parser_derive::node_id!("annotation_appl")
34 | xidl_parser_derive::node_id!("extend_annotation_appl") => {
35 annotations.push(AnnotationAppl::from_node(ch, ctx)?);
36 }
37 xidl_parser_derive::node_id!("interface_def")
38 | xidl_parser_derive::node_id!("interface_forward_dcl") => {
39 decl = Some(InterfaceDclInner::from_node(ch, ctx)?);
40 }
41 _ => {}
42 }
43 }
44 Ok(Self {
45 annotations,
46 decl: decl.ok_or_else(|| {
47 crate::error::ParseError::UnexpectedNode(format!(
48 "parent: {}, got: missing interface decl",
49 node.kind()
50 ))
51 })?,
52 })
53 }
54}
55
56#[derive(Debug, Parser, Serialize, Deserialize)]
57pub struct InterfaceForwardDcl {
58 pub kind: InterfaceKind,
59 pub ident: Identifier,
60}
61
62#[derive(Debug, Parser, Serialize, Deserialize)]
63#[ts(mark)]
64pub struct InterfaceKind;
65
66#[derive(Debug, Parser, Serialize, Deserialize)]
67pub struct InterfaceDef {
68 pub header: InterfaceHeader,
69 pub interface_body: Option<InterfaceBody>,
70}
71
72#[derive(Debug, Parser, Serialize, Deserialize)]
73pub struct InterfaceHeader {
74 pub kind: InterfaceKind,
75 pub ident: Identifier,
76 pub parent: Option<InterfaceInheritanceSpec>,
77}
78
79#[derive(Debug, Parser, Serialize, Deserialize)]
80pub struct InterfaceInheritanceSpec(pub Vec<InterfaceName>);
81
82#[derive(Debug, Parser, Serialize, Deserialize)]
83pub struct InterfaceName(pub ScopedName);
84
85#[derive(Debug, Parser, Serialize, Deserialize)]
86pub struct InterfaceBody(pub Vec<Export>);
87
88#[derive(Debug, Parser, Serialize, Deserialize)]
89pub enum Export {
90 OpDcl(OpDcl),
91 AttrDcl(AttrDcl),
92 TypeDcl(TypeDcl),
93 ConstDcl(ConstDcl),
94 ExceptDcl(ExceptDcl),
95}
96
97#[derive(Debug, Serialize, Deserialize)]
98pub struct OpDcl {
99 pub annotations: Vec<AnnotationAppl>,
100 pub ty: OpTypeSpec,
101 pub ident: Identifier,
102 pub parameter: Option<ParameterDcls>,
103 pub raises: Option<RaisesExpr>,
104}
105
106impl<'a> crate::parser::FromTreeSitter<'a> for OpDcl {
107 fn from_node(
108 node: tree_sitter::Node<'a>,
109 ctx: &mut crate::parser::ParseContext<'a>,
110 ) -> crate::error::ParserResult<Self> {
111 assert_eq!(node.kind_id(), xidl_parser_derive::node_id!("op_dcl"));
112 let mut annotations = Vec::new();
113 let mut ty = None;
114 let mut ident = None;
115 let mut parameter = None;
116 let mut raises = None;
117 for ch in node.children(&mut node.walk()) {
118 match ch.kind_id() {
119 xidl_parser_derive::node_id!("annotation_appl")
120 | xidl_parser_derive::node_id!("extend_annotation_appl") => {
121 annotations.push(AnnotationAppl::from_node(ch, ctx)?);
122 }
123 xidl_parser_derive::node_id!("op_type_spec") => {
124 ty = Some(OpTypeSpec::from_node(ch, ctx)?);
125 }
126 xidl_parser_derive::node_id!("identifier") => {
127 ident = Some(Identifier::from_node(ch, ctx)?);
128 }
129 xidl_parser_derive::node_id!("parameter_dcls") => {
130 parameter = Some(ParameterDcls::from_node(ch, ctx)?);
131 }
132 xidl_parser_derive::node_id!("raises_expr") => {
133 raises = Some(RaisesExpr::from_node(ch, ctx)?);
134 }
135 _ => {}
136 }
137 }
138 Ok(Self {
139 annotations,
140 ty: ty.ok_or_else(|| {
141 crate::error::ParseError::UnexpectedNode(format!(
142 "parent: {}, got: missing op type",
143 node.kind()
144 ))
145 })?,
146 ident: ident.ok_or_else(|| {
147 crate::error::ParseError::UnexpectedNode(format!(
148 "parent: {}, got: missing identifier",
149 node.kind()
150 ))
151 })?,
152 parameter,
153 raises,
154 })
155 }
156}
157#[derive(Debug, Serialize, Deserialize)]
158#[allow(clippy::large_enum_variant)]
159pub enum OpTypeSpec {
160 Void,
161 TypeSpec(TypeSpec),
162}
163
164impl<'a> crate::parser::FromTreeSitter<'a> for OpTypeSpec {
165 fn from_node(
166 node: tree_sitter::Node<'a>,
167 ctx: &mut crate::parser::ParseContext<'a>,
168 ) -> crate::error::ParserResult<Self> {
169 #[allow(clippy::never_loop)]
170 for ch in node.children(&mut node.walk()) {
171 if ctx.node_text(&ch)? == "void" {
172 return Ok(Self::Void);
173 }
174
175 return match ch.kind_id() {
176 xidl_parser_derive::node_id!("type_spec") => Ok(Self::TypeSpec(
177 crate::parser::FromTreeSitter::from_node(ch, ctx)?,
178 )),
179 _ => Err(crate::error::ParseError::UnexpectedNode(format!(
180 "parent: {}, got: {}",
181 node.kind(),
182 ch.kind()
183 ))),
184 };
185 }
186 unreachable!()
187 }
188}
189
190#[derive(Debug, Parser, Serialize, Deserialize)]
191pub struct ParameterDcls(pub Vec<ParamDcl>);
192
193#[derive(Debug, Parser, Serialize, Deserialize)]
194pub struct ParamDcl {
195 pub attr: Option<ParamAttribute>,
196 pub ty: TypeSpec,
197 pub declarator: SimpleDeclarator,
198}
199
200#[derive(Debug, Serialize, Deserialize)]
201pub struct ParamAttribute(pub String);
202
203impl<'a> crate::parser::FromTreeSitter<'a> for ParamAttribute {
204 fn from_node(
205 node: tree_sitter::Node<'a>,
206 ctx: &mut crate::parser::ParseContext<'a>,
207 ) -> crate::error::ParserResult<Self> {
208 assert_eq!(
209 node.kind_id(),
210 xidl_parser_derive::node_id!("param_attribute")
211 );
212
213 Ok(Self(ctx.node_text(&node)?.to_string()))
214 }
215}
216
217#[derive(Debug, Parser, Serialize, Deserialize)]
218pub struct RaisesExpr(pub Vec<ScopedName>);
219
220#[derive(Debug, Serialize, Deserialize)]
221pub struct AttrDcl {
222 pub annotations: Vec<AnnotationAppl>,
223 pub decl: AttrDclInner,
224}
225
226#[derive(Debug, Parser, Serialize, Deserialize)]
227#[ts(transparent)]
228pub enum AttrDclInner {
229 ReadonlyAttrSpec(ReadonlyAttrSpec),
230 AttrSpec(AttrSpec),
231}
232
233impl<'a> crate::parser::FromTreeSitter<'a> for AttrDcl {
234 fn from_node(
235 node: tree_sitter::Node<'a>,
236 ctx: &mut crate::parser::ParseContext<'a>,
237 ) -> crate::error::ParserResult<Self> {
238 assert_eq!(node.kind_id(), xidl_parser_derive::node_id!("attr_dcl"));
239 let mut annotations = vec![];
240 let mut decl = None;
241 for ch in node.children(&mut node.walk()) {
242 match ch.kind_id() {
243 xidl_parser_derive::node_id!("annotation_appl")
244 | xidl_parser_derive::node_id!("extend_annotation_appl") => {
245 annotations.push(AnnotationAppl::from_node(ch, ctx)?);
246 }
247 xidl_parser_derive::node_id!("readonly_attr_spec")
248 | xidl_parser_derive::node_id!("attr_spec") => {
249 decl = Some(AttrDclInner::from_node(ch, ctx)?);
250 }
251 _ => {}
252 }
253 }
254 Ok(Self {
255 annotations,
256 decl: decl.ok_or_else(|| {
257 crate::error::ParseError::UnexpectedNode(format!(
258 "parent: {}, got: missing attr decl",
259 node.kind()
260 ))
261 })?,
262 })
263 }
264}
265
266#[derive(Debug, Parser, Serialize, Deserialize)]
267pub struct ReadonlyAttrSpec {
268 pub ty: TypeSpec,
269 pub declarator: ReadonlyAttrDeclarator,
270}
271
272#[derive(Debug, Parser, Serialize, Deserialize)]
273pub enum ReadonlyAttrDeclarator {
274 SimpleDeclarator(SimpleDeclarator),
275 RaisesExpr(RaisesExpr),
276}
277
278#[derive(Debug, Parser, Serialize, Deserialize)]
279pub struct AttrSpec {
280 pub type_spec: TypeSpec,
281 pub declarator: AttrDeclarator,
282}
283
284#[derive(Debug, Serialize, Deserialize)]
285pub enum AttrDeclarator {
286 SimpleDeclarator(Vec<SimpleDeclarator>),
287 WithRaises {
288 declarator: SimpleDeclarator,
289 raises: AttrRaisesExpr,
290 },
291}
292
293impl<'a> crate::parser::FromTreeSitter<'a> for AttrDeclarator {
294 fn from_node(
295 node: tree_sitter::Node<'a>,
296 ctx: &mut crate::parser::ParseContext<'a>,
297 ) -> crate::error::ParserResult<Self> {
298 let mut declarator = vec![];
299 let mut raises = None;
300
301 for ch in node.children(&mut node.walk()) {
302 match ch.kind_id() {
303 xidl_parser_derive::node_id!("simple_declarator") => {
304 declarator.push(SimpleDeclarator::from_node(ch, ctx)?);
305 }
306 xidl_parser_derive::node_id!("attr_raises_expr") => {
307 raises = Some(AttrRaisesExpr::from_node(ch, ctx)?);
308 }
309 _ => {}
310 };
311 }
312 if let Some(raises) = raises {
313 let mut iter = declarator.into_iter();
314 let declarator = iter.next().ok_or_else(|| {
315 crate::error::ParseError::UnexpectedNode(format!(
316 "parent: {}, got: missing declarator",
317 node.kind(),
318 ))
319 })?;
320 if iter.next().is_some() {
321 return Err(crate::error::ParseError::UnexpectedNode(format!(
322 "parent: {}, got: extra declarator",
323 node.kind()
324 )));
325 }
326 Ok(Self::WithRaises { declarator, raises })
327 } else {
328 Ok(Self::SimpleDeclarator(declarator))
329 }
330 }
331}
332
333#[derive(Debug, Serialize, Deserialize)]
334pub enum AttrRaisesExpr {
335 Case1(GetExcepExpr, Option<SetExcepExpr>),
336 SetExcepExpr(SetExcepExpr),
337}
338
339#[derive(Debug, Parser, Serialize, Deserialize)]
340pub struct GetExcepExpr {
341 pub expr: ExceptionList,
342}
343
344#[derive(Debug, Parser, Serialize, Deserialize)]
345pub struct SetExcepExpr {
346 pub expr: ExceptionList,
347}
348
349#[derive(Debug, Parser, Serialize, Deserialize)]
350pub struct ExceptionList(pub Vec<ScopedName>);
351
352impl<'a> crate::parser::FromTreeSitter<'a> for AttrRaisesExpr {
353 fn from_node(
354 node: tree_sitter::Node<'a>,
355 ctx: &mut crate::parser::ParseContext<'a>,
356 ) -> crate::error::ParserResult<Self> {
357 assert_eq!(
358 node.kind_id(),
359 xidl_parser_derive::node_id!("attr_raises_expr")
360 );
361 let mut get_excep = None;
362 let mut set_excep = None;
363 for ch in node.children(&mut node.walk()) {
364 match ch.kind_id() {
365 xidl_parser_derive::node_id!("get_excep_expr") => {
366 get_excep = Some(crate::parser::FromTreeSitter::from_node(ch, ctx)?);
367 }
368 xidl_parser_derive::node_id!("set_excep_expr") => {
369 set_excep = Some(crate::parser::FromTreeSitter::from_node(ch, ctx)?);
370 }
371 _ => {}
372 }
373 }
374 if let Some(get_excep) = get_excep {
375 Ok(Self::Case1(get_excep, set_excep))
376 } else if let Some(set_excep) = set_excep {
377 Ok(Self::SetExcepExpr(set_excep))
378 } else {
379 Err(crate::error::ParseError::UnexpectedNode(format!(
380 "parent: {}, got: missing raises",
381 node.kind()
382 )))
383 }
384 }
385}