rust/src/librustdoc/attr_pass.rs

312 lines
8.0 KiB
Rust
Raw Normal View History

// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
/*!
The attribute parsing pass
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-17 17:44:32 -08:00
2012-09-18 16:48:40 -07:00
use doc::ItemUtils;
2012-09-05 10:41:47 -07:00
use extract::to_str;
use fold::Fold;
2012-09-05 10:41:47 -07:00
use syntax::ast;
use syntax::ast_map;
2012-09-10 15:38:28 -07:00
use std::map::HashMap;
2012-11-19 18:00:12 -08:00
pub fn mk_pass() -> Pass {
2012-02-27 18:07:16 -08:00
{
name: ~"attr",
2012-02-27 18:07:16 -08:00
f: run
}
}
fn run(
2012-09-18 16:48:40 -07:00
srv: astsrv::Srv,
2012-11-19 18:48:46 -08:00
+doc: doc::Doc
2012-09-18 16:48:40 -07:00
) -> doc::Doc {
let fold = Fold {
2012-01-19 00:14:41 -08:00
fold_crate: fold_crate,
2012-02-17 16:52:27 -08:00
fold_item: fold_item,
fold_enum: fold_enum,
fold_trait: fold_trait,
2012-09-04 13:29:32 -07:00
fold_impl: fold_impl,
.. fold::default_any_fold(srv)
};
(fold.fold_doc)(&fold, doc)
}
fn fold_crate(
2012-11-19 18:48:46 -08:00
fold: &fold::Fold<astsrv::Srv>,
+doc: doc::CrateDoc
2012-09-18 16:48:40 -07:00
) -> doc::CrateDoc {
let srv = fold.ctxt;
let doc = fold::default_seq_fold_crate(fold, doc);
2012-06-30 16:19:07 -07:00
let attrs = do astsrv::exec(srv) |ctxt| {
let attrs = ctxt.ast.node.attrs;
attr_parser::parse_crate(attrs)
};
2012-01-30 13:05:25 -08:00
{
2012-09-18 16:48:40 -07:00
topmod: doc::ModDoc_({
item: {
name: option::get_default(attrs.name, doc.topmod.name()),
2012-09-04 13:29:32 -07:00
.. doc.topmod.item
},
.. *doc.topmod
})
}
}
#[test]
fn should_replace_top_module_name_with_crate_name() {
let doc = test::mk_doc(~"#[link(name = \"bond\")];");
assert doc.cratemod().name() == ~"bond";
}
2012-02-17 16:52:27 -08:00
fn fold_item(
2012-11-19 18:48:46 -08:00
fold: &fold::Fold<astsrv::Srv>,
+doc: doc::ItemDoc
2012-09-18 16:48:40 -07:00
) -> doc::ItemDoc {
2012-02-17 16:52:27 -08:00
let srv = fold.ctxt;
let doc = fold::default_seq_fold_item(fold, doc);
2012-03-09 18:12:15 -08:00
let desc = if doc.id == ast::crate_node_id {
2012-02-17 16:52:27 -08:00
// This is the top-level mod, use the crate attributes
2012-06-30 16:19:07 -07:00
do astsrv::exec(srv) |ctxt| {
2012-03-09 18:12:15 -08:00
attr_parser::parse_desc(ctxt.ast.node.attrs)
2012-02-17 16:52:27 -08:00
}
} else {
2012-03-09 18:12:15 -08:00
parse_item_attrs(srv, doc.id, attr_parser::parse_desc)
2012-02-17 16:52:27 -08:00
};
{
2012-09-04 13:29:32 -07:00
desc: desc,
.. doc
2012-02-17 16:52:27 -08:00
}
}
2012-12-11 13:50:04 -08:00
fn parse_item_attrs<T:Owned>(
2012-09-18 16:48:40 -07:00
srv: astsrv::Srv,
id: doc::AstId,
2012-11-19 18:48:46 -08:00
+parse_attrs: fn~(+a: ~[ast::attribute]) -> T) -> T {
2012-09-18 22:43:54 -07:00
do astsrv::exec(srv) |move parse_attrs, ctxt| {
2012-08-06 12:34:08 -07:00
let attrs = match ctxt.ast_map.get(id) {
2012-08-03 19:59:04 -07:00
ast_map::node_item(item, _) => item.attrs,
ast_map::node_foreign_item(item, _, _) => item.attrs,
_ => fail ~"parse_item_attrs: not an item"
2012-01-24 00:38:21 -08:00
};
parse_attrs(attrs)
}
}
#[test]
2012-02-17 16:52:27 -08:00
fn should_should_extract_mod_attributes() {
let doc = test::mk_doc(~"#[doc = \"test\"] mod a { }");
2012-08-20 12:23:37 -07:00
assert doc.cratemod().mods()[0].desc() == Some(~"test");
}
#[test]
2012-02-17 16:52:27 -08:00
fn should_extract_top_mod_attributes() {
let doc = test::mk_doc(~"#[doc = \"test\"];");
2012-08-20 12:23:37 -07:00
assert doc.cratemod().desc() == Some(~"test");
}
#[test]
fn should_extract_foreign_mod_attributes() {
let doc = test::mk_doc(~"#[doc = \"test\"] extern mod a { }");
2012-08-20 12:23:37 -07:00
assert doc.cratemod().nmods()[0].desc() == Some(~"test");
}
#[test]
fn should_extract_foreign_fn_attributes() {
let doc = test::mk_doc(~"extern mod a { #[doc = \"test\"] fn a(); }");
2012-08-20 12:23:37 -07:00
assert doc.cratemod().nmods()[0].fns[0].desc() == Some(~"test");
}
#[test]
2012-02-17 16:52:27 -08:00
fn should_extract_fn_attributes() {
let doc = test::mk_doc(~"#[doc = \"test\"] fn a() -> int { }");
2012-08-20 12:23:37 -07:00
assert doc.cratemod().fns()[0].desc() == Some(~"test");
}
fn fold_enum(
2012-11-19 18:48:46 -08:00
fold: &fold::Fold<astsrv::Srv>,
+doc: doc::EnumDoc
2012-09-18 16:48:40 -07:00
) -> doc::EnumDoc {
let srv = fold.ctxt;
let doc_id = doc.id();
2012-02-17 16:52:27 -08:00
let doc = fold::default_seq_fold_enum(fold, doc);
2012-01-30 13:05:25 -08:00
{
variants: do par::map(doc.variants) |variant| {
2012-09-28 17:17:20 -07:00
let variant = *variant;
2012-06-30 16:19:07 -07:00
let desc = do astsrv::exec(srv) |ctxt| {
2012-08-23 15:14:56 -07:00
match ctxt.ast_map.get(doc_id) {
ast_map::node_item(@{
node: ast::item_enum(enum_definition, _), _
2012-08-03 19:59:04 -07:00
}, _) => {
let ast_variant = option::get(
vec::find(enum_definition.variants, |v| {
2012-07-18 16:18:02 -07:00
to_str(v.node.name) == variant.name
}));
2012-03-09 18:12:15 -08:00
attr_parser::parse_desc(ast_variant.node.attrs)
}
_ => fail fmt!("Enum variant %s has id that's not bound \
2012-08-23 15:14:56 -07:00
to an enum item", variant.name)
}
};
2012-01-30 13:05:25 -08:00
{
2012-09-04 13:29:32 -07:00
desc: desc,
.. variant
}
2012-09-04 13:29:32 -07:00
},
.. doc
}
}
#[test]
2012-02-17 16:52:27 -08:00
fn should_extract_enum_docs() {
let doc = test::mk_doc(~"#[doc = \"b\"]\
2012-01-31 18:32:37 -08:00
enum a { v }");
2012-08-20 12:23:37 -07:00
assert doc.cratemod().enums()[0].desc() == Some(~"b");
}
#[test]
2012-02-17 16:52:27 -08:00
fn should_extract_variant_docs() {
let doc = test::mk_doc(~"enum a { #[doc = \"c\"] v }");
2012-08-20 12:23:37 -07:00
assert doc.cratemod().enums()[0].variants[0].desc == Some(~"c");
}
fn fold_trait(
2012-11-19 18:48:46 -08:00
fold: &fold::Fold<astsrv::Srv>,
+doc: doc::TraitDoc
2012-09-18 16:48:40 -07:00
) -> doc::TraitDoc {
let srv = fold.ctxt;
let doc = fold::default_seq_fold_trait(fold, doc);
{
2012-09-04 13:29:32 -07:00
methods: merge_method_attrs(srv, doc.id(), doc.methods),
.. doc
}
}
fn merge_method_attrs(
2012-09-18 16:48:40 -07:00
srv: astsrv::Srv,
item_id: doc::AstId,
docs: ~[doc::MethodDoc]
) -> ~[doc::MethodDoc] {
2012-02-17 16:52:27 -08:00
// Create an assoc list from method name to attributes
2012-08-20 12:23:37 -07:00
let attrs: ~[(~str, Option<~str>)] = do astsrv::exec(srv) |ctxt| {
2012-08-06 12:34:08 -07:00
match ctxt.ast_map.get(item_id) {
ast_map::node_item(@{
node: ast::item_trait(_, _, methods), _
2012-08-03 19:59:04 -07:00
}, _) => {
vec::map(methods, |method| {
match *method {
2012-08-03 19:59:04 -07:00
ast::required(ty_m) => {
2012-07-18 16:18:02 -07:00
(to_str(ty_m.ident), attr_parser::parse_desc(ty_m.attrs))
}
2012-08-03 19:59:04 -07:00
ast::provided(m) => {
2012-07-18 16:18:02 -07:00
(to_str(m.ident), attr_parser::parse_desc(m.attrs))
}
}
})
}
ast_map::node_item(@{
node: ast::item_impl(_, _, _, methods), _
2012-08-03 19:59:04 -07:00
}, _) => {
vec::map(methods, |method| {
(to_str(method.ident),
attr_parser::parse_desc(method.attrs))
})
}
2012-08-03 19:59:04 -07:00
_ => fail ~"unexpected item"
}
};
2012-06-30 16:19:07 -07:00
do vec::map2(docs, attrs) |doc, attrs| {
assert doc.name == attrs.first();
let desc = attrs.second();
{
2012-09-04 13:29:32 -07:00
desc: desc,
2012-09-27 22:20:47 -07:00
..*doc
}
}
}
#[test]
fn should_extract_trait_docs() {
let doc = test::mk_doc(~"#[doc = \"whatever\"] trait i { fn a(); }");
2012-08-20 12:23:37 -07:00
assert doc.cratemod().traits()[0].desc() == Some(~"whatever");
}
#[test]
fn should_extract_trait_method_docs() {
2012-01-31 18:32:37 -08:00
let doc = test::mk_doc(
~"trait i {\
#[doc = \"desc\"]\
2012-01-31 18:32:37 -08:00
fn f(a: bool) -> bool;\
}");
2012-08-20 12:23:37 -07:00
assert doc.cratemod().traits()[0].methods[0].desc == Some(~"desc");
}
fn fold_impl(
2012-11-19 18:48:46 -08:00
fold: &fold::Fold<astsrv::Srv>,
+doc: doc::ImplDoc
2012-09-18 16:48:40 -07:00
) -> doc::ImplDoc {
let srv = fold.ctxt;
let doc = fold::default_seq_fold_impl(fold, doc);
{
2012-09-04 13:29:32 -07:00
methods: merge_method_attrs(srv, doc.id(), doc.methods),
.. doc
}
}
#[test]
fn should_extract_impl_docs() {
2012-01-31 18:32:37 -08:00
let doc = test::mk_doc(
2012-08-08 17:19:06 -07:00
~"#[doc = \"whatever\"] impl int { fn a() { } }");
2012-08-20 12:23:37 -07:00
assert doc.cratemod().impls()[0].desc() == Some(~"whatever");
}
#[test]
fn should_extract_impl_method_docs() {
2012-01-31 18:32:37 -08:00
let doc = test::mk_doc(
2012-08-08 17:19:06 -07:00
~"impl int {\
#[doc = \"desc\"]\
2012-01-31 18:32:37 -08:00
fn f(a: bool) -> bool { }\
}");
2012-08-20 12:23:37 -07:00
assert doc.cratemod().impls()[0].methods[0].desc == Some(~"desc");
}
2012-01-31 18:32:37 -08:00
#[cfg(test)]
mod test {
#[legacy_exports];
2012-09-18 16:48:40 -07:00
fn mk_doc(source: ~str) -> doc::Doc {
2012-06-30 16:19:07 -07:00
do astsrv::from_str(source) |srv| {
let doc = extract::from_srv(srv, ~"");
run(srv, doc)
}
2012-01-31 18:32:37 -08:00
}
}