Skip to content

Commit f9946f3

Browse files
Daniel Pattersonbrson
Daniel Patterson
authored andcommitted
syntax: better formatting of closures in pretty printer and more verbose debugging messages
1 parent 06b2804 commit f9946f3

File tree

2 files changed

+72
-21
lines changed

2 files changed

+72
-21
lines changed

src/libsyntax/print/pp.rs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,8 @@ impl printer {
254254
self.left = 0u;
255255
self.right = 0u;
256256
} else { self.advance_right(); }
257-
debug!{"pp BEGIN/buffer ~[%u,%u]", self.left, self.right};
257+
debug!{"pp BEGIN(%d)/buffer ~[%u,%u]",
258+
b.offset, self.left, self.right};
258259
self.token[self.right] = t;
259260
self.size[self.right] = -self.right_total;
260261
self.scan_push(self.right);
@@ -278,7 +279,8 @@ impl printer {
278279
self.left = 0u;
279280
self.right = 0u;
280281
} else { self.advance_right(); }
281-
debug!{"pp BREAK/buffer ~[%u,%u]", self.left, self.right};
282+
debug!{"pp BREAK(%d)/buffer ~[%u,%u]",
283+
b.offset, self.left, self.right};
282284
self.check_stack(0);
283285
self.scan_push(self.right);
284286
self.token[self.right] = t;
@@ -287,10 +289,12 @@ impl printer {
287289
}
288290
STRING(s, len) => {
289291
if self.scan_stack_empty {
290-
debug!{"pp STRING/print ~[%u,%u]", self.left, self.right};
292+
debug!{"pp STRING('%s')/print ~[%u,%u]",
293+
*s, self.left, self.right};
291294
self.print(t, len);
292295
} else {
293-
debug!{"pp STRING/buffer ~[%u,%u]", self.left, self.right};
296+
debug!{"pp STRING('%s')/buffer ~[%u,%u]",
297+
*s, self.left, self.right};
294298
self.advance_right();
295299
self.token[self.right] = t;
296300
self.size[self.right] = len;
@@ -444,30 +448,33 @@ impl printer {
444448
let top = self.get_top();
445449
match top.pbreak {
446450
fits => {
447-
debug!{"print BREAK in fitting block"};
451+
debug!{"print BREAK(%d) in fitting block", b.blank_space};
448452
self.space -= b.blank_space;
449453
self.indent(b.blank_space);
450454
}
451455
broken(consistent) => {
452-
debug!{"print BREAK in consistent block"};
456+
debug!{"print BREAK(%d+%d) in consistent block",
457+
top.offset, b.offset};
453458
self.print_newline(top.offset + b.offset);
454459
self.space = self.margin - (top.offset + b.offset);
455460
}
456461
broken(inconsistent) => {
457462
if L > self.space {
458-
debug!{"print BREAK w/ newline in inconsistent"};
463+
debug!{"print BREAK(%d+%d) w/ newline in inconsistent",
464+
top.offset, b.offset};
459465
self.print_newline(top.offset + b.offset);
460466
self.space = self.margin - (top.offset + b.offset);
461467
} else {
462-
debug!{"print BREAK w/o newline in inconsistent"};
468+
debug!{"print BREAK(%d) w/o newline in inconsistent",
469+
b.blank_space};
463470
self.indent(b.blank_space);
464471
self.space -= b.blank_space;
465472
}
466473
}
467474
}
468475
}
469476
STRING(s, len) => {
470-
debug!{"print STRING"};
477+
debug!{"print STRING(%s)", *s};
471478
assert (L == len);
472479
// assert L <= space;
473480
self.space -= len;

src/libsyntax/print/pprust.rs

Lines changed: 56 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -237,10 +237,16 @@ fn bopen(s: ps) {
237237
}
238238
239239
fn bclose_(s: ps, span: codemap::span, indented: uint) {
240+
bclose_maybe_open(s, span, indented, true);
241+
}
242+
fn bclose_maybe_open (s: ps, span: codemap::span, indented: uint,
243+
close_box: bool) {
240244
maybe_print_comment(s, span.hi);
241245
break_offset_if_not_bol(s, 1u, -(indented as int));
242246
word(s.s, ~"}");
243-
end(s); // close the outer-box
247+
if close_box {
248+
end(s); // close the outer-box
249+
}
244250
}
245251
fn bclose(s: ps, span: codemap::span) { bclose_(s, span, indent_unit); }
246252

@@ -827,20 +833,27 @@ fn print_block(s: ps, blk: ast::blk) {
827833
print_possibly_embedded_block(s, blk, block_normal, indent_unit);
828834
}
829835
836+
fn print_block_unclosed(s: ps, blk: ast::blk) {
837+
print_possibly_embedded_block_(s, blk, block_normal, indent_unit, ~[],
838+
false);
839+
}
840+
830841
fn print_block_with_attrs(s: ps, blk: ast::blk, attrs: ~[ast::attribute]) {
831-
print_possibly_embedded_block_(s, blk, block_normal, indent_unit, attrs);
842+
print_possibly_embedded_block_(s, blk, block_normal, indent_unit, attrs,
843+
true);
832844
}
833845
834846
enum embed_type { block_block_fn, block_normal, }
835847
836848
fn print_possibly_embedded_block(s: ps, blk: ast::blk, embedded: embed_type,
837849
indented: uint) {
838850
print_possibly_embedded_block_(
839-
s, blk, embedded, indented, ~[]);
851+
s, blk, embedded, indented, ~[], true);
840852
}
841853
842854
fn print_possibly_embedded_block_(s: ps, blk: ast::blk, embedded: embed_type,
843-
indented: uint, attrs: ~[ast::attribute]) {
855+
indented: uint, attrs: ~[ast::attribute],
856+
close_box: bool) {
844857
match blk.node.rules {
845858
ast::unchecked_blk => word(s.s, ~"unchecked"),
846859
ast::unsafe_blk => word(s.s, ~"unsafe"),
@@ -868,7 +881,7 @@ fn print_possibly_embedded_block_(s: ps, blk: ast::blk, embedded: embed_type,
868881
}
869882
_ => ()
870883
}
871-
bclose_(s, blk.span, indented);
884+
bclose_maybe_open(s, blk.span, indented, close_box);
872885
s.ann.post(ann_node);
873886
}
874887

@@ -1060,9 +1073,9 @@ fn print_expr(s: ps, &&expr: @ast::expr) {
10601073
let blk = if has_block {
10611074
let blk_arg = vec::pop(base_args);
10621075
match blk_arg.node {
1063-
ast::expr_loop_body(_) => word_nbsp(s, ~"for"),
1064-
ast::expr_do_body(_) => word_nbsp(s, ~"do"),
1065-
_ => ()
1076+
ast::expr_loop_body(_) => { head(s, ~"for"); }
1077+
ast::expr_do_body(_) => { head(s, ~"do"); }
1078+
_ => {}
10661079
}
10671080
some(blk_arg)
10681081
} else { none };
@@ -1074,7 +1087,19 @@ fn print_expr(s: ps, &&expr: @ast::expr) {
10741087
}
10751088
if has_block {
10761089
nbsp(s);
1077-
print_expr(s, option::get(blk));
1090+
match blk.get().node {
1091+
// need to handle closures specifically
1092+
ast::expr_do_body(e) | ast::expr_loop_body(e) => {
1093+
end(s); // we close our head box; closure
1094+
// will create it's own.
1095+
print_expr(s, e);
1096+
end(s); // close outer box, as closures don't
1097+
}
1098+
_ => {
1099+
// not sure if this can happen.
1100+
print_expr(s, blk.get());
1101+
}
1102+
}
10781103
}
10791104
}
10801105
ast::expr_binary(op, lhs, rhs) => {
@@ -1174,12 +1199,31 @@ fn print_expr(s: ps, &&expr: @ast::expr) {
11741199
print_block(s, body);
11751200
}
11761201
ast::expr_fn_block(decl, body, cap_clause) => {
1202+
// in do/for blocks we don't want to show an empty
1203+
// argument list, but at this point we don't know which
1204+
// we are inside.
1205+
//
1206+
// if !decl.inputs.is_empty() {
11771207
print_fn_block_args(s, decl, *cap_clause);
1178-
// The parser always adds an extra implicit block around lambdas
1208+
space(s.s);
1209+
// }
11791210
assert body.node.stmts.is_empty();
11801211
assert body.node.expr.is_some();
1181-
space(s.s);
1182-
print_expr(s, body.node.expr.get());
1212+
// we extract the block, so as not to create another set of boxes
1213+
match body.node.expr.get().node {
1214+
ast::expr_block(blk) => {
1215+
print_block_unclosed(s, blk);
1216+
}
1217+
_ => {
1218+
// this is a bare expression
1219+
print_expr(s, body.node.expr.get());
1220+
end(s); // need to close a box
1221+
}
1222+
}
1223+
// a box will be closed by print_expr, but we didn't want an overall
1224+
// wrapper so we closed the corresponding opening. so create an
1225+
// empty box to satisfy the close.
1226+
ibox(s, 0);
11831227
}
11841228
ast::expr_loop_body(body) => {
11851229
print_expr(s, body);

0 commit comments

Comments
 (0)