2012-01-17 19:44:32 -06:00
|
|
|
#[doc(
|
|
|
|
brief = "Attribute parsing",
|
|
|
|
desc =
|
|
|
|
"The attribute parser provides methods for pulling documentation out of \
|
|
|
|
an AST's attributes."
|
|
|
|
)];
|
|
|
|
|
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-17 12:43:29 -06:00
|
|
|
import core::tuple;
|
2012-01-16 15:22:25 -06:00
|
|
|
|
2012-01-24 02:20:15 -06:00
|
|
|
export crate_attrs, mod_attrs, fn_attrs, arg_attrs, const_attrs;
|
|
|
|
export parse_crate, parse_mod, parse_fn, parse_const;
|
2012-01-18 16:20:26 -06:00
|
|
|
|
|
|
|
type crate_attrs = {
|
2012-01-23 18:23:08 -06:00
|
|
|
name: option<str>
|
2012-01-18 16:20:26 -06:00
|
|
|
};
|
2012-01-15 23:50:55 -06:00
|
|
|
|
2012-01-18 20:02:56 -06:00
|
|
|
type mod_attrs = {
|
|
|
|
brief: option<str>,
|
|
|
|
desc: option<str>
|
|
|
|
};
|
|
|
|
|
2012-01-16 18:27:28 -06:00
|
|
|
type fn_attrs = {
|
|
|
|
brief: option<str>,
|
|
|
|
desc: option<str>,
|
|
|
|
args: [arg_attrs],
|
2012-01-24 20:02:33 -06:00
|
|
|
return: option<str>,
|
|
|
|
failure: option<str>
|
2012-01-16 18:27:28 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
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-24 02:20:15 -06:00
|
|
|
type const_attrs = {
|
|
|
|
brief: option<str>,
|
|
|
|
desc: option<str>
|
|
|
|
};
|
|
|
|
|
2012-01-16 22:00:46 -06:00
|
|
|
fn doc_meta(
|
|
|
|
attrs: [ast::attribute]
|
|
|
|
) -> option<@ast::meta_item> {
|
2012-01-18 16:06:22 -06:00
|
|
|
|
|
|
|
#[doc =
|
|
|
|
"Given a vec of attributes, extract the meta_items contained in the \
|
|
|
|
doc attribute"];
|
|
|
|
|
2012-01-16 22:00:46 -06:00
|
|
|
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-18 16:20:26 -06:00
|
|
|
fn parse_crate(attrs: [ast::attribute]) -> crate_attrs {
|
|
|
|
let link_metas = attr::find_linkage_metas(attrs);
|
|
|
|
|
|
|
|
{
|
2012-01-23 18:23:08 -06:00
|
|
|
name: attr::meta_item_value_from_list(link_metas, "name")
|
2012-01-18 16:20:26 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_extract_crate_name_from_link_attribute() {
|
|
|
|
let source = "#[link(name = \"snuggles\")]";
|
|
|
|
let attrs = test::parse_attributes(source);
|
|
|
|
let attrs = parse_crate(attrs);
|
|
|
|
assert attrs.name == some("snuggles");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_not_extract_crate_name_if_no_link_attribute() {
|
|
|
|
let source = "";
|
|
|
|
let attrs = test::parse_attributes(source);
|
|
|
|
let attrs = parse_crate(attrs);
|
|
|
|
assert attrs.name == none;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_not_extract_crate_name_if_no_name_value_in_link_attribute() {
|
|
|
|
let source = "#[link(whatever)]";
|
|
|
|
let attrs = test::parse_attributes(source);
|
|
|
|
let attrs = parse_crate(attrs);
|
|
|
|
assert attrs.name == none;
|
|
|
|
}
|
|
|
|
|
2012-01-18 20:02:56 -06:00
|
|
|
fn parse_mod(attrs: [ast::attribute]) -> mod_attrs {
|
|
|
|
parse_short_doc_or(
|
|
|
|
attrs,
|
|
|
|
{|desc|
|
|
|
|
{
|
|
|
|
brief: none,
|
|
|
|
desc: desc
|
|
|
|
}
|
|
|
|
},
|
|
|
|
parse_mod_long_doc
|
|
|
|
)
|
|
|
|
}
|
2012-01-16 00:07:23 -06:00
|
|
|
|
2012-01-18 20:02:56 -06:00
|
|
|
fn parse_mod_long_doc(
|
|
|
|
_items: [@ast::meta_item],
|
|
|
|
brief: option<str>,
|
|
|
|
desc: option<str>
|
|
|
|
) -> mod_attrs {
|
|
|
|
{
|
|
|
|
brief: brief,
|
|
|
|
desc: desc
|
|
|
|
}
|
|
|
|
}
|
2012-01-16 22:00:46 -06:00
|
|
|
|
2012-01-18 20:02:56 -06:00
|
|
|
#[test]
|
|
|
|
fn parse_mod_should_handle_undocumented_mods() {
|
|
|
|
let source = "";
|
|
|
|
let attrs = test::parse_attributes(source);
|
|
|
|
let attrs = parse_mod(attrs);
|
|
|
|
assert attrs.brief == none;
|
|
|
|
assert attrs.desc == none;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parse_mod_should_parse_simple_doc_attributes() {
|
|
|
|
let source = "#[doc = \"basic\"]";
|
|
|
|
let attrs = test::parse_attributes(source);
|
|
|
|
let attrs = parse_mod(attrs);
|
|
|
|
assert attrs.desc == some("basic");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parse_mod_should_parse_the_brief_description() {
|
|
|
|
let source = "#[doc(brief = \"short\")]";
|
|
|
|
let attrs = test::parse_attributes(source);
|
|
|
|
let attrs = parse_mod(attrs);
|
|
|
|
assert attrs.brief == some("short");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parse_mod_should_parse_the_long_description() {
|
|
|
|
let source = "#[doc(desc = \"description\")]";
|
|
|
|
let attrs = test::parse_attributes(source);
|
|
|
|
let attrs = parse_mod(attrs);
|
|
|
|
assert attrs.desc == some("description");
|
|
|
|
}
|
|
|
|
|
|
|
|
fn parse_short_doc_or<T>(
|
|
|
|
attrs: [ast::attribute],
|
|
|
|
handle_short: fn&(
|
|
|
|
short_desc: option<str>
|
|
|
|
) -> T,
|
|
|
|
parse_long: fn&(
|
|
|
|
doc_items: [@ast::meta_item],
|
|
|
|
brief: option<str>,
|
|
|
|
desc: option<str>
|
|
|
|
) -> T
|
|
|
|
) -> T {
|
|
|
|
alt doc_meta(attrs) {
|
2012-01-16 22:00:46 -06:00
|
|
|
some(meta) {
|
|
|
|
alt attr::get_meta_item_value_str(meta) {
|
2012-01-18 20:02:56 -06:00
|
|
|
some(desc) { handle_short(some(desc)) }
|
2012-01-19 00:37:22 -06:00
|
|
|
none {
|
2012-01-16 22:00:46 -06:00
|
|
|
alt attr::get_meta_item_list(meta) {
|
|
|
|
some(list) {
|
2012-01-18 20:02:56 -06:00
|
|
|
let brief = attr::meta_item_value_from_list(list, "brief");
|
|
|
|
let desc = attr::meta_item_value_from_list(list, "desc");
|
|
|
|
parse_long(list, brief, desc)
|
2012-01-16 22:00:46 -06:00
|
|
|
}
|
2012-01-19 00:37:22 -06:00
|
|
|
none {
|
2012-01-18 20:02:56 -06:00
|
|
|
handle_short(none)
|
2012-01-16 22:00:46 -06:00
|
|
|
}
|
|
|
|
}
|
2012-01-16 00:07:23 -06:00
|
|
|
}
|
|
|
|
}
|
2012-01-16 22:00:46 -06:00
|
|
|
}
|
2012-01-19 00:37:22 -06:00
|
|
|
none {
|
2012-01-18 20:02:56 -06:00
|
|
|
handle_short(none)
|
2012-01-16 22:00:46 -06:00
|
|
|
}
|
2012-01-18 20:02:56 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn parse_fn(
|
|
|
|
attrs: [ast::attribute]
|
|
|
|
) -> fn_attrs {
|
|
|
|
|
|
|
|
parse_short_doc_or(
|
|
|
|
attrs,
|
|
|
|
{|desc|
|
|
|
|
{
|
|
|
|
brief: none,
|
|
|
|
desc: desc,
|
|
|
|
args: [],
|
2012-01-24 20:02:33 -06:00
|
|
|
return: none,
|
|
|
|
failure: none
|
2012-01-18 20:02:56 -06:00
|
|
|
}
|
|
|
|
},
|
|
|
|
parse_fn_long_doc
|
|
|
|
)
|
2012-01-16 22:00:46 -06:00
|
|
|
}
|
|
|
|
|
2012-01-18 20:02:56 -06:00
|
|
|
fn parse_fn_long_doc(
|
|
|
|
items: [@ast::meta_item],
|
|
|
|
brief: option<str>,
|
|
|
|
desc: option<str>
|
2012-01-16 18:27:28 -06:00
|
|
|
) -> fn_attrs {
|
2012-01-16 23:12:08 -06:00
|
|
|
let return = attr::meta_item_value_from_list(items, "return");
|
2012-01-24 20:02:33 -06:00
|
|
|
let failure = attr::meta_item_value_from_list(items, "failure");
|
2012-01-16 23:12:08 -06:00
|
|
|
let args = alt attr::meta_item_list_from_list(items, "args") {
|
2012-01-16 22:00:46 -06:00
|
|
|
some(items) {
|
|
|
|
vec::filter_map(items) {|item|
|
2012-01-16 23:12:08 -06:00
|
|
|
option::map(attr::name_value_str_pair(item)) { |pair|
|
2012-01-16 22:00:46 -06:00
|
|
|
{
|
2012-01-17 12:14:05 -06:00
|
|
|
name: tuple::first(pair),
|
|
|
|
desc: tuple::second(pair)
|
2012-01-15 23:50:55 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-01-16 22:00:46 -06:00
|
|
|
}
|
2012-01-19 00:37:22 -06:00
|
|
|
none { [] }
|
2012-01-16 22:00:46 -06:00
|
|
|
};
|
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-24 20:02:33 -06:00
|
|
|
return: return,
|
|
|
|
failure: failure
|
2012-01-16 18:27:28 -06:00
|
|
|
}
|
2012-01-15 23:50:55 -06:00
|
|
|
}
|
2012-01-16 00:47:23 -06:00
|
|
|
|
2012-01-18 20:02:56 -06:00
|
|
|
#[test]
|
|
|
|
fn parse_fn_should_handle_undocumented_functions() {
|
|
|
|
let source = "";
|
|
|
|
let attrs = test::parse_attributes(source);
|
|
|
|
let attrs = parse_fn(attrs);
|
|
|
|
assert attrs.brief == none;
|
|
|
|
assert attrs.desc == none;
|
|
|
|
assert attrs.return == none;
|
|
|
|
assert vec::len(attrs.args) == 0u;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parse_fn_should_parse_simple_doc_attributes() {
|
|
|
|
let source = "#[doc = \"basic\"]";
|
|
|
|
let attrs = test::parse_attributes(source);
|
|
|
|
let attrs = parse_fn(attrs);
|
|
|
|
assert attrs.desc == some("basic");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parse_fn_should_parse_the_brief_description() {
|
|
|
|
let source = "#[doc(brief = \"short\")]";
|
|
|
|
let attrs = test::parse_attributes(source);
|
|
|
|
let attrs = parse_fn(attrs);
|
|
|
|
assert attrs.brief == some("short");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parse_fn_should_parse_the_long_description() {
|
|
|
|
let source = "#[doc(desc = \"description\")]";
|
|
|
|
let attrs = test::parse_attributes(source);
|
|
|
|
let attrs = parse_fn(attrs);
|
|
|
|
assert attrs.desc == some("description");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parse_fn_should_parse_the_return_value_description() {
|
|
|
|
let source = "#[doc(return = \"return value\")]";
|
|
|
|
let attrs = test::parse_attributes(source);
|
|
|
|
let attrs = parse_fn(attrs);
|
|
|
|
assert attrs.return == some("return value");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parse_fn_should_parse_the_argument_descriptions() {
|
|
|
|
let source = "#[doc(args(a = \"arg a\", b = \"arg b\"))]";
|
|
|
|
let attrs = test::parse_attributes(source);
|
|
|
|
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-24 20:02:33 -06:00
|
|
|
#[test]
|
|
|
|
fn parse_fn_should_parse_failure_conditions() {
|
|
|
|
let source = "#[doc(failure = \"it's the fail\")]";
|
|
|
|
let attrs = test::parse_attributes(source);
|
|
|
|
let attrs = parse_fn(attrs);
|
|
|
|
assert attrs.failure == some("it's the fail");
|
|
|
|
}
|
|
|
|
|
2012-01-24 02:20:15 -06:00
|
|
|
fn parse_const(attrs: [ast::attribute]) -> const_attrs {
|
|
|
|
parse_short_doc_or(
|
|
|
|
attrs,
|
|
|
|
{|desc|
|
|
|
|
{
|
|
|
|
brief: none,
|
|
|
|
desc: desc
|
|
|
|
}
|
|
|
|
},
|
|
|
|
parse_const_long_doc
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn parse_const_long_doc(
|
|
|
|
_items: [@ast::meta_item],
|
|
|
|
brief: option<str>,
|
|
|
|
desc: option<str>
|
|
|
|
) -> const_attrs {
|
|
|
|
{
|
|
|
|
brief: brief,
|
|
|
|
desc: desc
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_parse_const_short_doc() {
|
|
|
|
let source = "#[doc = \"description\"]";
|
|
|
|
let attrs = test::parse_attributes(source);
|
|
|
|
let attrs = parse_fn(attrs);
|
|
|
|
assert attrs.desc == some("description");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_parse_const_long_doc() {
|
|
|
|
let source = "#[doc(brief = \"a\", desc = \"b\")]";
|
|
|
|
let attrs = test::parse_attributes(source);
|
|
|
|
let attrs = parse_fn(attrs);
|
|
|
|
assert attrs.brief == some("a");
|
|
|
|
assert attrs.desc == some("b");
|
|
|
|
}
|
|
|
|
|
2012-01-16 00:47:23 -06:00
|
|
|
#[cfg(test)]
|
2012-01-18 16:20:26 -06:00
|
|
|
mod test {
|
2012-01-16 00:47:23 -06:00
|
|
|
|
|
|
|
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-25 01:24:31 -06:00
|
|
|
let handler = diagnostic::mk_handler(none);
|
2012-01-16 00:47:23 -06:00
|
|
|
let parse_sess = @{
|
|
|
|
cm: cm,
|
|
|
|
mutable next_id: 0,
|
2012-01-25 01:24:31 -06:00
|
|
|
span_diagnostic: diagnostic::mk_span_handler(handler, cm),
|
2012-01-22 18:24:55 -06:00
|
|
|
mutable chpos: 0u,
|
|
|
|
mutable byte_pos: 0u
|
2012-01-16 00:47:23 -06:00
|
|
|
};
|
|
|
|
let parser = parser::new_parser_from_source_str(
|
|
|
|
parse_sess, [], "-", source);
|
|
|
|
|
|
|
|
parser::parse_outer_attributes(parser)
|
|
|
|
}
|
2012-01-17 12:14:05 -06:00
|
|
|
}
|