2012-01-15 19:23:19 -06:00
|
|
|
import rustc::syntax::ast;
|
2012-01-15 23:45:13 -06:00
|
|
|
import rustc::syntax::visit;
|
|
|
|
|
|
|
|
export extract;
|
2012-01-15 19:23:19 -06:00
|
|
|
|
|
|
|
#[doc = "Converts the Rust AST to the rustdoc document model"]
|
2012-01-15 23:45:13 -06:00
|
|
|
fn extract(
|
2012-01-16 02:19:35 -06:00
|
|
|
crate: @ast::crate,
|
|
|
|
default_name: str
|
2012-01-15 23:45:13 -06:00
|
|
|
) -> doc::cratedoc {
|
|
|
|
~{
|
2012-01-16 02:19:35 -06:00
|
|
|
topmod: top_moddoc_from_crate(crate, default_name),
|
2012-01-15 23:45:13 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn top_moddoc_from_crate(
|
2012-01-16 02:19:35 -06:00
|
|
|
crate: @ast::crate,
|
|
|
|
default_name: str
|
2012-01-15 23:45:13 -06:00
|
|
|
) -> doc::moddoc {
|
2012-01-16 02:19:35 -06:00
|
|
|
moddoc_from_mod(crate.node.module, default_name, crate.node.attrs)
|
2012-01-15 23:45:13 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn moddoc_from_mod(
|
|
|
|
module: ast::_mod,
|
|
|
|
name: ast::ident,
|
|
|
|
_attrs: [ast::attribute]
|
|
|
|
) -> doc::moddoc {
|
|
|
|
~{
|
|
|
|
name: name,
|
|
|
|
mods: doc::modlist(
|
|
|
|
vec::filter_map(module.items) {|item|
|
|
|
|
alt item.node {
|
|
|
|
ast::item_mod(m) {
|
|
|
|
some(moddoc_from_mod(m, item.ident, item.attrs))
|
|
|
|
}
|
|
|
|
_ {
|
|
|
|
none
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
fns: doc::fnlist(
|
|
|
|
vec::filter_map(module.items) {|item|
|
|
|
|
alt item.node {
|
|
|
|
ast::item_fn(decl, typarams, _) {
|
|
|
|
some(fndoc_from_fn(
|
2012-01-16 02:15:03 -06:00
|
|
|
decl, typarams, item.ident, item.id, item.attrs))
|
2012-01-15 23:45:13 -06:00
|
|
|
}
|
|
|
|
_ {
|
|
|
|
none
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn fndoc_from_fn(
|
|
|
|
_decl: ast::fn_decl,
|
|
|
|
_typarams: [ast::ty_param],
|
|
|
|
name: ast::ident,
|
2012-01-16 02:15:03 -06:00
|
|
|
id: ast::node_id,
|
2012-01-16 00:49:57 -06:00
|
|
|
attrs: [ast::attribute]
|
2012-01-15 23:45:13 -06:00
|
|
|
) -> doc::fndoc {
|
2012-01-16 02:15:03 -06:00
|
|
|
attr_parser::parse_fn(name, id, attrs)
|
2012-01-15 19:23:19 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn extract_empty_crate() {
|
|
|
|
let source = ""; // empty crate
|
|
|
|
let ast = parse::from_str(source);
|
2012-01-16 02:19:35 -06:00
|
|
|
let doc = extract(ast, "");
|
2012-01-15 23:45:13 -06:00
|
|
|
// FIXME #1535: These are boxed to prevent a crash
|
|
|
|
assert ~doc.topmod.mods == ~doc::modlist([]);
|
|
|
|
assert ~doc.topmod.fns == ~doc::fnlist([]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn extract_mods() {
|
|
|
|
let source = "mod a { mod b { } mod c { } }";
|
|
|
|
let ast = parse::from_str(source);
|
2012-01-16 02:19:35 -06:00
|
|
|
let doc = extract(ast, "");
|
2012-01-15 23:45:13 -06:00
|
|
|
assert doc.topmod.mods[0].name == "a";
|
|
|
|
assert doc.topmod.mods[0].mods[0].name == "b";
|
|
|
|
assert doc.topmod.mods[0].mods[1].name == "c";
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn extract_mods_deep() {
|
|
|
|
let source = "mod a { mod b { mod c { } } }";
|
|
|
|
let ast = parse::from_str(source);
|
2012-01-16 02:19:35 -06:00
|
|
|
let doc = extract(ast, "");
|
2012-01-15 23:45:13 -06:00
|
|
|
assert doc.topmod.mods[0].mods[0].mods[0].name == "c";
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn extract_fns() {
|
|
|
|
let source =
|
|
|
|
"fn a() { } \
|
|
|
|
mod b { fn c() { } }";
|
|
|
|
let ast = parse::from_str(source);
|
2012-01-16 02:19:35 -06:00
|
|
|
let doc = extract(ast, "");
|
2012-01-15 23:45:13 -06:00
|
|
|
assert doc.topmod.fns[0].name == "a";
|
|
|
|
assert doc.topmod.mods[0].fns[0].name == "c";
|
2012-01-15 19:23:19 -06:00
|
|
|
}
|
2012-01-16 02:15:03 -06:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn extract_should_set_fn_ast_id() {
|
|
|
|
let source = "fn a() { }";
|
|
|
|
let ast = parse::from_str(source);
|
2012-01-16 02:19:35 -06:00
|
|
|
let doc = extract(ast, "");
|
2012-01-16 02:15:03 -06:00
|
|
|
assert doc.topmod.fns[0].id != 0;
|
|
|
|
}
|
2012-01-16 02:19:35 -06:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn extract_should_use_default_crate_name_if_no_link_name_exists() {
|
|
|
|
let source = "";
|
|
|
|
let ast = parse::from_str(source);
|
|
|
|
let doc = extract(ast, "burp");
|
|
|
|
assert doc.topmod.name == "burp";
|
|
|
|
}
|
2012-01-15 19:23:19 -06:00
|
|
|
}
|