Skip to content

Commit 82f84a2

Browse files
committed
Handle RBSLocation and RBSLocationList types
Many AST nodes in config.yml have location fields (rbs_location, rbs_location_list). This change adds the necessary wrapper structs (RBSLocation, RBSLocationList) and updates build.rs to generate accessors for these fields. The RBSLocation wrapper includes a reference to the parser to support future functionality like source extraction.
1 parent 78be623 commit 82f84a2

2 files changed

Lines changed: 82 additions & 0 deletions

File tree

rust/ruby-rbs/build.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,28 @@ fn generate(config: &Config) -> Result<(), Box<dyn Error>> {
125125
)?;
126126
writeln!(file, " }}")?;
127127
}
128+
"rbs_location" => {
129+
writeln!(file, " pub fn {}(&self) -> RBSLocation {{", field.name)?;
130+
writeln!(
131+
file,
132+
" RBSLocation::new(unsafe {{ (*self.pointer).{} }}, self.parser)",
133+
field.name
134+
)?;
135+
writeln!(file, " }}")?;
136+
}
137+
"rbs_location_list" => {
138+
writeln!(
139+
file,
140+
" pub fn {}(&self) -> RBSLocationList {{",
141+
field.name
142+
)?;
143+
writeln!(
144+
file,
145+
" RBSLocationList::new(unsafe {{ (*self.pointer).{} }}, self.parser)",
146+
field.name
147+
)?;
148+
writeln!(file, " }}")?;
149+
}
128150
_ => eprintln!("Unknown field type: {}", field.c_type),
129151
}
130152
}

rust/ruby-rbs/src/lib.rs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,66 @@ impl NodeList {
7676
}
7777
}
7878

79+
pub struct RBSLocation {
80+
pointer: *const rbs_location_t,
81+
#[allow(dead_code)]
82+
parser: *mut rbs_parser_t,
83+
}
84+
85+
impl RBSLocation {
86+
pub fn new(pointer: *const rbs_location_t, parser: *mut rbs_parser_t) -> Self {
87+
Self { pointer, parser }
88+
}
89+
90+
pub fn start_loc(&self) -> usize {
91+
unsafe { (*self.pointer).rg.start.byte_pos as usize }
92+
}
93+
94+
pub fn end_loc(&self) -> usize {
95+
unsafe { (*self.pointer).rg.end.byte_pos as usize }
96+
}
97+
}
98+
99+
pub struct RBSLocationListIter {
100+
current: *mut rbs_location_list_node_t,
101+
parser: *mut rbs_parser_t,
102+
}
103+
104+
impl Iterator for RBSLocationListIter {
105+
type Item = RBSLocation;
106+
107+
fn next(&mut self) -> Option<Self::Item> {
108+
if self.current.is_null() {
109+
None
110+
} else {
111+
let pointer_data = unsafe { *self.current };
112+
let loc = RBSLocation::new(pointer_data.loc, self.parser);
113+
self.current = pointer_data.next;
114+
Some(loc)
115+
}
116+
}
117+
}
118+
119+
pub struct RBSLocationList {
120+
pointer: *mut rbs_location_list,
121+
parser: *mut rbs_parser_t,
122+
}
123+
124+
impl RBSLocationList {
125+
pub fn new(pointer: *mut rbs_location_list, parser: *mut rbs_parser_t) -> Self {
126+
Self { pointer, parser }
127+
}
128+
129+
/// Returns an iterator over the locations.
130+
#[must_use]
131+
pub fn iter(&self) -> RBSLocationListIter {
132+
RBSLocationListIter {
133+
current: unsafe { (*self.pointer).head },
134+
parser: self.parser,
135+
}
136+
}
137+
}
138+
79139
pub struct RBSString {
80140
pointer: *const rbs_string_t,
81141
}

0 commit comments

Comments
 (0)