Skip to main content

xidl_parser/typed_ast/
bitmask.rs

1//! ```js
2//! exports.rules = {
3//!   bitset_dcl: $ =>
4//!     seq(
5//!       'bitset',
6//!       $.identifier,
7//!       optional(seq(':', $.scoped_name)),
8//!       '{',
9//!       repeat($.bitfield),
10//!       '}',
11//!     ),
12//!   bitfield: $ => seq($.bitfield_spec, repeat($.identifier), ';'),
13//!   bitfield_spec: $ =>
14//!     seq(
15//!       'bitfield',
16//!       '<',
17//!       $.positive_int_const,
18//!       optional(seq(',', $.destination_type)),
19//!       '>',
20//!     ),
21//!   destination_type: $ => choice($.boolean_type, $.octet_type, $.integer_type),
22//!
23//!   bitmask_dcl: $ =>
24//!     seq(
25//!       'bitmask',
26//!       $.identifier,
27//!       '{',
28//!       commaSep($.bit_value),
29//!       optional(','),
30//!       '}',
31//!     ),
32//!   bit_value: $ => seq(repeat($.annotation_appl), $.identifier),
33//! }
34//! ```
35
36use super::*;
37use serde::{Deserialize, Serialize};
38
39#[derive(Debug, Parser, Serialize, Deserialize)]
40pub struct BitsetDcl {
41    pub ident: Identifier,
42    pub parent: Option<ScopedName>,
43    #[ts(id = "bitfield")]
44    pub field: Vec<BitField>,
45}
46
47#[derive(Debug, Parser, Serialize, Deserialize)]
48#[ts(id = "bitfield")]
49pub struct BitField {
50    pub spec: BitfieldSpec,
51    pub ident: Vec<Identifier>,
52}
53
54#[derive(Debug, Parser, Serialize, Deserialize)]
55pub struct BitfieldSpec {
56    pub pos: PositiveIntConst,
57    pub dst_ty: Option<DestinationType>,
58}
59
60#[derive(Debug, Parser, Serialize, Deserialize)]
61pub enum DestinationType {
62    BooleanType(BooleanType),
63    OctetType(OctetType),
64    IntegerType(IntegerType),
65}
66
67#[derive(Debug, Parser, Serialize, Deserialize)]
68pub struct BitmaskDcl {
69    pub ident: Identifier,
70
71    pub value: Vec<BitValue>,
72}
73
74#[derive(Debug, Serialize, Deserialize)]
75pub struct BitValue {
76    pub annotations: Vec<AnnotationAppl>,
77    pub ident: Identifier,
78}
79
80impl<'a> crate::parser::FromTreeSitter<'a> for BitValue {
81    fn from_node(
82        node: tree_sitter::Node<'a>,
83        ctx: &mut crate::parser::ParseContext<'a>,
84    ) -> crate::error::ParserResult<Self> {
85        assert_eq!(node.kind_id(), xidl_parser_derive::node_id!("bit_value"));
86        let mut annotations = Vec::new();
87        let mut ident = None;
88        for ch in node.children(&mut node.walk()) {
89            match ch.kind_id() {
90                xidl_parser_derive::node_id!("annotation_appl")
91                | xidl_parser_derive::node_id!("extend_annotation_appl") => {
92                    annotations.push(AnnotationAppl::from_node(ch, ctx)?);
93                }
94                xidl_parser_derive::node_id!("identifier") => {
95                    ident = Some(Identifier::from_node(ch, ctx)?);
96                }
97                _ => {}
98            }
99        }
100        Ok(Self {
101            annotations,
102            ident: ident.ok_or_else(|| {
103                crate::error::ParseError::UnexpectedNode(format!(
104                    "parent: {}, got: missing identifier",
105                    node.kind()
106                ))
107            })?,
108        })
109    }
110}