Make the pretty-printer output paren-free and case-free code

We should probably do another pretty-printing pass soon.
This commit is contained in:
Marijn Haverbeke 2011-07-21 15:36:41 +02:00
parent 45748a3be4
commit c32f525f73

View File

@ -69,6 +69,7 @@ fn rust_printer(ioivec::writer writer) -> ps {
} }
const uint indent_unit = 4u; const uint indent_unit = 4u;
const uint alt_indent_unit = 2u;
const uint default_columns = 78u; const uint default_columns = 78u;
@ -165,12 +166,14 @@ fn bopen(&ps s) {
} }
fn bclose(&ps s, codemap::span span) { fn bclose_(&ps s, codemap::span span, uint indented) {
maybe_print_comment(s, span.hi); maybe_print_comment(s, span.hi);
break_offset(s.s, 1u, -(indent_unit as int)); break_offset(s.s, 1u, -(indented as int));
word(s.s, "}"); word(s.s, "}");
end(s); // close the outer-box end(s); // close the outer-box
}
fn bclose(&ps s, codemap::span span) {
bclose_(s, span, indent_unit);
} }
fn hardbreak_if_not_bol(&ps s) { fn hardbreak_if_not_bol(&ps s) {
@ -572,10 +575,11 @@ fn print_stmt(&ps s, &ast::stmt st) {
} }
fn print_block(&ps s, &ast::block blk) { fn print_block(&ps s, &ast::block blk) {
print_possibly_embedded_block(s, blk, false); print_possibly_embedded_block(s, blk, false, indent_unit);
} }
fn print_possibly_embedded_block(&ps s, &ast::block blk, bool embedded) { fn print_possibly_embedded_block(&ps s, &ast::block blk, bool embedded,
uint indented) {
maybe_print_comment(s, blk.span.lo); maybe_print_comment(s, blk.span.lo);
auto ann_node = node_block(s, blk); auto ann_node = node_block(s, blk);
s.ann.pre(ann_node); s.ann.pre(ann_node);
@ -593,7 +597,7 @@ fn print_possibly_embedded_block(&ps s, &ast::block blk, bool embedded) {
} }
case (_) { } case (_) { }
} }
bclose(s, blk.span); bclose_(s, blk.span, indented);
s.ann.post(ann_node); s.ann.post(ann_node);
} }
@ -603,9 +607,7 @@ fn print_if(&ps s, &@ast::expr test, &ast::block block,
if (chk) { if (chk) {
word_nbsp(s, "check"); word_nbsp(s, "check");
} }
popen(s);
print_expr(s, test); print_expr(s, test);
pclose(s);
space(s.s); space(s.s);
print_block(s, block); print_block(s, block);
fn do_else(&ps s, option::t[@ast::expr] els) { fn do_else(&ps s, option::t[@ast::expr] els) {
@ -658,7 +660,7 @@ fn print_mac(&ps s, &ast::mac m) {
word(s.s, ">"); word(s.s, ">");
} }
case (ast::mac_embed_block(?blk)) { case (ast::mac_embed_block(?blk)) {
print_possibly_embedded_block(s, blk, true); print_possibly_embedded_block(s, blk, true, indent_unit);
} }
case (ast::mac_ellipsis) { case (ast::mac_ellipsis) {
word(s.s, "..."); word(s.s, "...");
@ -788,9 +790,7 @@ fn print_expr(&ps s, &@ast::expr expr) {
} }
case (ast::expr_while(?test, ?block)) { case (ast::expr_while(?test, ?block)) {
head(s, "while"); head(s, "while");
popen(s);
print_expr(s, test); print_expr(s, test);
pclose(s);
space(s.s); space(s.s);
print_block(s, block); print_block(s, block);
} }
@ -822,20 +822,19 @@ fn print_expr(&ps s, &@ast::expr expr) {
print_block(s, block); print_block(s, block);
space(s.s); space(s.s);
word_space(s, "while"); word_space(s, "while");
popen(s);
print_expr(s, expr); print_expr(s, expr);
pclose(s);
} }
case (ast::expr_alt(?expr, ?arms)) { case (ast::expr_alt(?expr, ?arms)) {
head(s, "alt"); cbox(s, alt_indent_unit);
popen(s); ibox(s, 4u);
word_nbsp(s, "alt");
print_expr(s, expr); print_expr(s, expr);
pclose(s);
space(s.s); space(s.s);
bopen(s); bopen(s);
for (ast::arm arm in arms) { for (ast::arm arm in arms) {
space(s.s); space(s.s);
head(s, "case"); cbox(s, alt_indent_unit);
ibox(s, 0u);
auto first = true; auto first = true;
for (@ast::pat p in arm.pats) { for (@ast::pat p in arm.pats) {
if (first) { first = false; } if (first) { first = false; }
@ -843,9 +842,10 @@ fn print_expr(&ps s, &@ast::expr expr) {
print_pat(s, p); print_pat(s, p);
} }
space(s.s); space(s.s);
print_block(s, arm.block); print_possibly_embedded_block(s, arm.block, false,
alt_indent_unit);
} }
bclose(s, expr.span); bclose_(s, expr.span, alt_indent_unit);
} }
case (ast::expr_fn(?f)) { case (ast::expr_fn(?f)) {
head(s, proto_to_str(f.proto)); head(s, proto_to_str(f.proto));