[英]How can I get the absolute name of a syntax::ast::Ident?
I'm trying to extract the function name from following Rust code. 我正在尝试从以下Rust代码中提取函数名称。
// 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
. 这是试图从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
. 问题是它打印greetings
作为输出,但我想要完全限定的名称,即hello::world::greetings
。 How do I do that? 我怎么做?
You can't. 你不能。 An Ident
is just a name (+ some info about macro expansion). Ident
只是一个名称(+有关宏扩展的一些信息)。
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: 你可以做的是使用你的访问者来构建模块路径,同时实现visit_item
方法并存储当前路径:
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!("");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.