Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions rust/ruby-rbs/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,22 @@ fn main() -> Result<(), Box<dyn Error>> {
let config_file = File::open(&config_path)?;
let mut config: Config = serde_yaml::from_reader(config_file)?;

// Keyword and Symbol represent identifiers (interned strings), not traditional AST nodes.
// However, the C parser defines them in `rbs_node_type` (RBS_KEYWORD, RBS_AST_SYMBOL) and
// treats them as nodes (rbs_node_t*) in many contexts (lists, hashes).
// We inject them into the config so they are generated as structs matching the Node pattern,
// allowing them to be wrapped in the Node enum and handled uniformly in Rust.
config.nodes.push(Node {
name: "RBS::Keyword".to_string(),
rust_name: "KeywordNode".to_string(),
fields: None,
});
config.nodes.push(Node {
name: "RBS::AST::Symbol".to_string(),
rust_name: "SymbolNode".to_string(),
fields: None,
});

config.nodes.sort_by(|a, b| a.name.cmp(&b.name));
generate(&config)?;

Expand Down Expand Up @@ -148,10 +164,10 @@ fn generate(config: &Config) -> Result<(), Box<dyn Error>> {
writeln!(file, " }}")?;
}
"rbs_ast_symbol" => {
writeln!(file, " pub fn {}(&self) -> RBSSymbol {{", field.name)?;
writeln!(file, " pub fn {}(&self) -> SymbolNode {{", field.name)?;
writeln!(
file,
" RBSSymbol::new(unsafe {{ (*self.pointer).{} }}, self.parser)",
" SymbolNode {{ parser: self.parser, pointer: unsafe {{ (*self.pointer).{} }} }}",
field.c_name()
)?;
writeln!(file, " }}")?;
Expand Down Expand Up @@ -212,10 +228,10 @@ fn generate(config: &Config) -> Result<(), Box<dyn Error>> {
writeln!(file, " }}")?;
}
"rbs_keyword" => {
writeln!(file, " pub fn {}(&self) -> RBSKeyword {{", field.name)?;
writeln!(file, " pub fn {}(&self) -> KeywordNode {{", field.name)?;
writeln!(
file,
" RBSKeyword::new(self.parser, unsafe {{ (*self.pointer).{} }})",
" KeywordNode {{ parser: self.parser, pointer: unsafe {{ (*self.pointer).{} }} }}",
field.c_name()
)?;
writeln!(file, " }}")?;
Expand Down
22 changes: 2 additions & 20 deletions rust/ruby-rbs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,16 +169,7 @@ impl RBSString {
}
}

pub struct RBSSymbol {
pointer: *const rbs_ast_symbol_t,
parser: *mut rbs_parser_t,
}

impl RBSSymbol {
pub fn new(pointer: *const rbs_ast_symbol_t, parser: *mut rbs_parser_t) -> Self {
Self { pointer, parser }
}

impl SymbolNode {
pub fn name(&self) -> &[u8] {
unsafe {
let constant_ptr = rbs_constant_pool_id_to_constant(
Expand All @@ -195,16 +186,7 @@ impl RBSSymbol {
}
}

pub struct RBSKeyword {
parser: *mut rbs_parser_t,
pointer: *const rbs_keyword,
}

impl RBSKeyword {
pub fn new(parser: *mut rbs_parser_t, pointer: *const rbs_keyword) -> Self {
Self { parser, pointer }
}

impl KeywordNode {
pub fn name(&self) -> &[u8] {
unsafe {
let constant_ptr = rbs_constant_pool_id_to_constant(
Expand Down
Loading