2012-12-03 18:48:01 -06:00
|
|
|
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2012-09-20 18:09:46 -05:00
|
|
|
/*!
|
|
|
|
Attribute parsing
|
|
|
|
|
|
|
|
The attribute parser provides methods for pulling documentation out of
|
|
|
|
an AST's attributes.
|
|
|
|
*/
|
2012-01-17 19:44:32 -06:00
|
|
|
|
2013-01-08 21:37:25 -06:00
|
|
|
use core::prelude::*;
|
|
|
|
|
2012-12-23 16:41:37 -06:00
|
|
|
use core::str;
|
|
|
|
use core::tuple;
|
|
|
|
use core::vec;
|
2012-09-05 12:41:47 -05:00
|
|
|
use syntax::ast;
|
|
|
|
use syntax::attr;
|
2012-12-23 16:41:37 -06:00
|
|
|
use syntax::codemap;
|
|
|
|
use syntax;
|
2012-01-16 15:22:25 -06:00
|
|
|
|
2012-11-19 20:00:12 -06:00
|
|
|
pub type CrateAttrs = {
|
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-12-29 19:38:20 -06:00
|
|
|
use syntax::ast;
|
|
|
|
use syntax;
|
|
|
|
|
2013-01-08 21:37:25 -06:00
|
|
|
use core::option::None;
|
|
|
|
|
|
|
|
pub fn parse_attributes(+source: ~str) -> ~[ast::attribute] {
|
2012-09-07 20:08:21 -05:00
|
|
|
use syntax::parse;
|
2012-12-13 17:01:57 -06:00
|
|
|
use syntax::parse::parser;
|
|
|
|
use syntax::parse::attr::parser_attr;
|
2012-09-07 20:08:21 -05:00
|
|
|
use syntax::codemap;
|
|
|
|
use 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-11-12 20:59:37 -06:00
|
|
|
parse_sess, ~[], ~"-", codemap::FssNone, @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-09-19 20:50:41 -05:00
|
|
|
fn doc_metas(
|
2012-11-19 20:48:46 -06:00
|
|
|
+attrs: ~[ast::attribute]
|
2012-09-20 20:15:39 -05:00
|
|
|
) -> ~[@ast::meta_item] {
|
2012-09-19 20:50:41 -05:00
|
|
|
|
|
|
|
let doc_attrs = attr::find_attrs_by_name(attrs, ~"doc");
|
|
|
|
let doc_metas = do doc_attrs.map |attr| {
|
|
|
|
attr::attr_meta(attr::desugar_doc_attr(attr))
|
|
|
|
};
|
|
|
|
|
|
|
|
return doc_metas;
|
2012-01-16 22:00:46 -06:00
|
|
|
}
|
|
|
|
|
2012-11-19 20:48:46 -06:00
|
|
|
pub fn parse_crate(+attrs: ~[ast::attribute]) -> CrateAttrs {
|
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-11-19 20:48:46 -06:00
|
|
|
pub fn parse_desc(+attrs: ~[ast::attribute]) -> Option<~str> {
|
2012-10-22 14:56:00 -05:00
|
|
|
let doc_strs = do doc_metas(attrs).filter_map |meta| {
|
|
|
|
attr::get_meta_item_value_str(*meta)
|
|
|
|
};
|
|
|
|
if doc_strs.is_empty() {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(str::connect(doc_strs, "\n"))
|
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-11-19 20:48:46 -06:00
|
|
|
pub fn parse_hidden(+attrs: ~[ast::attribute]) -> bool {
|
2012-09-19 20:50:41 -05:00
|
|
|
do doc_metas(attrs).find |meta| {
|
2012-12-17 22:36:12 -06: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");
|
2013-01-24 22:24:57 -06:00
|
|
|
!hiddens.is_empty()
|
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-09-19 20:50:41 -05:00
|
|
|
}.is_some()
|
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]
|
2012-09-19 20:30:14 -05:00
|
|
|
fn should_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;
|
|
|
|
}
|
|
|
|
|
2012-09-19 20:50:41 -05:00
|
|
|
#[test]
|
|
|
|
fn should_parse_hidden_attribute_with_other_docs() {
|
|
|
|
let source = ~"#[doc = \"foo\"] #[doc(hidden)] #[doc = \"foo\"]";
|
|
|
|
let attrs = test::parse_attributes(source);
|
|
|
|
assert parse_hidden(attrs) == true;
|
|
|
|
}
|
|
|
|
|
2012-03-07 16:49:52 -06:00
|
|
|
#[test]
|
2012-09-19 20:30:14 -05:00
|
|
|
fn should_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;
|
|
|
|
}
|
2012-10-22 14:56:00 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn should_concatenate_multiple_doc_comments() {
|
|
|
|
let source = ~"/// foo\n/// bar";
|
|
|
|
let desc = parse_desc(test::parse_attributes(source));
|
|
|
|
assert desc == Some(~"foo\nbar");
|
|
|
|
}
|
|
|
|
|
|
|
|
|