I'm trying to extract the function name from following Rust code.
// example.rs
pub mod hello {
pub mod world {
pub fn greetings() {
println!("Hello, world!")
}
}
}
Here is code which tries to extract function name from example.rs
.
//runner.rs
/*
* This program will only compile with nightly Rust
*
* To compile
* rustc runner.rs
*
* To run
* LD_LIBRARY_PATH=$(rustc --print sysroot)/lib ./runner
*/
#![feature(rustc_private)]
extern crate syntax;
use syntax::visit::{ self, Visitor, FnKind };
use syntax::ast::{ FnDecl, Block, NodeId, Mac };
use syntax::codemap::{ Span };
use syntax::{ parse, ast };
use std::path::Path;
struct MyVisitor;
impl<'x> Visitor<'x> for MyVisitor {
fn visit_fn<'v>(&mut self, fk: FnKind<'v>, fd: &'v FnDecl, b: &'v Block, s: Span, _: NodeId) {
let name;
match fk {
visit::FnKind::Method(_ident, ref _method_sig, _option) => {
name = (*_ident.name.as_str()).to_string();
}
visit::FnKind::ItemFn(_ident, ref _generics, _unsafety, _constness, _abi, _visibility) => {
name = (*_ident.name.as_str()).to_string();
}
visit::FnKind::Closure => {
name = "".to_string();
}
};
println!("{}", name);
visit::walk_fn(self, fk, fd, b, s);
}
fn visit_mac<'v>(&mut self, _mac: &'v Mac) {
// do nothing
// just overriding here because parent panics as
// panic!("visit_mac disabled by default");
}
}
fn build_crate(path: &std::path::Path) -> ast::Crate {
let sess = parse::ParseSess::new();
let filemap = sess.codemap().load_file(path).unwrap();
let cfg = ast::CrateConfig::new();
let reader = parse::lexer::StringReader::new(&sess.span_diagnostic, filemap);
let mut parser = parse::parser::Parser::new(&sess, cfg, Box::new(reader));
return parser.parse_crate_mod().unwrap();
}
fn main() {
let krate = build_crate(Path::new("./example.rs"));
let mut visitor = MyVisitor {};
visit::walk_crate(&mut visitor, &krate);
}
The problem is that it prints greetings
as output, but I want the fully qualified name, ie, hello::world::greetings
. How do I do that?
You can't. An Ident
is just a name (+ some info about macro expansion).
What you can do is use your visitor to build the module path, by also implementing the visit_item
method and storing the current path:
fn visit_item(&mut self, i: &'v Item) {
self.modules.push(i.ident);
walk_item(self, i);
self.modules.pop();
}
Then you can print the entire path:
for ident in &self.modules {
print!("::{}", ident.name.as_str());
}
println!("");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.