Skip to content

Commit aa65a27

Browse files
committed
Refactor Symbol and Keyword as nodes (#67)
Refactor the previous implementation of `Symbol`/`Keyword` handling to treat them as first-class nodes in the build configuration. `Keyword` and `Symbol` represent identifiers (interned strings), not traditional AST nodes. However, the C parser defines them in `rbs_node_type` (as `RBS_KEYWORD` and `RBS_AST_SYMBOL`) and treats them as nodes (`rbs_node_t*`) in many contexts (lists, hashes). Instead of manually defining `RBSSymbol`/`RBSKeyword` structs, we now inject them into the `config.yml` node list in `build.rs`. This allows them to be generated as `SymbolNode`/`KeywordNode` variants in the `Node` enum, enabling polymorphic handling (in Node lists and Hashes)
1 parent b2b88ba commit aa65a27

2 files changed

Lines changed: 22 additions & 24 deletions

File tree

rust/ruby-rbs/build.rs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,22 @@ fn main() -> Result<(), Box<dyn Error>> {
3737
let config_file = File::open(&config_path)?;
3838
let mut config: Config = serde_yaml::from_reader(config_file)?;
3939

40+
// Keyword and Symbol represent identifiers (interned strings), not traditional AST nodes.
41+
// However, the C parser defines them in `rbs_node_type` (RBS_KEYWORD, RBS_AST_SYMBOL) and
42+
// treats them as nodes (rbs_node_t*) in many contexts (lists, hashes).
43+
// We inject them into the config so they are generated as structs matching the Node pattern,
44+
// allowing them to be wrapped in the Node enum and handled uniformly in Rust.
45+
config.nodes.push(Node {
46+
name: "RBS::Keyword".to_string(),
47+
rust_name: "KeywordNode".to_string(),
48+
fields: None,
49+
});
50+
config.nodes.push(Node {
51+
name: "RBS::AST::Symbol".to_string(),
52+
rust_name: "SymbolNode".to_string(),
53+
fields: None,
54+
});
55+
4056
config.nodes.sort_by(|a, b| a.name.cmp(&b.name));
4157
generate(&config)?;
4258

@@ -148,10 +164,10 @@ fn generate(config: &Config) -> Result<(), Box<dyn Error>> {
148164
writeln!(file, " }}")?;
149165
}
150166
"rbs_ast_symbol" => {
151-
writeln!(file, " pub fn {}(&self) -> RBSSymbol {{", field.name)?;
167+
writeln!(file, " pub fn {}(&self) -> SymbolNode {{", field.name)?;
152168
writeln!(
153169
file,
154-
" RBSSymbol::new(unsafe {{ (*self.pointer).{} }}, self.parser)",
170+
" SymbolNode {{ parser: self.parser, pointer: unsafe {{ (*self.pointer).{} }} }}",
155171
field.c_name()
156172
)?;
157173
writeln!(file, " }}")?;
@@ -212,10 +228,10 @@ fn generate(config: &Config) -> Result<(), Box<dyn Error>> {
212228
writeln!(file, " }}")?;
213229
}
214230
"rbs_keyword" => {
215-
writeln!(file, " pub fn {}(&self) -> RBSKeyword {{", field.name)?;
231+
writeln!(file, " pub fn {}(&self) -> KeywordNode {{", field.name)?;
216232
writeln!(
217233
file,
218-
" RBSKeyword::new(self.parser, unsafe {{ (*self.pointer).{} }})",
234+
" KeywordNode {{ parser: self.parser, pointer: unsafe {{ (*self.pointer).{} }} }}",
219235
field.c_name()
220236
)?;
221237
writeln!(file, " }}")?;

rust/ruby-rbs/src/lib.rs

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -169,16 +169,7 @@ impl RBSString {
169169
}
170170
}
171171

172-
pub struct RBSSymbol {
173-
pointer: *const rbs_ast_symbol_t,
174-
parser: *mut rbs_parser_t,
175-
}
176-
177-
impl RBSSymbol {
178-
pub fn new(pointer: *const rbs_ast_symbol_t, parser: *mut rbs_parser_t) -> Self {
179-
Self { pointer, parser }
180-
}
181-
172+
impl SymbolNode {
182173
pub fn name(&self) -> &[u8] {
183174
unsafe {
184175
let constant_ptr = rbs_constant_pool_id_to_constant(
@@ -195,16 +186,7 @@ impl RBSSymbol {
195186
}
196187
}
197188

198-
pub struct RBSKeyword {
199-
parser: *mut rbs_parser_t,
200-
pointer: *const rbs_keyword,
201-
}
202-
203-
impl RBSKeyword {
204-
pub fn new(parser: *mut rbs_parser_t, pointer: *const rbs_keyword) -> Self {
205-
Self { parser, pointer }
206-
}
207-
189+
impl KeywordNode {
208190
pub fn name(&self) -> &[u8] {
209191
unsafe {
210192
let constant_ptr = rbs_constant_pool_id_to_constant(

0 commit comments

Comments
 (0)