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-03-09 19:23:56 -06:00
|
|
|
export crate_attrs, basic_attrs, variant_attrs;
|
|
|
|
export parse_crate, parse_basic, parse_variant;
|
2012-03-07 16:49:52 -06:00
|
|
|
export parse_hidden;
|
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-02-17 18:52:27 -06:00
|
|
|
type basic_attrs = {
|
2012-01-18 20:02:56 -06:00
|
|
|
brief: option<str>,
|
|
|
|
desc: option<str>
|
|
|
|
};
|
|
|
|
|
2012-01-25 19:31:50 -06:00
|
|
|
type variant_attrs = {
|
|
|
|
desc: option<str>
|
|
|
|
};
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
|
|
|
|
fn parse_attributes(source: str) -> [ast::attribute] {
|
|
|
|
import rustc::syntax::parse::parser;
|
2012-02-05 18:38:18 -06:00
|
|
|
import rustc::syntax::codemap;
|
2012-01-25 19:31:50 -06:00
|
|
|
import rustc::driver::diagnostic;
|
|
|
|
|
2012-02-05 18:38:18 -06:00
|
|
|
let cm = codemap::new_codemap();
|
2012-01-25 19:31:50 -06:00
|
|
|
let handler = diagnostic::mk_handler(none);
|
|
|
|
let parse_sess = @{
|
|
|
|
cm: cm,
|
|
|
|
mutable next_id: 0,
|
|
|
|
span_diagnostic: diagnostic::mk_span_handler(handler, cm),
|
|
|
|
mutable chpos: 0u,
|
|
|
|
mutable byte_pos: 0u
|
|
|
|
};
|
|
|
|
let parser = parser::new_parser_from_source_str(
|
2012-02-10 04:03:21 -06:00
|
|
|
parse_sess, [], "-", codemap::fss_none, @source);
|
2012-01-25 19:31:50 -06:00
|
|
|
|
|
|
|
parser::parse_outer_attributes(parser)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-25 19:35:39 -06:00
|
|
|
fn parse_basic(
|
|
|
|
attrs: [ast::attribute]
|
|
|
|
) -> {
|
|
|
|
brief: option<str>,
|
|
|
|
desc: option<str>
|
|
|
|
} {
|
2012-01-18 20:02:56 -06:00
|
|
|
parse_short_doc_or(
|
|
|
|
attrs,
|
|
|
|
{|desc|
|
|
|
|
{
|
|
|
|
brief: none,
|
|
|
|
desc: desc
|
|
|
|
}
|
|
|
|
},
|
2012-01-25 19:35:39 -06:00
|
|
|
{|_items, brief, desc|
|
|
|
|
{
|
|
|
|
brief: brief,
|
|
|
|
desc: desc
|
|
|
|
}
|
|
|
|
}
|
2012-01-18 20:02:56 -06:00
|
|
|
)
|
|
|
|
}
|
2012-01-16 00:07:23 -06:00
|
|
|
|
2012-01-18 20:02:56 -06:00
|
|
|
#[test]
|
2012-02-17 18:52:27 -06:00
|
|
|
fn parse_basic_should_handle_undocumented_mods() {
|
2012-01-18 20:02:56 -06:00
|
|
|
let source = "";
|
|
|
|
let attrs = test::parse_attributes(source);
|
2012-02-17 18:52:27 -06:00
|
|
|
let attrs = parse_basic(attrs);
|
2012-01-18 20:02:56 -06:00
|
|
|
assert attrs.brief == none;
|
|
|
|
assert attrs.desc == none;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2012-02-17 18:52:27 -06:00
|
|
|
fn parse_basic_should_parse_simple_doc_attributes() {
|
2012-01-18 20:02:56 -06:00
|
|
|
let source = "#[doc = \"basic\"]";
|
|
|
|
let attrs = test::parse_attributes(source);
|
2012-02-17 18:52:27 -06:00
|
|
|
let attrs = parse_basic(attrs);
|
2012-01-18 20:02:56 -06:00
|
|
|
assert attrs.desc == some("basic");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2012-02-17 18:52:27 -06:00
|
|
|
fn parse_basic_should_parse_the_brief_description() {
|
2012-01-18 20:02:56 -06:00
|
|
|
let source = "#[doc(brief = \"short\")]";
|
|
|
|
let attrs = test::parse_attributes(source);
|
2012-02-17 18:52:27 -06:00
|
|
|
let attrs = parse_basic(attrs);
|
2012-01-18 20:02:56 -06:00
|
|
|
assert attrs.brief == some("short");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2012-02-17 18:52:27 -06:00
|
|
|
fn parse_basic_should_parse_the_long_description() {
|
2012-01-18 20:02:56 -06:00
|
|
|
let source = "#[doc(desc = \"description\")]";
|
|
|
|
let attrs = test::parse_attributes(source);
|
2012-02-17 18:52:27 -06:00
|
|
|
let attrs = parse_basic(attrs);
|
2012-01-18 20:02:56 -06:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-17 18:52:27 -06:00
|
|
|
fn parse_long_doc<T>(
|
|
|
|
attrs: [ast::attribute],
|
|
|
|
parse_long: fn&(doc_items: [@ast::meta_item]) -> T
|
|
|
|
) -> T {
|
|
|
|
alt doc_meta(attrs) {
|
|
|
|
some(meta) {
|
|
|
|
alt attr::get_meta_item_list(meta) {
|
|
|
|
some(list) {
|
|
|
|
parse_long(list)
|
|
|
|
}
|
|
|
|
none {
|
|
|
|
parse_long([])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
none { parse_long([]) }
|
|
|
|
}
|
|
|
|
}
|
2012-01-18 20:02:56 -06:00
|
|
|
|
2012-01-25 19:48:06 -06:00
|
|
|
fn parse_variant(attrs: [ast::attribute]) -> variant_attrs {
|
|
|
|
parse_short_doc_or(
|
|
|
|
attrs,
|
|
|
|
{|desc|
|
|
|
|
{
|
|
|
|
desc: desc
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{|_items, brief, desc|
|
|
|
|
if option::is_some(brief) && option::is_some(desc) {
|
|
|
|
// FIXME: Warn about dropping brief description
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// Prefer desc over brief
|
|
|
|
desc: option::maybe(brief, desc, {|s| some(s) })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_parse_variant_short_doc() {
|
|
|
|
let source = "#[doc = \"a\"]";
|
|
|
|
let attrs = test::parse_attributes(source);
|
|
|
|
let attrs = parse_variant(attrs);
|
|
|
|
assert attrs.desc == some("a");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_parse_variant_brief_doc() {
|
|
|
|
let source = "#[doc(brief = \"a\")]";
|
|
|
|
let attrs = test::parse_attributes(source);
|
|
|
|
let attrs = parse_variant(attrs);
|
|
|
|
assert attrs.desc == some("a");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_parse_variant_long_doc() {
|
|
|
|
let source = "#[doc(desc = \"a\")]";
|
|
|
|
let attrs = test::parse_attributes(source);
|
|
|
|
let attrs = parse_variant(attrs);
|
|
|
|
assert attrs.desc == some("a");
|
|
|
|
}
|
2012-01-26 23:20:17 -06:00
|
|
|
|
2012-03-07 16:49:52 -06:00
|
|
|
fn parse_hidden(attrs: [ast::attribute]) -> bool {
|
|
|
|
parse_short_doc_or(
|
|
|
|
attrs,
|
|
|
|
{|_desc| false },
|
|
|
|
{|metas, _brief, _desc|
|
|
|
|
let hiddens = attr::find_meta_items_by_name(metas, "hidden");
|
|
|
|
vec::is_not_empty(hiddens)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn shoulde_parse_hidden_attribute() {
|
|
|
|
let source = "#[doc(hidden)]";
|
|
|
|
let attrs = test::parse_attributes(source);
|
|
|
|
assert parse_hidden(attrs) == true;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn shoulde_not_parse_non_hidden_attribute() {
|
|
|
|
let source = "#[doc = \"\"]";
|
|
|
|
let attrs = test::parse_attributes(source);
|
|
|
|
assert parse_hidden(attrs) == false;
|
|
|
|
}
|