2012-09-04 13:37:29 -05:00
|
|
|
use io::WriterUtil;
|
|
|
|
use dvec::DVec;
|
2011-06-15 13:19:50 -05:00
|
|
|
|
2011-05-28 21:16:18 -05:00
|
|
|
/*
|
|
|
|
* 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.
|
2011-05-30 16:10:54 -05:00
|
|
|
*
|
|
|
|
* I also implemented a small change in the STRING token, in that I store an
|
|
|
|
* explicit length for the string. For most tokens this is just the length of
|
|
|
|
* the accompanying string. But it's necessary to permit it to differ, for
|
|
|
|
* encoding things that are supposed to "go on their own line" -- certain
|
|
|
|
* classes of comment and blank-line -- where relying on adjacent
|
|
|
|
* hardbreak-like BREAK tokens with long blankness indication doesn't actually
|
|
|
|
* work. To see why, consider when there is a "thing that should be on its own
|
|
|
|
* line" between two long blocks, say functions. If you put a hardbreak after
|
|
|
|
* each function (or before each) and the breaking algorithm decides to break
|
|
|
|
* there anyways (because the functions themselves are long) you wind up with
|
|
|
|
* extra blank lines. If you don't put hardbreaks you can wind up with the
|
|
|
|
* "thing which should be on its own line" not getting its own line in the
|
|
|
|
* rare case of "really small functions" or such. This re-occurs with comments
|
|
|
|
* and explicit blank lines. So in those cases we use a string with a payload
|
|
|
|
* we want isolated to a line and an explicit length that's huge, surrounded
|
|
|
|
* by two zero-length breaks. The algorithm will try its best to fit it on a
|
|
|
|
* line (which it can't) and so naturally place the content on its own line to
|
|
|
|
* avoid combining it with other lines and making matters even worse.
|
2011-05-28 21:16:18 -05:00
|
|
|
*/
|
2012-01-19 19:56:05 -06:00
|
|
|
enum breaks { consistent, inconsistent, }
|
2011-06-15 13:19:50 -05:00
|
|
|
|
2012-09-19 20:00:26 -05:00
|
|
|
#[cfg(stage0)]
|
2012-08-27 18:26:35 -05:00
|
|
|
impl breaks : cmp::Eq {
|
|
|
|
pure fn eq(&&other: breaks) -> bool {
|
|
|
|
match (self, other) {
|
|
|
|
(consistent, consistent) => true,
|
|
|
|
(inconsistent, inconsistent) => true,
|
|
|
|
(consistent, _) => false,
|
|
|
|
(inconsistent, _) => false,
|
|
|
|
}
|
|
|
|
}
|
2012-09-07 14:06:02 -05:00
|
|
|
pure fn ne(&&other: breaks) -> bool { !self.eq(other) }
|
2012-08-27 18:26:35 -05:00
|
|
|
}
|
2012-09-19 20:00:26 -05:00
|
|
|
#[cfg(stage1)]
|
|
|
|
#[cfg(stage2)]
|
|
|
|
impl breaks : cmp::Eq {
|
|
|
|
pure fn eq(other: &breaks) -> bool {
|
|
|
|
match (self, (*other)) {
|
|
|
|
(consistent, consistent) => true,
|
|
|
|
(inconsistent, inconsistent) => true,
|
|
|
|
(consistent, _) => false,
|
|
|
|
(inconsistent, _) => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pure fn ne(other: &breaks) -> bool { !self.eq(other) }
|
|
|
|
}
|
2012-08-27 18:26:35 -05:00
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
type break_t = {offset: int, blank_space: int};
|
2011-03-04 00:22:43 -06:00
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
type begin_t = {offset: int, breaks: breaks};
|
2011-03-04 00:22:43 -06:00
|
|
|
|
2012-07-14 00:57:48 -05:00
|
|
|
enum token { STRING(@~str, int), BREAK(break_t), BEGIN(begin_t), END, EOF, }
|
2011-03-24 10:33:20 -05:00
|
|
|
|
2012-08-27 18:26:35 -05:00
|
|
|
impl token {
|
|
|
|
fn is_eof() -> bool {
|
|
|
|
match self { EOF => true, _ => false }
|
|
|
|
}
|
|
|
|
fn is_hardbreak_tok() -> bool {
|
|
|
|
match self {
|
|
|
|
BREAK({offset: 0, blank_space: bs }) if bs == size_infinity =>
|
|
|
|
true,
|
|
|
|
_ =>
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-14 00:57:48 -05:00
|
|
|
fn tok_str(++t: token) -> ~str {
|
2012-08-06 14:34:08 -05:00
|
|
|
match t {
|
2012-08-22 19:24:52 -05:00
|
|
|
STRING(s, len) => return fmt!("STR(%s,%d)", *s, len),
|
2012-08-03 21:59:04 -05:00
|
|
|
BREAK(_) => return ~"BREAK",
|
|
|
|
BEGIN(_) => return ~"BEGIN",
|
|
|
|
END => return ~"END",
|
|
|
|
EOF => return ~"EOF"
|
2011-05-28 21:16:18 -05:00
|
|
|
}
|
2011-03-04 00:22:43 -06:00
|
|
|
}
|
|
|
|
|
2012-06-29 18:26:56 -05:00
|
|
|
fn buf_str(toks: ~[mut token], szs: ~[mut int], left: uint, right: uint,
|
2012-07-14 00:57:48 -05:00
|
|
|
lim: uint) -> ~str {
|
2011-08-15 18:38:23 -05:00
|
|
|
let n = vec::len(toks);
|
|
|
|
assert (n == vec::len(szs));
|
2012-03-15 08:47:03 -05:00
|
|
|
let mut i = left;
|
|
|
|
let mut L = lim;
|
2012-07-14 00:57:48 -05:00
|
|
|
let mut s = ~"[";
|
2011-07-27 07:19:39 -05:00
|
|
|
while i != right && L != 0u {
|
2011-05-28 21:16:18 -05:00
|
|
|
L -= 1u;
|
2012-07-14 00:57:48 -05:00
|
|
|
if i != left { s += ~", "; }
|
2012-08-22 19:24:52 -05:00
|
|
|
s += fmt!("%d=%s", szs[i], tok_str(toks[i]));
|
2011-05-28 21:16:18 -05:00
|
|
|
i += 1u;
|
|
|
|
i %= n;
|
|
|
|
}
|
2012-07-14 00:57:48 -05:00
|
|
|
s += ~"]";
|
2012-08-01 19:30:05 -05:00
|
|
|
return s;
|
2011-03-04 00:22:43 -06:00
|
|
|
}
|
|
|
|
|
2012-01-19 19:56:05 -06:00
|
|
|
enum print_stack_break { fits, broken(breaks), }
|
2011-06-15 13:19:50 -05:00
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
type print_stack_elt = {offset: int, pbreak: print_stack_break};
|
2011-03-04 00:22:43 -06:00
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
const size_infinity: int = 0xffff;
|
2011-03-09 04:41:50 -06:00
|
|
|
|
2012-08-14 15:38:35 -05:00
|
|
|
fn mk_printer(out: io::Writer, linewidth: uint) -> printer {
|
2011-05-28 21:16:18 -05:00
|
|
|
// Yes 3, it makes the ring buffers big enough to never
|
|
|
|
// fall behind.
|
2012-09-10 20:28:00 -05:00
|
|
|
let n: uint = 3 * linewidth;
|
2012-08-22 19:24:52 -05:00
|
|
|
debug!("mk_printer %u", linewidth);
|
2012-06-29 18:26:56 -05:00
|
|
|
let token: ~[mut token] = vec::to_mut(vec::from_elem(n, EOF));
|
|
|
|
let size: ~[mut int] = vec::to_mut(vec::from_elem(n, 0));
|
|
|
|
let scan_stack: ~[mut uint] = vec::to_mut(vec::from_elem(n, 0u));
|
2012-07-11 17:00:40 -05:00
|
|
|
printer_(@{out: out,
|
|
|
|
buf_len: n,
|
|
|
|
mut margin: linewidth as int,
|
|
|
|
mut space: linewidth as int,
|
2012-09-10 20:28:00 -05:00
|
|
|
mut left: 0,
|
|
|
|
mut right: 0,
|
|
|
|
token: move token,
|
|
|
|
size: move size,
|
2012-07-11 17:00:40 -05:00
|
|
|
mut left_total: 0,
|
|
|
|
mut right_total: 0,
|
2012-09-10 20:28:00 -05:00
|
|
|
mut scan_stack: move scan_stack,
|
2012-07-11 17:00:40 -05:00
|
|
|
mut scan_stack_empty: true,
|
2012-09-10 20:28:00 -05:00
|
|
|
mut top: 0,
|
|
|
|
mut bottom: 0,
|
2012-08-27 16:22:25 -05:00
|
|
|
print_stack: DVec(),
|
2012-07-30 19:56:53 -05:00
|
|
|
mut pending_indentation: 0,
|
|
|
|
mut token_tree_last_was_ident: false})
|
2011-03-24 10:33:20 -05:00
|
|
|
}
|
|
|
|
|
2011-06-15 13:19:50 -05:00
|
|
|
|
2011-05-28 21:16:18 -05:00
|
|
|
/*
|
|
|
|
* 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'.
|
|
|
|
*/
|
2012-07-11 17:00:40 -05:00
|
|
|
type printer_ = {
|
2012-08-14 15:38:35 -05:00
|
|
|
out: io::Writer,
|
2012-01-13 02:32:05 -06:00
|
|
|
buf_len: uint,
|
2012-03-26 20:35:18 -05:00
|
|
|
mut margin: int, // width of lines we're constrained to
|
|
|
|
mut space: int, // number of spaces left on line
|
|
|
|
mut left: uint, // index of left side of input stream
|
|
|
|
mut right: uint, // index of right side of input stream
|
2012-06-29 18:26:56 -05:00
|
|
|
token: ~[mut token], // ring-buffr stream goes through
|
|
|
|
size: ~[mut int], // ring-buffer of calculated sizes
|
2012-03-26 20:35:18 -05:00
|
|
|
mut left_total: int, // running size of stream "...left"
|
|
|
|
mut right_total: int, // running size of stream "...right"
|
2012-01-13 02:32:05 -06:00
|
|
|
// 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.
|
2012-06-29 18:26:56 -05:00
|
|
|
mut scan_stack: ~[mut uint],
|
2012-03-26 20:35:18 -05:00
|
|
|
mut scan_stack_empty: bool, // top==bottom disambiguator
|
|
|
|
mut top: uint, // index of top of scan_stack
|
|
|
|
mut bottom: uint, // index of bottom of scan_stack
|
2012-01-13 02:32:05 -06:00
|
|
|
// stack of blocks-in-progress being flushed by print
|
2012-08-14 18:54:13 -05:00
|
|
|
print_stack: DVec<print_stack_elt>,
|
2012-01-13 02:32:05 -06:00
|
|
|
// buffered indentation to avoid writing trailing whitespace
|
2012-07-30 19:56:53 -05:00
|
|
|
mut pending_indentation: int,
|
|
|
|
mut token_tree_last_was_ident: bool
|
2012-01-13 02:32:05 -06:00
|
|
|
};
|
|
|
|
|
2012-07-11 17:00:40 -05:00
|
|
|
enum printer {
|
|
|
|
printer_(@printer_)
|
|
|
|
}
|
|
|
|
|
2012-08-07 20:10:06 -05:00
|
|
|
impl printer {
|
2012-01-13 02:32:05 -06:00
|
|
|
fn last_token() -> token { self.token[self.right] }
|
2011-07-26 17:37:05 -05:00
|
|
|
// be very careful with this!
|
2012-01-13 02:32:05 -06:00
|
|
|
fn replace_last_token(t: token) { self.token[self.right] = t; }
|
2011-07-27 07:19:39 -05:00
|
|
|
fn pretty_print(t: token) {
|
2012-08-22 19:24:52 -05:00
|
|
|
debug!("pp ~[%u,%u]", self.left, self.right);
|
2012-08-06 14:34:08 -05:00
|
|
|
match t {
|
2012-08-03 21:59:04 -05:00
|
|
|
EOF => {
|
2012-01-13 02:32:05 -06:00
|
|
|
if !self.scan_stack_empty {
|
2011-07-27 07:19:39 -05:00
|
|
|
self.check_stack(0);
|
2012-01-13 02:32:05 -06:00
|
|
|
self.advance_left(self.token[self.left],
|
|
|
|
self.size[self.left]);
|
2011-05-28 21:16:18 -05:00
|
|
|
}
|
2011-07-27 07:19:39 -05:00
|
|
|
self.indent(0);
|
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
BEGIN(b) => {
|
2012-01-13 02:32:05 -06:00
|
|
|
if self.scan_stack_empty {
|
|
|
|
self.left_total = 1;
|
|
|
|
self.right_total = 1;
|
|
|
|
self.left = 0u;
|
|
|
|
self.right = 0u;
|
2011-07-27 07:19:39 -05:00
|
|
|
} else { self.advance_right(); }
|
2012-08-22 19:24:52 -05:00
|
|
|
debug!("pp BEGIN(%d)/buffer ~[%u,%u]",
|
|
|
|
b.offset, self.left, self.right);
|
2012-01-13 02:32:05 -06:00
|
|
|
self.token[self.right] = t;
|
|
|
|
self.size[self.right] = -self.right_total;
|
|
|
|
self.scan_push(self.right);
|
2011-07-27 07:19:39 -05:00
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
END => {
|
2012-01-13 02:32:05 -06:00
|
|
|
if self.scan_stack_empty {
|
2012-08-22 19:24:52 -05:00
|
|
|
debug!("pp END/print ~[%u,%u]", self.left, self.right);
|
2011-07-27 07:19:39 -05:00
|
|
|
self.print(t, 0);
|
|
|
|
} else {
|
2012-08-22 19:24:52 -05:00
|
|
|
debug!("pp END/buffer ~[%u,%u]", self.left, self.right);
|
2011-07-27 07:19:39 -05:00
|
|
|
self.advance_right();
|
2012-01-13 02:32:05 -06:00
|
|
|
self.token[self.right] = t;
|
|
|
|
self.size[self.right] = -1;
|
|
|
|
self.scan_push(self.right);
|
2011-05-28 21:16:18 -05:00
|
|
|
}
|
2011-07-27 07:19:39 -05:00
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
BREAK(b) => {
|
2012-01-13 02:32:05 -06:00
|
|
|
if self.scan_stack_empty {
|
|
|
|
self.left_total = 1;
|
|
|
|
self.right_total = 1;
|
|
|
|
self.left = 0u;
|
|
|
|
self.right = 0u;
|
2011-07-27 07:19:39 -05:00
|
|
|
} else { self.advance_right(); }
|
2012-08-22 19:24:52 -05:00
|
|
|
debug!("pp BREAK(%d)/buffer ~[%u,%u]",
|
|
|
|
b.offset, self.left, self.right);
|
2011-07-27 07:19:39 -05:00
|
|
|
self.check_stack(0);
|
2012-01-13 02:32:05 -06:00
|
|
|
self.scan_push(self.right);
|
|
|
|
self.token[self.right] = t;
|
|
|
|
self.size[self.right] = -self.right_total;
|
|
|
|
self.right_total += b.blank_space;
|
2011-07-27 07:19:39 -05:00
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
STRING(s, len) => {
|
2012-01-13 02:32:05 -06:00
|
|
|
if self.scan_stack_empty {
|
2012-08-22 19:24:52 -05:00
|
|
|
debug!("pp STRING('%s')/print ~[%u,%u]",
|
|
|
|
*s, self.left, self.right);
|
2011-07-27 07:19:39 -05:00
|
|
|
self.print(t, len);
|
|
|
|
} else {
|
2012-08-22 19:24:52 -05:00
|
|
|
debug!("pp STRING('%s')/buffer ~[%u,%u]",
|
|
|
|
*s, self.left, self.right);
|
2011-07-27 07:19:39 -05:00
|
|
|
self.advance_right();
|
2012-01-13 02:32:05 -06:00
|
|
|
self.token[self.right] = t;
|
|
|
|
self.size[self.right] = len;
|
|
|
|
self.right_total += len;
|
2011-07-27 07:19:39 -05:00
|
|
|
self.check_stream();
|
2011-05-28 21:16:18 -05:00
|
|
|
}
|
2011-07-27 07:19:39 -05:00
|
|
|
}
|
2011-05-28 21:16:18 -05:00
|
|
|
}
|
2011-03-04 00:22:43 -06:00
|
|
|
}
|
2011-05-28 21:16:18 -05:00
|
|
|
fn check_stream() {
|
2012-08-22 19:24:52 -05:00
|
|
|
debug!("check_stream ~[%u, %u] with left_total=%d, right_total=%d",
|
|
|
|
self.left, self.right, self.left_total, self.right_total);
|
2012-01-13 02:32:05 -06:00
|
|
|
if self.right_total - self.left_total > self.space {
|
2012-08-22 19:24:52 -05:00
|
|
|
debug!("scan window is %d, longer than space on line (%d)",
|
|
|
|
self.right_total - self.left_total, self.space);
|
2012-01-13 02:32:05 -06:00
|
|
|
if !self.scan_stack_empty {
|
|
|
|
if self.left == self.scan_stack[self.bottom] {
|
2012-08-22 19:24:52 -05:00
|
|
|
debug!("setting %u to infinity and popping", self.left);
|
2012-01-13 02:32:05 -06:00
|
|
|
self.size[self.scan_pop_bottom()] = size_infinity;
|
2011-05-28 21:16:18 -05:00
|
|
|
}
|
|
|
|
}
|
2012-01-13 02:32:05 -06:00
|
|
|
self.advance_left(self.token[self.left], self.size[self.left]);
|
|
|
|
if self.left != self.right { self.check_stream(); }
|
2011-05-28 21:16:18 -05:00
|
|
|
}
|
2011-03-24 10:33:20 -05:00
|
|
|
}
|
2011-07-27 07:19:39 -05:00
|
|
|
fn scan_push(x: uint) {
|
2012-08-22 19:24:52 -05:00
|
|
|
debug!("scan_push %u", x);
|
2012-01-13 02:32:05 -06:00
|
|
|
if self.scan_stack_empty {
|
|
|
|
self.scan_stack_empty = false;
|
|
|
|
} else {
|
|
|
|
self.top += 1u;
|
|
|
|
self.top %= self.buf_len;
|
|
|
|
assert (self.top != self.bottom);
|
|
|
|
}
|
|
|
|
self.scan_stack[self.top] = x;
|
2011-03-04 00:22:43 -06:00
|
|
|
}
|
2011-05-28 21:16:18 -05:00
|
|
|
fn scan_pop() -> uint {
|
2012-01-13 02:32:05 -06:00
|
|
|
assert (!self.scan_stack_empty);
|
|
|
|
let x = self.scan_stack[self.top];
|
|
|
|
if self.top == self.bottom {
|
|
|
|
self.scan_stack_empty = true;
|
|
|
|
} else { self.top += self.buf_len - 1u; self.top %= self.buf_len; }
|
2012-08-01 19:30:05 -05:00
|
|
|
return x;
|
2011-05-28 21:16:18 -05:00
|
|
|
}
|
2012-01-13 02:32:05 -06:00
|
|
|
fn scan_top() -> uint {
|
|
|
|
assert (!self.scan_stack_empty);
|
2012-08-01 19:30:05 -05:00
|
|
|
return self.scan_stack[self.top];
|
2012-01-13 02:32:05 -06:00
|
|
|
}
|
2011-05-28 21:16:18 -05:00
|
|
|
fn scan_pop_bottom() -> uint {
|
2012-01-13 02:32:05 -06:00
|
|
|
assert (!self.scan_stack_empty);
|
|
|
|
let x = self.scan_stack[self.bottom];
|
|
|
|
if self.top == self.bottom {
|
|
|
|
self.scan_stack_empty = true;
|
|
|
|
} else { self.bottom += 1u; self.bottom %= self.buf_len; }
|
2012-08-01 19:30:05 -05:00
|
|
|
return x;
|
2011-05-28 21:16:18 -05:00
|
|
|
}
|
|
|
|
fn advance_right() {
|
2012-01-13 02:32:05 -06:00
|
|
|
self.right += 1u;
|
|
|
|
self.right %= self.buf_len;
|
|
|
|
assert (self.right != self.left);
|
2011-03-04 00:22:43 -06:00
|
|
|
}
|
2012-06-03 09:36:40 -05:00
|
|
|
fn advance_left(++x: token, L: int) {
|
2012-08-22 19:24:52 -05:00
|
|
|
debug!("advnce_left ~[%u,%u], sizeof(%u)=%d", self.left, self.right,
|
|
|
|
self.left, L);
|
2011-07-27 07:19:39 -05:00
|
|
|
if L >= 0 {
|
2011-05-28 21:16:18 -05:00
|
|
|
self.print(x, L);
|
2012-08-06 14:34:08 -05:00
|
|
|
match x {
|
2012-08-03 21:59:04 -05:00
|
|
|
BREAK(b) => self.left_total += b.blank_space,
|
|
|
|
STRING(_, len) => { assert (len == L); self.left_total += len; }
|
|
|
|
_ => ()
|
2011-05-28 21:16:18 -05:00
|
|
|
}
|
2012-01-13 02:32:05 -06:00
|
|
|
if self.left != self.right {
|
|
|
|
self.left += 1u;
|
|
|
|
self.left %= self.buf_len;
|
|
|
|
self.advance_left(self.token[self.left],
|
|
|
|
self.size[self.left]);
|
2011-05-28 21:16:18 -05:00
|
|
|
}
|
|
|
|
}
|
2011-03-24 10:33:20 -05:00
|
|
|
}
|
2011-07-27 07:19:39 -05:00
|
|
|
fn check_stack(k: int) {
|
2012-01-13 02:32:05 -06:00
|
|
|
if !self.scan_stack_empty {
|
2011-07-27 07:19:39 -05:00
|
|
|
let x = self.scan_top();
|
2012-08-06 14:34:08 -05:00
|
|
|
match copy self.token[x] {
|
2012-08-26 14:12:05 -05:00
|
|
|
BEGIN(_) => {
|
2011-07-27 07:19:39 -05:00
|
|
|
if k > 0 {
|
2012-01-13 02:32:05 -06:00
|
|
|
self.size[self.scan_pop()] = self.size[x] +
|
|
|
|
self.right_total;
|
2011-07-27 07:19:39 -05:00
|
|
|
self.check_stack(k - 1);
|
2011-05-28 21:16:18 -05:00
|
|
|
}
|
2011-07-27 07:19:39 -05:00
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
END => {
|
2011-07-27 07:19:39 -05:00
|
|
|
// paper says + not =, but that makes no sense.
|
2012-01-13 02:32:05 -06:00
|
|
|
self.size[self.scan_pop()] = 1;
|
2011-07-27 07:19:39 -05:00
|
|
|
self.check_stack(k + 1);
|
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => {
|
2012-01-13 02:32:05 -06:00
|
|
|
self.size[self.scan_pop()] = self.size[x] + self.right_total;
|
2011-07-27 07:19:39 -05:00
|
|
|
if k > 0 { self.check_stack(k); }
|
|
|
|
}
|
2011-05-28 21:16:18 -05:00
|
|
|
}
|
|
|
|
}
|
2011-03-04 00:22:43 -06:00
|
|
|
}
|
2011-07-27 07:19:39 -05:00
|
|
|
fn print_newline(amount: int) {
|
2012-08-22 19:24:52 -05:00
|
|
|
debug!("NEWLINE %d", amount);
|
2012-07-14 00:57:48 -05:00
|
|
|
self.out.write_str(~"\n");
|
2012-01-13 02:32:05 -06:00
|
|
|
self.pending_indentation = 0;
|
2011-05-28 21:16:18 -05:00
|
|
|
self.indent(amount);
|
2011-03-04 00:22:43 -06:00
|
|
|
}
|
2011-07-27 07:19:39 -05:00
|
|
|
fn indent(amount: int) {
|
2012-08-22 19:24:52 -05:00
|
|
|
debug!("INDENT %d", amount);
|
2012-01-13 02:32:05 -06:00
|
|
|
self.pending_indentation += amount;
|
2011-03-04 00:22:43 -06:00
|
|
|
}
|
2012-01-13 02:32:05 -06:00
|
|
|
fn get_top() -> print_stack_elt {
|
2012-05-10 09:24:56 -05:00
|
|
|
let n = self.print_stack.len();
|
|
|
|
if n != 0u {
|
|
|
|
self.print_stack[n - 1u]
|
|
|
|
} else {
|
|
|
|
{offset: 0, pbreak: broken(inconsistent)}
|
|
|
|
}
|
2011-06-01 12:54:11 -05:00
|
|
|
}
|
2012-08-02 19:17:07 -05:00
|
|
|
fn print_str(s: ~str) {
|
2012-01-13 02:32:05 -06:00
|
|
|
while self.pending_indentation > 0 {
|
2012-07-14 00:57:48 -05:00
|
|
|
self.out.write_str(~" ");
|
2012-01-13 02:32:05 -06:00
|
|
|
self.pending_indentation -= 1;
|
2011-06-01 14:01:42 -05:00
|
|
|
}
|
2012-01-13 02:32:05 -06:00
|
|
|
self.out.write_str(s);
|
2011-06-01 14:01:42 -05:00
|
|
|
}
|
2011-07-27 07:19:39 -05:00
|
|
|
fn print(x: token, L: int) {
|
2012-08-22 19:24:52 -05:00
|
|
|
debug!("print %s %d (remaining line space=%d)", tok_str(x), L,
|
|
|
|
self.space);
|
2012-01-13 02:32:05 -06:00
|
|
|
log(debug, buf_str(self.token, self.size, self.left, self.right, 6u));
|
2012-08-06 14:34:08 -05:00
|
|
|
match x {
|
2012-08-03 21:59:04 -05:00
|
|
|
BEGIN(b) => {
|
2012-01-13 02:32:05 -06:00
|
|
|
if L > self.space {
|
|
|
|
let col = self.margin - self.space + b.offset;
|
2012-08-22 19:24:52 -05:00
|
|
|
debug!("print BEGIN -> push broken block at col %d", col);
|
2012-05-10 09:24:56 -05:00
|
|
|
self.print_stack.push({offset: col,
|
|
|
|
pbreak: broken(b.breaks)});
|
2011-07-27 07:19:39 -05:00
|
|
|
} else {
|
2012-08-22 19:24:52 -05:00
|
|
|
debug!("print BEGIN -> push fitting block");
|
2012-05-10 09:24:56 -05:00
|
|
|
self.print_stack.push({offset: 0,
|
|
|
|
pbreak: fits});
|
2011-05-28 21:16:18 -05:00
|
|
|
}
|
2011-07-27 07:19:39 -05:00
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
END => {
|
2012-08-22 19:24:52 -05:00
|
|
|
debug!("print END -> pop END");
|
2012-05-10 09:24:56 -05:00
|
|
|
assert (self.print_stack.len() != 0u);
|
|
|
|
self.print_stack.pop();
|
2011-07-27 07:19:39 -05:00
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
BREAK(b) => {
|
2012-01-13 02:32:05 -06:00
|
|
|
let top = self.get_top();
|
2012-08-06 14:34:08 -05:00
|
|
|
match top.pbreak {
|
2012-08-03 21:59:04 -05:00
|
|
|
fits => {
|
2012-08-22 19:24:52 -05:00
|
|
|
debug!("print BREAK(%d) in fitting block", b.blank_space);
|
2012-01-13 02:32:05 -06:00
|
|
|
self.space -= b.blank_space;
|
2011-07-27 07:19:39 -05:00
|
|
|
self.indent(b.blank_space);
|
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
broken(consistent) => {
|
2012-08-22 19:24:52 -05:00
|
|
|
debug!("print BREAK(%d+%d) in consistent block",
|
|
|
|
top.offset, b.offset);
|
2011-07-27 07:19:39 -05:00
|
|
|
self.print_newline(top.offset + b.offset);
|
2012-01-13 02:32:05 -06:00
|
|
|
self.space = self.margin - (top.offset + b.offset);
|
2011-07-27 07:19:39 -05:00
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
broken(inconsistent) => {
|
2012-01-13 02:32:05 -06:00
|
|
|
if L > self.space {
|
2012-08-22 19:24:52 -05:00
|
|
|
debug!("print BREAK(%d+%d) w/ newline in inconsistent",
|
|
|
|
top.offset, b.offset);
|
2011-07-27 07:19:39 -05:00
|
|
|
self.print_newline(top.offset + b.offset);
|
2012-01-13 02:32:05 -06:00
|
|
|
self.space = self.margin - (top.offset + b.offset);
|
2011-07-27 07:19:39 -05:00
|
|
|
} else {
|
2012-08-22 19:24:52 -05:00
|
|
|
debug!("print BREAK(%d) w/o newline in inconsistent",
|
|
|
|
b.blank_space);
|
2011-07-27 07:19:39 -05:00
|
|
|
self.indent(b.blank_space);
|
2012-01-13 02:32:05 -06:00
|
|
|
self.space -= b.blank_space;
|
2011-05-28 21:16:18 -05:00
|
|
|
}
|
2011-07-27 07:19:39 -05:00
|
|
|
}
|
2011-05-28 21:16:18 -05:00
|
|
|
}
|
2011-07-27 07:19:39 -05:00
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
STRING(s, len) => {
|
2012-08-22 19:24:52 -05:00
|
|
|
debug!("print STRING(%s)", *s);
|
2011-07-27 07:19:39 -05:00
|
|
|
assert (L == len);
|
|
|
|
// assert L <= space;
|
2012-01-13 02:32:05 -06:00
|
|
|
self.space -= len;
|
2012-08-02 19:17:07 -05:00
|
|
|
self.print_str(*s);
|
2011-07-27 07:19:39 -05:00
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
EOF => {
|
2011-07-27 07:19:39 -05:00
|
|
|
// EOF should never get here.
|
|
|
|
fail;
|
|
|
|
}
|
2011-05-28 21:16:18 -05:00
|
|
|
}
|
2011-03-24 10:33:20 -05:00
|
|
|
}
|
2011-03-04 00:22:43 -06:00
|
|
|
}
|
|
|
|
|
2011-05-28 21:16:18 -05:00
|
|
|
// Convenience functions to talk to the printer.
|
2011-07-27 07:19:39 -05:00
|
|
|
fn box(p: printer, indent: uint, b: breaks) {
|
|
|
|
p.pretty_print(BEGIN({offset: indent as int, breaks: b}));
|
2011-05-29 02:43:33 -05:00
|
|
|
}
|
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
fn ibox(p: printer, indent: uint) { box(p, indent, inconsistent); }
|
2011-05-29 02:43:33 -05:00
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
fn cbox(p: printer, indent: uint) { box(p, indent, consistent); }
|
2011-05-28 21:16:18 -05:00
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
fn break_offset(p: printer, n: uint, off: int) {
|
|
|
|
p.pretty_print(BREAK({offset: off, blank_space: n as int}));
|
2011-03-04 00:22:43 -06:00
|
|
|
}
|
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
fn end(p: printer) { p.pretty_print(END); }
|
2011-06-15 13:19:50 -05:00
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
fn eof(p: printer) { p.pretty_print(EOF); }
|
2011-06-15 13:19:50 -05:00
|
|
|
|
2012-07-14 00:57:48 -05:00
|
|
|
fn word(p: printer, wrd: ~str) {
|
2012-06-03 09:36:40 -05:00
|
|
|
p.pretty_print(STRING(@wrd, str::len(wrd) as int));
|
2011-05-30 16:10:54 -05:00
|
|
|
}
|
2011-06-15 13:19:50 -05:00
|
|
|
|
2012-07-14 00:57:48 -05:00
|
|
|
fn huge_word(p: printer, wrd: ~str) {
|
2012-06-03 09:36:40 -05:00
|
|
|
p.pretty_print(STRING(@wrd, size_infinity));
|
2011-06-20 12:07:29 -05:00
|
|
|
}
|
2011-06-15 13:19:50 -05:00
|
|
|
|
2012-07-14 00:57:48 -05:00
|
|
|
fn zero_word(p: printer, wrd: ~str) { p.pretty_print(STRING(@wrd, 0)); }
|
2011-06-15 13:19:50 -05:00
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
fn spaces(p: printer, n: uint) { break_offset(p, n, 0); }
|
2011-05-28 21:16:18 -05:00
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
fn zerobreak(p: printer) { spaces(p, 0u); }
|
2011-05-28 21:16:18 -05:00
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
fn space(p: printer) { spaces(p, 1u); }
|
2011-05-28 21:16:18 -05:00
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
fn hardbreak(p: printer) { spaces(p, size_infinity as uint); }
|
2011-06-19 21:55:28 -05:00
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
fn hardbreak_tok_offset(off: int) -> token {
|
2012-08-01 19:30:05 -05:00
|
|
|
return BREAK({offset: off, blank_space: size_infinity});
|
2011-07-26 17:37:05 -05:00
|
|
|
}
|
|
|
|
|
2012-08-01 19:30:05 -05:00
|
|
|
fn hardbreak_tok() -> token { return hardbreak_tok_offset(0); }
|
2011-06-19 21:55:28 -05:00
|
|
|
|
2011-07-26 17:37:05 -05:00
|
|
|
|
2011-05-28 21:16:18 -05:00
|
|
|
//
|
|
|
|
// Local Variables:
|
|
|
|
// mode: rust
|
|
|
|
// fill-column: 78;
|
|
|
|
// indent-tabs-mode: nil
|
|
|
|
// c-basic-offset: 4
|
|
|
|
// buffer-file-coding-system: utf-8-unix
|
|
|
|
// End:
|
|
|
|
//
|