2012-01-17 19:44:32 -06:00
|
|
|
#[doc(
|
|
|
|
brief = "The attribute parsing pass",
|
|
|
|
desc =
|
|
|
|
"Traverses the document tree, pulling relevant documention out of the \
|
|
|
|
corresponding AST nodes. The information gathered here is the basis \
|
|
|
|
of the natural-language documentation for a crate."
|
|
|
|
)];
|
|
|
|
|
2012-01-16 18:05:59 -06:00
|
|
|
import rustc::syntax::ast;
|
2012-01-16 21:39:53 -06:00
|
|
|
import rustc::middle::ast_map;
|
2012-01-16 18:05:59 -06:00
|
|
|
|
2012-01-17 18:12:50 -06:00
|
|
|
export mk_pass;
|
|
|
|
|
|
|
|
fn mk_pass() -> pass {
|
|
|
|
run
|
|
|
|
}
|
2012-01-16 18:05:59 -06:00
|
|
|
|
|
|
|
fn run(
|
2012-01-17 18:12:31 -06:00
|
|
|
srv: astsrv::srv,
|
2012-01-16 21:39:53 -06:00
|
|
|
doc: doc::cratedoc
|
2012-01-16 18:05:59 -06:00
|
|
|
) -> doc::cratedoc {
|
2012-01-16 21:39:53 -06:00
|
|
|
let fold = fold::fold({
|
2012-01-18 16:30:48 -06:00
|
|
|
fold_crate: fn~(
|
|
|
|
f: fold::fold<astsrv::srv>,
|
|
|
|
d: doc::cratedoc
|
|
|
|
) -> doc::cratedoc {
|
|
|
|
fold_crate(f, d)
|
|
|
|
},
|
2012-01-16 21:39:53 -06:00
|
|
|
fold_fn: fn~(
|
2012-01-17 18:12:31 -06:00
|
|
|
f: fold::fold<astsrv::srv>,
|
2012-01-16 21:39:53 -06:00
|
|
|
d: doc::fndoc
|
|
|
|
) -> doc::fndoc {
|
|
|
|
fold_fn(f, d)
|
|
|
|
}
|
|
|
|
with *fold::default_seq_fold(srv)
|
|
|
|
});
|
|
|
|
fold.fold_crate(fold, doc)
|
|
|
|
}
|
|
|
|
|
2012-01-18 16:30:48 -06:00
|
|
|
fn fold_crate(
|
|
|
|
fold: fold::fold<astsrv::srv>,
|
|
|
|
doc: doc::cratedoc
|
|
|
|
) -> doc::cratedoc {
|
|
|
|
|
|
|
|
let srv = fold.ctxt;
|
|
|
|
let doc = fold::default_seq_fold_crate(fold, doc);
|
|
|
|
|
|
|
|
let attrs = astsrv::exec(srv) {|ctxt|
|
|
|
|
let attrs = ctxt.ast.node.attrs;
|
|
|
|
attr_parser::parse_crate(attrs)
|
|
|
|
};
|
|
|
|
|
|
|
|
~{
|
|
|
|
topmod: ~{
|
|
|
|
name: option::from_maybe(doc.topmod.name, attrs.name),
|
|
|
|
mods: doc.topmod.mods,
|
|
|
|
fns: doc.topmod.fns
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_replace_top_module_name_with_crate_name() {
|
|
|
|
let source = "#[link(name = \"bond\")];";
|
|
|
|
let srv = astsrv::mk_srv_from_str(source);
|
|
|
|
let doc = extract::from_srv(srv, "");
|
|
|
|
let fold = fold::default_seq_fold(srv);
|
|
|
|
let doc = fold_crate(fold, doc);
|
|
|
|
assert doc.topmod.name == "bond";
|
|
|
|
}
|
|
|
|
|
2012-01-16 21:39:53 -06:00
|
|
|
fn fold_fn(
|
2012-01-17 18:12:31 -06:00
|
|
|
fold: fold::fold<astsrv::srv>,
|
2012-01-16 21:39:53 -06:00
|
|
|
doc: doc::fndoc
|
|
|
|
) -> doc::fndoc {
|
|
|
|
|
|
|
|
let srv = fold.ctxt;
|
|
|
|
|
2012-01-17 18:12:31 -06:00
|
|
|
let attrs = astsrv::exec(srv) {|ctxt|
|
|
|
|
let attrs = alt ctxt.map.get(doc.id) {
|
|
|
|
ast_map::node_item(item) { item.attrs }
|
|
|
|
};
|
|
|
|
attr_parser::parse_fn(attrs)
|
2012-01-16 21:39:53 -06:00
|
|
|
};
|
|
|
|
ret merge_fn_attrs(doc, attrs);
|
|
|
|
|
|
|
|
fn merge_fn_attrs(
|
|
|
|
doc: doc::fndoc,
|
|
|
|
attrs: attr_parser::fn_attrs
|
|
|
|
) -> doc::fndoc {
|
|
|
|
ret ~{
|
|
|
|
id: doc.id,
|
|
|
|
name: doc.name,
|
|
|
|
brief: attrs.brief,
|
|
|
|
desc: attrs.desc,
|
|
|
|
return: merge_ret_attrs(doc.return, attrs.return),
|
|
|
|
args: merge_arg_attrs(doc.args, attrs.args)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
fn merge_arg_attrs(
|
2012-01-18 16:47:18 -06:00
|
|
|
doc: [doc::argdoc],
|
2012-01-16 21:39:53 -06:00
|
|
|
_attrs: [attr_parser::arg_attrs]
|
2012-01-18 16:47:18 -06:00
|
|
|
) -> [doc::argdoc] {
|
2012-01-16 21:39:53 -06:00
|
|
|
// FIXME
|
|
|
|
doc
|
|
|
|
}
|
|
|
|
|
|
|
|
fn merge_ret_attrs(
|
|
|
|
doc: option<doc::retdoc>,
|
|
|
|
_attrs: option<str>
|
|
|
|
) -> option<doc::retdoc> {
|
|
|
|
// FIXME
|
|
|
|
doc
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn fold_fn_should_extract_fn_attributes() {
|
|
|
|
let source = "#[doc = \"test\"] fn a() -> int { }";
|
2012-01-17 18:12:31 -06:00
|
|
|
let srv = astsrv::mk_srv_from_str(source);
|
2012-01-16 21:39:53 -06:00
|
|
|
let doc = extract::from_srv(srv, "");
|
|
|
|
let fold = fold::default_seq_fold(srv);
|
|
|
|
let doc = fold_fn(fold, doc.topmod.fns[0]);
|
|
|
|
assert doc.desc == some("test");
|
2012-01-16 18:05:59 -06:00
|
|
|
}
|