rust/src/rustdoc/fold.rs

372 lines
9.4 KiB
Rust
Raw Normal View History

2012-09-18 18:48:40 -05:00
export Fold;
2012-01-16 16:49:40 -06:00
export default_seq_fold;
export default_seq_fold_doc;
export default_seq_fold_crate;
2012-02-17 17:51:58 -06:00
export default_seq_fold_item;
export default_seq_fold_mod;
2012-02-24 15:34:35 -06:00
export default_seq_fold_nmod;
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;
export default_seq_fold_trait;
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-02-21 00:08:24 -06:00
export default_par_fold;
export default_par_fold_mod;
export default_par_fold_nmod;
export default_any_fold;
export default_any_fold_mod;
export default_any_fold_nmod;
2012-01-16 16:49:40 -06:00
2012-09-18 18:48:40 -05:00
enum Fold<T> = Fold_<T>;
2012-01-16 16:49:40 -06:00
2012-09-18 18:48:40 -05:00
type FoldDoc<T> = fn~(fold: Fold<T>, doc: doc::Doc) -> doc::Doc;
type FoldCrate<T> = fn~(fold: Fold<T>, doc: doc::CrateDoc) -> doc::CrateDoc;
type FoldItem<T> = fn~(fold: Fold<T>, doc: doc::ItemDoc) -> doc::ItemDoc;
type FoldMod<T> = fn~(fold: Fold<T>, doc: doc::ModDoc) -> doc::ModDoc;
type FoldNmod<T> = fn~(fold: Fold<T>, doc: doc::NmodDoc) -> doc::NmodDoc;
type FoldFn<T> = fn~(fold: Fold<T>, doc: doc::FnDoc) -> doc::FnDoc;
type FoldConst<T> = fn~(fold: Fold<T>, doc: doc::ConstDoc) -> doc::ConstDoc;
type FoldEnum<T> = fn~(fold: Fold<T>, doc: doc::EnumDoc) -> doc::EnumDoc;
type FoldTrait<T> = fn~(fold: Fold<T>, doc: doc::TraitDoc) -> doc::TraitDoc;
type FoldImpl<T> = fn~(fold: Fold<T>, doc: doc::ImplDoc) -> doc::ImplDoc;
type FoldType<T> = fn~(fold: Fold<T>, doc: doc::TyDoc) -> doc::TyDoc;
2012-01-16 16:49:40 -06:00
2012-09-18 18:48:40 -05:00
type Fold_<T> = {
ctxt: T,
2012-09-18 18:48:40 -05:00
fold_doc: FoldDoc<T>,
fold_crate: FoldCrate<T>,
fold_item: FoldItem<T>,
fold_mod: FoldMod<T>,
fold_nmod: FoldNmod<T>,
fold_fn: FoldFn<T>,
fold_const: FoldConst<T>,
fold_enum: FoldEnum<T>,
fold_trait: FoldTrait<T>,
fold_impl: FoldImpl<T>,
fold_type: FoldType<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,
2012-09-18 18:48:40 -05:00
+fold_doc: FoldDoc<T>,
+fold_crate: FoldCrate<T>,
+fold_item: FoldItem<T>,
+fold_mod: FoldMod<T>,
+fold_nmod: FoldNmod<T>,
+fold_fn: FoldFn<T>,
+fold_const: FoldConst<T>,
+fold_enum: FoldEnum<T>,
+fold_trait: FoldTrait<T>,
+fold_impl: FoldImpl<T>,
+fold_type: FoldType<T>
) -> Fold<T> {
Fold({
ctxt: ctxt,
fold_doc: fold_doc,
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,
2012-02-24 15:34:35 -06:00
fold_nmod: fold_nmod,
2012-01-16 16:49:40 -06:00
fold_fn: fold_fn,
fold_const: fold_const,
2012-01-25 18:56:28 -06:00
fold_enum: fold_enum,
fold_trait: fold_trait,
2012-02-02 00:41:41 -06:00
fold_impl: fold_impl,
fold_type: fold_type
2012-01-16 16:49:40 -06:00
})
}
2012-09-18 18:48:40 -05:00
fn default_any_fold<T:Send Copy>(ctxt: T) -> Fold<T> {
2012-02-27 00:38:35 -06:00
mk_fold(
ctxt,
2012-06-30 18:19:07 -05:00
|f, d| default_seq_fold_doc(f, d),
|f, d| default_seq_fold_crate(f, d),
|f, d| default_seq_fold_item(f, d),
|f, d| default_any_fold_mod(f, d),
|f, d| default_any_fold_nmod(f, d),
|f, d| default_seq_fold_fn(f, d),
|f, d| default_seq_fold_const(f, d),
|f, d| default_seq_fold_enum(f, d),
|f, d| default_seq_fold_trait(f, d),
2012-06-30 18:19:07 -05:00
|f, d| default_seq_fold_impl(f, d),
|f, d| default_seq_fold_type(f, d)
2012-02-27 00:38:35 -06:00
)
}
2012-09-18 18:48:40 -05:00
fn default_seq_fold<T:Copy>(ctxt: T) -> Fold<T> {
2012-01-16 16:49:40 -06:00
mk_fold(
ctxt,
2012-06-30 18:19:07 -05:00
|f, d| default_seq_fold_doc(f, d),
|f, d| default_seq_fold_crate(f, d),
|f, d| default_seq_fold_item(f, d),
|f, d| default_seq_fold_mod(f, d),
|f, d| default_seq_fold_nmod(f, d),
|f, d| default_seq_fold_fn(f, d),
|f, d| default_seq_fold_const(f, d),
|f, d| default_seq_fold_enum(f, d),
|f, d| default_seq_fold_trait(f, d),
2012-06-30 18:19:07 -05: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
)
}
2012-09-18 18:48:40 -05:00
fn default_par_fold<T:Send Copy>(ctxt: T) -> Fold<T> {
2012-02-21 00:08:24 -06:00
mk_fold(
ctxt,
2012-06-30 18:19:07 -05:00
|f, d| default_seq_fold_doc(f, d),
|f, d| default_seq_fold_crate(f, d),
|f, d| default_seq_fold_item(f, d),
|f, d| default_par_fold_mod(f, d),
|f, d| default_par_fold_nmod(f, d),
|f, d| default_seq_fold_fn(f, d),
|f, d| default_seq_fold_const(f, d),
|f, d| default_seq_fold_enum(f, d),
|f, d| default_seq_fold_trait(f, d),
2012-06-30 18:19:07 -05:00
|f, d| default_seq_fold_impl(f, d),
|f, d| default_seq_fold_type(f, d)
2012-02-21 00:08:24 -06:00
)
}
2012-09-18 18:48:40 -05:00
fn default_seq_fold_doc<T>(fold: Fold<T>, doc: doc::Doc) -> doc::Doc {
doc::Doc_({
pages: do vec::map(doc.pages) |page| {
2012-08-06 14:34:08 -05:00
match page {
2012-09-18 18:48:40 -05:00
doc::CratePage(doc) => {
doc::CratePage(fold.fold_crate(fold, doc))
}
2012-09-18 18:48:40 -05:00
doc::ItemPage(doc) => {
doc::ItemPage(fold_ItemTag(fold, doc))
}
}
2012-09-04 15:29:32 -05:00
},
.. *doc
})
}
fn default_seq_fold_crate<T>(
2012-09-18 18:48:40 -05:00
fold: Fold<T>,
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>(
2012-09-18 18:48:40 -05:00
_fold: Fold<T>,
doc: doc::ItemDoc
) -> doc::ItemDoc {
2012-02-17 17:51:58 -06:00
doc
}
fn default_any_fold_mod<T:Send Copy>(
2012-09-18 18:48:40 -05:00
fold: Fold<T>,
doc: doc::ModDoc
) -> doc::ModDoc {
doc::ModDoc_({
2012-02-17 17:51:58 -06:00
item: fold.fold_item(fold, doc.item),
2012-09-18 18:48:40 -05:00
items: par::map(doc.items, |ItemTag, copy fold| {
fold_ItemTag(fold, ItemTag)
2012-09-04 15:29:32 -05:00
}),
.. *doc
})
2012-02-21 00:08:24 -06:00
}
2012-02-27 00:38:35 -06:00
fn default_seq_fold_mod<T>(
2012-09-18 18:48:40 -05:00
fold: Fold<T>,
doc: doc::ModDoc
) -> doc::ModDoc {
doc::ModDoc_({
2012-02-24 15:34:35 -06:00
item: fold.fold_item(fold, doc.item),
2012-09-18 18:48:40 -05:00
items: vec::map(doc.items, |ItemTag| {
fold_ItemTag(fold, ItemTag)
2012-09-04 15:29:32 -05:00
}),
.. *doc
})
2012-02-24 15:34:35 -06:00
}
fn default_par_fold_mod<T:Send Copy>(
2012-09-18 18:48:40 -05:00
fold: Fold<T>,
doc: doc::ModDoc
) -> doc::ModDoc {
doc::ModDoc_({
2012-02-21 00:08:24 -06:00
item: fold.fold_item(fold, doc.item),
2012-09-18 18:48:40 -05:00
items: par::map(doc.items, |ItemTag, copy fold| {
fold_ItemTag(fold, ItemTag)
2012-09-04 15:29:32 -05:00
}),
.. *doc
})
2012-01-16 16:49:40 -06:00
}
fn default_any_fold_nmod<T:Send Copy>(
2012-09-18 18:48:40 -05:00
fold: Fold<T>,
doc: doc::NmodDoc
) -> doc::NmodDoc {
{
item: fold.fold_item(fold, doc.item),
2012-09-18 18:48:40 -05:00
fns: par::map(doc.fns, |FnDoc, copy fold| {
fold.fold_fn(fold, FnDoc)
2012-09-04 15:29:32 -05:00
}),
.. doc
}
}
2012-02-27 00:38:35 -06:00
fn default_seq_fold_nmod<T>(
2012-09-18 18:48:40 -05:00
fold: Fold<T>,
doc: doc::NmodDoc
) -> doc::NmodDoc {
2012-02-27 00:38:35 -06:00
{
item: fold.fold_item(fold, doc.item),
2012-09-18 18:48:40 -05:00
fns: vec::map(doc.fns, |FnDoc| {
fold.fold_fn(fold, FnDoc)
2012-09-04 15:29:32 -05:00
}),
.. doc
}
}
fn default_par_fold_nmod<T:Send Copy>(
2012-09-18 18:48:40 -05:00
fold: Fold<T>,
doc: doc::NmodDoc
) -> doc::NmodDoc {
{
item: fold.fold_item(fold, doc.item),
2012-09-18 18:48:40 -05:00
fns: par::map(doc.fns, |FnDoc, copy fold| {
fold.fold_fn(fold, FnDoc)
2012-09-04 15:29:32 -05:00
}),
.. doc
2012-02-27 00:38:35 -06:00
}
}
2012-09-18 18:48:40 -05:00
fn fold_ItemTag<T>(fold: Fold<T>, doc: doc::ItemTag) -> doc::ItemTag {
2012-08-06 14:34:08 -05:00
match doc {
2012-09-18 18:48:40 -05:00
doc::ModTag(ModDoc) => {
doc::ModTag(fold.fold_mod(fold, ModDoc))
2012-02-21 00:08:24 -06:00
}
2012-09-18 18:48:40 -05:00
doc::NmodTag(nModDoc) => {
doc::NmodTag(fold.fold_nmod(fold, nModDoc))
2012-02-24 15:34:35 -06:00
}
2012-09-18 18:48:40 -05:00
doc::FnTag(FnDoc) => {
doc::FnTag(fold.fold_fn(fold, FnDoc))
2012-02-21 00:08:24 -06:00
}
2012-09-18 18:48:40 -05:00
doc::ConstTag(ConstDoc) => {
doc::ConstTag(fold.fold_const(fold, ConstDoc))
2012-02-21 00:08:24 -06:00
}
2012-09-18 18:48:40 -05:00
doc::EnumTag(EnumDoc) => {
doc::EnumTag(fold.fold_enum(fold, EnumDoc))
2012-02-21 00:08:24 -06:00
}
2012-09-18 18:48:40 -05:00
doc::TraitTag(TraitDoc) => {
doc::TraitTag(fold.fold_trait(fold, TraitDoc))
2012-02-21 00:08:24 -06:00
}
2012-09-18 18:48:40 -05:00
doc::ImplTag(ImplDoc) => {
doc::ImplTag(fold.fold_impl(fold, ImplDoc))
2012-02-21 00:08:24 -06:00
}
2012-09-18 18:48:40 -05:00
doc::TyTag(TyDoc) => {
doc::TyTag(fold.fold_type(fold, TyDoc))
2012-02-21 00:08:24 -06:00
}
}
}
fn default_seq_fold_fn<T>(
2012-09-18 18:48:40 -05:00
fold: Fold<T>,
doc: doc::FnDoc
) -> doc::FnDoc {
2012-02-17 17:51:58 -06:00
{
2012-09-04 15:29:32 -05:00
item: fold.fold_item(fold, doc.item),
.. doc
2012-02-17 17:51:58 -06:00
}
2012-01-16 16:49:40 -06:00
}
fn default_seq_fold_const<T>(
2012-09-18 18:48:40 -05:00
fold: Fold<T>,
doc: doc::ConstDoc
) -> doc::ConstDoc {
2012-02-17 17:51:58 -06:00
{
2012-09-04 15:29:32 -05:00
item: fold.fold_item(fold, doc.item),
.. doc
2012-02-17 17:51:58 -06:00
}
}
2012-01-25 18:56:28 -06:00
fn default_seq_fold_enum<T>(
2012-09-18 18:48:40 -05:00
fold: Fold<T>,
doc: doc::EnumDoc
) -> doc::EnumDoc {
2012-02-17 17:51:58 -06:00
{
2012-09-04 15:29:32 -05:00
item: fold.fold_item(fold, doc.item),
.. doc
2012-02-17 17:51:58 -06:00
}
2012-01-25 18:56:28 -06:00
}
fn default_seq_fold_trait<T>(
2012-09-18 18:48:40 -05:00
fold: Fold<T>,
doc: doc::TraitDoc
) -> doc::TraitDoc {
2012-02-17 17:51:58 -06:00
{
2012-09-04 15:29:32 -05:00
item: fold.fold_item(fold, doc.item),
.. doc
2012-02-17 17:51:58 -06:00
}
2012-01-30 16:11:35 -06:00
}
2012-01-31 17:28:09 -06:00
fn default_seq_fold_impl<T>(
2012-09-18 18:48:40 -05:00
fold: Fold<T>,
doc: doc::ImplDoc
) -> doc::ImplDoc {
2012-02-17 17:51:58 -06:00
{
2012-09-04 15:29:32 -05:00
item: fold.fold_item(fold, doc.item),
.. doc
2012-02-17 17:51:58 -06:00
}
2012-01-31 17:28:09 -06:00
}
2012-02-02 00:41:41 -06:00
fn default_seq_fold_type<T>(
2012-09-18 18:48:40 -05:00
fold: Fold<T>,
doc: doc::TyDoc
) -> doc::TyDoc {
2012-02-17 17:51:58 -06:00
{
2012-09-04 15:29:32 -05:00
item: fold.fold_item(fold, doc.item),
.. doc
2012-02-17 17:51:58 -06:00
}
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_doc(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_doc(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, ~"");
2012-01-25 18:56:28 -06:00
let fld = default_seq_fold(());
let folded = fld.fold_doc(fld, doc);
2012-01-25 18:56:28 -06:00
assert doc == folded;
2012-02-21 00:08:24 -06:00
}
#[test]
fn default_parallel_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, ~"");
2012-02-21 00:08:24 -06:00
let fld = default_par_fold(());
let folded = fld.fold_doc(fld, doc);
2012-02-21 00:08:24 -06:00
assert doc == folded;
}