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-05-13 19:12:56 -05:00
|
|
|
import syntax::ast;
|
|
|
|
import syntax::attr;
|
2012-01-17 12:43:29 -06:00
|
|
|
import core::tuple;
|
2012-01-16 15:22:25 -06:00
|
|
|
|
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;
|
2012-01-18 16:20:26 -06:00
|
|
|
|
|
|
|
type crate_attrs = {
|
2012-08-20 14:23:37 -05:00
|
|
|
name: Option<~str>
|
2012-01-18 16:20:26 -06:00
|
|
|
};
|
2012-01-15 23:50:55 -06:00
|
|
|
|
2012-01-25 19:31:50 -06:00
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
|
2012-07-14 00:57:48 -05:00
|
|
|
fn parse_attributes(source: ~str) -> ~[ast::attribute] {
|
2012-05-13 19:12:56 -05:00
|
|
|
import syntax::parse;
|
|
|
|
import parse::parser;
|
2012-05-24 15:44:42 -05:00
|
|
|
import parse::attr::parser_attr;
|
2012-05-13 19:12:56 -05:00
|
|
|
import syntax::codemap;
|
|
|
|
import syntax::diagnostic;
|
2012-01-25 19:31:50 -06:00
|
|
|
|
2012-08-20 14:23:37 -05:00
|
|
|
let parse_sess = syntax::parse::new_parse_sess(None);
|
2012-04-18 01:34:48 -05:00
|
|
|
let parser = parse::new_parser_from_source_str(
|
2012-07-14 00:57:48 -05:00
|
|
|
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(
|
2012-06-29 18:26:56 -05:00
|
|
|
attrs: ~[ast::attribute]
|
2012-08-20 14:23:37 -05:00
|
|
|
) -> Option<@ast::meta_item> {
|
2012-01-18 16:06:22 -06:00
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/*!
|
|
|
|
* Given a vec of attributes, extract the meta_items contained in the \
|
|
|
|
* doc attribute
|
|
|
|
*/
|
2012-01-18 16:06:22 -06:00
|
|
|
|
2012-07-14 00:57:48 -05:00
|
|
|
let doc_attrs = attr::find_attrs_by_name(attrs, ~"doc");
|
2012-07-02 16:44:31 -05:00
|
|
|
let doc_metas = do doc_attrs.map |attr| {
|
2012-06-30 05:54:54 -05:00
|
|
|
attr::attr_meta(attr::desugar_doc_attr(attr))
|
|
|
|
};
|
|
|
|
|
2012-01-16 22:00:46 -06:00
|
|
|
if vec::is_not_empty(doc_metas) {
|
|
|
|
if vec::len(doc_metas) != 1u {
|
2012-08-22 19:24:52 -05:00
|
|
|
warn!("ignoring %u doc attributes", vec::len(doc_metas) - 1u);
|
2012-01-16 22:00:46 -06:00
|
|
|
}
|
2012-08-20 14:23:37 -05:00
|
|
|
Some(doc_metas[0])
|
2012-01-16 22:00:46 -06:00
|
|
|
} else {
|
2012-08-20 14:23:37 -05:00
|
|
|
None
|
2012-01-16 22:00:46 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-29 18:26:56 -05:00
|
|
|
fn parse_crate(attrs: ~[ast::attribute]) -> crate_attrs {
|
2012-01-18 16:20:26 -06:00
|
|
|
let link_metas = attr::find_linkage_metas(attrs);
|
|
|
|
|
|
|
|
{
|
2012-07-18 18:18:02 -05:00
|
|
|
name: attr::last_meta_item_value_str_by_name(link_metas, ~"name")
|
2012-01-18 16:20:26 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_extract_crate_name_from_link_attribute() {
|
2012-07-14 00:57:48 -05:00
|
|
|
let source = ~"#[link(name = \"snuggles\")]";
|
2012-01-18 16:20:26 -06:00
|
|
|
let attrs = test::parse_attributes(source);
|
|
|
|
let attrs = parse_crate(attrs);
|
2012-08-20 14:23:37 -05:00
|
|
|
assert attrs.name == Some(~"snuggles");
|
2012-01-18 16:20:26 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_not_extract_crate_name_if_no_link_attribute() {
|
2012-07-14 00:57:48 -05:00
|
|
|
let source = ~"";
|
2012-01-18 16:20:26 -06:00
|
|
|
let attrs = test::parse_attributes(source);
|
|
|
|
let attrs = parse_crate(attrs);
|
2012-08-20 14:23:37 -05:00
|
|
|
assert attrs.name == None;
|
2012-01-18 16:20:26 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_not_extract_crate_name_if_no_name_value_in_link_attribute() {
|
2012-07-14 00:57:48 -05:00
|
|
|
let source = ~"#[link(whatever)]";
|
2012-01-18 16:20:26 -06:00
|
|
|
let attrs = test::parse_attributes(source);
|
|
|
|
let attrs = parse_crate(attrs);
|
2012-08-20 14:23:37 -05:00
|
|
|
assert attrs.name == None;
|
2012-01-18 16:20:26 -06:00
|
|
|
}
|
|
|
|
|
2012-08-20 14:23:37 -05:00
|
|
|
fn parse_desc(attrs: ~[ast::attribute]) -> Option<~str> {
|
2012-08-06 14:34:08 -05:00
|
|
|
match doc_meta(attrs) {
|
2012-08-20 14:23:37 -05:00
|
|
|
Some(meta) => {
|
2012-07-18 18:18:02 -05:00
|
|
|
attr::get_meta_item_value_str(meta)
|
2012-03-09 20:12:15 -06:00
|
|
|
}
|
2012-08-20 14:23:37 -05:00
|
|
|
None => None
|
2012-03-09 20:12:15 -06:00
|
|
|
}
|
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-03-09 20:12:15 -06:00
|
|
|
fn parse_desc_should_handle_undocumented_mods() {
|
2012-07-14 00:57:48 -05:00
|
|
|
let source = ~"";
|
2012-01-18 20:02:56 -06:00
|
|
|
let attrs = test::parse_attributes(source);
|
2012-03-09 20:12:15 -06:00
|
|
|
let attrs = parse_desc(attrs);
|
2012-08-20 14:23:37 -05:00
|
|
|
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-07-14 00:57:48 -05:00
|
|
|
let source = ~"#[doc = \"basic\"]";
|
2012-01-18 20:02:56 -06:00
|
|
|
let attrs = test::parse_attributes(source);
|
2012-03-09 20:12:15 -06:00
|
|
|
let attrs = parse_desc(attrs);
|
2012-08-20 14:23:37 -05:00
|
|
|
assert attrs == Some(~"basic");
|
2012-01-18 20:02:56 -06:00
|
|
|
}
|
|
|
|
|
2012-06-29 18:26:56 -05:00
|
|
|
fn parse_hidden(attrs: ~[ast::attribute]) -> bool {
|
2012-08-06 14:34:08 -05:00
|
|
|
match doc_meta(attrs) {
|
2012-08-20 14:23:37 -05:00
|
|
|
Some(meta) => {
|
2012-08-06 14:34:08 -05:00
|
|
|
match attr::get_meta_item_list(meta) {
|
2012-08-20 14:23:37 -05:00
|
|
|
Some(metas) => {
|
2012-07-14 00:57:48 -05:00
|
|
|
let hiddens = attr::find_meta_items_by_name(metas, ~"hidden");
|
2012-03-09 20:12:15 -06:00
|
|
|
vec::is_not_empty(hiddens)
|
2012-02-17 18:52:27 -06:00
|
|
|
}
|
2012-08-20 14:23:37 -05:00
|
|
|
None => false
|
2012-02-17 18:52:27 -06:00
|
|
|
}
|
|
|
|
}
|
2012-08-20 14:23:37 -05: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() {
|
2012-07-14 00:57:48 -05:00
|
|
|
let source = ~"#[doc(hidden)]";
|
2012-03-07 16:49:52 -06:00
|
|
|
let attrs = test::parse_attributes(source);
|
|
|
|
assert parse_hidden(attrs) == true;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn shoulde_not_parse_non_hidden_attribute() {
|
2012-07-14 00:57:48 -05:00
|
|
|
let source = ~"#[doc = \"\"]";
|
2012-03-07 16:49:52 -06:00
|
|
|
let attrs = test::parse_attributes(source);
|
|
|
|
assert parse_hidden(attrs) == false;
|
|
|
|
}
|