Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[rust] add a converter from concrete nodes to generic Node #1303

Merged
merged 2 commits into from
Aug 23, 2023
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
6 changes: 6 additions & 0 deletions rust/yarp/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,12 @@ fn write_node(file: &mut File, node: &Node) -> Result<(), Box<dyn std::error::Er
writeln!(file, "}}")?;
writeln!(file)?;
writeln!(file, "impl<'pr> {}<'pr> {{", node.name)?;
writeln!(file, " /// Converts this node to a generic node.")?;
writeln!(file, " #[must_use]")?;
writeln!(file, " pub fn as_node(&self) -> Node<'pr> {{")?;
writeln!(file, " Node::{} {{ pointer: self.pointer, marker: PhantomData }}", node.name)?;
writeln!(file, " }}")?;
writeln!(file)?;
writeln!(file, " /// Returns the location of this node.")?;
writeln!(file, " #[must_use]")?;
writeln!(file, " pub fn location(&self) -> Location<'pr> {{")?;
Expand Down
16 changes: 16 additions & 0 deletions rust/yarp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,4 +308,20 @@ mod tests {

assert_eq!(visitor.count, 2);
}

#[test]
fn node_upcast_test() {
use super::Node;

let source = "module Foo; end";
let result = parse(source.as_ref());

let node = result.node();
let upcast_node = node.as_program_node().unwrap().as_node();
assert!(matches!(upcast_node, Node::ProgramNode { .. }));

let node = node.as_program_node().unwrap().statements().body().iter().next().unwrap();
let upcast_node = node.as_module_node().unwrap().as_node();
assert!(matches!(upcast_node, Node::ModuleNode { .. }));
}
}