Skip to content

Commit 04acba7

Browse files
committed
Move pretty-printing 'modes' into a callback hook
This way, the pretty-printer does not have to know about middle::ty. (This is a preparation for separating the AST functionality into a separate crate.)
1 parent 001397d commit 04acba7

File tree

4 files changed

+85
-73
lines changed

4 files changed

+85
-73
lines changed

src/comp/driver/rustc.rs

+53-6
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import middle::resolve;
1212
import middle::ty;
1313
import middle::typeck;
1414
import middle::tstate::ck;
15+
import pretty::pp;
1516
import pretty::pprust;
1617
import pretty::ppaux;
1718
import back::link;
@@ -24,6 +25,7 @@ import std::option::some;
2425
import std::option::none;
2526
import std::str;
2627
import std::vec;
28+
import std::int;
2729
import std::io;
2830
import std::run;
2931
import std::getopts;
@@ -127,22 +129,67 @@ fn compile_input(session::session sess, ast::crate_cfg cfg, str input,
127129

128130
fn pretty_print_input(session::session sess, ast::crate_cfg cfg,
129131
str input, pp_mode ppm) {
132+
fn ann_paren_for_expr(&ppaux::ann_node node) {
133+
alt (node) {
134+
case (ppaux::node_expr(?s, ?expr)) {
135+
pprust::popen(s);
136+
}
137+
case (_) {}
138+
}
139+
}
140+
fn ann_typed_post(&ty::ctxt tcx, &ppaux::ann_node node) {
141+
alt (node) {
142+
case (ppaux::node_expr(?s, ?expr)) {
143+
pp::space(s.s);
144+
pp::word(s.s, "as");
145+
pp::space(s.s);
146+
pp::word(s.s, ppaux::ty_to_str(tcx, ty::expr_ty(tcx, expr)));
147+
pprust::pclose(s);
148+
}
149+
case (_) {}
150+
}
151+
}
152+
fn ann_identified_post(&ppaux::ann_node node) {
153+
alt (node) {
154+
case (ppaux::node_item(?s, ?item)) {
155+
pp::space(s.s);
156+
pprust::synth_comment(s, int::to_str(item.id, 10u));
157+
}
158+
case (ppaux::node_block(?s, ?blk)) {
159+
pp::space(s.s);
160+
pprust::synth_comment(s, "block " +
161+
int::to_str(blk.node.id, 10u));
162+
}
163+
case (ppaux::node_expr(?s, ?expr)) {
164+
pp::space(s.s);
165+
pprust::synth_comment(s, int::to_str(expr.id, 10u));
166+
pprust::pclose(s);
167+
}
168+
case (_) {}
169+
}
170+
}
171+
130172
auto p = front::parser::new_parser(sess, cfg, input, 0u, 0);
131173
auto crate = parse_input(sess, p, input);
132-
auto mode;
174+
auto ann;
133175
alt (ppm) {
134176
case (ppm_typed) {
135177
auto amap = middle::ast_map::map_crate(*crate);
136178
auto d = resolve::resolve_crate(sess, amap, crate);
137179
auto ty_cx = ty::mk_ctxt(sess, d._0, d._1, amap);
138180
typeck::check_crate(ty_cx, crate);
139-
mode = ppaux::mo_typed(ty_cx);
181+
ann = rec(pre=ann_paren_for_expr,
182+
post=bind ann_typed_post(ty_cx, _));
183+
}
184+
case (ppm_identified) {
185+
ann = rec(pre=ann_paren_for_expr,
186+
post=ann_identified_post);
187+
}
188+
case (ppm_normal) {
189+
ann = ppaux::no_ann();
140190
}
141-
case (ppm_normal) { mode = ppaux::mo_untyped; }
142-
case (ppm_identified) { mode = ppaux::mo_identified; }
143191
}
144-
pprust::print_crate(sess, crate, input, std::io::stdout(),
145-
mode);
192+
pprust::print_crate(sess, crate, input, std::io::stdout(), ann);
146193
}
147194

148195
fn version(str argv0) {

src/comp/pretty/ppaux.rs

+18-4
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,22 @@ fn next_comment(&ps s) -> option::t[lexer::cmnt] {
337337
}
338338
}
339339

340+
// The ps is stored here to prevent recursive type.
341+
// FIXME use a nominal tag instead
342+
tag ann_node {
343+
node_block(ps, ast::block);
344+
node_item(ps, @ast::item);
345+
node_expr(ps, @ast::expr);
346+
node_pat(ps, @ast::pat);
347+
}
348+
type pp_ann = rec(fn(&ann_node node) pre,
349+
fn(&ann_node node) post);
350+
351+
fn no_ann() -> pp_ann {
352+
fn ignore(&ann_node node) {}
353+
ret rec(pre=ignore, post=ignore);
354+
}
355+
340356
type ps =
341357
@rec(pp::printer s,
342358
option::t[codemap] cm,
@@ -345,7 +361,7 @@ type ps =
345361
mutable uint cur_cmnt,
346362
mutable uint cur_lit,
347363
mutable vec[pp::breaks] boxes,
348-
mode mode);
364+
pp_ann ann);
349365

