rust/src/rustdoc/attr_parser.rs
Brian Anderson 569467eb0d Merge remote-tracking branch 'Dretch/prettydocs'
Conflicts:
	src/compiletest/errors.rs
	src/libsyntax/parse/attr.rs
	src/libsyntax/parse/comments.rs
	src/test/compile-fail/ambig_impl_unify.rs
	src/test/compile-fail/assign-super.rs
	src/test/compile-fail/bad-for-loop.rs
	src/test/compile-fail/bad-var-env-capture-in-block-arg.rs
	src/test/compile-fail/block-arg-as-stmt-with-value.rs
	src/test/compile-fail/borrowck-assign-comp-idx.rs
	src/test/compile-fail/borrowck-lend-flow.rs
	src/test/compile-fail/borrowck-loan-blocks-move-cc.rs
	src/test/compile-fail/borrowck-loan-blocks-mut-uniq.rs
	src/test/compile-fail/borrowck-loan-rcvr.rs
	src/test/compile-fail/borrowck-loan-vec-content.rs
	src/test/compile-fail/borrowck-mut-vec-as-imm-slice-bad.rs
	src/test/compile-fail/cap-clause-with-stack-closure.rs
	src/test/compile-fail/do1.rs
	src/test/compile-fail/do2.rs
	src/test/compile-fail/empty-vec-trailing-comma.rs
	src/test/compile-fail/evec-subtyping.rs
	src/test/compile-fail/issue-1896.rs
	src/test/compile-fail/issue-2149.rs
	src/test/compile-fail/issue-2150.rs
	src/test/compile-fail/issue-2487-b.rs
	src/test/compile-fail/kindck-implicit-close-over-mut-var.rs
	src/test/compile-fail/liveness-issue-2163.rs
	src/test/compile-fail/liveness-use-in-index-lvalue.rs
	src/test/compile-fail/no-reuse-move-arc.rs
	src/test/compile-fail/no-send-res-ports.rs
	src/test/compile-fail/non-const.rs
	src/test/compile-fail/pure-higher-order.rs
	src/test/compile-fail/pure-loop-body.rs
	src/test/compile-fail/regions-addr-of-upvar-self.rs
	src/test/compile-fail/regions-escape-loop-via-vec.rs
	src/test/compile-fail/regions-scoping.rs
	src/test/compile-fail/seq-args.rs
	src/test/compile-fail/tstate-unsat-in-called-fn-expr.rs
	src/test/compile-fail/tstate-unsat-in-fn-expr.rs
	src/test/compile-fail/vec-add.rs
	src/test/compile-fail/vec-concat-bug.rs
	src/test/compile-fail/vector-no-ann.rs
2012-07-02 15:23:41 -07:00

147 lines
3.6 KiB
Rust

#[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;
import core::tuple;
export crate_attrs;
export parse_crate, parse_desc;
export parse_hidden;
type crate_attrs = {
name: option<str>
};
#[cfg(test)]
mod test {
fn parse_attributes(source: str) -> ~[ast::attribute] {
import syntax::parse;
import parse::parser;
import parse::attr::parser_attr;
import syntax::codemap;
import syntax::diagnostic;
let parse_sess = syntax::parse::new_parse_sess(none);
let parser = parse::new_parser_from_source_str(
parse_sess, ~[], "-", codemap::fss_none, @source);
parser.parse_outer_attributes()
}
}
fn doc_meta(
attrs: ~[ast::attribute]
) -> option<@ast::meta_item> {
#[doc =
"Given a vec of attributes, extract the meta_items contained in the \
doc attribute"];
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))
};
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);
{
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> {
alt doc_meta(attrs) {
some(meta) {
attr::get_meta_item_value_str(meta).map(|x| *x )
}
none { none }
}
}
#[test]
fn parse_desc_should_handle_undocumented_mods() {
let source = "";
let attrs = test::parse_attributes(source);
let attrs = parse_desc(attrs);
assert attrs == none;
}
#[test]
fn parse_desc_should_parse_simple_doc_attributes() {
let source = "#[doc = \"basic\"]";
let attrs = test::parse_attributes(source);
let attrs = parse_desc(attrs);
assert attrs == some("basic");
}
fn parse_hidden(attrs: ~[ast::attribute]) -> bool {
alt doc_meta(attrs) {
some(meta) {
alt attr::get_meta_item_list(meta) {
some(metas) {
let hiddens = attr::find_meta_items_by_name(metas, "hidden");
vec::is_not_empty(hiddens)
}
none { false }
}
}
none { false }
}
}
#[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;
}