rust/src/rustdoc/fold.rs

241 lines
6.1 KiB
Rust
Raw Normal View History

2012-01-16 16:49:40 -06:00
export fold;
export fold_crate, fold_mod, fold_fn;
2012-01-16 16:49:40 -06:00
export default_seq_fold;
export default_seq_fold_crate;
2012-02-17 17:51:58 -06:00
export default_seq_fold_item;
export default_seq_fold_mod;
export default_seq_fold_fn;
2012-01-24 02:44:47 -06:00
export default_seq_fold_const;
2012-01-25 20:40:47 -06:00
export default_seq_fold_enum;
2012-01-27 00:07:14 -06:00
export default_seq_fold_res;
2012-01-30 16:11:35 -06:00
export default_seq_fold_iface;
2012-01-31 17:28:09 -06:00
export default_seq_fold_impl;
2012-02-02 00:41:41 -06:00
export default_seq_fold_type;
2012-01-16 16:49:40 -06:00
enum fold<T> = t<T>;
2012-01-16 16:49:40 -06:00
2012-01-19 02:14:41 -06:00
type fold_crate<T> = fn~(fold: fold<T>, doc: doc::cratedoc) -> doc::cratedoc;
2012-02-17 17:51:58 -06:00
type fold_item<T> = fn~(fold: fold<T>, doc: doc::itemdoc) -> doc::itemdoc;
2012-01-19 02:14:41 -06:00
type fold_mod<T> = fn~(fold: fold<T>, doc: doc::moddoc) -> doc::moddoc;
type fold_fn<T> = fn~(fold: fold<T>, doc: doc::fndoc) -> doc::fndoc;
type fold_const<T> = fn~(fold: fold<T>, doc: doc::constdoc) -> doc::constdoc;
2012-01-25 18:56:28 -06:00
type fold_enum<T> = fn~(fold: fold<T>, doc: doc::enumdoc) -> doc::enumdoc;
2012-01-26 20:13:04 -06:00
type fold_res<T> = fn~(fold: fold<T>, doc: doc::resdoc) -> doc::resdoc;
2012-01-30 16:11:35 -06:00
type fold_iface<T> = fn~(fold: fold<T>, doc: doc::ifacedoc) -> doc::ifacedoc;
2012-01-31 17:28:09 -06:00
type fold_impl<T> = fn~(fold: fold<T>, doc: doc::impldoc) -> doc::impldoc;
2012-02-02 00:41:41 -06:00
type fold_type<T> = fn~(fold: fold<T>, doc: doc::tydoc) -> doc::tydoc;
2012-01-16 16:49:40 -06:00
type t<T> = {
ctxt: T,
fold_crate: fold_crate<T>,
2012-02-17 17:51:58 -06:00
fold_item: fold_item<T>,
fold_mod: fold_mod<T>,
fold_fn: fold_fn<T>,
fold_const: fold_const<T>,
2012-01-25 18:56:28 -06:00
fold_enum: fold_enum<T>,
2012-01-30 16:11:35 -06:00
fold_res: fold_res<T>,
2012-01-31 17:28:09 -06:00
fold_iface: fold_iface<T>,
2012-02-02 00:41:41 -06:00
fold_impl: fold_impl<T>,
fold_type: fold_type<T>
2012-01-16 16:49:40 -06:00
};
// This exists because fn types don't infer correctly as record
// initializers, but they do as function arguments
fn mk_fold<T:copy>(
ctxt: T,
fold_crate: fold_crate<T>,
2012-02-17 17:51:58 -06:00
fold_item: fold_item<T>,
fold_mod: fold_mod<T>,
fold_fn: fold_fn<T>,
fold_const: fold_const<T>,
2012-01-25 18:56:28 -06:00
fold_enum: fold_enum<T>,
2012-01-30 16:11:35 -06:00
fold_res: fold_res<T>,
2012-01-31 17:28:09 -06:00
fold_iface: fold_iface<T>,
2012-02-02 00:41:41 -06:00
fold_impl: fold_impl<T>,
fold_type: fold_type<T>
) -> fold<T> {
2012-01-16 16:49:40 -06:00
fold({
ctxt: ctxt,
2012-01-16 16:49:40 -06:00
fold_crate: fold_crate,
2012-02-17 17:51:58 -06:00
fold_item: fold_item,
2012-01-16 16:49:40 -06:00
fold_mod: fold_mod,
fold_fn: fold_fn,
fold_const: fold_const,
2012-01-25 18:56:28 -06:00
fold_enum: fold_enum,
2012-01-30 16:11:35 -06:00
fold_res: fold_res,
2012-01-31 17:28:09 -06:00
fold_iface: fold_iface,
2012-02-02 00:41:41 -06:00
fold_impl: fold_impl,
fold_type: fold_type
2012-01-16 16:49:40 -06:00
})
}
fn default_seq_fold<T:copy>(ctxt: T) -> fold<T> {
2012-01-16 16:49:40 -06:00
mk_fold(
ctxt,
{|f, d| default_seq_fold_crate(f, d)},
2012-02-17 17:51:58 -06:00
{|f, d| default_seq_fold_item(f, d)},
{|f, d| default_seq_fold_mod(f, d)},
{|f, d| default_seq_fold_fn(f, d)},
{|f, d| default_seq_fold_const(f, d)},
2012-01-25 18:56:28 -06:00
{|f, d| default_seq_fold_enum(f, d)},
2012-01-30 16:11:35 -06:00
{|f, d| default_seq_fold_res(f, d)},
2012-01-31 17:28:09 -06:00
{|f, d| default_seq_fold_iface(f, d)},
2012-02-02 00:41:41 -06:00
{|f, d| default_seq_fold_impl(f, d)},
{|f, d| default_seq_fold_type(f, d)}
2012-01-16 16:49:40 -06:00
)
}
fn default_seq_fold_crate<T>(
fold: fold<T>,
2012-01-16 16:49:40 -06:00
doc: doc::cratedoc
) -> doc::cratedoc {
2012-01-30 15:05:25 -06:00
{
2012-01-16 16:49:40 -06:00
topmod: fold.fold_mod(fold, doc.topmod)
}
}
2012-02-17 17:51:58 -06:00
fn default_seq_fold_item<T>(
_fold: fold<T>,
doc: doc::itemdoc
) -> doc::itemdoc {
doc
}
fn default_seq_fold_mod<T>(
fold: fold<T>,
2012-01-16 16:49:40 -06:00
doc: doc::moddoc
) -> doc::moddoc {
2012-01-30 15:05:25 -06:00
{
2012-02-17 17:51:58 -06:00
item: fold.fold_item(fold, doc.item),
2012-01-30 15:05:25 -06:00
items: ~vec::map(*doc.items) {|itemtag|
alt itemtag {
doc::modtag(moddoc) {
doc::modtag(fold.fold_mod(fold, moddoc))
}
doc::fntag(fndoc) {
doc::fntag(fold.fold_fn(fold, fndoc))
}
doc::consttag(constdoc) {
doc::consttag(fold.fold_const(fold, constdoc))
}
doc::enumtag(enumdoc) {
doc::enumtag(fold.fold_enum(fold, enumdoc))
}
doc::restag(resdoc) {
doc::restag(fold.fold_res(fold, resdoc))
}
2012-01-30 16:11:35 -06:00
doc::ifacetag(ifacedoc) {
doc::ifacetag(fold.fold_iface(fold, ifacedoc))
}
2012-01-31 17:28:09 -06:00
doc::impltag(impldoc) {
doc::impltag(fold.fold_impl(fold, impldoc))
}
2012-02-02 00:41:41 -06:00
doc::tytag(tydoc) {
doc::tytag(fold.fold_type(fold, tydoc))
}
}
}
2012-01-30 15:05:25 -06:00
with doc
2012-01-16 16:49:40 -06:00
}
}
fn default_seq_fold_fn<T>(
2012-02-17 17:51:58 -06:00
fold: fold<T>,
2012-01-16 16:49:40 -06:00
doc: doc::fndoc
) -> doc::fndoc {
2012-02-17 17:51:58 -06:00
{
item: fold.fold_item(fold, doc.item)
with doc
}
2012-01-16 16:49:40 -06:00
}
fn default_seq_fold_const<T>(
2012-02-17 17:51:58 -06:00
fold: fold<T>,
doc: doc::constdoc
) -> doc::constdoc {
2012-02-17 17:51:58 -06:00
{
item: fold.fold_item(fold, doc.item)
with doc
}
}
2012-01-25 18:56:28 -06:00
fn default_seq_fold_enum<T>(
2012-02-17 17:51:58 -06:00
fold: fold<T>,
2012-01-25 18:56:28 -06:00
doc: doc::enumdoc
) -> doc::enumdoc {
2012-02-17 17:51:58 -06:00
{
item: fold.fold_item(fold, doc.item)
with doc
}
2012-01-25 18:56:28 -06:00
}
2012-01-26 20:13:04 -06:00
fn default_seq_fold_res<T>(
2012-02-17 17:51:58 -06:00
fold: fold<T>,
2012-01-26 20:13:04 -06:00
doc: doc::resdoc
) -> doc::resdoc {
2012-02-17 17:51:58 -06:00
{
item: fold.fold_item(fold, doc.item)
with doc
}
2012-01-26 20:13:04 -06:00
}
2012-01-30 16:11:35 -06:00
fn default_seq_fold_iface<T>(
2012-02-17 17:51:58 -06:00
fold: fold<T>,
2012-01-30 16:11:35 -06:00
doc: doc::ifacedoc
) -> doc::ifacedoc {
2012-02-17 17:51:58 -06:00
{
item: fold.fold_item(fold, doc.item)
with doc
}
2012-01-30 16:11:35 -06:00
}
2012-01-31 17:28:09 -06:00
fn default_seq_fold_impl<T>(
2012-02-17 17:51:58 -06:00
fold: fold<T>,
2012-01-31 17:28:09 -06:00
doc: doc::impldoc
) -> doc::impldoc {
2012-02-17 17:51:58 -06:00
{
item: fold.fold_item(fold, doc.item)
with doc
}
2012-01-31 17:28:09 -06:00
}
2012-02-02 00:41:41 -06:00
fn default_seq_fold_type<T>(
2012-02-17 17:51:58 -06:00
fold: fold<T>,
2012-02-02 00:41:41 -06:00
doc: doc::tydoc
) -> doc::tydoc {
2012-02-17 17:51:58 -06:00
{
item: fold.fold_item(fold, doc.item)
with doc
}
2012-02-02 00:41:41 -06:00
}
#[test]
fn default_fold_should_produce_same_doc() {
let source = "mod a { fn b() { } mod c { fn d() { } } }";
let ast = parse::from_str(source);
let doc = extract::extract(ast, "");
let fld = default_seq_fold(());
let folded = fld.fold_crate(fld, doc);
assert doc == folded;
}
#[test]
fn default_fold_should_produce_same_consts() {
let source = "const a: int = 0;";
let ast = parse::from_str(source);
let doc = extract::extract(ast, "");
let fld = default_seq_fold(());
let folded = fld.fold_crate(fld, doc);
assert doc == folded;
2012-01-25 18:56:28 -06:00
}
#[test]
fn default_fold_should_produce_same_enums() {
let source = "enum a { b }";
let ast = parse::from_str(source);
let doc = extract::extract(ast, "");
let fld = default_seq_fold(());
let folded = fld.fold_crate(fld, doc);
assert doc == folded;
2012-01-16 16:49:40 -06:00
}