我试图从后面的Rust代码中提取函数名。
// example.rs
pub mod hello {
pub mod world {
pub fn greetings() {
println!("Hello, world!")
}
}
}
以下是尝试从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);
}
问题是它打印greetings
作为输出,但我想要完全限定的名称,即hello::world::greetings
。我该怎么做?
答案 0 :(得分:3)
你不能。 Ident
只是一个名称(+有关宏扩展的一些信息)。
您可以使用访问者构建模块路径,同时实现visit_item
方法并存储当前路径:
fn visit_item(&mut self, i: &'v Item) {
self.modules.push(i.ident);
walk_item(self, i);
self.modules.pop();
}
然后你可以打印整个路径:
for ident in &self.modules {
print!("::{}", ident.name.as_str());
}
println!("");