forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpage_pass.rs
176 lines (144 loc) · 3.79 KB
/
page_pass.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#[doc = "
Divides the document tree into pages.
Each page corresponds is a logical section. There may be pages for individual
modules, pages for the crate, indexes, etc.
"];
export mk_pass;
fn mk_pass(output_style: config::output_style) -> pass {
{
name: "page",
f: fn~(srv: astsrv::srv, doc: doc::doc) -> doc::doc {
run(srv, doc, output_style)
}
}
}
fn run(
_srv: astsrv::srv,
doc: doc::doc,
output_style: config::output_style
) -> doc::doc {
if output_style == config::doc_per_crate {
ret doc;
}
let result_port = comm::port();
let result_chan = comm::chan(result_port);
let page_chan = task::spawn_listener {|page_port|
comm::send(result_chan, make_doc_from_pages(page_port));
};
find_pages(doc, page_chan);
comm::recv(result_port)
}
type page_port = comm::port<option<doc::page>>;
type page_chan = comm::chan<option<doc::page>>;
fn make_doc_from_pages(page_port: page_port) -> doc::doc {
let mut pages = [];
loop {
let val = comm::recv(page_port);
if option::is_some(val) {
pages += [option::unwrap(val)];
} else {
break;
}
}
{
pages: pages
}
}
fn find_pages(doc: doc::doc, page_chan: page_chan) {
let fold = fold::fold({
fold_crate: fold_crate,
fold_mod: fold_mod,
fold_nmod: fold_nmod
with *fold::default_any_fold(page_chan)
});
fold.fold_doc(fold, doc);
comm::send(page_chan, none);
}
fn fold_crate(
fold: fold::fold<page_chan>,
doc: doc::cratedoc
) -> doc::cratedoc {
let doc = fold::default_seq_fold_crate(fold, doc);
let page = doc::cratepage({
topmod: strip_mod(doc.topmod)
with doc
});
comm::send(fold.ctxt, some(page));
doc
}
fn fold_mod(
fold: fold::fold<page_chan>,
doc: doc::moddoc
) -> doc::moddoc {
let doc = fold::default_any_fold_mod(fold, doc);
if doc.id() != rustc::syntax::ast::crate_node_id {
let doc = strip_mod(doc);
let page = doc::itempage(doc::modtag(doc));
comm::send(fold.ctxt, some(page));
}
doc
}
fn strip_mod(doc: doc::moddoc) -> doc::moddoc {
{
items: vec::filter(doc.items) {|item|
alt item {
doc::modtag(_) { false }
doc::nmodtag(_) { false }
_ { true }
}
}
with doc
}
}
fn fold_nmod(
fold: fold::fold<page_chan>,
doc: doc::nmoddoc
) -> doc::nmoddoc {
let doc = fold::default_seq_fold_nmod(fold, doc);
let page = doc::itempage(doc::nmodtag(doc));
comm::send(fold.ctxt, some(page));
ret doc;
}
#[test]
fn should_not_split_the_doc_into_pages_for_doc_per_crate() {
let doc = test::mk_doc_(
config::doc_per_crate,
"mod a { } mod b { mod c { } }"
);
assert doc.pages.len() == 1u;
}
#[test]
fn should_make_a_page_for_every_mod() {
let doc = test::mk_doc("mod a { }");
assert doc.pages.mods()[0].name() == "a";
}
#[test]
fn should_remove_mods_from_containing_mods() {
let doc = test::mk_doc("mod a { }");
assert vec::is_empty(doc.cratemod().mods());
}
#[test]
fn should_make_a_page_for_every_native_mod() {
let doc = test::mk_doc("native mod a { }");
assert doc.pages.nmods()[0].name() == "a";
}
#[test]
fn should_remove_native_mods_from_containing_mods() {
let doc = test::mk_doc("native mod a { }");
assert vec::is_empty(doc.cratemod().nmods());
}
#[cfg(test)]
mod test {
fn mk_doc_(
output_style: config::output_style,
source: str
) -> doc::doc {
astsrv::from_str(source) {|srv|
let doc = extract::from_srv(srv, "");
run(srv, doc, output_style)
}
}
fn mk_doc(source: str) -> doc::doc {
mk_doc_(config::doc_per_mod, source)
}
}