2012-01-16 15:22:25 -06:00
|
|
|
import rustc::syntax::ast;
|
|
|
|
|
2012-01-16 18:27:28 -06:00
|
|
|
export fn_attrs, arg_attrs;
|
2012-01-16 00:00:07 -06:00
|
|
|
export parse_fn;
|
2012-01-15 23:50:55 -06:00
|
|
|
|
2012-01-16 18:27:28 -06:00
|
|
|
type fn_attrs = {
|
|
|
|
brief: option<str>,
|
|
|
|
desc: option<str>,
|
|
|
|
args: [arg_attrs],
|
|
|
|
return: option<str>
|
|
|
|
};
|
|
|
|
|
|
|
|
type arg_attrs = {
|
2012-01-16 02:15:03 -06:00
|
|
|
name: str,
|
2012-01-16 18:27:28 -06:00
|
|
|
desc: str
|
|
|
|
};
|
|
|
|
|
|
|
|
fn parse_fn(
|
2012-01-16 02:15:03 -06:00
|
|
|
attrs: [ast::attribute]
|
2012-01-16 18:27:28 -06:00
|
|
|
) -> fn_attrs {
|
2012-01-16 00:07:23 -06:00
|
|
|
|
2012-01-16 18:27:28 -06:00
|
|
|
for attr in attrs {
|
|
|
|
alt attr.node.value.node {
|
|
|
|
ast::meta_name_value(
|
|
|
|
"doc", {node: ast::lit_str(value), span: _}) {
|
|
|
|
ret {
|
|
|
|
brief: none,
|
|
|
|
desc: some(value),
|
|
|
|
args: [],
|
|
|
|
return: none
|
|
|
|
};
|
|
|
|
}
|
|
|
|
ast::meta_list("doc", docs) {
|
|
|
|
ret parse_fn_(docs);
|
2012-01-16 00:07:23 -06:00
|
|
|
}
|
2012-01-16 21:41:04 -06:00
|
|
|
_ { }
|
2012-01-16 00:07:23 -06:00
|
|
|
}
|
2012-01-16 18:27:28 -06:00
|
|
|
}
|
2012-01-16 00:07:23 -06:00
|
|
|
|
2012-01-16 18:27:28 -06:00
|
|
|
{
|
|
|
|
brief: none,
|
|
|
|
desc: none,
|
|
|
|
args: [],
|
|
|
|
return: none
|
|
|
|
}
|
2012-01-16 00:07:23 -06:00
|
|
|
}
|
|
|
|
|
2012-01-16 02:15:03 -06:00
|
|
|
fn parse_fn_(
|
|
|
|
items: [@ast::meta_item]
|
2012-01-16 18:27:28 -06:00
|
|
|
) -> fn_attrs {
|
2012-01-15 23:50:55 -06:00
|
|
|
let brief = none;
|
|
|
|
let desc = none;
|
|
|
|
let return = none;
|
2012-01-16 15:53:17 -06:00
|
|
|
let argdocs = [];
|
2012-01-15 23:50:55 -06:00
|
|
|
let argdocsfound = none;
|
|
|
|
for item: @ast::meta_item in items {
|
|
|
|
alt item.node {
|
|
|
|
ast::meta_name_value("brief", {node: ast::lit_str(value),
|
|
|
|
span: _}) {
|
|
|
|
brief = some(value);
|
|
|
|
}
|
|
|
|
ast::meta_name_value("desc", {node: ast::lit_str(value),
|
|
|
|
span: _}) {
|
|
|
|
desc = some(value);
|
|
|
|
}
|
|
|
|
ast::meta_name_value("return", {node: ast::lit_str(value),
|
|
|
|
span: _}) {
|
|
|
|
return = some(value);
|
|
|
|
}
|
|
|
|
ast::meta_list("args", args) {
|
|
|
|
argdocsfound = some(args);
|
|
|
|
}
|
|
|
|
_ { }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
alt argdocsfound {
|
|
|
|
none. { }
|
|
|
|
some(ds) {
|
|
|
|
for d: @ast::meta_item in ds {
|
|
|
|
alt d.node {
|
2012-01-16 15:53:17 -06:00
|
|
|
ast::meta_name_value(key, {node: ast::lit_str(value),
|
|
|
|
span: _}) {
|
2012-01-16 18:27:28 -06:00
|
|
|
argdocs += [{
|
|
|
|
name: key,
|
|
|
|
desc: value
|
|
|
|
}];
|
2012-01-16 15:53:17 -06:00
|
|
|
}
|
2012-01-15 23:50:55 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-16 18:27:28 -06:00
|
|
|
{
|
2012-01-16 18:01:33 -06:00
|
|
|
brief: brief,
|
2012-01-15 23:50:55 -06:00
|
|
|
desc: desc,
|
2012-01-16 18:27:28 -06:00
|
|
|
args: argdocs,
|
|
|
|
return: return
|
|
|
|
}
|
2012-01-15 23:50:55 -06:00
|
|
|
}
|
2012-01-16 00:47:23 -06:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
|
|
|
|
fn parse_attributes(source: str) -> [ast::attribute] {
|
|
|
|
import rustc::driver::diagnostic;
|
|
|
|
import rustc::syntax::codemap;
|
|
|
|
import rustc::syntax::parse::parser;
|
|
|
|
|
|
|
|
let cm = codemap::new_codemap();
|
|
|
|
let parse_sess = @{
|
|
|
|
cm: cm,
|
|
|
|
mutable next_id: 0,
|
|
|
|
diagnostic: diagnostic::mk_handler(cm, none)
|
|
|
|
};
|
|
|
|
let parser = parser::new_parser_from_source_str(
|
|
|
|
parse_sess, [], "-", source);
|
|
|
|
|
|
|
|
parser::parse_outer_attributes(parser)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parse_fn_should_handle_undocumented_functions() {
|
|
|
|
let source = "";
|
|
|
|
let attrs = parse_attributes(source);
|
2012-01-16 18:27:28 -06:00
|
|
|
let attrs = parse_fn(attrs);
|
|
|
|
assert attrs.brief == none;
|
|
|
|
assert attrs.desc == none;
|
|
|
|
assert attrs.return == none;
|
|
|
|
assert vec::len(attrs.args) == 0u;
|
2012-01-16 00:47:23 -06:00
|
|
|
}
|
|
|
|
|
2012-01-16 18:27:28 -06:00
|
|
|
#[tes]
|
2012-01-16 00:47:23 -06:00
|
|
|
fn parse_fn_should_parse_simple_doc_attributes() {
|
|
|
|
let source = "#[doc = \"basic\"]";
|
|
|
|
let attrs = parse_attributes(source);
|
2012-01-16 18:27:28 -06:00
|
|
|
let attrs = parse_fn(attrs);
|
|
|
|
assert attrs.brief == some("basic");
|
2012-01-16 00:47:23 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parse_fn_should_parse_the_brief_description() {
|
|
|
|
let source = "#[doc(brief = \"short\")]";
|
|
|
|
let attrs = parse_attributes(source);
|
2012-01-16 18:27:28 -06:00
|
|
|
let attrs = parse_fn(attrs);
|
|
|
|
assert attrs.brief == some("short");
|
2012-01-16 00:47:23 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parse_fn_should_parse_the_long_description() {
|
|
|
|
let source = "#[doc(desc = \"description\")]";
|
|
|
|
let attrs = parse_attributes(source);
|
2012-01-16 18:27:28 -06:00
|
|
|
let attrs = parse_fn(attrs);
|
|
|
|
assert attrs.desc == some("description");
|
2012-01-16 00:47:23 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parse_fn_should_parse_the_return_value_description() {
|
|
|
|
let source = "#[doc(return = \"return value\")]";
|
|
|
|
let attrs = parse_attributes(source);
|
2012-01-16 18:27:28 -06:00
|
|
|
let attrs = parse_fn(attrs);
|
|
|
|
assert attrs.return == some("return value");
|
2012-01-16 00:47:23 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parse_fn_should_parse_the_argument_descriptions() {
|
|
|
|
let source = "#[doc(args(a = \"arg a\", b = \"arg b\"))]";
|
|
|
|
let attrs = parse_attributes(source);
|
2012-01-16 18:27:28 -06:00
|
|
|
let attrs = parse_fn(attrs);
|
|
|
|
assert attrs.args[0] == {name: "a", desc: "arg a"};
|
|
|
|
assert attrs.args[1] == {name: "b", desc: "arg b"};
|
2012-01-16 00:47:23 -06:00
|
|
|
}
|
|
|
|
}
|