2014-06-09 20:12:30 +00:00
|
|
|
//! This pretty-printer is a direct reimplementation of Philip Karlton's
|
|
|
|
//! Mesa pretty-printer, as described in appendix A of
|
|
|
|
//!
|
2019-02-08 13:53:55 +00:00
|
|
|
//! ```text
|
2015-11-03 16:34:11 +00:00
|
|
|
//! STAN-CS-79-770: "Pretty Printing", by Derek C. Oppen.
|
|
|
|
//! Stanford Department of Computer Science, 1979.
|
2019-02-08 13:53:55 +00:00
|
|
|
//! ```
|
2014-06-09 20:12:30 +00:00
|
|
|
//!
|
|
|
|
//! 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. 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
|
2015-01-17 23:33:05 +00:00
|
|
|
//! somewhat readily? In any case, I've used usize for indices-in-buffers and
|
2014-06-09 20:12:30 +00:00
|
|
|
//! 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.
|
|
|
|
//!
|
|
|
|
//! 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.
|
2017-02-05 08:44:49 +00:00
|
|
|
//!
|
|
|
|
//! # Explanation
|
|
|
|
//!
|
|
|
|
//! 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
|
2018-11-27 02:59:49 +00:00
|
|
|
//! apart (i.e., "not wrapped to ring-buffer boundaries"). The paper will switch
|
2017-02-05 08:44:49 +00:00
|
|
|
//! between using 'left' and 'right' terms to denote the wrapped-to-ring-buffer
|
|
|
|
//! and point-in-infinite-stream senses freely.
|
|
|
|
//!
|
2017-05-12 18:05:39 +00:00
|
|
|
//! There is a parallel ring buffer, `size`, that holds the calculated size of
|
2017-02-05 08:44:49 +00:00
|
|
|
//! each token. Why calculated? Because for Begin/End pairs, the "size"
|
|
|
|
//! includes everything between the pair. That is, the "size" of Begin is
|
|
|
|
//! actually the sum of the sizes of everything between Begin and the paired
|
2017-05-12 18:05:39 +00:00
|
|
|
//! End that follows. Since that is arbitrarily far in the future, `size` is
|
2017-02-05 08:44:49 +00:00
|
|
|
//! being rewritten regularly while the printer runs; in fact most of the
|
2017-05-12 18:05:39 +00:00
|
|
|
//! machinery is here to work out `size` entries on the fly (and give up when
|
2017-02-05 08:44:49 +00:00
|
|
|
//! 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
|
2017-05-12 18:05:39 +00:00
|
|
|
//! SCAN, which uses `scan_stack`, to manage calculating `size`. SCAN is, in
|
2017-02-05 08:44:49 +00:00
|
|
|
//! other words, the process of calculating 'size' entries.
|
|
|
|
//!
|
|
|
|
//! The "output side" of the printer is managed by an abstract process called
|
2017-05-12 18:05:39 +00:00
|
|
|
//! PRINT, which uses `print_stack`, `margin` and `space` to figure out what to
|
2017-02-05 08:44:49 +00:00
|
|
|
//! 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 takes 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.
|
|
|
|
//!
|
2018-11-29 02:58:58 +00:00
|
|
|
//! In this implementation (following the paper, again) the SCAN process is the
|
|
|
|
//! methods called `Printer::pretty_print_*`, and the 'PRINT' process is the
|
|
|
|
//! method called `Printer::print`.
|
2013-05-17 22:28:44 +00:00
|
|
|
|
2016-05-01 10:22:05 +00:00
|
|
|
use std::collections::VecDeque;
|
2016-05-01 10:01:12 +00:00
|
|
|
use std::fmt;
|
2018-11-29 00:36:58 +00:00
|
|
|
use std::borrow::Cow;
|
2019-02-06 17:33:01 +00:00
|
|
|
use log::debug;
|
2013-05-25 02:35:29 +00:00
|
|
|
|
2017-02-05 08:44:49 +00:00
|
|
|
/// How to break. Described in more detail in the module docs.
|
2015-01-04 03:54:18 +00:00
|
|
|
#[derive(Clone, Copy, PartialEq)]
|
2014-01-09 13:05:33 +00:00
|
|
|
pub enum Breaks {
|
|
|
|
Consistent,
|
|
|
|
Inconsistent,
|
2013-07-02 19:47:32 +00:00
|
|
|
}
|
2011-06-15 18:19:50 +00:00
|
|
|
|
2015-01-04 03:54:18 +00:00
|
|
|
#[derive(Clone, Copy)]
|
2014-01-09 13:05:33 +00:00
|
|
|
pub struct BreakToken {
|
2015-01-18 00:18:19 +00:00
|
|
|
offset: isize,
|
|
|
|
blank_space: isize
|
2013-02-21 08:16:31 +00:00
|
|
|
}
|
2011-03-04 06:22:43 +00:00
|
|
|
|
2015-01-04 03:54:18 +00:00
|
|
|
#[derive(Clone, Copy)]
|
2014-01-09 13:05:33 +00:00
|
|
|
pub struct BeginToken {
|
2015-01-18 00:18:19 +00:00
|
|
|
offset: isize,
|
2014-01-09 13:05:33 +00:00
|
|
|
breaks: Breaks
|
2013-02-21 08:16:31 +00:00
|
|
|
}
|
2011-03-04 06:22:43 +00:00
|
|
|
|
2015-01-04 03:54:18 +00:00
|
|
|
#[derive(Clone)]
|
2014-01-09 13:05:33 +00:00
|
|
|
pub enum Token {
|
2018-11-29 00:36:58 +00:00
|
|
|
// In practice a string token contains either a `&'static str` or a
|
|
|
|
// `String`. `Cow` is overkill for this because we never modify the data,
|
|
|
|
// but it's more convenient than rolling our own more specialized type.
|
|
|
|
String(Cow<'static, str>, isize),
|
2014-01-09 13:05:33 +00:00
|
|
|
Break(BreakToken),
|
|
|
|
Begin(BeginToken),
|
|
|
|
End,
|
|
|
|
Eof,
|
2013-01-29 22:41:40 +00:00
|
|
|
}
|
2011-03-24 15:33:20 +00:00
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
impl Token {
|
2019-06-24 16:12:56 +00:00
|
|
|
crate fn is_eof(&self) -> bool {
|
2015-01-14 05:12:39 +00:00
|
|
|
match *self {
|
|
|
|
Token::Eof => true,
|
|
|
|
_ => false,
|
|
|
|
}
|
2012-08-27 23:26:35 +00:00
|
|
|
}
|
2013-05-31 22:17:22 +00:00
|
|
|
|
|
|
|
pub fn is_hardbreak_tok(&self) -> bool {
|
2013-02-04 22:02:01 +00:00
|
|
|
match *self {
|
2015-01-14 05:12:39 +00:00
|
|
|
Token::Break(BreakToken {
|
2013-02-21 08:16:31 +00:00
|
|
|
offset: 0,
|
|
|
|
blank_space: bs
|
2014-01-09 13:05:33 +00:00
|
|
|
}) if bs == SIZE_INFINITY =>
|
2012-08-27 23:26:35 +00:00
|
|
|
true,
|
|
|
|
_ =>
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-01 10:01:12 +00:00
|
|
|
impl fmt::Display for Token {
|
2019-02-06 17:33:01 +00:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2016-05-01 10:01:12 +00:00
|
|
|
match *self {
|
|
|
|
Token::String(ref s, len) => write!(f, "STR({},{})", s, len),
|
|
|
|
Token::Break(_) => f.write_str("BREAK"),
|
|
|
|
Token::Begin(_) => f.write_str("BEGIN"),
|
|
|
|
Token::End => f.write_str("END"),
|
|
|
|
Token::Eof => f.write_str("EOF"),
|
|
|
|
}
|
2011-05-29 02:16:18 +00:00
|
|
|
}
|
2011-03-04 06:22:43 +00:00
|
|
|
}
|
|
|
|
|
2016-10-07 03:46:47 +00:00
|
|
|
fn buf_str(buf: &[BufEntry], left: usize, right: usize, lim: usize) -> String {
|
|
|
|
let n = buf.len();
|
2012-03-15 13:47:03 +00:00
|
|
|
let mut i = left;
|
2014-02-15 21:15:03 +00:00
|
|
|
let mut l = lim;
|
2016-05-01 10:01:12 +00:00
|
|
|
let mut s = String::from("[");
|
2015-01-28 01:01:48 +00:00
|
|
|
while i != right && l != 0 {
|
|
|
|
l -= 1;
|
2013-06-12 02:13:42 +00:00
|
|
|
if i != left {
|
|
|
|
s.push_str(", ");
|
|
|
|
}
|
2016-10-07 03:46:47 +00:00
|
|
|
s.push_str(&format!("{}={}", buf[i].size, &buf[i].token));
|
2015-01-28 01:01:48 +00:00
|
|
|
i += 1;
|
2011-05-29 02:16:18 +00:00
|
|
|
i %= n;
|
|
|
|
}
|
2014-10-15 06:05:01 +00:00
|
|
|
s.push(']');
|
2014-12-11 03:46:38 +00:00
|
|
|
s
|
2011-03-04 06:22:43 +00:00
|
|
|
}
|
|
|
|
|
2015-03-30 13:38:59 +00:00
|
|
|
#[derive(Copy, Clone)]
|
2019-06-24 16:12:56 +00:00
|
|
|
crate enum PrintStackBreak {
|
2014-01-09 13:05:33 +00:00
|
|
|
Fits,
|
|
|
|
Broken(Breaks),
|
|
|
|
}
|
2011-06-15 18:19:50 +00:00
|
|
|
|
2015-03-30 13:38:59 +00:00
|
|
|
#[derive(Copy, Clone)]
|
2019-06-24 16:12:56 +00:00
|
|
|
crate struct PrintStackElem {
|
2015-01-18 00:18:19 +00:00
|
|
|
offset: isize,
|
2014-01-09 13:05:33 +00:00
|
|
|
pbreak: PrintStackBreak
|
2013-02-21 08:16:31 +00:00
|
|
|
}
|
2011-03-04 06:22:43 +00:00
|
|
|
|
2015-02-27 14:36:53 +00:00
|
|
|
const SIZE_INFINITY: isize = 0xffff;
|
2011-03-09 10:41:50 +00:00
|
|
|
|
2019-07-05 23:10:18 +00:00
|
|
|
pub fn mk_printer() -> Printer {
|
2019-06-24 16:42:21 +00:00
|
|
|
let linewidth = 78;
|
2016-10-07 03:46:47 +00:00
|
|
|
// Yes 55, it makes the ring buffers big enough to never fall behind.
|
2016-05-28 15:29:59 +00:00
|
|
|
let n: usize = 55 * linewidth;
|
2013-10-21 20:08:31 +00:00
|
|
|
debug!("mk_printer {}", linewidth);
|
2013-12-27 22:11:01 +00:00
|
|
|
Printer {
|
2019-07-05 23:10:18 +00:00
|
|
|
out: String::new(),
|
2018-04-27 21:33:34 +00:00
|
|
|
buf_max_len: n,
|
2015-01-18 00:18:19 +00:00
|
|
|
margin: linewidth as isize,
|
|
|
|
space: linewidth as isize,
|
2013-02-04 22:02:01 +00:00
|
|
|
left: 0,
|
|
|
|
right: 0,
|
2018-04-27 21:33:34 +00:00
|
|
|
// Initialize a single entry; advance_right() will extend it on demand
|
|
|
|
// up to `buf_max_len` elements.
|
|
|
|
buf: vec![BufEntry::default()],
|
2013-02-04 22:02:01 +00:00
|
|
|
left_total: 0,
|
|
|
|
right_total: 0,
|
2016-10-07 03:46:47 +00:00
|
|
|
scan_stack: VecDeque::new(),
|
2014-02-28 21:09:09 +00:00
|
|
|
print_stack: Vec::new(),
|
2013-02-04 22:02:01 +00:00
|
|
|
pending_indentation: 0
|
|
|
|
}
|
2011-03-24 15:33:20 +00:00
|
|
|
}
|
|
|
|
|
2019-07-05 23:10:18 +00:00
|
|
|
pub struct Printer {
|
|
|
|
out: String,
|
2018-04-27 21:33:34 +00:00
|
|
|
buf_max_len: usize,
|
2014-06-09 20:12:30 +00:00
|
|
|
/// Width of lines we're constrained to
|
2015-01-18 00:18:19 +00:00
|
|
|
margin: isize,
|
2014-06-09 20:12:30 +00:00
|
|
|
/// Number of spaces left on line
|
2015-01-18 00:18:19 +00:00
|
|
|
space: isize,
|
2014-06-09 20:12:30 +00:00
|
|
|
/// Index of left side of input stream
|
2015-01-17 23:33:05 +00:00
|
|
|
left: usize,
|
2014-06-09 20:12:30 +00:00
|
|
|
/// Index of right side of input stream
|
2015-01-17 23:33:05 +00:00
|
|
|
right: usize,
|
2016-10-07 03:46:47 +00:00
|
|
|
/// Ring-buffer of tokens and calculated sizes
|
|
|
|
buf: Vec<BufEntry>,
|
2014-06-09 20:12:30 +00:00
|
|
|
/// Running size of stream "...left"
|
2015-01-18 00:18:19 +00:00
|
|
|
left_total: isize,
|
2014-06-09 20:12:30 +00:00
|
|
|
/// Running size of stream "...right"
|
2015-01-18 00:18:19 +00:00
|
|
|
right_total: isize,
|
2014-06-09 20:12:30 +00: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.
|
2016-10-07 03:46:47 +00:00
|
|
|
scan_stack: VecDeque<usize>,
|
2014-06-09 20:12:30 +00:00
|
|
|
/// Stack of blocks-in-progress being flushed by print
|
2014-02-28 21:09:09 +00:00
|
|
|
print_stack: Vec<PrintStackElem> ,
|
2014-06-09 20:12:30 +00:00
|
|
|
/// Buffered indentation to avoid writing trailing whitespace
|
2015-01-18 00:18:19 +00:00
|
|
|
pending_indentation: isize,
|
2012-07-11 22:00:40 +00:00
|
|
|
}
|
|
|
|
|
2016-10-07 03:46:47 +00:00
|
|
|
#[derive(Clone)]
|
|
|
|
struct BufEntry {
|
|
|
|
token: Token,
|
|
|
|
size: isize,
|
|
|
|
}
|
|
|
|
|
2018-04-27 21:33:34 +00:00
|
|
|
impl Default for BufEntry {
|
|
|
|
fn default() -> Self {
|
|
|
|
BufEntry { token: Token::Eof, size: 0 }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-05 23:10:18 +00:00
|
|
|
impl Printer {
|
2014-02-01 00:14:42 +00:00
|
|
|
pub fn last_token(&mut self) -> Token {
|
2016-10-07 03:46:47 +00:00
|
|
|
self.buf[self.right].token.clone()
|
2014-02-01 00:14:42 +00:00
|
|
|
}
|
2018-11-28 05:46:43 +00:00
|
|
|
|
|
|
|
/// Be very careful with this!
|
2014-01-09 13:05:33 +00:00
|
|
|
pub fn replace_last_token(&mut self, t: Token) {
|
2016-10-07 03:46:47 +00:00
|
|
|
self.buf[self.right].token = t;
|
2013-05-31 22:17:22 +00:00
|
|
|
}
|
2018-11-28 05:46:43 +00:00
|
|
|
|
2019-06-24 18:15:11 +00:00
|
|
|
fn pretty_print_eof(&mut self) {
|
2018-11-29 02:58:58 +00:00
|
|
|
if !self.scan_stack.is_empty() {
|
|
|
|
self.check_stack(0);
|
2019-06-24 18:15:11 +00:00
|
|
|
self.advance_left();
|
2018-11-29 02:58:58 +00:00
|
|
|
}
|
|
|
|
self.indent(0);
|
|
|
|
}
|
|
|
|
|
2019-06-24 18:15:11 +00:00
|
|
|
fn pretty_print_begin(&mut self, b: BeginToken) {
|
2018-11-29 02:58:58 +00:00
|
|
|
if self.scan_stack.is_empty() {
|
|
|
|
self.left_total = 1;
|
|
|
|
self.right_total = 1;
|
|
|
|
self.left = 0;
|
|
|
|
self.right = 0;
|
|
|
|
} else {
|
|
|
|
self.advance_right();
|
|
|
|
}
|
|
|
|
debug!("pp Begin({})/buffer Vec<{},{}>",
|
|
|
|
b.offset, self.left, self.right);
|
|
|
|
self.buf[self.right] = BufEntry { token: Token::Begin(b), size: -self.right_total };
|
|
|
|
let right = self.right;
|
|
|
|
self.scan_push(right);
|
|
|
|
}
|
|
|
|
|
2019-06-24 18:15:11 +00:00
|
|
|
fn pretty_print_end(&mut self) {
|
2018-11-29 02:58:58 +00:00
|
|
|
if self.scan_stack.is_empty() {
|
|
|
|
debug!("pp End/print Vec<{},{}>", self.left, self.right);
|
2019-06-24 18:15:11 +00:00
|
|
|
self.print_end();
|
2018-11-29 02:58:58 +00:00
|
|
|
} else {
|
|
|
|
debug!("pp End/buffer Vec<{},{}>", self.left, self.right);
|
|
|
|
self.advance_right();
|
|
|
|
self.buf[self.right] = BufEntry { token: Token::End, size: -1 };
|
|
|
|
let right = self.right;
|
|
|
|
self.scan_push(right);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-24 18:15:11 +00:00
|
|
|
fn pretty_print_break(&mut self, b: BreakToken) {
|
2018-11-29 02:58:58 +00:00
|
|
|
if self.scan_stack.is_empty() {
|
|
|
|
self.left_total = 1;
|
|
|
|
self.right_total = 1;
|
|
|
|
self.left = 0;
|
|
|
|
self.right = 0;
|
|
|
|
} else {
|
|
|
|
self.advance_right();
|
|
|
|
}
|
|
|
|
debug!("pp Break({})/buffer Vec<{},{}>",
|
|
|
|
b.offset, self.left, self.right);
|
|
|
|
self.check_stack(0);
|
|
|
|
let right = self.right;
|
|
|
|
self.scan_push(right);
|
|
|
|
self.buf[self.right] = BufEntry { token: Token::Break(b), size: -self.right_total };
|
|
|
|
self.right_total += b.blank_space;
|
|
|
|
}
|
|
|
|
|
2019-06-24 18:15:11 +00:00
|
|
|
fn pretty_print_string(&mut self, s: Cow<'static, str>, len: isize) {
|
2018-11-29 02:58:58 +00:00
|
|
|
if self.scan_stack.is_empty() {
|
|
|
|
debug!("pp String('{}')/print Vec<{},{}>",
|
|
|
|
s, self.left, self.right);
|
2019-06-24 18:15:11 +00:00
|
|
|
self.print_string(s, len);
|
2018-11-29 02:58:58 +00:00
|
|
|
} else {
|
|
|
|
debug!("pp String('{}')/buffer Vec<{},{}>",
|
|
|
|
s, self.left, self.right);
|
|
|
|
self.advance_right();
|
|
|
|
self.buf[self.right] = BufEntry { token: Token::String(s, len), size: len };
|
|
|
|
self.right_total += len;
|
2019-06-24 18:15:11 +00:00
|
|
|
self.check_stream();
|
2011-05-29 02:16:18 +00:00
|
|
|
}
|
2011-03-04 06:22:43 +00:00
|
|
|
}
|
2018-11-28 05:46:43 +00:00
|
|
|
|
2019-06-24 18:15:11 +00:00
|
|
|
crate fn check_stream(&mut self) {
|
2015-05-02 20:25:49 +00:00
|
|
|
debug!("check_stream Vec<{}, {}> with left_total={}, right_total={}",
|
2012-08-23 00:24:52 +00:00
|
|
|
self.left, self.right, self.left_total, self.right_total);
|
2012-01-13 08:32:05 +00:00
|
|
|
if self.right_total - self.left_total > self.space {
|
2013-10-21 20:08:31 +00:00
|
|
|
debug!("scan window is {}, longer than space on line ({})",
|
2012-08-23 00:24:52 +00:00
|
|
|
self.right_total - self.left_total, self.space);
|
2016-05-01 10:22:05 +00:00
|
|
|
if Some(&self.left) == self.scan_stack.back() {
|
|
|
|
debug!("setting {} to infinity and popping", self.left);
|
|
|
|
let scanned = self.scan_pop_bottom();
|
2016-10-07 03:46:47 +00:00
|
|
|
self.buf[scanned].size = SIZE_INFINITY;
|
2011-05-29 02:16:18 +00:00
|
|
|
}
|
2019-06-24 18:15:11 +00:00
|
|
|
self.advance_left();
|
2014-02-01 19:24:42 +00:00
|
|
|
if self.left != self.right {
|
2019-06-24 18:15:11 +00:00
|
|
|
self.check_stream();
|
2014-02-01 19:24:42 +00:00
|
|
|
}
|
2011-05-29 02:16:18 +00:00
|
|
|
}
|
2011-03-24 15:33:20 +00:00
|
|
|
}
|
2018-11-28 05:46:43 +00:00
|
|
|
|
2019-06-24 16:12:56 +00:00
|
|
|
crate fn scan_push(&mut self, x: usize) {
|
2013-10-21 20:08:31 +00:00
|
|
|
debug!("scan_push {}", x);
|
2016-05-01 10:22:05 +00:00
|
|
|
self.scan_stack.push_front(x);
|
2011-03-04 06:22:43 +00:00
|
|
|
}
|
2018-11-28 05:46:43 +00:00
|
|
|
|
2019-06-24 16:12:56 +00:00
|
|
|
crate fn scan_pop(&mut self) -> usize {
|
2016-05-01 10:22:05 +00:00
|
|
|
self.scan_stack.pop_front().unwrap()
|
2011-05-29 02:16:18 +00:00
|
|
|
}
|
2018-11-28 05:46:43 +00:00
|
|
|
|
2019-06-24 16:12:56 +00:00
|
|
|
crate fn scan_top(&mut self) -> usize {
|
2016-05-01 10:22:05 +00:00
|
|
|
*self.scan_stack.front().unwrap()
|
2012-01-13 08:32:05 +00:00
|
|
|
}
|
2018-11-28 05:46:43 +00:00
|
|
|
|
2019-06-24 16:12:56 +00:00
|
|
|
crate fn scan_pop_bottom(&mut self) -> usize {
|
2016-05-01 10:22:05 +00:00
|
|
|
self.scan_stack.pop_back().unwrap()
|
2011-05-29 02:16:18 +00:00
|
|
|
}
|
2018-11-28 05:46:43 +00:00
|
|
|
|
2019-06-24 16:12:56 +00:00
|
|
|
crate fn advance_right(&mut self) {
|
2015-01-28 01:01:48 +00:00
|
|
|
self.right += 1;
|
2018-04-27 21:33:34 +00:00
|
|
|
self.right %= self.buf_max_len;
|
|
|
|
// Extend the buf if necessary.
|
|
|
|
if self.right == self.buf.len() {
|
|
|
|
self.buf.push(BufEntry::default());
|
|
|
|
}
|
2017-05-12 18:05:39 +00:00
|
|
|
assert_ne!(self.right, self.left);
|
2011-03-04 06:22:43 +00:00
|
|
|
}
|
2018-11-28 05:46:43 +00:00
|
|
|
|
2019-06-24 18:15:11 +00:00
|
|
|
crate fn advance_left(&mut self) {
|
2015-05-02 20:25:49 +00:00
|
|
|
debug!("advance_left Vec<{},{}>, sizeof({})={}", self.left, self.right,
|
2016-10-07 03:46:47 +00:00
|
|
|
self.left, self.buf[self.left].size);
|
2015-01-14 05:14:56 +00:00
|
|
|
|
2016-10-07 03:46:47 +00:00
|
|
|
let mut left_size = self.buf[self.left].size;
|
2015-01-14 05:14:56 +00:00
|
|
|
|
|
|
|
while left_size >= 0 {
|
2016-10-07 03:46:47 +00:00
|
|
|
let left = self.buf[self.left].token.clone();
|
2015-01-14 05:14:56 +00:00
|
|
|
|
|
|
|
let len = match left {
|
|
|
|
Token::Break(b) => b.blank_space,
|
|
|
|
Token::String(_, len) => {
|
|
|
|
assert_eq!(len, left_size);
|
|
|
|
len
|
|
|
|
}
|
|
|
|
_ => 0
|
|
|
|
};
|
|
|
|
|
2019-06-24 18:15:11 +00:00
|
|
|
self.print(left, left_size);
|
2015-01-14 05:14:56 +00:00
|
|
|
|
|
|
|
self.left_total += len;
|
|
|
|
|
|
|
|
if self.left == self.right {
|
|
|
|
break;
|
2011-05-29 02:16:18 +00:00
|
|
|
}
|
2015-01-14 05:14:56 +00:00
|
|
|
|
2015-01-28 01:01:48 +00:00
|
|
|
self.left += 1;
|
2018-04-27 21:33:34 +00:00
|
|
|
self.left %= self.buf_max_len;
|
2015-01-14 05:14:56 +00:00
|
|
|
|
2016-10-07 03:46:47 +00:00
|
|
|
left_size = self.buf[self.left].size;
|
2011-05-29 02:16:18 +00:00
|
|
|
}
|
2011-03-24 15:33:20 +00:00
|
|
|
}
|
2018-11-28 05:46:43 +00:00
|
|
|
|
2019-06-24 16:12:56 +00:00
|
|
|
crate fn check_stack(&mut self, k: isize) {
|
2016-05-01 10:22:05 +00:00
|
|
|
if !self.scan_stack.is_empty() {
|
2011-07-27 12:19:39 +00:00
|
|
|
let x = self.scan_top();
|
2016-10-07 03:46:47 +00:00
|
|
|
match self.buf[x].token {
|
2015-01-14 05:12:39 +00:00
|
|
|
Token::Begin(_) => {
|
2014-10-15 06:05:01 +00:00
|
|
|
if k > 0 {
|
|
|
|
let popped = self.scan_pop();
|
2016-10-07 03:46:47 +00:00
|
|
|
self.buf[popped].size = self.buf[x].size + self.right_total;
|
2014-10-15 06:05:01 +00:00
|
|
|
self.check_stack(k - 1);
|
|
|
|
}
|
|
|
|
}
|
2015-01-14 05:12:39 +00:00
|
|
|
Token::End => {
|
2014-10-15 06:05:01 +00:00
|
|
|
// paper says + not =, but that makes no sense.
|
2014-02-28 20:54:01 +00:00
|
|
|
let popped = self.scan_pop();
|
2016-10-07 03:46:47 +00:00
|
|
|
self.buf[popped].size = 1;
|
2014-10-15 06:05:01 +00:00
|
|
|
self.check_stack(k + 1);
|
2011-05-29 02:16:18 +00:00
|
|
|
}
|
2014-10-15 06:05:01 +00:00
|
|
|
_ => {
|
|
|
|
let popped = self.scan_pop();
|
2016-10-07 03:46:47 +00:00
|
|
|
self.buf[popped].size = self.buf[x].size + self.right_total;
|
2014-10-15 06:05:01 +00:00
|
|
|
if k > 0 {
|
|
|
|
self.check_stack(k);
|
|
|
|
}
|
2014-02-28 20:54:01 +00:00
|
|
|
}
|
2011-05-29 02:16:18 +00:00
|
|
|
}
|
|
|
|
}
|
2011-03-04 06:22:43 +00:00
|
|
|
}
|
2018-11-28 05:46:43 +00:00
|
|
|
|
2019-06-24 18:15:11 +00:00
|
|
|
crate fn print_newline(&mut self, amount: isize) {
|
2013-10-21 20:08:31 +00:00
|
|
|
debug!("NEWLINE {}", amount);
|
2019-06-24 16:42:21 +00:00
|
|
|
self.out.push('\n');
|
2012-01-13 08:32:05 +00:00
|
|
|
self.pending_indentation = 0;
|
2011-05-29 02:16:18 +00:00
|
|
|
self.indent(amount);
|
2011-03-04 06:22:43 +00:00
|
|
|
}
|
2018-11-28 05:46:43 +00:00
|
|
|
|
2019-06-24 16:12:56 +00:00
|
|
|
crate fn indent(&mut self, amount: isize) {
|
2013-10-21 20:08:31 +00:00
|
|
|
debug!("INDENT {}", amount);
|
2012-01-13 08:32:05 +00:00
|
|
|
self.pending_indentation += amount;
|
2011-03-04 06:22:43 +00:00
|
|
|
}
|
2018-11-28 05:46:43 +00:00
|
|
|
|
2019-06-24 16:12:56 +00:00
|
|
|
crate fn get_top(&mut self) -> PrintStackElem {
|
2016-05-01 10:01:12 +00:00
|
|
|
match self.print_stack.last() {
|
|
|
|
Some(el) => *el,
|
|
|
|
None => PrintStackElem {
|
2013-02-21 08:16:31 +00:00
|
|
|
offset: 0,
|
2015-01-14 05:12:39 +00:00
|
|
|
pbreak: PrintStackBreak::Broken(Breaks::Inconsistent)
|
2013-02-21 08:16:31 +00:00
|
|
|
}
|
2012-05-10 14:24:56 +00:00
|
|
|
}
|
2011-06-01 17:54:11 +00:00
|
|
|
}
|
2018-11-28 05:46:43 +00:00
|
|
|
|
2019-06-24 18:15:11 +00:00
|
|
|
crate fn print_begin(&mut self, b: BeginToken, l: isize) {
|
2018-11-29 02:58:58 +00:00
|
|
|
if l > self.space {
|
|
|
|
let col = self.margin - self.space + b.offset;
|
|
|
|
debug!("print Begin -> push broken block at col {}", col);
|
|
|
|
self.print_stack.push(PrintStackElem {
|
|
|
|
offset: col,
|
|
|
|
pbreak: PrintStackBreak::Broken(b.breaks)
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
debug!("print Begin -> push fitting block");
|
|
|
|
self.print_stack.push(PrintStackElem {
|
|
|
|
offset: 0,
|
|
|
|
pbreak: PrintStackBreak::Fits
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-24 18:15:11 +00:00
|
|
|
crate fn print_end(&mut self) {
|
2018-11-29 02:58:58 +00:00
|
|
|
debug!("print End -> pop End");
|
|
|
|
let print_stack = &mut self.print_stack;
|
|
|
|
assert!(!print_stack.is_empty());
|
|
|
|
print_stack.pop().unwrap();
|
|
|
|
}
|
|
|
|
|
2019-06-24 18:15:11 +00:00
|
|
|
crate fn print_break(&mut self, b: BreakToken, l: isize) {
|
2018-11-29 02:58:58 +00:00
|
|
|
let top = self.get_top();
|
|
|
|
match top.pbreak {
|
|
|
|
PrintStackBreak::Fits => {
|
|
|
|
debug!("print Break({}) in fitting block", b.blank_space);
|
|
|
|
self.space -= b.blank_space;
|
|
|
|
self.indent(b.blank_space);
|
|
|
|
}
|
|
|
|
PrintStackBreak::Broken(Breaks::Consistent) => {
|
|
|
|
debug!("print Break({}+{}) in consistent block",
|
|
|
|
top.offset, b.offset);
|
2019-06-24 18:15:11 +00:00
|
|
|
self.print_newline(top.offset + b.offset);
|
2018-11-29 02:58:58 +00:00
|
|
|
self.space = self.margin - (top.offset + b.offset);
|
|
|
|
}
|
|
|
|
PrintStackBreak::Broken(Breaks::Inconsistent) => {
|
|
|
|
if l > self.space {
|
|
|
|
debug!("print Break({}+{}) w/ newline in inconsistent",
|
|
|
|
top.offset, b.offset);
|
2019-06-24 18:15:11 +00:00
|
|
|
self.print_newline(top.offset + b.offset);
|
2018-11-29 02:58:58 +00:00
|
|
|
self.space = self.margin - (top.offset + b.offset);
|
|
|
|
} else {
|
|
|
|
debug!("print Break({}) w/o newline in inconsistent",
|
|
|
|
b.blank_space);
|
|
|
|
self.indent(b.blank_space);
|
|
|
|
self.space -= b.blank_space;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-24 18:15:11 +00:00
|
|
|
crate fn print_string(&mut self, s: Cow<'static, str>, len: isize) {
|
2018-11-29 02:58:58 +00:00
|
|
|
debug!("print String({})", s);
|
|
|
|
// assert!(len <= space);
|
|
|
|
self.space -= len;
|
2019-03-28 21:32:13 +00:00
|
|
|
|
|
|
|
// Write the pending indent. A more concise way of doing this would be:
|
|
|
|
//
|
|
|
|
// write!(self.out, "{: >n$}", "", n = self.pending_indentation as usize)?;
|
|
|
|
//
|
2019-06-24 16:42:21 +00:00
|
|
|
// But that is significantly slower. This code is sufficiently hot, and indents can get
|
|
|
|
// sufficiently large, that the difference is significant on some workloads.
|
|
|
|
self.out.reserve(self.pending_indentation as usize);
|
|
|
|
self.out.extend(std::iter::repeat(' ').take(self.pending_indentation as usize));
|
|
|
|
self.pending_indentation = 0;
|
|
|
|
self.out.push_str(&s);
|
2011-06-01 19:01:42 +00:00
|
|
|
}
|
2018-11-28 05:46:43 +00:00
|
|
|
|
2019-06-24 18:15:11 +00:00
|
|
|
crate fn print(&mut self, token: Token, l: isize) {
|
2016-05-01 10:01:12 +00:00
|
|
|
debug!("print {} {} (remaining line space={})", token, l,
|
2012-08-23 00:24:52 +00:00
|
|
|
self.space);
|
2016-10-07 03:46:47 +00:00
|
|
|
debug!("{}", buf_str(&self.buf,
|
2013-07-02 19:47:32 +00:00
|
|
|
self.left,
|
|
|
|
self.right,
|
|
|
|
6));
|
2015-01-14 05:12:39 +00:00
|
|
|
match token {
|
2018-11-29 02:58:58 +00:00
|
|
|
Token::Begin(b) => self.print_begin(b, l),
|
|
|
|
Token::End => self.print_end(),
|
|
|
|
Token::Break(b) => self.print_break(b, l),
|
|
|
|
Token::String(s, len) => {
|
|
|
|
assert_eq!(len, l);
|
2019-06-24 18:15:11 +00:00
|
|
|
self.print_string(s, len);
|
2011-05-29 02:16:18 +00:00
|
|
|
}
|
2018-11-29 02:58:58 +00:00
|
|
|
Token::Eof => panic!(), // Eof should never get here.
|
2011-05-29 02:16:18 +00:00
|
|
|
}
|
2011-03-24 15:33:20 +00:00
|
|
|
}
|
2011-03-04 06:22:43 +00:00
|
|
|
|
2017-06-25 03:22:42 +00:00
|
|
|
// Convenience functions to talk to the printer.
|
2017-02-05 08:44:49 +00:00
|
|
|
|
2017-06-25 03:22:42 +00:00
|
|
|
/// "raw box"
|
2019-06-24 18:15:11 +00:00
|
|
|
crate fn rbox(&mut self, indent: usize, b: Breaks) {
|
2018-11-29 02:58:58 +00:00
|
|
|
self.pretty_print_begin(BeginToken {
|
2017-06-25 03:22:42 +00:00
|
|
|
offset: indent as isize,
|
|
|
|
breaks: b
|
2018-11-29 02:58:58 +00:00
|
|
|
})
|
2017-06-25 03:22:42 +00:00
|
|
|
}
|
2011-05-29 07:43:33 +00:00
|
|
|
|
2017-06-25 03:22:42 +00:00
|
|
|
/// Inconsistent breaking box
|
2019-06-24 18:15:11 +00:00
|
|
|
crate fn ibox(&mut self, indent: usize) {
|
2017-06-25 03:22:42 +00:00
|
|
|
self.rbox(indent, Breaks::Inconsistent)
|
|
|
|
}
|
2011-05-29 07:43:33 +00:00
|
|
|
|
2017-06-25 03:22:42 +00:00
|
|
|
/// Consistent breaking box
|
2019-06-24 18:15:11 +00:00
|
|
|
pub fn cbox(&mut self, indent: usize) {
|
2017-06-25 03:22:42 +00:00
|
|
|
self.rbox(indent, Breaks::Consistent)
|
|
|
|
}
|
2011-05-29 02:16:18 +00:00
|
|
|
|
2019-06-24 18:15:11 +00:00
|
|
|
pub fn break_offset(&mut self, n: usize, off: isize) {
|
2018-11-29 02:58:58 +00:00
|
|
|
self.pretty_print_break(BreakToken {
|
2017-06-25 03:22:42 +00:00
|
|
|
offset: off,
|
|
|
|
blank_space: n as isize
|
2018-11-29 02:58:58 +00:00
|
|
|
})
|
2017-06-25 03:22:42 +00:00
|
|
|
}
|
2011-03-04 06:22:43 +00:00
|
|
|
|
2019-06-24 18:15:11 +00:00
|
|
|
crate fn end(&mut self) {
|
2018-11-29 02:58:58 +00:00
|
|
|
self.pretty_print_end()
|
2017-06-25 03:22:42 +00:00
|
|
|
}
|
2011-06-15 18:19:50 +00:00
|
|
|
|
2019-07-05 23:10:18 +00:00
|
|
|
pub fn eof(mut self) -> String {
|
|
|
|
self.pretty_print_eof();
|
|
|
|
self.out
|
2017-06-25 03:22:42 +00:00
|
|
|
}
|
2011-06-15 18:19:50 +00:00
|
|
|
|
2019-06-24 18:15:11 +00:00
|
|
|
pub fn word<S: Into<Cow<'static, str>>>(&mut self, wrd: S) {
|
2018-11-29 00:36:58 +00:00
|
|
|
let s = wrd.into();
|
|
|
|
let len = s.len() as isize;
|
2018-11-29 02:58:58 +00:00
|
|
|
self.pretty_print_string(s, len)
|
2017-06-25 03:22:42 +00:00
|
|
|
}
|
2011-06-15 18:19:50 +00:00
|
|
|
|
2019-06-24 18:15:11 +00:00
|
|
|
fn spaces(&mut self, n: usize) {
|
2017-06-25 03:22:42 +00:00
|
|
|
self.break_offset(n, 0)
|
|
|
|
}
|
2011-05-29 02:16:18 +00:00
|
|
|
|
2019-06-24 18:15:11 +00:00
|
|
|
crate fn zerobreak(&mut self) {
|
2017-06-25 03:22:42 +00:00
|
|
|
self.spaces(0)
|
|
|
|
}
|
2011-05-29 02:16:18 +00:00
|
|
|
|
2019-06-24 18:15:11 +00:00
|
|
|
pub fn space(&mut self) {
|
2017-06-25 03:22:42 +00:00
|
|
|
self.spaces(1)
|
|
|
|
}
|
2011-05-29 02:16:18 +00:00
|
|
|
|
2019-06-24 18:15:11 +00:00
|
|
|
pub fn hardbreak(&mut self) {
|
2017-06-25 03:22:42 +00:00
|
|
|
self.spaces(SIZE_INFINITY as usize)
|
|
|
|
}
|
2011-06-20 02:55:28 +00:00
|
|
|
|
2017-06-25 03:22:42 +00:00
|
|
|
pub fn hardbreak_tok_offset(off: isize) -> Token {
|
|
|
|
Token::Break(BreakToken {offset: off, blank_space: SIZE_INFINITY})
|
|
|
|
}
|
2015-01-14 05:12:39 +00:00
|
|
|
}
|