diff --git a/src/comp/pretty/pp.rs b/src/comp/pretty/pp.rs index 11c18edd88a3..88d50e340835 100644 --- a/src/comp/pretty/pp.rs +++ b/src/comp/pretty/pp.rs @@ -2,267 +2,541 @@ import std::io; import std::vec; import std::str; -tag boxtype {box_h; box_v; box_hv; box_align;} -tag contexttype {cx_h; cx_v;} -tag scantype {scan_hv; scan_h; scan_none;} +/* + * This pretty-printer is a direct reimplementation of Philip Karlton's + * Mesa pretty-printer, as described in appendix A of + * + * STAN-CS-79-770: "Pretty Printing", by Derek C. Oppen. + * Stanford Department of Computer Science, 1979. + * + * The algorithm's aim is to break a stream into as few lines as possible + * while respecting the indentation-consistency requirements of the enclosing + * block, and avoiding breaking at silly places on block boundaries, for + * example, between "x" and ")" in "x)". + * + * I am implementing this algorithm because it comes with 20 pages of + * documentation explaining its theory, and because it addresses the set of + * concerns I've seen other pretty-printers fall down on. Weirdly. Even though + * it's 32 years old and not written in Haskell. What can I say? + * + * Despite some redundancies and quirks in the way it's implemented in that + * paper, I've opted to keep the implementation here as similar as I can, + * changing only what was blatantly wrong, a typo, or sufficiently + * non-idiomatic rust that it really stuck out. + * + * In particular you'll see a certain amount of churn related to INTEGER vs. + * CARDINAL in the Mesa implementation. Mesa apparently interconverts the two + * somewhat readily? In any case, I've used uint for indices-in-buffers and + * ints for character-sizes-and-indentation-offsets. This respects the need + * for ints to "go negative" while carrying a pending-calculation balance, and + * helps differentiate all the numbers flying around internally (slightly). + * + * I also inverted the indentation arithmetic used in the print stack, since + * the Mesa implementation (somewhat randomly) stores the offset on the print + * stack in terms of margin-col rather than col itself. I store col. + */ + +tag breaks { consistent; inconsistent; } +type break_t = rec(int offset, int blank_space); +type begin_t = rec(int offset, breaks breaks); tag token { - brk(uint); - hardbrk; - word(str); - cword(str); // closing token - open(boxtype, uint); - close; + STRING(str); + BREAK(break_t); + BEGIN(begin_t); + END; + EOF; } -type context = rec(contexttype tp, uint indent); -type ps = @rec(mutable vec[context] context, - uint width, - io::writer out, - mutable uint col, - mutable uint spaces, - mutable vec[token] buffered, - mutable scantype scanning, - mutable vec[boxtype] scandepth, - mutable uint scancol, - mutable bool start_of_line, - mutable bool start_of_box, - mutable bool potential_brk); - -fn mkstate(io::writer out, uint width) -> ps { - let vec[context] stack = [rec(tp=cx_v, indent=0u)]; - let vec[token] buff = []; - let vec[boxtype] sd = []; - ret @rec(mutable context=stack, - width=width, - out=out, - mutable col=0u, - mutable spaces=0u, - mutable buffered=buff, - mutable scanning=scan_none, - mutable scandepth=sd, - mutable scancol=0u, - mutable start_of_line=true, - mutable start_of_box=true, - mutable potential_brk=false); +fn tok_str(token t) -> str { + alt (t) { + case (STRING(?s)) { ret "STR(" + s + ")"; } + case (BREAK(_)) { ret "BREAK"; } + case (BEGIN(_)) { ret "BEGIN"; } + case (END) { ret "END"; } + case (EOF) { ret "EOF"; } + } } -fn write_spaces(ps p, uint i) { - while (i > 0u) { - i -= 1u; - p.out.write_str(" "); - } -} - -fn push_context(ps p, contexttype tp, uint indent) { - before_print(p, false); - vec::push[context](p.context, rec(tp=tp, indent=indent)); - p.start_of_box = true; -} - -fn pop_context(ps p) { - vec::pop[context](p.context); -} - -fn add_token(ps p, token tok) { - if (p.width == 0u) {direct_token(p, tok);} - else if (p.scanning == scan_none) {do_token(p, tok);} - else {buffer_token(p, tok);} -} - -fn direct_token(ps p, token tok) { - alt (tok) { - case (brk(?sz)) {write_spaces(p, sz);} - case (word(?w)) {p.out.write_str(w);} - case (cword(?w)) {p.out.write_str(w);} - case (_) {} - } -} - -fn buffer_token(ps p, token tok) { - p.buffered += [tok]; - auto col = p.scancol; - p.scancol = col + token_size(tok); - if (p.scancol > p.width) { - finish_scan(p, false); - } else { - alt (tok) { - case (open(?tp,_)) { - vec::push[boxtype](p.scandepth, tp); - if (p.scanning == scan_h) { - if (tp == box_h) { - check_potential_brk(p); - } +fn buf_str(vec[token] toks, vec[int] szs, + uint left, uint right, uint lim) -> str { + auto n = vec::len(toks); + assert n == vec::len(szs); + auto i = left; + auto L = lim; + auto s = "["; + while (i != right && L != 0u) { + L -= 1u; + if (i != left) { + s += ", "; } - } - case (close) { - vec::pop[boxtype](p.scandepth); - if (vec::len[boxtype](p.scandepth) == 0u) { - finish_scan(p, true); + s += #fmt("%d=%s", szs.(i), tok_str(toks.(i))); + i += 1u; + i %= n; + } + s += "]"; + ret s; +} + + +tag print_stack_break { fits; broken(breaks); } +type print_stack_elt = rec(int offset, print_stack_break pbreak); + +const int size_infinity = 0xffff; + +fn mk_printer(io::writer out, uint linewidth) -> printer { + + // Yes 3, it makes the ring buffers big enough to never + // fall behind. + let uint n = 3u * linewidth; + + log #fmt("mk_printer %u", linewidth); + + let vec[token] token = vec::init_elt[token](EOF, n); + let vec[int] size = vec::init_elt[int](0, n); + let vec[uint] scan_stack = vec::init_elt[uint](0u, n); + let vec[print_stack_elt] print_stack = []; + + ret printer(out, + n, + linewidth as int, // margin + linewidth as int, // space + 0u, // left + 0u, // right + token, + size, + 0, // left_total + 0, // right_total + scan_stack, + true, // scan_stack_empty + 0u, // top + 0u, // bottom + print_stack); +} + +/* + * In case you do not have the paper, here is an explanation of what's going + * on. + * + * There is a stream of input tokens flowing through this printer. + * + * The printer buffers up to 3N tokens inside itself, where N is linewidth. + * Yes, linewidth is chars and tokens are multi-char, but in the worst + * case every token worth buffering is 1 char long, so it's ok. + * + * Tokens are STRING, BREAK, and BEGIN/END to delimit blocks. + * + * BEGIN tokens can carry an offset, saying "how far to indent when you break + * inside here", as well as a flag indicating "consistent" or "inconsistent" + * breaking. Consistent breaking means that after the first break, no attempt + * will be made to flow subsequent breaks together onto lines. Inconsistent + * is the opposite. Inconsistent breaking example would be, say: + * + * foo(hello, there, good, friends) + * + * breaking inconsistently to become + * + * foo(hello, there + * good, friends); + * + * whereas a consistent breaking would yield: + * + * foo(hello, + * there + * good, + * friends); + * + * That is, in the consistent-break blocks we value vertical alignment + * more than the ability to cram stuff onto a line. But in all cases if it + * can make a block a one-liner, it'll do so. + * + * Carrying on with high-level logic: + * + * The buffered tokens go through a ring-buffer, 'tokens'. The 'left' and + * 'right' indices denote the active portion of the ring buffer as well as + * describing hypothetical points-in-the-infinite-stream at most 3N tokens + * apart (i.e. "not wrapped to ring-buffer boundaries"). The paper will switch + * between using 'left' and 'right' terms to denote the wrapepd-to-ring-buffer + * and point-in-infinite-stream senses freely. + * + * There is a parallel ring buffer, 'size', that holds the calculated size of + * each token. Why calculated? Because for BEGIN/END pairs, the "size" + * includes everything betwen the pair. That is, the "size" of BEGIN is + * actually the sum of the sizes of everything between BEGIN and the paired + * END that follows. Since that is arbitrarily far in the future, 'size' is + * being rewritten regularly while the printer runs; in fact most of the + * machinery is here to work out 'size' entries on the fly (and give up when + * they're so obviously over-long that "infinity" is a good enough + * approximation for purposes of line breaking). + * + * The "input side" of the printer is managed as an abstract process called + * SCAN, which uses 'scan_stack', 'scan_stack_empty', 'top' and 'bottom', to + * manage calculating 'size'. SCAN is, in other words, the process of + * calculating 'size' entries. + * + * The "output side" of the printer is managed by an abstract process called + * PRINT, which uses 'print_stack', 'margin' and 'space' to figure out what to + * do with each token/size pair it consumes as it goes. It's trying to consume + * the entire buffered window, but can't output anything until the size is >= + * 0 (sizes are set to negative while they're pending calculation). + * + * So SCAN takeks input and buffers tokens and pending calculations, while + * PRINT gobbles up completed calculations and tokens from the buffer. The + * theory is that the two can never get more than 3N tokens apart, because + * once there's "obviously" too much data to fit on a line, in a size + * calculation, SCAN will write "infinity" to the size and let PRINT consume + * it. + * + * In this implementation (following the paper, again) the SCAN process is + * the method called 'pretty_print', and the 'PRINT' process is the method + * called 'print'. + */ + +obj printer(io::writer out, + uint buf_len, + mutable int margin, // width of lines we're constrained to + mutable int space, // number of spaces left on line + + mutable uint left, // index of left side of input stream + mutable uint right, // index of right side of input stream + mutable vec[token] token, // ring-buffer stream goes through + mutable vec[int] size, // ring-buffer of calculated sizes + mutable int left_total, // running size of stream "...left" + mutable int right_total, // running size of stream "...right" + + // pseudo-stack, really a ring too. Holds the primary-ring-buffers + // index of the BEGIN that started the current block, possibly + // with the most recent BREAK after that BEGIN (if there is any) + // on top of it. Stuff is flushed off the bottom as it becomes + // irrelevant due to the primary ring-buffer advancing. + + mutable vec[uint] scan_stack, + mutable bool scan_stack_empty, // top==bottom disambiguator + mutable uint top, // index of top of scan_stack + mutable uint bottom, // index of bottom of scan_stack + + // stack of blocks-in-progress being flushed by print + mutable vec[print_stack_elt] print_stack + ) { + + + fn pretty_print(token t) { + + log #fmt("pp [%u,%u]", left, right); + alt (t) { + + case (EOF) { + if (!scan_stack_empty) { + self.check_stack(0); + self.advance_left(token.(left), size.(left)); + } + self.indent(0); + } + + case (BEGIN(?b)) { + if (scan_stack_empty) { + left_total = 1; + right_total = 1; + left = 0u; + right = 0u; + } else { + self.advance_right(); + } + log #fmt("pp BEGIN/buffer [%u,%u]", left, right); + token.(right) = t; + size.(right) = -right_total; + self.scan_push(right); + } + + case (END) { + if (scan_stack_empty) { + log #fmt("pp END/print [%u,%u]", left, right); + self.print(t, 0); + } else { + log #fmt("pp END/buffer [%u,%u]", left, right); + self.advance_right(); + token.(right) = t; + size.(right) = -1; + self.scan_push(right); + } + } + + case (BREAK(?b)) { + if (scan_stack_empty) { + left_total = 1; + right_total = 1; + left = 0u; + right = 0u; + } else { + self.advance_right(); + } + log #fmt("pp BREAK/buffer [%u,%u]", left, right); + self.check_stack(0); + self.scan_push(right); + token.(right) = t; + size.(right) = -right_total; + right_total += b.blank_space; + } + + case (STRING(?s)) { + auto len = str::char_len(s) as int; + if (scan_stack_empty) { + log #fmt("pp STRING/print [%u,%u]", left, right); + self.print(t, len); + } else { + log #fmt("pp STRING/buffer [%u,%u]", left, right); + self.advance_right(); + token.(right) = t; + size.(right) = len; + right_total += len; + self.check_stream(); + } + } } - } - case (brk(_)) { - if (p.scanning == scan_h) { - if (p.scandepth.(vec::len[boxtype](p.scandepth)-1u) == box_v) { - finish_scan(p, true); - } + } + + fn check_stream() { + log #fmt("check_stream [%u, %u] with left_total=%d, right_total=%d", + left, right, left_total, right_total);; + if (right_total - left_total > space) { + log #fmt("scan window is %d, longer than space on line (%d)", + right_total - left_total, space); + if (!scan_stack_empty) { + if (left == scan_stack.(bottom)) { + log #fmt("setting %u to infinity and popping", left); + size.(self.scan_pop_bottom()) = size_infinity; + } + } + self.advance_left(token.(left), size.(left)); + if (left != right) { + self.check_stream(); + } + } + } + + fn scan_push(uint x) { + log #fmt("scan_push %u", x); + if (scan_stack_empty) { + scan_stack_empty = false; + } else { + top += 1u; + top %= buf_len; + assert top != bottom; + } + scan_stack.(top) = x; + } + + fn scan_pop() -> uint { + assert !scan_stack_empty; + auto x = scan_stack.(top); + if (top == bottom) { + scan_stack_empty = true; + } else { + top += (buf_len - 1u); + top %= buf_len; + } + ret x; + } + + fn scan_top() -> uint { + assert !scan_stack_empty; + ret scan_stack.(top); + } + + fn scan_pop_bottom() -> uint { + assert !scan_stack_empty; + auto x = scan_stack.(bottom); + if (top == bottom) { + scan_stack_empty = true; + } else { + bottom += 1u; + bottom %= buf_len; + } + ret x; + } + + fn advance_right() { + right += 1u; + right %= buf_len; + assert right != left; + } + + fn advance_left(token x, int L) { + log #fmt("advnce_left [%u,%u], sizeof(%u)=%d", left, right, left, L); + if (L >= 0) { + self.print(x, L); + alt (x) { + case (BREAK(?b)) { + left_total += b.blank_space; + } + case (STRING(?s)) { + // I think? paper says '1' here but 1 and L look same in + // it. + left_total += L; + } + case (_) {} + } + if (left != right) { + left += 1u; + left %= buf_len; + self.advance_left(token.(left), size.(left)); + } + } + } + + fn check_stack(int k) { + if (!scan_stack_empty) { + auto x = self.scan_top(); + alt (token.(x)) { + case (BEGIN(?b)) { + if (k > 0) { + size.(self.scan_pop()) = size.(x) + right_total; + self.check_stack(k - 1); + } + } + case (END) { + // paper says + not =, but that makes no sense. + size.(self.scan_pop()) = 1; + self.check_stack(k + 1); + } + case (_) { + size.(self.scan_pop()) = size.(x) + right_total; + if (k > 0) { + self.check_stack(k); + } + } + } + } + } + + fn print_newline(int amount) { + log #fmt("NEWLINE %d", amount); + out.write_str("\n"); + self.indent(amount); + } + + fn indent(int amount) { + log #fmt("INDENT %d", amount); + auto u = 0; + while (u < amount) { + out.write_str(" "); + u += 1; + } + } + + fn print(token x, int L) { + log #fmt("print %s %d (remaining line space=%d)", tok_str(x), L, space); + log buf_str(token, size, left, right, 6u); + alt (x) { + case (BEGIN(?b)) { + if (L > space) { + auto col = (margin - space) + b.offset; + log #fmt("print BEGIN -> push broken block at col %d", col); + vec::push(print_stack, + rec(offset = col, + pbreak = broken(b.breaks))); + } else { + log "print BEGIN -> push fitting block"; + vec::push(print_stack, + rec(offset = 0, + pbreak = fits)); + } + } + + case (END) { + log "print END -> pop END"; + assert vec::len(print_stack) != 0u; + vec::pop(print_stack); + } + + case (BREAK(?b)) { + + auto n = vec::len(print_stack); + let print_stack_elt top = + rec(offset=0, pbreak=broken(inconsistent));; + if (n != 0u) { + top = print_stack.(n - 1u); + } + + alt (top.pbreak) { + case (fits) { + log "print BREAK in fitting block"; + space -= b.blank_space; + self.indent(b.blank_space); + } + + case (broken(consistent)) { + log "print BREAK in consistent block"; + self.print_newline(top.offset + b.offset); + space = margin - (top.offset + b.offset); + } + + case (broken(inconsistent)) { + if (L > space) { + log "print BREAK w/ newline in inconsistent block"; + self.print_newline(top.offset + b.offset); + space = margin - (top.offset + b.offset); + } else { + log "print BREAK w/o newline in inconsistent block"; + self.indent(b.blank_space); + space -= b.blank_space; + } + } + } + } + + case (STRING(?s)) { + log "print STRING"; + assert L as uint == str::char_len(s); + // assert L <= space; + space -= L; + out.write_str(s); + } + + case (EOF) { + // EOF should never get here. + fail; + } } - } - case (_) {} } - } } -fn check_potential_brk(ps p) { - for (boxtype tp in p.scandepth) { - if (tp != box_h) {ret;} - } - p.potential_brk = true; + +// Convenience functions to talk to the printer. + +fn ibox(printer p, uint indent) { + p.pretty_print(BEGIN(rec(offset = indent as int, + breaks = inconsistent))); } -fn finish_scan(ps p, bool fits) { - auto buf = p.buffered; - auto front = vec::shift[token](buf); - auto chosen_tp = cx_h; - if (!fits) {chosen_tp = cx_v;} - alt (front) { - case (open(box_hv, ?ind)) { - push_context(p, chosen_tp, base_indent(p) + ind); - } - case (open(box_align, _)) { - push_context(p, chosen_tp, p.col); - } - case (open(box_h, ?ind)) { - if (!fits && !p.start_of_box && !p.start_of_line && !p.potential_brk) { - line_break(p); - } - push_context(p, cx_h, base_indent(p) + ind); - } - } - p.scandepth = []; - p.scanning = scan_none; - for (token t in buf) { add_token(p, t); } +fn cbox(printer p, uint indent) { + p.pretty_print(BEGIN(rec(offset = indent as int, + breaks = consistent))); } -fn start_scan(ps p, token tok, scantype tp) { - p.buffered = []; - p.scancol = p.col; - p.scanning = tp; - buffer_token(p, tok); - p.potential_brk = false; + +fn break_offset(printer p, uint n, int off) { + p.pretty_print(BREAK(rec(offset = off, + blank_space = n as int))); } -fn cur_context(ps p) -> context { - ret p.context.(vec::len[context](p.context)-1u); -} -fn base_indent(ps p) -> uint { - auto i = vec::len[context](p.context); - while (i > 0u) { - i -= 1u; - auto cx = p.context.(i); - if (cx.tp == cx_v) {ret cx.indent;} - } - ret 0u; -} +fn end(printer p) { p.pretty_print(END); } +fn eof(printer p) { p.pretty_print(EOF); } +fn wrd(printer p, str wrd) { p.pretty_print(STRING(wrd)); } +fn spaces(printer p, uint n) { break_offset(p, n, 0); } +fn space(printer p) { spaces(p, 1u); } +fn hardbreak(printer p) { spaces(p, 0xffffu); } -fn cx_is(contexttype a, contexttype b) -> bool { - if (a == b) {ret true;} - else {ret false;} -} -fn box_is(boxtype a, boxtype b) -> bool { - if (a == b) {ret true;} - else {ret false;} -} -fn do_token(ps p, token tok) { - auto start_of_box = p.start_of_box; - p.start_of_box = false; - alt (tok) { - case (brk(?sz)) { - if (cx_is(cur_context(p).tp, cx_v) || sz + p.col > p.width) { - line_break(p); - } - else { - p.spaces += sz; - } - } - case (hardbrk) { - line_break(p); - } - case (word(?w)) { - auto len = str::char_len(w); - if (len + p.col + p.spaces > p.width && !start_of_box && - !p.start_of_line) { - line_break(p); - } - before_print(p, false); - p.out.write_str(w); - p.col += len; - } - case (cword(?w)) { - before_print(p, true); - p.out.write_str(w); - p.col += str::char_len(w); - } - case (open(?tp, ?indent)) { - if (tp == box_v) { - push_context(p, cx_v, base_indent(p) + indent); - } else if (box_is(tp, box_h) && cx_is(cur_context(p).tp, cx_v)) { - push_context(p, cx_h, base_indent(p) + indent); - } else if (tp == box_h) { - p.start_of_box = start_of_box; - start_scan(p, tok, scan_h); - } else { - p.start_of_box = start_of_box; - start_scan(p, tok, scan_hv); - } - } - case (close) { - pop_context(p); - } - } -} -fn line_break(ps p) { - p.out.write_str("\n"); - p.col = 0u; - p.spaces = cur_context(p).indent; - p.start_of_line = true; -} - -fn before_print(ps p, bool closing) { - if (p.start_of_line) { - p.start_of_line = false; - if (closing) {p.spaces = base_indent(p);} - else {p.spaces = cur_context(p).indent;} - } - if (p.spaces > 0u) { - write_spaces(p, p.spaces); - p.col += p.spaces; - p.spaces = 0u; - } -} - -fn token_size(token tok) -> uint { - alt (tok) { - case (brk(?sz)) {ret sz;} - case (hardbrk) {ret 0xFFFFFFu;} - case (word(?w)) {ret str::char_len(w);} - case (cword(?w)) {ret str::char_len(w);} - case (open(_, _)) {ret 0u;} - case (close) {ret 0u;} - } -} - -fn box(ps p, uint indent) {add_token(p, open(box_hv, indent));} -fn abox(ps p) {add_token(p, open(box_align, 0u));} -fn vbox(ps p, uint indent) {add_token(p, open(box_v, indent));} -fn hbox(ps p, uint indent) {add_token(p, open(box_h, indent));} -fn end(ps p) {add_token(p, close);} -fn wrd(ps p, str wrd) {add_token(p, word(wrd));} -fn cwrd(ps p, str wrd) {add_token(p, cword(wrd));} -fn space(ps p) {add_token(p, brk(1u));} -fn spaces(ps p, uint n) {add_token(p, brk(n));} -fn line(ps p) {add_token(p, brk(0u));} -fn hardbreak(ps p) {add_token(p, hardbrk);} +// +// Local Variables: +// mode: rust +// fill-column: 78; +// indent-tabs-mode: nil +// c-basic-offset: 4 +// buffer-file-coding-system: utf-8-unix +// compile-command: "make -k -C $RBUILD 2>&1 | sed -e 's/\\/x\\//x:\\//g'"; +// End: +// diff --git a/src/comp/pretty/pprust.rs b/src/comp/pretty/pprust.rs index 302172303577..1684cb6f66e6 100644 --- a/src/comp/pretty/pprust.rs +++ b/src/comp/pretty/pprust.rs @@ -7,7 +7,17 @@ import front::ast; import front::lexer; import middle::ty; import util::common; -import pp::end; import pp::wrd; import pp::space; import pp::line; +import pp; + +import pp::printer; +import pp::break_offset; +import pp::cbox; +import pp::ibox; +import pp::wrd; +import pp::space; +import pp::hardbreak; +import pp::end; +import pp::eof; const uint indent_unit = 4u; const uint default_columns = 78u; @@ -17,7 +27,7 @@ tag mode { mo_typed(ty::ctxt); } -type ps = @rec(pp::ps s, +type ps = @rec(pp::printer s, option::t[vec[lexer::cmnt]] comments, mutable uint cur_cmnt, mode mode); @@ -25,85 +35,102 @@ type ps = @rec(pp::ps s, fn print_file(session sess, ast::_mod _mod, str filename, io::writer out, mode mode) { auto cmnts = lexer::gather_comments(sess, filename); - auto s = @rec(s=pp::mkstate(out, default_columns), + auto s = @rec(s=pp::mk_printer(out, default_columns), comments=option::some[vec[lexer::cmnt]](cmnts), mutable cur_cmnt=0u, mode=mode); print_mod(s, _mod); + eof(s.s); } fn ty_to_str(&@ast::ty ty) -> str { auto writer = io::string_writer(); - auto s = @rec(s=pp::mkstate(writer.get_writer(), 0u), + auto s = @rec(s=pp::mk_printer(writer.get_writer(), default_columns), comments=option::none[vec[lexer::cmnt]], mutable cur_cmnt=0u, mode=mo_untyped); print_type(s, ty); + eof(s.s); ret writer.get_str(); } fn block_to_str(&ast::block blk) -> str { auto writer = io::string_writer(); - auto s = @rec(s=pp::mkstate(writer.get_writer(), 78u), + auto s = @rec(s=pp::mk_printer(writer.get_writer(), default_columns), comments=option::none[vec[lexer::cmnt]], mutable cur_cmnt=0u, mode=mo_untyped); + cbox(s.s, indent_unit); // containing cbox, will be closed by print-block at } + ibox(s.s, 0u); // head-ibox, will be closed by print-block after { print_block(s, blk); + eof(s.s); ret writer.get_str(); } fn pat_to_str(&@ast::pat p) -> str { auto writer = io::string_writer(); - auto s = @rec(s=pp::mkstate(writer.get_writer(), 78u), + auto s = @rec(s=pp::mk_printer(writer.get_writer(), default_columns), comments=option::none[vec[lexer::cmnt]], mutable cur_cmnt=0u, mode=mo_untyped); print_pat(s, p); + eof(s.s); ret writer.get_str(); } -fn hbox(ps s) { - pp::hbox(s.s, indent_unit); +fn word_nbsp(ps s, str word) { + wrd(s.s, word); + wrd(s.s, " "); } -fn wrd1(ps s, str word) { + +fn word_space(ps s, str word) { wrd(s.s, word); space(s.s); } + fn popen(ps s) { wrd(s.s, "("); - pp::abox(s.s); -} -fn popen_h(ps s) { - wrd(s.s, "("); - pp::hbox(s.s, 0u); } + fn pclose(ps s) { - end(s.s); wrd(s.s, ")"); } + +fn head(ps s, str word) { + // outer-box is consistent + cbox(s.s, indent_unit); + // head-box is inconsistent + ibox(s.s, str::char_len(word) + 1u); + // keyword that starts the head + word_nbsp(s, word); +} + fn bopen(ps s) { wrd(s.s, "{"); - pp::vbox(s.s, indent_unit); - line(s.s); + end(s.s); // close the head-box } -fn bclose(ps s) { - end(s.s); - pp::cwrd(s.s, "}"); -} -fn bclose_c(ps s, common::span span) { + +fn bclose(ps s, common::span span) { maybe_print_comment(s, span.hi); - bclose(s); + break_offset(s.s, 1u, -(indent_unit as int)); + wrd(s.s, "}"); + end(s.s); // close the outer-box } + fn commasep[IN](ps s, vec[IN] elts, fn(ps, &IN) op) { + ibox(s.s, 0u); auto first = true; for (IN elt in elts) { if (first) {first = false;} - else {wrd1(s, ",");} + else {word_space(s, ",");} op(s, elt); } + end(s.s); } + fn commasep_cmnt[IN](ps s, vec[IN] elts, fn(ps, &IN) op, - fn(&IN) -> common::span get_span) { + fn(&IN) -> common::span get_span) { + ibox(s.s, 0u); auto len = vec::len[IN](elts); auto i = 0u; for (IN elt in elts) { @@ -114,7 +141,9 @@ fn commasep_cmnt[IN](ps s, vec[IN] elts, fn(ps, &IN) op, if (!maybe_print_line_comment(s, get_span(elt))) {space(s.s);} } } + end(s.s); } + fn commasep_exprs(ps s, vec[@ast::expr] exprs) { fn expr_span(&@ast::expr expr) -> common::span {ret expr.span;} auto f = print_expr; @@ -126,14 +155,18 @@ fn print_mod(ps s, ast::_mod _mod) { for (@ast::view_item vitem in _mod.view_items) { print_view_item(s, vitem); } - line(s.s); - for (@ast::item item in _mod.items) {print_item(s, item);} + for (@ast::item item in _mod.items) { + // Mod-level item printing we're a little more space-y about. + hardbreak(s.s); + print_item(s, item); + } print_remaining_comments(s); } fn print_type(ps s, &@ast::ty ty) { + maybe_print_comment(s, ty.span.lo); - hbox(s); + ibox(s.s, 0u); alt (ty.node) { case (ast::ty_nil) {wrd(s.s, "()");} case (ast::ty_bool) {wrd(s.s, "bool");} @@ -166,7 +199,7 @@ fn print_type(ps s, &@ast::ty ty) { wrd(s.s, "rec"); popen(s); fn print_field(ps s, &ast::ty_field f) { - hbox(s); + cbox(s.s, indent_unit); print_mt(s, f.mt); space(s.s); wrd(s.s, f.ident); @@ -184,17 +217,16 @@ fn print_type(ps s, &@ast::ty ty) { pclose(s); } case (ast::ty_obj(?methods)) { - wrd1(s, "obj"); + head(s, "obj"); bopen(s); for (ast::ty_method m in methods) { - hbox(s); + cbox(s.s, indent_unit); print_ty_fn(s, m.proto, option::some[str](m.ident), m.inputs, m.output, m.cf); wrd(s.s, ";"); end(s.s); - line(s.s); } - bclose_c(s, ty.span); + bclose(s, ty.span); } case (ast::ty_fn(?proto,?inputs,?output,?cf)) { print_ty_fn(s, proto, option::none[str], inputs, output, cf); @@ -207,53 +239,56 @@ fn print_type(ps s, &@ast::ty ty) { } fn print_item(ps s, @ast::item item) { + + hardbreak(s.s); maybe_print_comment(s, item.span.lo); - hbox(s); alt (item.node) { case (ast::item_const(?id, ?ty, ?expr, _, _)) { - wrd1(s, "const"); + head(s, "const"); print_type(s, ty); space(s.s); - wrd1(s, id); - wrd1(s, "="); + word_space(s, id); + end(s.s); // end the head-ibox + word_space(s, "="); print_expr(s, expr); wrd(s.s, ";"); + end(s.s); // end the outer cbox } case (ast::item_fn(?name,?_fn,?typarams,_,_)) { print_fn(s, _fn.decl, name, typarams); - space(s.s); + wrd(s.s, " "); print_block(s, _fn.body); } case (ast::item_mod(?id,?_mod,_)) { - wrd1(s, "mod"); - wrd1(s, id); + head(s, "mod"); bopen(s); for (@ast::item itm in _mod.items) {print_item(s, itm);} - bclose_c(s, item.span); + bclose(s, item.span); } case (ast::item_native_mod(?id,?nmod,_)) { - wrd1(s, "native"); + head(s, "native"); alt (nmod.abi) { - case (ast::native_abi_rust) {wrd1(s, "\"rust\"");} - case (ast::native_abi_cdecl) {wrd1(s, "\"cdecl\"");} + case (ast::native_abi_rust) {word_nbsp(s, "\"rust\"");} + case (ast::native_abi_cdecl) {word_nbsp(s, "\"cdecl\"");} case (ast::native_abi_rust_intrinsic) { - wrd1(s, "\"rust-intrinsic\""); + word_nbsp(s, "\"rust-intrinsic\""); } } - wrd1(s, "mod"); - wrd1(s, id); + word_nbsp(s, "mod"); + word_nbsp(s, id); bopen(s); for (@ast::native_item item in nmod.items) { - hbox(s); + ibox(s.s, indent_unit); maybe_print_comment(s, item.span.lo); alt (item.node) { case (ast::native_item_ty(?id,_)) { - wrd1(s, "type"); + word_nbsp(s, "type"); wrd(s.s, id); } case (ast::native_item_fn(?id,?lname,?decl, ?typarams,_,_)) { print_fn(s, decl, id, typarams); + end(s.s); // end head-ibox alt (lname) { case (option::none[str]) {} case (option::some[str](?ss)) { @@ -265,24 +300,30 @@ fn print_item(ps s, @ast::item item) { wrd(s.s, ";"); end(s.s); } - bclose_c(s, item.span); + bclose(s, item.span); } case (ast::item_ty(?id,?ty,?params,_,_)) { - wrd1(s, "type"); + ibox(s.s, indent_unit); + ibox(s.s, 0u); + word_nbsp(s, "type"); wrd(s.s, id); print_type_params(s, params); + end(s.s); // end the inner ibox space(s.s); - wrd1(s, "="); + word_space(s, "="); print_type(s, ty); wrd(s.s, ";"); + end(s.s); // end the outer ibox + break_offset(s.s, 0u, 0); } case (ast::item_tag(?id,?variants,?params,_,_)) { - wrd1(s, "tag"); + head(s, "tag"); wrd(s.s, id); print_type_params(s, params); space(s.s); bopen(s); for (ast::variant v in variants) { + space(s.s); maybe_print_comment(s, v.span.lo); wrd(s.s, v.node.name); if (vec::len[ast::variant_arg](v.node.args) > 0u) { @@ -295,17 +336,17 @@ fn print_item(ps s, @ast::item item) { pclose(s); } wrd(s.s, ";"); - if (!maybe_print_line_comment(s, v.span)) {line(s.s);} + maybe_print_line_comment(s, v.span); } - bclose_c(s, item.span); + bclose(s, item.span); } case (ast::item_obj(?id,?_obj,?params,_,_)) { - wrd1(s, "obj"); + head(s, "obj"); wrd(s.s, id); print_type_params(s, params); popen(s); fn print_field(ps s, &ast::obj_field field) { - hbox(s); + ibox(s.s, indent_unit); print_type(s, field.ty); space(s.s); wrd(s.s, field.ident); @@ -319,53 +360,52 @@ fn print_item(ps s, @ast::item item) { space(s.s); bopen(s); for (@ast::method meth in _obj.methods) { - hbox(s); let vec[ast::ty_param] typarams = []; + hardbreak(s.s); maybe_print_comment(s, meth.span.lo); print_fn(s, meth.node.meth.decl, meth.node.ident, typarams); - space(s.s); + wrd(s.s, " "); print_block(s, meth.node.meth.body); - end(s.s); - line(s.s); } alt (_obj.dtor) { case (option::some[@ast::method](?dtor)) { - hbox(s); - wrd1(s, "close"); + head(s, "drop"); print_block(s, dtor.node.meth.body); - end(s.s); - line(s.s); } case (_) {} } - bclose_c(s, item.span); + bclose(s, item.span); } } - end(s.s); - line(s.s); - line(s.s); } fn print_block(ps s, ast::block blk) { maybe_print_comment(s, blk.span.lo); bopen(s); + auto first = true; for (@ast::stmt st in blk.node.stmts) { maybe_print_comment(s, st.span.lo); alt (st.node) { - case (ast::stmt_decl(?decl,_)) {print_decl(s, decl);} - case (ast::stmt_expr(?expr,_)) {print_expr(s, expr);} + case (ast::stmt_decl(?decl,_)) { + print_decl(s, decl); + } + case (ast::stmt_expr(?expr,_)) { + space(s.s); + print_expr(s, expr); + } } if (front::parser::stmt_ends_with_semi(st)) {wrd(s.s, ";");} - if (!maybe_print_line_comment(s, st.span)) {line(s.s);} + maybe_print_line_comment(s, st.span); } alt (blk.node.expr) { case (option::some[@ast::expr](?expr)) { + space(s.s); print_expr(s, expr); - if (!maybe_print_line_comment(s, expr.span)) {line(s.s);} + maybe_print_line_comment(s, expr.span); } case (_) {} } - bclose_c(s, blk.span); + bclose(s, blk.span); } fn print_literal(ps s, @ast::lit lit) { @@ -403,7 +443,7 @@ fn print_literal(ps s, @ast::lit lit) { fn print_expr(ps s, &@ast::expr expr) { maybe_print_comment(s, expr.span.lo); - hbox(s); + ibox(s.s, indent_unit); alt (s.mode) { case (mo_untyped) { /* no-op */ } @@ -413,18 +453,18 @@ fn print_expr(ps s, &@ast::expr expr) { alt (expr.node) { case (ast::expr_vec(?exprs,?mut,_)) { if (mut == ast::mut) { - wrd1(s, "mutable"); + word_nbsp(s, "mutable"); } + ibox(s.s, indent_unit); wrd(s.s, "["); - pp::abox(s.s); commasep_exprs(s, exprs); - end(s.s); wrd(s.s, "]"); + end(s.s); } case (ast::expr_tup(?exprs,_)) { fn printElt(ps s, &ast::elt elt) { - hbox(s); - if (elt.mut == ast::mut) {wrd1(s, "mutable");} + ibox(s.s, indent_unit); + if (elt.mut == ast::mut) {word_nbsp(s, "mutable");} print_expr(s, elt.expr); end(s.s); } @@ -438,8 +478,8 @@ fn print_expr(ps s, &@ast::expr expr) { } case (ast::expr_rec(?fields,?wth,_)) { fn print_field(ps s, &ast::field field) { - hbox(s); - if (field.mut == ast::mut) {wrd1(s, "mutable");} + ibox(s.s, indent_unit); + if (field.mut == ast::mut) {word_nbsp(s, "mutable");} wrd(s.s, field.ident); wrd(s.s, "="); print_expr(s, field.expr); @@ -456,8 +496,8 @@ fn print_expr(ps s, &@ast::expr expr) { alt (wth) { case (option::some[@ast::expr](?expr)) { if (vec::len[ast::field](fields) > 0u) {space(s.s);} - hbox(s); - wrd1(s, "with"); + ibox(s.s, indent_unit); + word_space(s, "with"); print_expr(s, expr); end(s.s); } @@ -484,7 +524,7 @@ fn print_expr(ps s, &@ast::expr expr) { case (_) {wrd(s.s, "_");} } } - wrd1(s, "bind"); + word_nbsp(s, "bind"); print_expr(s, func); popen(s); auto f = print_opt; @@ -492,7 +532,7 @@ fn print_expr(ps s, &@ast::expr expr) { pclose(s); } case (ast::expr_spawn(_,_,?e,?es,_)) { - wrd1(s, "spawn"); + word_nbsp(s, "spawn"); print_expr(s, e); popen(s); commasep_exprs(s, es); @@ -502,7 +542,7 @@ fn print_expr(ps s, &@ast::expr expr) { auto prec = operator_prec(op); print_maybe_parens(s, lhs, prec); space(s.s); - wrd1(s, ast::binop_to_str(op)); + word_space(s, ast::binop_to_str(op)); print_maybe_parens(s, rhs, prec + 1); } case (ast::expr_unary(?op,?expr,_)) { @@ -515,111 +555,119 @@ fn print_expr(ps s, &@ast::expr expr) { case (ast::expr_cast(?expr,?ty,_)) { print_maybe_parens(s, expr, front::parser::as_prec); space(s.s); - wrd1(s, "as"); + word_space(s, "as"); print_type(s, ty); } case (ast::expr_if(?test,?block,?elseopt,_)) { - wrd1(s, "if"); - popen_h(s); + head(s, "if"); + popen(s); print_expr(s, test); pclose(s); space(s.s); print_block(s, block); alt (elseopt) { case (option::some[@ast::expr](?_else)) { - space(s.s); - wrd1(s, "else"); - print_expr(s, _else); + // NB: we can't use 'head' here since + // it builds a block that starts in the + // wrong column. + cbox(s.s, indent_unit-1u); + ibox(s.s, 0u); + wrd(s.s, " else "); + alt (_else.node) { + case (ast::expr_block(?b, _)) { + print_block(s, block); + } + } } case (_) { /* fall through */ } } } case (ast::expr_while(?test,?block,_)) { - wrd1(s, "while"); - popen_h(s); + head(s, "while"); + popen(s); print_expr(s, test); pclose(s); space(s.s); print_block(s, block); } case (ast::expr_for(?decl,?expr,?block,_)) { - wrd1(s, "for"); - popen_h(s); + head(s, "for"); + popen(s); print_for_decl(s, decl); space(s.s); - wrd1(s, "in"); + word_space(s, "in"); print_expr(s, expr); pclose(s); space(s.s); print_block(s, block); } case (ast::expr_for_each(?decl,?expr,?block,_)) { - wrd1(s, "for each"); - popen_h(s); + head(s, "for each"); + popen(s); print_for_decl(s, decl); space(s.s); - wrd1(s, "in"); + word_space(s, "in"); print_expr(s, expr); pclose(s); space(s.s); print_block(s, block); } case (ast::expr_do_while(?block,?expr,_)) { - wrd1(s, "do"); + head(s, "do"); space(s.s); print_block(s, block); space(s.s); - wrd1(s, "while"); - popen_h(s); + word_space(s, "while"); + popen(s); print_expr(s, expr); pclose(s); } case (ast::expr_alt(?expr,?arms,_)) { - wrd1(s, "alt"); - popen_h(s); + head(s, "alt"); + popen(s); print_expr(s, expr); pclose(s); space(s.s); bopen(s); for (ast::arm arm in arms) { - hbox(s); - wrd1(s, "case"); - popen_h(s); + space(s.s); + head(s, "case"); + popen(s); print_pat(s, arm.pat); pclose(s); space(s.s); print_block(s, arm.block); - end(s.s); - line(s.s); } - bclose_c(s, expr.span); + bclose(s, expr.span); } case (ast::expr_block(?block,_)) { + cbox(s.s, indent_unit); // containing cbox, will be closed by print-block at } + ibox(s.s, 0u); // head-box, will be closed by print-block after { print_block(s, block); } case (ast::expr_assign(?lhs,?rhs,_)) { print_expr(s, lhs); space(s.s); - wrd1(s, "="); + word_space(s, "="); print_expr(s, rhs); } case (ast::expr_assign_op(?op,?lhs,?rhs,_)) { print_expr(s, lhs); space(s.s); wrd(s.s, ast::binop_to_str(op)); - wrd1(s, "="); + word_space(s, "="); print_expr(s, rhs); } case (ast::expr_send(?lhs, ?rhs, _)) { print_expr(s, lhs); space(s.s); - wrd1(s, "<|"); + word_space(s, "<|"); print_expr(s, rhs); } case (ast::expr_recv(?lhs, ?rhs, _)) { print_expr(s, rhs); space(s.s); - wrd1(s, "|>"); + word_space(s, "|>"); print_expr(s, lhs); } case (ast::expr_field(?expr,?id,_)) { @@ -630,7 +678,7 @@ fn print_expr(ps s, &@ast::expr expr) { case (ast::expr_index(?expr,?index,_)) { print_expr(s, expr); wrd(s.s, "."); - popen_h(s); + popen(s); print_expr(s, index); pclose(s); } @@ -650,7 +698,7 @@ fn print_expr(ps s, &@ast::expr expr) { wrd(s.s, "ret"); alt (result) { case (option::some[@ast::expr](?expr)) { - space(s.s); + wrd(s.s, " "); print_expr(s, expr); } case (_) {} @@ -660,32 +708,32 @@ fn print_expr(ps s, &@ast::expr expr) { wrd(s.s, "put"); alt (result) { case (option::some[@ast::expr](?expr)) { - space(s.s); + wrd(s.s, " "); print_expr(s, expr); } case (_) {} } } case (ast::expr_be(?result,_)) { - wrd1(s, "be"); + word_nbsp(s, "be"); print_expr(s, result); } case (ast::expr_log(?lvl,?expr,_)) { alt (lvl) { - case (1) {wrd1(s, "log");} - case (0) {wrd1(s, "log_err");} + case (1) {word_nbsp(s, "log");} + case (0) {word_nbsp(s, "log_err");} } print_expr(s, expr); } case (ast::expr_check(?expr,_)) { - wrd1(s, "check"); - popen_h(s); + word_nbsp(s, "check"); + popen(s); print_expr(s, expr); pclose(s); } case (ast::expr_assert(?expr,_)) { - wrd1(s, "assert"); - popen_h(s); + word_nbsp(s, "assert"); + popen(s); print_expr(s, expr); pclose(s); } @@ -701,12 +749,12 @@ fn print_expr(ps s, &@ast::expr expr) { } case (ast::expr_port(_)) { wrd(s.s, "port"); - popen_h(s); + popen(s); pclose(s); } case (ast::expr_chan(?expr, _)) { wrd(s.s, "chan"); - popen_h(s); + popen(s); print_expr(s, expr); pclose(s); } @@ -722,7 +770,8 @@ fn print_expr(ps s, &@ast::expr expr) { case (mo_untyped) { /* no-op */ } case (mo_typed(?tcx)) { space(s.s); - wrd1(s, "as"); + wrd(s.s, "as"); + space(s.s); wrd(s.s, ty::ty_to_str(tcx, ty::expr_ty(tcx, expr))); pclose(s); } @@ -733,17 +782,18 @@ fn print_expr(ps s, &@ast::expr expr) { fn print_decl(ps s, @ast::decl decl) { maybe_print_comment(s, decl.span.lo); - hbox(s); alt (decl.node) { case (ast::decl_local(?loc)) { + space(s.s); + ibox(s.s, indent_unit); alt (loc.ty) { case (option::some[@ast::ty](?ty)) { - wrd1(s, "let"); + word_nbsp(s, "let"); print_type(s, ty); space(s.s); } case (_) { - wrd1(s, "auto"); + word_nbsp(s, "auto"); // Print the type if necessary. alt (s.mode) { @@ -751,7 +801,7 @@ fn print_decl(ps s, @ast::decl decl) { case (mo_typed(?tcx)) { auto lty = ty::ann_to_type(tcx.node_types, loc.ann); - wrd1(s, ty::ty_to_str(tcx, lty)); + word_space(s, ty::ty_to_str(tcx, lty)); } } } @@ -762,22 +812,22 @@ fn print_decl(ps s, @ast::decl decl) { space(s.s); alt (init.op) { case (ast::init_assign) { - wrd1(s, "="); + word_space(s, "="); } case (ast::init_recv) { - wrd1(s, "<-"); + word_space(s, "<-"); } } print_expr(s, init.expr); } case (_) {} } + end(s.s); } case (ast::decl_item(?item)) { print_item(s, item); } } - end(s.s); } fn print_ident(ps s, ast::ident ident) { @@ -819,7 +869,7 @@ fn print_pat(ps s, &@ast::pat pat) { case (ast::pat_tag(?path,?args,_)) { print_path(s, path); if (vec::len[@ast::pat](args) > 0u) { - popen_h(s); + popen(s); auto f = print_pat; commasep[@ast::pat](s, args, f); pclose(s); @@ -832,17 +882,17 @@ fn print_fn(ps s, ast::fn_decl decl, str name, vec[ast::ty_param] typarams) { alt (decl.purity) { case (ast::impure_fn) { - wrd1(s, "fn"); + head(s, "fn"); } case (_) { - wrd1(s, "pred"); + head(s, "pred"); } } wrd(s.s, name); print_type_params(s, typarams); popen(s); fn print_arg(ps s, &ast::arg x) { - hbox(s); + ibox(s.s, indent_unit); if (x.mode == ast::alias) {wrd(s.s, "&");} print_type(s, x.ty); space(s.s); @@ -855,10 +905,8 @@ fn print_fn(ps s, ast::fn_decl decl, str name, maybe_print_comment(s, decl.output.span.lo); if (decl.output.node != ast::ty_nil) { space(s.s); - hbox(s); - wrd1(s, "->"); + word_space(s, "->"); print_type(s, decl.output); - end(s.s); } } @@ -875,18 +923,18 @@ fn print_type_params(ps s, vec[ast::ty_param] params) { } fn print_view_item(ps s, @ast::view_item item) { + hardbreak(s.s); maybe_print_comment(s, item.span.lo); - hbox(s); alt (item.node) { case (ast::view_item_use(?id,?mta,_,_)) { - wrd1(s, "use"); + head(s, "use"); wrd(s.s, id); if (vec::len[@ast::meta_item](mta) > 0u) { popen(s); fn print_meta(ps s, &@ast::meta_item item) { - hbox(s); - wrd1(s, item.node.name); - wrd1(s, "="); + ibox(s.s, indent_unit); + word_space(s, item.node.name); + word_space(s, "="); print_string(s, item.node.value); end(s.s); } @@ -896,10 +944,10 @@ fn print_view_item(ps s, @ast::view_item item) { } } case (ast::view_item_import(?id,?ids,_)) { - wrd1(s, "import"); + head(s, "import"); if (!str::eq(id, ids.(vec::len[str](ids)-1u))) { - wrd1(s, id); - wrd1(s, "="); + word_space(s, id); + word_space(s, "="); } auto first = true; for (str elt in ids) { @@ -909,13 +957,13 @@ fn print_view_item(ps s, @ast::view_item item) { } } case (ast::view_item_export(?id)) { - wrd1(s, "export"); + head(s, "export"); wrd(s.s, id); } } - end(s.s); wrd(s.s, ";"); - line(s.s); + end(s.s); // end inner head-block + end(s.s); // end outer head-block } // FIXME: The fact that this builds up the table anew for every call is @@ -968,8 +1016,8 @@ fn escape_str(str st, char to_escape) -> str { fn print_mt(ps s, &ast::mt mt) { alt (mt.mut) { - case (ast::mut) { wrd1(s, "mutable"); } - case (ast::maybe_mut) { wrd1(s, "mutable?"); } + case (ast::mut) { word_nbsp(s, "mutable"); } + case (ast::maybe_mut) { word_nbsp(s, "mutable?"); } case (ast::imm) { /* nothing */ } } print_type(s, mt.ty); @@ -988,7 +1036,7 @@ fn print_ty_fn(ps s, ast::proto proto, option::t[str] id, case (option::some[str](?id)) {space(s.s); wrd(s.s, id);} case (_) {} } - popen_h(s); + popen(s); fn print_arg(ps s, &ast::ty_arg input) { if (input.mode == ast::alias) {wrd(s.s, "&");} print_type(s, input.ty); @@ -999,14 +1047,14 @@ fn print_ty_fn(ps s, ast::proto proto, option::t[str] id, maybe_print_comment(s, output.span.lo); if (output.node != ast::ty_nil) { space(s.s); - hbox(s); - wrd1(s, "->"); + ibox(s.s, indent_unit); + word_space(s, "->"); alt (cf) { case (ast::return) { print_type(s, output); } case (ast::noreturn) { - wrd1(s, "!"); + word_nbsp(s, "!"); } } end(s.s); @@ -1025,12 +1073,16 @@ fn next_comment(ps s) -> option::t[lexer::cmnt] { } fn maybe_print_comment(ps s, uint pos) { + auto first = true; while (true) { alt (next_comment(s)) { case (option::some[lexer::cmnt](?cmnt)) { if (cmnt.pos < pos) { + if (first) { + first = false; + break_offset(s.s, 0u, 0); + } print_comment(s, cmnt.val); - if (cmnt.space_after) {line(s.s);} s.cur_cmnt += 1u; } else { break; } } @@ -1055,11 +1107,15 @@ fn maybe_print_line_comment(ps s, common::span span) -> bool { } fn print_remaining_comments(ps s) { + auto first = true; while (true) { alt (next_comment(s)) { case (option::some[lexer::cmnt](?cmnt)) { + if (first) { + first = false; + break_offset(s.s, 0u, 0); + } print_comment(s, cmnt.val); - if (cmnt.space_after) {line(s.s);} s.cur_cmnt += 1u; } case (_) {break;} @@ -1071,22 +1127,23 @@ fn print_comment(ps s, lexer::cmnt_ cmnt) { alt (cmnt) { case (lexer::cmnt_line(?val)) { wrd(s.s, "// " + val); - pp::hardbreak(s.s); + hardbreak(s.s); } case (lexer::cmnt_block(?lines)) { - pp::abox(s.s); - wrd(s.s, "/* "); - pp::abox(s.s); + cbox(s.s, 1u); + wrd(s.s, "/*"); auto first = true; for (str ln in lines) { - if (first) {first = false;} - else {pp::hardbreak(s.s);} + if (first) { + first = false; + } else { + hardbreak(s.s); + } wrd(s.s, ln); } - end(s.s); wrd(s.s, "*/"); end(s.s); - line(s.s); + hardbreak(s.s); } } } diff --git a/src/comp/util/common.rs b/src/comp/util/common.rs index 070324dd9e83..119fcce806c2 100644 --- a/src/comp/util/common.rs +++ b/src/comp/util/common.rs @@ -20,7 +20,7 @@ import pretty::pprust::print_decl; import pretty::pprust::print_fn; import pretty::pprust::print_type; import pretty::pprust::mo_untyped; -import pretty::pp::mkstate; +import pretty::pp::mk_printer; type filename = str; type span = rec(uint lo, uint hi); @@ -127,7 +127,7 @@ fn field_exprs(vec[ast::field] fields) -> vec [@ast::expr] { fn expr_to_str(&@ast::expr e) -> str { let str_writer s = string_writer(); - auto out_ = mkstate(s.get_writer(), 80u); + auto out_ = mk_printer(s.get_writer(), 80u); auto out = @rec(s=out_, comments=none[vec[front::lexer::cmnt]], mutable cur_cmnt=0u, @@ -138,7 +138,7 @@ fn expr_to_str(&@ast::expr e) -> str { fn ty_to_str(&ty t) -> str { let str_writer s = string_writer(); - auto out_ = mkstate(s.get_writer(), 80u); + auto out_ = mk_printer(s.get_writer(), 80u); auto out = @rec(s=out_, comments=none[vec[front::lexer::cmnt]], mutable cur_cmnt=0u, @@ -165,7 +165,7 @@ fn log_pat_err(&@pat p) -> () { fn block_to_str(&ast::block b) -> str { let str_writer s = string_writer(); - auto out_ = mkstate(s.get_writer(), 80u); + auto out_ = mk_printer(s.get_writer(), 80u); auto out = @rec(s=out_, comments=none[vec[front::lexer::cmnt]], mutable cur_cmnt=0u, @@ -177,7 +177,7 @@ fn block_to_str(&ast::block b) -> str { fn item_to_str(&@ast::item i) -> str { let str_writer s = string_writer(); - auto out_ = mkstate(s.get_writer(), 80u); + auto out_ = mk_printer(s.get_writer(), 80u); auto out = @rec(s=out_, comments=none[vec[front::lexer::cmnt]], mutable cur_cmnt=0u, @@ -200,7 +200,7 @@ fn log_item_err(&@ast::item i) -> () { fn fun_to_str(&ast::_fn f, str name, vec[ast::ty_param] params) -> str { let str_writer s = string_writer(); - auto out_ = mkstate(s.get_writer(), 80u); + auto out_ = mk_printer(s.get_writer(), 80u); auto out = @rec(s=out_, comments=none[vec[front::lexer::cmnt]], mutable cur_cmnt=0u, @@ -220,7 +220,7 @@ fn log_fn_err(&ast::_fn f, str name, vec[ast::ty_param] params) -> () { fn stmt_to_str(&ast::stmt st) -> str { let str_writer s = string_writer(); - auto out_ = mkstate(s.get_writer(), 80u); + auto out_ = mk_printer(s.get_writer(), 80u); auto out = @rec(s=out_, comments=none[vec[front::lexer::cmnt]], mutable cur_cmnt=0u,