rustdoc: Build mod and fn nodes from the AST
This commit is contained in:
parent
b9cd983f36
commit
46a662ecb2
3 changed files with 129 additions and 20 deletions
|
@ -1,14 +1,21 @@
|
||||||
type cratedoc = {
|
type cratedoc = ~{
|
||||||
mods: [moddoc]
|
topmod: moddoc,
|
||||||
};
|
};
|
||||||
|
|
||||||
type moddoc = {
|
type moddoc = ~{
|
||||||
fns: [fndoc]
|
name: str,
|
||||||
|
mods: modlist,
|
||||||
|
fns: fnlist
|
||||||
};
|
};
|
||||||
|
|
||||||
type fndoc = {
|
type fndoc = ~{
|
||||||
|
name: str,
|
||||||
brief: str,
|
brief: str,
|
||||||
desc: option::t<str>,
|
desc: option::t<str>,
|
||||||
return: option::t<str>,
|
return: option::t<str>,
|
||||||
args: map::hashmap<str, str>
|
args: map::hashmap<str, str>
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Just to break the structural recursive types
|
||||||
|
tag modlist = [moddoc];
|
||||||
|
tag fnlist = [fndoc];
|
||||||
|
|
|
@ -1,11 +1,69 @@
|
||||||
#[doc = "Converting the Rust AST to the rustdoc document model"];
|
|
||||||
|
|
||||||
import rustc::syntax::ast;
|
import rustc::syntax::ast;
|
||||||
|
import rustc::syntax::visit;
|
||||||
|
|
||||||
|
export extract;
|
||||||
|
|
||||||
#[doc = "Converts the Rust AST to the rustdoc document model"]
|
#[doc = "Converts the Rust AST to the rustdoc document model"]
|
||||||
fn extract(crate: @ast::crate) -> doc::cratedoc {
|
fn extract(
|
||||||
{
|
crate: @ast::crate
|
||||||
mods: []
|
) -> doc::cratedoc {
|
||||||
|
~{
|
||||||
|
topmod: top_moddoc_from_crate(crate),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn top_moddoc_from_crate(
|
||||||
|
crate: @ast::crate
|
||||||
|
) -> doc::moddoc {
|
||||||
|
moddoc_from_mod(crate.node.module, "crate", crate.node.attrs)
|
||||||
|
}
|
||||||
|
|
||||||
|
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(
|
||||||
|
decl, typarams, item.ident, item.attrs))
|
||||||
|
}
|
||||||
|
_ {
|
||||||
|
none
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn fndoc_from_fn(
|
||||||
|
_decl: ast::fn_decl,
|
||||||
|
_typarams: [ast::ty_param],
|
||||||
|
name: ast::ident,
|
||||||
|
_attrs: [ast::attribute]
|
||||||
|
) -> doc::fndoc {
|
||||||
|
~{
|
||||||
|
name: name,
|
||||||
|
brief: "todo",
|
||||||
|
desc: none,
|
||||||
|
return: none,
|
||||||
|
args: map::new_str_hash::<str>()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -17,6 +75,37 @@ mod tests {
|
||||||
let source = ""; // empty crate
|
let source = ""; // empty crate
|
||||||
let ast = parse::from_str(source);
|
let ast = parse::from_str(source);
|
||||||
let doc = extract(ast);
|
let doc = extract(ast);
|
||||||
assert doc.mods == [];
|
// 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);
|
||||||
|
let doc = extract(ast);
|
||||||
|
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);
|
||||||
|
let doc = extract(ast);
|
||||||
|
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);
|
||||||
|
let doc = extract(ast);
|
||||||
|
assert doc.topmod.fns[0].name == "a";
|
||||||
|
assert doc.topmod.mods[0].fns[0].name == "c";
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -80,7 +80,12 @@ fn parse_compound_fndoc(items: [@ast::meta_item]) -> doc::fndoc {
|
||||||
none. { "_undocumented_" }
|
none. { "_undocumented_" }
|
||||||
};
|
};
|
||||||
|
|
||||||
{ brief: _brief, desc: desc, return: return, args: argdocs }
|
~{
|
||||||
|
name: "todo",
|
||||||
|
brief: _brief,
|
||||||
|
desc: desc,
|
||||||
|
return: return,
|
||||||
|
args: argdocs }
|
||||||
}
|
}
|
||||||
|
|
||||||
#[doc(
|
#[doc(
|
||||||
|
@ -95,10 +100,13 @@ fn doc_item(rd: rustdoc, item: @ast::item) {
|
||||||
alt attr.node.value.node {
|
alt attr.node.value.node {
|
||||||
ast::meta_name_value(
|
ast::meta_name_value(
|
||||||
"doc", {node: ast::lit_str(value), span: _}) {
|
"doc", {node: ast::lit_str(value), span: _}) {
|
||||||
_fndoc = some({ brief: value,
|
_fndoc = some(~{
|
||||||
desc: none,
|
name: "todo",
|
||||||
return: none,
|
brief: value,
|
||||||
args: noargdocs });
|
desc: none,
|
||||||
|
return: none,
|
||||||
|
args: noargdocs
|
||||||
|
});
|
||||||
}
|
}
|
||||||
ast::meta_list("doc", docs) {
|
ast::meta_list("doc", docs) {
|
||||||
_fndoc = some(parse_compound_fndoc(docs));
|
_fndoc = some(parse_compound_fndoc(docs));
|
||||||
|
@ -108,10 +116,15 @@ fn doc_item(rd: rustdoc, item: @ast::item) {
|
||||||
|
|
||||||
let _fndoc0 = alt _fndoc {
|
let _fndoc0 = alt _fndoc {
|
||||||
some(_d) { _d }
|
some(_d) { _d }
|
||||||
none. { { brief: "_undocumented_",
|
none. {
|
||||||
desc: none,
|
~{
|
||||||
return: none,
|
name: "todo",
|
||||||
args: noargdocs } }
|
brief: "_undocumented_",
|
||||||
|
desc: none,
|
||||||
|
return: none,
|
||||||
|
args: noargdocs
|
||||||
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
alt item.node {
|
alt item.node {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue