2012-01-16 15:22:25 -06:00
|
|
|
import rustc::syntax::ast;
|
2012-01-16 22:00:46 -06:00
|
|
|
import rustc::front::attr;
|
2012-01-16 15:22:25 -06:00
|
|
|
|
2012-01-16 18:27:28 -06:00
|
|
|
export fn_attrs, arg_attrs;
|
2012-01-16 00:00:07 -06:00
|
|
|
export parse_fn;
|
2012-01-15 23:50:55 -06:00
|
|
|
|
2012-01-16 18:27:28 -06:00
|
|
|
type fn_attrs = {
|
|
|
|
brief: option<str>,
|
|
|
|
desc: option<str>,
|
|
|
|
args: [arg_attrs],
|
|
|
|
return: option<str>
|
|
|
|
};
|
|
|
|
|
|
|
|
type arg_attrs = {
|
2012-01-16 02:15:03 -06:00
|
|
|
name: str,
|
2012-01-16 18:27:28 -06:00
|
|
|
desc: str
|
|
|
|
};
|
|
|
|
|
2012-01-16 22:00:46 -06:00
|
|
|
fn doc_meta(
|
|
|
|
attrs: [ast::attribute]
|
|
|
|
) -> option<@ast::meta_item> {
|
|
|
|
let doc_attrs = attr::find_attrs_by_name(attrs, "doc");
|
|
|
|
let doc_metas = attr::attr_metas(doc_attrs);
|
|
|
|
if vec::is_not_empty(doc_metas) {
|
|
|
|
if vec::len(doc_metas) != 1u {
|
|
|
|
#warn("ignoring %u doc attributes", vec::len(doc_metas) - 1u);
|
|
|
|
}
|
|
|
|
some(doc_metas[0])
|
|
|
|
} else {
|
|
|
|
none
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-16 18:27:28 -06:00
|
|
|
fn parse_fn(
|
2012-01-16 02:15:03 -06:00
|
|
|
attrs: [ast::attribute]
|
2012-01-16 18:27:28 -06:00
|
|
|
) -> fn_attrs {
|
2012-01-16 00:07:23 -06:00
|
|
|
|
2012-01-16 22:00:46 -06:00
|
|
|
let no_attrs = {
|
|
|
|
brief: none,
|
|
|
|
desc: none,
|
|
|
|
args: [],
|
|
|
|
return: none
|
|
|
|
};
|
|
|
|
|
|
|
|
ret alt doc_meta(attrs) {
|
|
|
|
some(meta) {
|
|
|
|
alt attr::get_meta_item_value_str(meta) {
|
|
|
|
some(desc) {
|
|
|
|
{
|
2012-01-16 18:27:28 -06:00
|
|
|
brief: none,
|
2012-01-16 22:00:46 -06:00
|
|
|
desc: some(desc),
|
2012-01-16 18:27:28 -06:00
|
|
|
args: [],
|
|
|
|
return: none
|
2012-01-16 22:00:46 -06:00
|
|
|
}
|
2012-01-16 18:27:28 -06:00
|
|
|
}
|
2012-01-16 22:00:46 -06:00
|
|
|
none. {
|
|
|
|
alt attr::get_meta_item_list(meta) {
|
|
|
|
some(list) {
|
|
|
|
parse_fn_(list)
|
|
|
|
}
|
|
|
|
none. {
|
|
|
|
no_attrs
|
|
|
|
}
|
|
|
|
}
|
2012-01-16 00:07:23 -06:00
|
|
|
}
|
|
|
|
}
|
2012-01-16 22:00:46 -06:00
|
|
|
}
|
|
|
|
none. {
|
|
|
|
no_attrs
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
fn meta_item_from_list(
|
|
|
|
items: [@ast::meta_item],
|
|
|
|
name: str
|
|
|
|
) -> option<@ast::meta_item> {
|
|
|
|
let items = attr::find_meta_items_by_name(items, name);
|
|
|
|
vec::last(items)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn meta_item_value_from_list(
|
|
|
|
items: [@ast::meta_item],
|
|
|
|
name: str
|
|
|
|
) -> option<str> {
|
|
|
|
alt meta_item_from_list(items, name) {
|
|
|
|
some(item) {
|
|
|
|
alt attr::get_meta_item_value_str(item) {
|
|
|
|
some(value) { some(value) }
|
|
|
|
none. { none }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
none. { none }
|
2012-01-16 18:27:28 -06:00
|
|
|
}
|
2012-01-16 22:00:46 -06:00
|
|
|
}
|
2012-01-16 00:07:23 -06:00
|
|
|
|
2012-01-16 22:00:46 -06:00
|
|
|
fn meta_item_list_from_list(
|
|
|
|
items: [@ast::meta_item],
|
|
|
|
name: str
|
|
|
|
) -> option<[@ast::meta_item]> {
|
|
|
|
alt meta_item_from_list(items, name) {
|
|
|
|
some(item) {
|
|
|
|
attr::get_meta_item_list(item)
|
|
|
|
}
|
|
|
|
none. { none }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn name_value_str_pair(
|
|
|
|
item: @ast::meta_item
|
|
|
|
) -> option<(str, str)> {
|
|
|
|
alt attr::get_meta_item_value_str(item) {
|
|
|
|
some(value) {
|
|
|
|
let name = attr::get_meta_item_name(item);
|
|
|
|
some((name, value))
|
|
|
|
}
|
|
|
|
none. { none }
|
2012-01-16 18:27:28 -06:00
|
|
|
}
|
2012-01-16 00:07:23 -06:00
|
|
|
}
|
|
|
|
|
2012-01-16 02:15:03 -06:00
|
|
|
fn parse_fn_(
|
|
|
|
items: [@ast::meta_item]
|
2012-01-16 18:27:28 -06:00
|
|
|
) -> fn_attrs {
|
2012-01-16 22:00:46 -06:00
|
|
|
let brief = meta_item_value_from_list(items, "brief");
|
|
|
|
let desc = meta_item_value_from_list(items, "desc");
|
|
|
|
let return = meta_item_value_from_list(items, "return");
|
|
|
|
|
|
|
|
let args = alt meta_item_list_from_list(items, "args") {
|
|
|
|
some(items) {
|
|
|
|
vec::filter_map(items) {|item|
|
|
|
|
option::map(name_value_str_pair(item)) { |pair|
|
|
|
|
{
|
2012-01-16 23:07:31 -06:00
|
|
|
name: util::fst(pair),
|
|
|
|
desc: util::snd(pair)
|
2012-01-15 23:50:55 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-01-16 22:00:46 -06:00
|
|
|
}
|
|
|
|
none. { [] }
|
|
|
|
};
|
2012-01-15 23:50:55 -06:00
|
|
|
|
2012-01-16 18:27:28 -06:00
|
|
|
{
|
2012-01-16 18:01:33 -06:00
|
|
|
brief: brief,
|
2012-01-15 23:50:55 -06:00
|
|
|
desc: desc,
|
2012-01-16 22:00:46 -06:00
|
|
|
args: args,
|
2012-01-16 18:27:28 -06:00
|
|
|
return: return
|
|
|
|
}
|
2012-01-15 23:50:55 -06:00
|
|
|
}
|
2012-01-16 00:47:23 -06:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
|
|
|
|
fn parse_attributes(source: str) -> [ast::attribute] {
|
|
|
|
import rustc::syntax::parse::parser;
|
2012-01-16 22:00:46 -06:00
|
|
|
// FIXME: Uncommenting this results in rustc bugs
|
|
|
|
//import rustc::syntax::codemap;
|
|
|
|
import rustc::driver::diagnostic;
|
2012-01-16 00:47:23 -06:00
|
|
|
|
2012-01-16 22:00:46 -06:00
|
|
|
let cm = rustc::syntax::codemap::new_codemap();
|
2012-01-16 00:47:23 -06:00
|
|
|
let parse_sess = @{
|
|
|
|
cm: cm,
|
|
|
|
mutable next_id: 0,
|
|
|
|
diagnostic: diagnostic::mk_handler(cm, none)
|
|
|
|
};
|
|
|
|
let parser = parser::new_parser_from_source_str(
|
|
|
|
parse_sess, [], "-", source);
|
|
|
|
|
|
|
|
parser::parse_outer_attributes(parser)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parse_fn_should_handle_undocumented_functions() {
|
|
|
|
let source = "";
|
|
|
|
let attrs = parse_attributes(source);
|
2012-01-16 18:27:28 -06:00
|
|
|
let attrs = parse_fn(attrs);
|
|
|
|
assert attrs.brief == none;
|
|
|
|
assert attrs.desc == none;
|
|
|
|
assert attrs.return == none;
|
|
|
|
assert vec::len(attrs.args) == 0u;
|
2012-01-16 00:47:23 -06:00
|
|
|
}
|
|
|
|
|
2012-01-16 22:00:46 -06:00
|
|
|
#[test]
|
2012-01-16 00:47:23 -06:00
|
|
|
fn parse_fn_should_parse_simple_doc_attributes() {
|
|
|
|
let source = "#[doc = \"basic\"]";
|
|
|
|
let attrs = parse_attributes(source);
|
2012-01-16 18:27:28 -06:00
|
|
|
let attrs = parse_fn(attrs);
|
2012-01-16 22:00:46 -06:00
|
|
|
assert attrs.desc == some("basic");
|
2012-01-16 00:47:23 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parse_fn_should_parse_the_brief_description() {
|
|
|
|
let source = "#[doc(brief = \"short\")]";
|
|
|
|
let attrs = parse_attributes(source);
|
2012-01-16 18:27:28 -06:00
|
|
|
let attrs = parse_fn(attrs);
|
|
|
|
assert attrs.brief == some("short");
|
2012-01-16 00:47:23 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parse_fn_should_parse_the_long_description() {
|
|
|
|
let source = "#[doc(desc = \"description\")]";
|
|
|
|
let attrs = parse_attributes(source);
|
2012-01-16 18:27:28 -06:00
|
|
|
let attrs = parse_fn(attrs);
|
|
|
|
assert attrs.desc == some("description");
|
2012-01-16 00:47:23 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parse_fn_should_parse_the_return_value_description() {
|
|
|
|
let source = "#[doc(return = \"return value\")]";
|
|
|
|
let attrs = parse_attributes(source);
|
2012-01-16 18:27:28 -06:00
|
|
|
let attrs = parse_fn(attrs);
|
|
|
|
assert attrs.return == some("return value");
|
2012-01-16 00:47:23 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parse_fn_should_parse_the_argument_descriptions() {
|
|
|
|
let source = "#[doc(args(a = \"arg a\", b = \"arg b\"))]";
|
|
|
|
let attrs = parse_attributes(source);
|
2012-01-16 18:27:28 -06:00
|
|
|
let attrs = parse_fn(attrs);
|
|
|
|
assert attrs.args[0] == {name: "a", desc: "arg a"};
|
|
|
|
assert attrs.args[1] == {name: "b", desc: "arg b"};
|
2012-01-16 00:47:23 -06:00
|
|
|
}
|
|
|
|
}
|