2012-07-04 16:53:12 -05:00
|
|
|
//! Converts the Rust AST to the rustdoc document model
|
2012-01-17 19:44:32 -06:00
|
|
|
|
2012-05-13 19:12:56 -05:00
|
|
|
import syntax::ast;
|
2012-01-15 23:45:13 -06:00
|
|
|
|
2012-01-16 19:30:57 -06:00
|
|
|
export from_srv, extract;
|
|
|
|
|
|
|
|
fn from_srv(
|
2012-01-17 18:12:31 -06:00
|
|
|
srv: astsrv::srv,
|
2012-07-14 00:57:48 -05:00
|
|
|
default_name: ~str
|
2012-03-02 20:33:25 -06:00
|
|
|
) -> doc::doc {
|
2012-01-18 16:06:22 -06:00
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
//! Use the AST service to create a document tree
|
2012-01-18 16:06:22 -06:00
|
|
|
|
2012-06-30 18:19:07 -05:00
|
|
|
do astsrv::exec(srv) |ctxt| {
|
2012-01-16 19:30:57 -06:00
|
|
|
extract(ctxt.ast, default_name)
|
|
|
|
}
|
|
|
|
}
|
2012-01-15 19:23:19 -06:00
|
|
|
|
2012-01-15 23:45:13 -06:00
|
|
|
fn extract(
|
2012-01-16 02:19:35 -06:00
|
|
|
crate: @ast::crate,
|
2012-07-14 00:57:48 -05:00
|
|
|
default_name: ~str
|
2012-03-02 20:33:25 -06:00
|
|
|
) -> doc::doc {
|
2012-01-30 15:05:25 -06:00
|
|
|
{
|
2012-06-29 18:26:56 -05:00
|
|
|
pages: ~[
|
2012-03-02 20:33:25 -06:00
|
|
|
doc::cratepage({
|
|
|
|
topmod: top_moddoc_from_crate(crate, default_name),
|
|
|
|
})
|
2012-06-29 18:26:56 -05:00
|
|
|
]
|
2012-01-15 23:45:13 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn top_moddoc_from_crate(
|
2012-01-16 02:19:35 -06:00
|
|
|
crate: @ast::crate,
|
2012-07-14 00:57:48 -05:00
|
|
|
default_name: ~str
|
2012-01-15 23:45:13 -06:00
|
|
|
) -> doc::moddoc {
|
2012-06-10 02:49:59 -05:00
|
|
|
moddoc_from_mod(mk_itemdoc(ast::crate_node_id, @default_name),
|
2012-02-17 17:46:04 -06:00
|
|
|
crate.node.module)
|
2012-01-15 23:45:13 -06:00
|
|
|
}
|
|
|
|
|
2012-02-17 17:39:05 -06:00
|
|
|
fn mk_itemdoc(id: ast::node_id, name: ast::ident) -> doc::itemdoc {
|
|
|
|
{
|
|
|
|
id: id,
|
2012-06-10 02:49:59 -05:00
|
|
|
name: *name,
|
2012-06-29 18:26:56 -05:00
|
|
|
path: ~[],
|
2012-02-17 17:39:05 -06:00
|
|
|
brief: none,
|
|
|
|
desc: none,
|
2012-06-29 18:26:56 -05:00
|
|
|
sections: ~[],
|
2012-02-24 17:43:57 -06:00
|
|
|
reexport: false
|
2012-02-17 17:39:05 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-15 23:45:13 -06:00
|
|
|
fn moddoc_from_mod(
|
2012-02-17 17:46:04 -06:00
|
|
|
itemdoc: doc::itemdoc,
|
|
|
|
module: ast::_mod
|
2012-01-15 23:45:13 -06:00
|
|
|
) -> doc::moddoc {
|
2012-01-30 15:05:25 -06:00
|
|
|
{
|
2012-02-17 17:46:04 -06:00
|
|
|
item: itemdoc,
|
2012-06-30 18:19:07 -05:00
|
|
|
items: do vec::filter_map(module.items) |item| {
|
2012-02-17 17:46:04 -06:00
|
|
|
let itemdoc = mk_itemdoc(item.id, item.ident);
|
2012-01-28 16:36:35 -06:00
|
|
|
alt item.node {
|
2012-01-29 15:08:18 -06:00
|
|
|
ast::item_mod(m) {
|
|
|
|
some(doc::modtag(
|
2012-02-17 17:46:04 -06:00
|
|
|
moddoc_from_mod(itemdoc, m)
|
2012-01-29 15:08:18 -06:00
|
|
|
))
|
|
|
|
}
|
2012-06-26 18:18:37 -05:00
|
|
|
ast::item_foreign_mod(nm) {
|
2012-02-24 15:50:40 -06:00
|
|
|
some(doc::nmodtag(
|
|
|
|
nmoddoc_from_mod(itemdoc, nm)
|
|
|
|
))
|
|
|
|
}
|
2012-03-09 19:23:56 -06:00
|
|
|
ast::item_fn(_, _, _) {
|
2012-01-29 14:43:21 -06:00
|
|
|
some(doc::fntag(
|
2012-03-09 19:23:56 -06:00
|
|
|
fndoc_from_fn(itemdoc)
|
2012-01-29 14:43:21 -06:00
|
|
|
))
|
|
|
|
}
|
2012-01-28 17:45:19 -06:00
|
|
|
ast::item_const(_, _) {
|
|
|
|
some(doc::consttag(
|
2012-02-17 17:46:04 -06:00
|
|
|
constdoc_from_const(itemdoc)
|
2012-01-28 17:45:19 -06:00
|
|
|
))
|
|
|
|
}
|
2012-07-11 12:28:30 -05:00
|
|
|
ast::item_enum(variants, _) {
|
2012-01-28 17:04:36 -06:00
|
|
|
some(doc::enumtag(
|
2012-02-17 17:46:04 -06:00
|
|
|
enumdoc_from_enum(itemdoc, variants)
|
2012-01-28 17:04:36 -06:00
|
|
|
))
|
|
|
|
}
|
2012-07-11 12:28:30 -05:00
|
|
|
ast::item_trait(_, methods) {
|
2012-07-03 18:30:42 -05:00
|
|
|
some(doc::traittag(
|
|
|
|
traitdoc_from_trait(itemdoc, methods)
|
2012-01-30 16:24:41 -06:00
|
|
|
))
|
|
|
|
}
|
2012-07-11 12:28:30 -05:00
|
|
|
ast::item_impl(_, _, _, methods) {
|
2012-01-31 17:55:57 -06:00
|
|
|
some(doc::impltag(
|
2012-02-17 17:46:04 -06:00
|
|
|
impldoc_from_impl(itemdoc, methods)
|
2012-01-31 17:55:57 -06:00
|
|
|
))
|
|
|
|
}
|
2012-07-11 12:28:30 -05:00
|
|
|
ast::item_ty(_, _) {
|
2012-02-02 00:41:41 -06:00
|
|
|
some(doc::tytag(
|
2012-02-17 17:46:04 -06:00
|
|
|
tydoc_from_ty(itemdoc)
|
2012-02-02 00:41:41 -06:00
|
|
|
))
|
|
|
|
}
|
2012-01-28 16:36:35 -06:00
|
|
|
_ {
|
|
|
|
none
|
|
|
|
}
|
|
|
|
}
|
2012-03-01 18:33:58 -06:00
|
|
|
},
|
|
|
|
index: none
|
2012-01-15 23:45:13 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-24 15:50:40 -06:00
|
|
|
fn nmoddoc_from_mod(
|
|
|
|
itemdoc: doc::itemdoc,
|
2012-06-26 18:18:37 -05:00
|
|
|
module: ast::foreign_mod
|
2012-02-24 15:50:40 -06:00
|
|
|
) -> doc::nmoddoc {
|
|
|
|
{
|
|
|
|
item: itemdoc,
|
2012-07-13 12:36:35 -05:00
|
|
|
fns: do vec::map(module.items) |item| {
|
2012-02-24 15:50:40 -06:00
|
|
|
let itemdoc = mk_itemdoc(item.id, item.ident);
|
|
|
|
alt item.node {
|
2012-06-26 18:18:37 -05:00
|
|
|
ast::foreign_item_fn(_, _) {
|
2012-03-09 19:23:56 -06:00
|
|
|
fndoc_from_fn(itemdoc)
|
2012-02-24 15:50:40 -06:00
|
|
|
}
|
|
|
|
}
|
2012-03-11 18:36:20 -05:00
|
|
|
},
|
|
|
|
index: none
|
2012-02-24 15:50:40 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-09 19:23:56 -06:00
|
|
|
fn fndoc_from_fn(itemdoc: doc::itemdoc) -> doc::fndoc {
|
2012-01-30 15:05:25 -06:00
|
|
|
{
|
2012-02-17 17:46:04 -06:00
|
|
|
item: itemdoc,
|
2012-01-19 20:09:19 -06:00
|
|
|
sig: none
|
2012-01-16 18:01:33 -06:00
|
|
|
}
|
2012-01-15 19:23:19 -06:00
|
|
|
}
|
|
|
|
|
2012-02-17 17:46:04 -06:00
|
|
|
fn constdoc_from_const(itemdoc: doc::itemdoc) -> doc::constdoc {
|
2012-01-30 15:05:25 -06:00
|
|
|
{
|
2012-02-17 17:46:04 -06:00
|
|
|
item: itemdoc,
|
2012-03-09 19:23:56 -06:00
|
|
|
sig: none
|
2012-01-24 01:17:13 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_extract_const_name_and_id() {
|
2012-07-14 00:57:48 -05:00
|
|
|
let doc = test::mk_doc(~"const a: int = 0;");
|
2012-03-02 20:33:25 -06:00
|
|
|
assert doc.cratemod().consts()[0].id() != 0;
|
2012-07-14 00:57:48 -05:00
|
|
|
assert doc.cratemod().consts()[0].name() == ~"a";
|
2012-01-24 01:17:13 -06:00
|
|
|
}
|
|
|
|
|
2012-01-25 18:50:32 -06:00
|
|
|
fn enumdoc_from_enum(
|
2012-02-17 17:46:04 -06:00
|
|
|
itemdoc: doc::itemdoc,
|
2012-06-29 18:26:56 -05:00
|
|
|
variants: ~[ast::variant]
|
2012-01-25 18:50:32 -06:00
|
|
|
) -> doc::enumdoc {
|
2012-01-30 15:05:25 -06:00
|
|
|
{
|
2012-02-17 17:46:04 -06:00
|
|
|
item: itemdoc,
|
2012-01-25 18:50:32 -06:00
|
|
|
variants: variantdocs_from_variants(variants)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn variantdocs_from_variants(
|
2012-06-29 18:26:56 -05:00
|
|
|
variants: ~[ast::variant]
|
|
|
|
) -> ~[doc::variantdoc] {
|
2012-07-13 12:36:35 -05:00
|
|
|
vec::map(variants, variantdoc_from_variant)
|
2012-01-25 18:50:32 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn variantdoc_from_variant(variant: ast::variant) -> doc::variantdoc {
|
2012-01-30 15:05:25 -06:00
|
|
|
{
|
2012-06-10 02:49:59 -05:00
|
|
|
name: *variant.node.name,
|
2012-01-25 18:50:32 -06:00
|
|
|
desc: none,
|
|
|
|
sig: none
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_extract_enums() {
|
2012-07-14 00:57:48 -05:00
|
|
|
let doc = test::mk_doc(~"enum e { v }");
|
2012-03-02 20:33:25 -06:00
|
|
|
assert doc.cratemod().enums()[0].id() != 0;
|
2012-07-14 00:57:48 -05:00
|
|
|
assert doc.cratemod().enums()[0].name() == ~"e";
|
2012-01-25 18:50:32 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_extract_enum_variants() {
|
2012-07-14 00:57:48 -05:00
|
|
|
let doc = test::mk_doc(~"enum e { v }");
|
|
|
|
assert doc.cratemod().enums()[0].variants[0].name == ~"v";
|
2012-01-25 18:50:32 -06:00
|
|
|
}
|
|
|
|
|
2012-07-03 18:30:42 -05:00
|
|
|
fn traitdoc_from_trait(
|
2012-02-17 17:46:04 -06:00
|
|
|
itemdoc: doc::itemdoc,
|
2012-07-10 15:44:20 -05:00
|
|
|
methods: ~[ast::trait_method]
|
2012-07-03 18:30:42 -05:00
|
|
|
) -> doc::traitdoc {
|
2012-01-30 16:24:41 -06:00
|
|
|
{
|
2012-02-17 17:46:04 -06:00
|
|
|
item: itemdoc,
|
2012-07-13 12:36:35 -05:00
|
|
|
methods: do vec::map(methods) |method| {
|
2012-07-10 15:44:20 -05:00
|
|
|
alt method {
|
|
|
|
ast::required(ty_m) {
|
|
|
|
{
|
|
|
|
name: *ty_m.ident,
|
|
|
|
brief: none,
|
|
|
|
desc: none,
|
|
|
|
sections: ~[],
|
|
|
|
sig: none,
|
|
|
|
implementation: doc::required,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ast::provided(m) {
|
|
|
|
{
|
|
|
|
name: *m.ident,
|
|
|
|
brief: none,
|
|
|
|
desc: none,
|
|
|
|
sections: ~[],
|
|
|
|
sig: none,
|
|
|
|
implementation: doc::provided,
|
|
|
|
}
|
|
|
|
}
|
2012-01-30 16:24:41 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2012-07-03 18:30:42 -05:00
|
|
|
fn should_extract_traits() {
|
2012-07-14 00:57:48 -05:00
|
|
|
let doc = test::mk_doc(~"trait i { fn f(); }");
|
|
|
|
assert doc.cratemod().traits()[0].name() == ~"i";
|
2012-01-30 16:24:41 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2012-07-03 18:30:42 -05:00
|
|
|
fn should_extract_trait_methods() {
|
2012-07-14 00:57:48 -05:00
|
|
|
let doc = test::mk_doc(~"trait i { fn f(); }");
|
|
|
|
assert doc.cratemod().traits()[0].methods[0].name == ~"f";
|
2012-01-30 16:24:41 -06:00
|
|
|
}
|
|
|
|
|
2012-01-31 17:55:57 -06:00
|
|
|
fn impldoc_from_impl(
|
2012-02-17 17:46:04 -06:00
|
|
|
itemdoc: doc::itemdoc,
|
2012-06-29 18:26:56 -05:00
|
|
|
methods: ~[@ast::method]
|
2012-01-31 17:55:57 -06:00
|
|
|
) -> doc::impldoc {
|
|
|
|
{
|
2012-02-17 17:46:04 -06:00
|
|
|
item: itemdoc,
|
2012-07-03 18:30:42 -05:00
|
|
|
trait_ty: none,
|
2012-01-31 18:13:29 -06:00
|
|
|
self_ty: none,
|
2012-07-13 12:36:35 -05:00
|
|
|
methods: do vec::map(methods) |method| {
|
2012-01-31 17:55:57 -06:00
|
|
|
{
|
2012-06-10 02:49:59 -05:00
|
|
|
name: *method.ident,
|
2012-01-31 17:55:57 -06:00
|
|
|
brief: none,
|
|
|
|
desc: none,
|
2012-06-29 18:26:56 -05:00
|
|
|
sections: ~[],
|
2012-07-10 15:44:20 -05:00
|
|
|
sig: none,
|
|
|
|
implementation: doc::provided,
|
2012-01-31 17:55:57 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_extract_impls_with_names() {
|
2012-07-14 00:57:48 -05:00
|
|
|
let doc = test::mk_doc(~"impl i for int { fn a() { } }");
|
|
|
|
assert doc.cratemod().impls()[0].name() == ~"i";
|
2012-01-31 17:55:57 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_extract_impls_without_names() {
|
2012-07-14 00:57:48 -05:00
|
|
|
let doc = test::mk_doc(~"impl of i for int { fn a() { } }");
|
|
|
|
assert doc.cratemod().impls()[0].name() == ~"i";
|
2012-01-31 17:55:57 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_extract_impl_methods() {
|
2012-07-14 00:57:48 -05:00
|
|
|
let doc = test::mk_doc(~"impl i for int { fn f() { } }");
|
|
|
|
assert doc.cratemod().impls()[0].methods[0].name == ~"f";
|
2012-01-31 17:55:57 -06:00
|
|
|
}
|
|
|
|
|
2012-02-02 00:41:41 -06:00
|
|
|
fn tydoc_from_ty(
|
2012-02-17 17:46:04 -06:00
|
|
|
itemdoc: doc::itemdoc
|
2012-02-02 00:41:41 -06:00
|
|
|
) -> doc::tydoc {
|
|
|
|
{
|
2012-02-17 17:46:04 -06:00
|
|
|
item: itemdoc,
|
2012-02-02 00:41:41 -06:00
|
|
|
sig: none
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_extract_tys() {
|
2012-07-14 00:57:48 -05:00
|
|
|
let doc = test::mk_doc(~"type a = int;");
|
|
|
|
assert doc.cratemod().types()[0].name() == ~"a";
|
2012-02-02 00:41:41 -06:00
|
|
|
}
|
|
|
|
|
2012-01-15 19:23:19 -06:00
|
|
|
#[cfg(test)]
|
2012-01-31 20:32:37 -06:00
|
|
|
mod test {
|
|
|
|
|
2012-07-14 00:57:48 -05:00
|
|
|
fn mk_doc(source: ~str) -> doc::doc {
|
2012-04-30 13:52:07 -05:00
|
|
|
let ast = parse::from_str(source);
|
2012-07-14 00:57:48 -05:00
|
|
|
extract(ast, ~"")
|
2012-01-31 20:32:37 -06:00
|
|
|
}
|
2012-01-15 19:23:19 -06:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn extract_empty_crate() {
|
2012-07-14 00:57:48 -05:00
|
|
|
let doc = mk_doc(~"");
|
2012-03-02 20:33:25 -06:00
|
|
|
assert vec::is_empty(doc.cratemod().mods());
|
|
|
|
assert vec::is_empty(doc.cratemod().fns());
|
2012-01-15 23:45:13 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn extract_mods() {
|
2012-07-14 00:57:48 -05:00
|
|
|
let doc = mk_doc(~"mod a { mod b { } mod c { } }");
|
|
|
|
assert doc.cratemod().mods()[0].name() == ~"a";
|
|
|
|
assert doc.cratemod().mods()[0].mods()[0].name() == ~"b";
|
|
|
|
assert doc.cratemod().mods()[0].mods()[1].name() == ~"c";
|
2012-01-15 23:45:13 -06:00
|
|
|
}
|
|
|
|
|
2012-02-24 15:50:40 -06:00
|
|
|
#[test]
|
2012-06-26 18:18:37 -05:00
|
|
|
fn extract_foreign_mods() {
|
2012-07-14 00:57:48 -05:00
|
|
|
let doc = mk_doc(~"extern mod a { }");
|
|
|
|
assert doc.cratemod().nmods()[0].name() == ~"a";
|
2012-02-24 15:50:40 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2012-06-26 18:18:37 -05:00
|
|
|
fn extract_fns_from_foreign_mods() {
|
2012-07-14 00:57:48 -05:00
|
|
|
let doc = mk_doc(~"extern mod a { fn a(); }");
|
|
|
|
assert doc.cratemod().nmods()[0].fns[0].name() == ~"a";
|
2012-02-24 15:50:40 -06:00
|
|
|
}
|
|
|
|
|
2012-01-15 23:45:13 -06:00
|
|
|
#[test]
|
|
|
|
fn extract_mods_deep() {
|
2012-07-14 00:57:48 -05:00
|
|
|
let doc = mk_doc(~"mod a { mod b { mod c { } } }");
|
|
|
|
assert doc.cratemod().mods()[0].mods()[0].mods()[0].name() == ~"c";
|
2012-01-15 23:45:13 -06:00
|
|
|
}
|
|
|
|
|
2012-01-18 20:35:55 -06:00
|
|
|
#[test]
|
|
|
|
fn extract_should_set_mod_ast_id() {
|
2012-07-14 00:57:48 -05:00
|
|
|
let doc = mk_doc(~"mod a { }");
|
2012-03-02 20:33:25 -06:00
|
|
|
assert doc.cratemod().mods()[0].id() != 0;
|
2012-01-18 20:35:55 -06:00
|
|
|
}
|
|
|
|
|
2012-01-15 23:45:13 -06:00
|
|
|
#[test]
|
|
|
|
fn extract_fns() {
|
2012-01-31 20:32:37 -06:00
|
|
|
let doc = mk_doc(
|
2012-07-14 00:57:48 -05:00
|
|
|
~"fn a() { } \
|
2012-01-31 20:32:37 -06:00
|
|
|
mod b { fn c() { } }");
|
2012-07-14 00:57:48 -05:00
|
|
|
assert doc.cratemod().fns()[0].name() == ~"a";
|
|
|
|
assert doc.cratemod().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() {
|
2012-07-14 00:57:48 -05:00
|
|
|
let doc = mk_doc(~"fn a() { }");
|
2012-03-02 20:33:25 -06:00
|
|
|
assert doc.cratemod().fns()[0].id() != 0;
|
2012-01-16 02:15:03 -06:00
|
|
|
}
|
2012-01-16 02:19:35 -06:00
|
|
|
|
|
|
|
#[test]
|
2012-01-16 18:01:33 -06:00
|
|
|
fn extract_should_use_default_crate_name() {
|
2012-07-14 00:57:48 -05:00
|
|
|
let source = ~"";
|
2012-01-16 02:19:35 -06:00
|
|
|
let ast = parse::from_str(source);
|
2012-07-14 00:57:48 -05:00
|
|
|
let doc = extract(ast, ~"burp");
|
|
|
|
assert doc.cratemod().name() == ~"burp";
|
2012-01-16 02:19:35 -06:00
|
|
|
}
|
2012-01-16 19:30:57 -06:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn extract_from_seq_srv() {
|
2012-07-14 00:57:48 -05:00
|
|
|
let source = ~"";
|
2012-06-30 18:19:07 -05:00
|
|
|
do astsrv::from_str(source) |srv| {
|
2012-07-14 00:57:48 -05:00
|
|
|
let doc = from_srv(srv, ~"name");
|
|
|
|
assert doc.cratemod().name() == ~"name";
|
2012-02-20 23:08:19 -06:00
|
|
|
}
|
2012-01-16 19:30:57 -06:00
|
|
|
}
|
2012-06-25 22:00:46 -05:00
|
|
|
}
|