rust/src/rustdoc/attr_parser.rs

144 lines
3.5 KiB
Rust
Raw Normal View History

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."
)];
import syntax::ast;
import syntax::attr;
2012-01-17 12:43:29 -06:00
import core::tuple;
2012-03-09 20:12:15 -06:00
export crate_attrs;
export parse_crate, parse_desc;
2012-03-07 16:49:52 -06:00
export parse_hidden;
type crate_attrs = {
name: option<str>
};
2012-01-25 19:31:50 -06:00
#[cfg(test)]
mod test {
fn parse_attributes(source: str) -> [ast::attribute]/~ {
import syntax::parse;
import parse::parser;
2012-05-24 15:44:42 -05:00
import parse::attr::parser_attr;
import syntax::codemap;
import syntax::diagnostic;
2012-01-25 19:31:50 -06:00
2012-06-06 11:50:08 -05:00
let parse_sess = syntax::parse::new_parse_sess(none);
let parser = parse::new_parser_from_source_str(
parse_sess, []/~, "-", codemap::fss_none, @source);
2012-01-25 19:31:50 -06:00
2012-05-24 15:44:42 -05:00
parser.parse_outer_attributes()
2012-01-25 19:31:50 -06:00
}
}
2012-01-16 22:00:46 -06:00
fn doc_meta(
attrs: [ast::attribute]/~
2012-01-16 22:00:46 -06:00
) -> 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
}
}
fn parse_crate(attrs: [ast::attribute]/~) -> crate_attrs {
let link_metas = attr::find_linkage_metas(attrs);
{
2012-06-10 02:49:59 -05:00
name: attr::last_meta_item_value_str_by_name(
link_metas, "name").map({|x|*x})
}
}
#[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;
}
fn parse_desc(attrs: [ast::attribute]/~) -> option<str> {
2012-03-09 20:12:15 -06:00
alt doc_meta(attrs) {
some(meta) {
2012-06-10 02:49:59 -05:00
attr::get_meta_item_value_str(meta).map({|x|*x})
2012-03-09 20:12:15 -06:00
}
none { none }
}
2012-01-18 20:02:56 -06:00
}
2012-01-18 20:02:56 -06:00
#[test]
2012-03-09 20:12:15 -06:00
fn parse_desc_should_handle_undocumented_mods() {
2012-01-18 20:02:56 -06:00
let source = "";
let attrs = test::parse_attributes(source);
2012-03-09 20:12:15 -06:00
let attrs = parse_desc(attrs);
assert attrs == none;
2012-01-18 20:02:56 -06:00
}
#[test]
2012-03-09 20:12:15 -06:00
fn parse_desc_should_parse_simple_doc_attributes() {
2012-01-18 20:02:56 -06:00
let source = "#[doc = \"basic\"]";
let attrs = test::parse_attributes(source);
2012-03-09 20:12:15 -06:00
let attrs = parse_desc(attrs);
assert attrs == some("basic");
2012-01-18 20:02:56 -06:00
}
fn parse_hidden(attrs: [ast::attribute]/~) -> bool {
2012-02-17 18:52:27 -06:00
alt doc_meta(attrs) {
some(meta) {
alt attr::get_meta_item_list(meta) {
2012-03-09 20:12:15 -06:00
some(metas) {
let hiddens = attr::find_meta_items_by_name(metas, "hidden");
vec::is_not_empty(hiddens)
2012-02-17 18:52:27 -06:00
}
2012-03-09 20:12:15 -06:00
none { false }
2012-02-17 18:52:27 -06:00
}
}
2012-03-09 20:12:15 -06:00
none { false }
2012-02-17 18:52:27 -06:00
}
}
2012-01-18 20:02:56 -06:00
2012-03-07 16:49:52 -06:00
#[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;
}