Skip to content

Commit 3d63b58

Browse files
committed
Handle rbs_node and rbs_node_list types
This adds support for `rbs_node` and `rbs_node_list` field types in the bindings generator. This enables access to child nodes and node lists from parent AST nodes. We handle fields named `type` (which is a Rust keyword) by escaping the accessor method to `r#type()` and correctly mapping to the underlying `type_` field in the C struct.
1 parent ea7ec90 commit 3d63b58

2 files changed

Lines changed: 27 additions & 0 deletions

File tree

rust/ruby-rbs/build.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,29 @@ fn generate(config: &Config) -> Result<(), Box<dyn Error>> {
147147
)?;
148148
writeln!(file, " }}")?;
149149
}
150+
"rbs_node" => {
151+
let (function_name, field_name) = match field.name.as_str() {
152+
"type" => ("r#type", "type_"),
153+
name => (name, name),
154+
};
155+
156+
writeln!(file, " pub fn {}(&self) -> Node {{", function_name)?;
157+
writeln!(
158+
file,
159+
" unsafe {{ Node::new(self.parser, (*self.pointer).{}) }}",
160+
field_name
161+
)?;
162+
writeln!(file, " }}")?;
163+
}
164+
"rbs_node_list" => {
165+
writeln!(file, " pub fn {}(&self) -> NodeList {{", field.name)?;
166+
writeln!(
167+
file,
168+
" NodeList::new(self.parser, unsafe {{ (*self.pointer).{} }})",
169+
field.name
170+
)?;
171+
writeln!(file, " }}")?;
172+
}
150173
_ => eprintln!("Unknown field type: {}", field.c_type),
151174
}
152175
}

rust/ruby-rbs/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ pub struct NodeList {
6666
}
6767

6868
impl NodeList {
69+
pub fn new(parser: *mut rbs_parser_t, pointer: *mut rbs_node_list_t) -> Self {
70+
Self { parser, pointer }
71+
}
72+
6973
/// Returns an iterator over the nodes.
7074
#[must_use]
7175
pub fn iter(&self) -> NodeListIter {

0 commit comments

Comments
 (0)