2012-01-24 01:40:30 -06:00
|
|
|
// FIXME: Random import to solve the mystery resolve bug
|
|
|
|
import std;
|
|
|
|
|
2012-01-16 16:49:40 -06:00
|
|
|
export fold;
|
|
|
|
export fold_crate, fold_mod, fold_fn, fold_modlist, fold_fnlist;
|
|
|
|
export default_seq_fold;
|
2012-01-18 16:30:48 -06:00
|
|
|
export default_seq_fold_crate;
|
2012-01-18 20:51:04 -06:00
|
|
|
export default_seq_fold_mod;
|
2012-01-16 17:31:49 -06:00
|
|
|
export default_seq_fold_fn;
|
2012-01-24 02:44:47 -06:00
|
|
|
export default_seq_fold_const;
|
2012-01-17 00:50:00 -06:00
|
|
|
export default_seq_fold_fnlist;
|
2012-01-16 16:49:40 -06:00
|
|
|
|
2012-01-19 18:21:33 -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;
|
|
|
|
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;
|
2012-01-24 01:40:30 -06:00
|
|
|
type fold_const<T> = fn~(fold: fold<T>, doc: doc::constdoc) -> doc::constdoc;
|
|
|
|
type fold_modlist<T> = fn~(fold: fold<T>, list: doc::modlist) -> doc::modlist;
|
|
|
|
type fold_fnlist<T> = fn~(fold: fold<T>, list: doc::fnlist) -> doc::fnlist;
|
|
|
|
type fold_constlist<T> = fn~(
|
|
|
|
fold: fold<T>,
|
|
|
|
list: doc::constlist
|
|
|
|
) -> doc::constlist;
|
2012-01-16 16:49:40 -06:00
|
|
|
|
2012-01-16 17:31:49 -06:00
|
|
|
type t<T> = {
|
|
|
|
ctxt: T,
|
|
|
|
fold_crate: fold_crate<T>,
|
|
|
|
fold_mod: fold_mod<T>,
|
|
|
|
fold_fn: fold_fn<T>,
|
2012-01-24 01:40:30 -06:00
|
|
|
fold_const: fold_const<T>,
|
2012-01-16 17:31:49 -06:00
|
|
|
fold_modlist: fold_modlist<T>,
|
2012-01-24 01:40:30 -06:00
|
|
|
fold_fnlist: fold_fnlist<T>,
|
|
|
|
fold_constlist: fold_constlist<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
|
2012-01-16 17:31:49 -06:00
|
|
|
fn mk_fold<T:copy>(
|
|
|
|
ctxt: T,
|
|
|
|
fold_crate: fold_crate<T>,
|
|
|
|
fold_mod: fold_mod<T>,
|
|
|
|
fold_fn: fold_fn<T>,
|
2012-01-24 01:40:30 -06:00
|
|
|
fold_const: fold_const<T>,
|
2012-01-16 17:31:49 -06:00
|
|
|
fold_modlist: fold_modlist<T>,
|
2012-01-24 01:40:30 -06:00
|
|
|
fold_fnlist: fold_fnlist<T>,
|
|
|
|
fold_constlist: fold_constlist<T>
|
2012-01-16 17:31:49 -06:00
|
|
|
) -> fold<T> {
|
2012-01-16 16:49:40 -06:00
|
|
|
fold({
|
2012-01-16 17:31:49 -06:00
|
|
|
ctxt: ctxt,
|
2012-01-16 16:49:40 -06:00
|
|
|
fold_crate: fold_crate,
|
|
|
|
fold_mod: fold_mod,
|
|
|
|
fold_fn: fold_fn,
|
2012-01-24 01:40:30 -06:00
|
|
|
fold_const: fold_const,
|
2012-01-16 16:49:40 -06:00
|
|
|
fold_modlist: fold_modlist,
|
2012-01-24 01:40:30 -06:00
|
|
|
fold_fnlist: fold_fnlist,
|
|
|
|
fold_constlist: fold_constlist
|
2012-01-16 16:49:40 -06:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2012-01-16 17:31:49 -06:00
|
|
|
fn default_seq_fold<T:copy>(ctxt: T) -> fold<T> {
|
2012-01-16 16:49:40 -06:00
|
|
|
mk_fold(
|
2012-01-16 17:31:49 -06:00
|
|
|
ctxt,
|
|
|
|
{|f, d| default_seq_fold_crate(f, d)},
|
|
|
|
{|f, d| default_seq_fold_mod(f, d)},
|
|
|
|
{|f, d| default_seq_fold_fn(f, d)},
|
2012-01-24 01:40:30 -06:00
|
|
|
{|f, d| default_seq_fold_const(f, d)},
|
2012-01-16 17:31:49 -06:00
|
|
|
{|f, d| default_seq_fold_modlist(f, d)},
|
2012-01-24 01:40:30 -06:00
|
|
|
{|f, d| default_seq_fold_fnlist(f, d)},
|
|
|
|
{|f, d| default_seq_fold_constlist(f, d)}
|
2012-01-16 16:49:40 -06:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2012-01-16 17:31:49 -06:00
|
|
|
fn default_seq_fold_crate<T>(
|
|
|
|
fold: fold<T>,
|
2012-01-16 16:49:40 -06:00
|
|
|
doc: doc::cratedoc
|
|
|
|
) -> doc::cratedoc {
|
|
|
|
~{
|
|
|
|
topmod: fold.fold_mod(fold, doc.topmod)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-16 17:31:49 -06:00
|
|
|
fn default_seq_fold_mod<T>(
|
|
|
|
fold: fold<T>,
|
2012-01-16 16:49:40 -06:00
|
|
|
doc: doc::moddoc
|
|
|
|
) -> doc::moddoc {
|
|
|
|
~{
|
|
|
|
mods: fold.fold_modlist(fold, doc.mods),
|
2012-01-24 02:14:31 -06:00
|
|
|
fns: fold.fold_fnlist(fold, doc.fns),
|
|
|
|
consts: fold.fold_constlist(fold, doc.consts)
|
2012-01-18 20:11:26 -06:00
|
|
|
with *doc
|
2012-01-16 16:49:40 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-16 17:31:49 -06:00
|
|
|
fn default_seq_fold_fn<T>(
|
|
|
|
_fold: fold<T>,
|
2012-01-16 16:49:40 -06:00
|
|
|
doc: doc::fndoc
|
|
|
|
) -> doc::fndoc {
|
|
|
|
doc
|
|
|
|
}
|
|
|
|
|
2012-01-24 01:40:30 -06:00
|
|
|
fn default_seq_fold_const<T>(
|
|
|
|
_fold: fold<T>,
|
|
|
|
doc: doc::constdoc
|
|
|
|
) -> doc::constdoc {
|
|
|
|
doc
|
|
|
|
}
|
|
|
|
|
2012-01-16 17:31:49 -06:00
|
|
|
fn default_seq_fold_modlist<T>(
|
|
|
|
fold: fold<T>,
|
2012-01-16 16:49:40 -06:00
|
|
|
list: doc::modlist
|
|
|
|
) -> doc::modlist {
|
|
|
|
doc::modlist(vec::map(*list) {|doc|
|
|
|
|
fold.fold_mod(fold, doc)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2012-01-16 17:31:49 -06:00
|
|
|
fn default_seq_fold_fnlist<T>(
|
|
|
|
fold: fold<T>,
|
2012-01-16 16:49:40 -06:00
|
|
|
list: doc::fnlist
|
|
|
|
) -> doc::fnlist {
|
|
|
|
doc::fnlist(vec::map(*list) {|doc|
|
|
|
|
fold.fold_fn(fold, doc)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2012-01-24 01:40:30 -06:00
|
|
|
fn default_seq_fold_constlist<T>(
|
|
|
|
fold: fold<T>,
|
|
|
|
list: doc::constlist
|
|
|
|
) -> doc::constlist {
|
|
|
|
doc::constlist(vec::map(*list) {|doc|
|
|
|
|
fold.fold_const(fold, doc)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[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-16 16:49:40 -06:00
|
|
|
}
|