350366
fn ibox(&ps s, uint u) {
351367
vec::push(s.boxes, pp::inconsistent);
@@ -354,8 +370,6 @@ fn ibox(&ps s, uint u) {
354370

355371
fn end(&ps s) { vec::pop(s.boxes); pp::end(s.s); }
356372

357-
tag mode { mo_untyped; mo_typed(ctxt); mo_identified; }
358-
359373
fn rust_printer(io::writer writer) -> ps {
360374
let vec[pp::breaks] boxes = [];
361375
ret @rec(s=pp::mk_printer(writer, default_columns),
@@ -365,7 +379,7 @@ fn rust_printer(io::writer writer) -> ps {
365379
mutable cur_cmnt=0u,
366380
mutable cur_lit=0u,
367381
mutable boxes=boxes,
368-
mode=mo_untyped);
382+
ann=no_ann());
369383
}
370384

371385
const uint indent_unit = 4u;

src/comp/pretty/pprust.rs

+14-62
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import front::lexer;
1010
import front::codemap;
1111
import front::codemap::codemap;
1212
import front::ast;
13-
import middle::ty;
1413
import util::common;
1514
import option::some;
1615
import option::none;
@@ -29,7 +28,7 @@ import pp::eof;
2928
import ppaux::*;
3029

3130
fn print_crate(session sess, @ast::crate crate, str filename,
32-
io::writer out, mode mode) {
31+
io::writer out, &pp_ann ann) {
3332
let vec[pp::breaks] boxes = [];
3433
auto r = lexer::gather_comments_and_literals(sess, filename);
3534
auto s =
@@ -40,7 +39,7 @@ fn print_crate(session sess, @ast::crate crate, str filename,
4039
mutable cur_cmnt=0u,
4140
mutable cur_lit=0u,
4241
mutable boxes=boxes,
43-
mode=mode);
42+
ann=ann);
4443
print_mod(s, crate.node.module, crate.node.attrs);
4544
eof(s.s);
4645
}
@@ -297,6 +296,8 @@ fn print_item(&ps s, &@ast::item item) {
297296
hardbreak_if_not_bol(s);
298297
maybe_print_comment(s, item.span.lo);
299298
print_outer_attributes(s, item.attrs);
299+
auto ann_node = node_item(s, item);
300+
s.ann.pre(ann_node);
300301
alt (item.node) {
301302
case (ast::item_const(?ty, ?expr)) {
302303
head(s, "const");
@@ -470,15 +471,7 @@ fn print_item(&ps s, &@ast::item item) {
470471
print_block(s, dt.body);
471472
}
472473
}
473-
474-
// Print the node ID if necessary. TODO: type as well.
475-
alt (s.mode) {
476-
case (mo_identified) {
477-
space(s.s);
478-
synth_comment(s, int::to_str(item.id, 10u));
479-
}
480-
case (_) {/* no-op */ }
481-
}
474+
s.ann.post(ann_node);
482475
}
483476

484477
fn print_outer_attributes(&ps s, vec[ast::attribute] attrs) {
@@ -530,6 +523,8 @@ fn print_stmt(&ps s, &ast::stmt st) {
530523

531524
fn print_block(&ps s, ast::block blk) {
532525
maybe_print_comment(s, blk.span.lo);
526+
auto ann_node = node_block(s, blk);
527+
s.ann.pre(ann_node);
533528
bopen(s);
534529
for (@ast::stmt st in blk.node.stmts) { print_stmt(s, *st) }
535530
alt (blk.node.expr) {
@@ -541,15 +536,7 @@ fn print_block(&ps s, ast::block blk) {
541536
case (_) { }
542537
}
543538
bclose(s, blk.span);
544-
545-
// Print the node ID if necessary: TODO: type as well.
546-
alt (s.mode) {
547-
case (mo_identified) {
548-
space(s.s);
549-
synth_comment(s, "block " + int::to_str(blk.node.id, 10u));
550-
}
551-
case (_) {/* no-op */ }
552-
}
539+
s.ann.post(ann_node);
553540
}
554541

555542
fn print_if(&ps s, &@ast::expr test, &ast::block block,
@@ -597,11 +584,8 @@ fn print_if(&ps s, &@ast::expr test, &ast::block block,
597584
fn print_expr(&ps s, &@ast::expr expr) {
598585
maybe_print_comment(s, expr.span.lo);
599586
ibox(s, indent_unit);
600-
alt (s.mode) {
601-
case (mo_untyped) {/* no-op */ }
602-
case (mo_typed(_)) { popen(s); }
603-
case (mo_identified) { popen(s); }
604-
}
587+
auto ann_node = node_expr(s, expr);
588+
s.ann.pre(ann_node);
605589
alt (expr.node) {
606590
case (ast::expr_vec(?exprs, ?mut, ?kind)) {
607591
ibox(s, indent_unit);
@@ -926,23 +910,7 @@ fn print_expr(&ps s, &@ast::expr expr) {
926910

927911
}
928912
}
929-
// Print the type or node ID if necessary.
930-
931-
alt (s.mode) {
932-
case (mo_untyped) {/* no-op */ }
933-
case (mo_typed(?tcx)) {
934-
space(s.s);
935-
word(s.s, "as");
936-
space(s.s);
937-
word(s.s, ppaux::ty_to_str(tcx, ty::expr_ty(tcx, expr)));
938-
pclose(s);
939-
}
940-
case (mo_identified) {
941-
space(s.s);
942-
synth_comment(s, int::to_str(expr.id, 10u));
943-
pclose(s);
944-
}
945-
}
913+
s.ann.post(ann_node);
946914
end(s);
947915
}
948916

@@ -960,16 +928,6 @@ fn print_decl(&ps s, &@ast::decl decl) {
960928
}
961929
case (_) {
962930
word_nbsp(s, "auto");
963-
964-
// Print the type or node ID if necessary.
965-
alt (s.mode) {
966-
case (mo_untyped) {/* no-op */ }
967-
case (mo_typed(?tcx)) {
968-
auto lty = ty::node_id_to_type(tcx, loc.node.id);
969-
word_space(s, ppaux::ty_to_str(tcx, lty));
970-
}
971-
case (mo_identified) {/* no-op */ }
972-
}
973931
}
974932
}
975933
word(s.s, loc.node.ident);
@@ -1015,6 +973,8 @@ fn print_path(&ps s, &ast::path path) {
1015973

1016974
fn print_pat(&ps s, &@ast::pat pat) {
1017975
maybe_print_comment(s, pat.span.lo);
976+
auto ann_node = node_pat(s, pat);
977+
s.ann.pre(ann_node);
1018978
alt (pat.node) {
1019979
case (ast::pat_wild) { word(s.s, "_"); }
1020980
case (ast::pat_bind(?id)) { word(s.s, "?" + id); }
@@ -1028,15 +988,7 @@ fn print_pat(&ps s, &@ast::pat pat) {
1028988
}
1029989
}
1030990
}
1031-
1032-
// Print the node ID if necessary. TODO: type as well.
1033-
alt (s.mode) {
1034-
case (mo_identified) {
1035-
space(s.s);
1036-
synth_comment(s, int::to_str(pat.id, 10u));
1037-
}
1038-
case (_) {/* no-op */ }
1039-
}
991+
s.ann.post(ann_node);
1040992
}
1041993

1042994
fn print_fn(&ps s, ast::fn_decl decl, ast::proto proto, str name,

src/comp/util/common.rs

-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import pretty::pprust::print_decl;
2525
import pretty::pprust::print_fn;
2626
import pretty::pprust::print_type;
2727
import pretty::ppaux::print_literal;
28-
import pretty::ppaux::mo_untyped;
2928
import pretty::pp::mk_printer;
3029

3130
type filename = str;

0 commit comments

Comments
 (0)