]>
Commit | Line | Data |
---|---|---|
223e47cc LB |
1 | // Copyright 2012 The Rust Project Developers. See the COPYRIGHT |
2 | // file at the top-level directory of this distribution and at | |
3 | // http://rust-lang.org/COPYRIGHT. | |
4 | // | |
5 | // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | |
6 | // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | |
7 | // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | |
8 | // option. This file may not be copied, modified, or distributed | |
9 | // except according to those terms. | |
10 | ||
1a4d82fc JJ |
11 | //! This pretty-printer is a direct reimplementation of Philip Karlton's |
12 | //! Mesa pretty-printer, as described in appendix A of | |
13 | //! | |
92a42be0 SL |
14 | //! ````ignore |
15 | //! STAN-CS-79-770: "Pretty Printing", by Derek C. Oppen. | |
16 | //! Stanford Department of Computer Science, 1979. | |
17 | //! ```` | |
1a4d82fc JJ |
18 | //! |
19 | //! The algorithm's aim is to break a stream into as few lines as possible | |
20 | //! while respecting the indentation-consistency requirements of the enclosing | |
21 | //! block, and avoiding breaking at silly places on block boundaries, for | |
22 | //! example, between "x" and ")" in "x)". | |
23 | //! | |
24 | //! I am implementing this algorithm because it comes with 20 pages of | |
25 | //! documentation explaining its theory, and because it addresses the set of | |
26 | //! concerns I've seen other pretty-printers fall down on. Weirdly. Even though | |
27 | //! it's 32 years old. What can I say? | |
28 | //! | |
29 | //! Despite some redundancies and quirks in the way it's implemented in that | |
30 | //! paper, I've opted to keep the implementation here as similar as I can, | |
31 | //! changing only what was blatantly wrong, a typo, or sufficiently | |
32 | //! non-idiomatic rust that it really stuck out. | |
33 | //! | |
34 | //! In particular you'll see a certain amount of churn related to INTEGER vs. | |
35 | //! CARDINAL in the Mesa implementation. Mesa apparently interconverts the two | |
85aaf69f | 36 | //! somewhat readily? In any case, I've used usize for indices-in-buffers and |
1a4d82fc JJ |
37 | //! ints for character-sizes-and-indentation-offsets. This respects the need |
38 | //! for ints to "go negative" while carrying a pending-calculation balance, and | |
39 | //! helps differentiate all the numbers flying around internally (slightly). | |
40 | //! | |
41 | //! I also inverted the indentation arithmetic used in the print stack, since | |
42 | //! the Mesa implementation (somewhat randomly) stores the offset on the print | |
43 | //! stack in terms of margin-col rather than col itself. I store col. | |
44 | //! | |
45 | //! I also implemented a small change in the String token, in that I store an | |
46 | //! explicit length for the string. For most tokens this is just the length of | |
47 | //! the accompanying string. But it's necessary to permit it to differ, for | |
48 | //! encoding things that are supposed to "go on their own line" -- certain | |
49 | //! classes of comment and blank-line -- where relying on adjacent | |
50 | //! hardbreak-like Break tokens with long blankness indication doesn't actually | |
51 | //! work. To see why, consider when there is a "thing that should be on its own | |
52 | //! line" between two long blocks, say functions. If you put a hardbreak after | |
53 | //! each function (or before each) and the breaking algorithm decides to break | |
54 | //! there anyways (because the functions themselves are long) you wind up with | |
55 | //! extra blank lines. If you don't put hardbreaks you can wind up with the | |
56 | //! "thing which should be on its own line" not getting its own line in the | |
57 | //! rare case of "really small functions" or such. This re-occurs with comments | |
58 | //! and explicit blank lines. So in those cases we use a string with a payload | |
59 | //! we want isolated to a line and an explicit length that's huge, surrounded | |
60 | //! by two zero-length breaks. The algorithm will try its best to fit it on a | |
61 | //! line (which it can't) and so naturally place the content on its own line to | |
62 | //! avoid combining it with other lines and making matters even worse. | |
63 | ||
c34b1796 | 64 | use std::io; |
1a4d82fc | 65 | use std::string; |
970d7e83 | 66 | |
1a4d82fc JJ |
67 | #[derive(Clone, Copy, PartialEq)] |
68 | pub enum Breaks { | |
69 | Consistent, | |
70 | Inconsistent, | |
71 | } | |
223e47cc | 72 | |
1a4d82fc JJ |
73 | #[derive(Clone, Copy)] |
74 | pub struct BreakToken { | |
85aaf69f SL |
75 | offset: isize, |
76 | blank_space: isize | |
223e47cc LB |
77 | } |
78 | ||
1a4d82fc JJ |
79 | #[derive(Clone, Copy)] |
80 | pub struct BeginToken { | |
85aaf69f | 81 | offset: isize, |
1a4d82fc | 82 | breaks: Breaks |
223e47cc LB |
83 | } |
84 | ||
1a4d82fc JJ |
85 | #[derive(Clone)] |
86 | pub enum Token { | |
85aaf69f | 87 | String(String, isize), |
1a4d82fc JJ |
88 | Break(BreakToken), |
89 | Begin(BeginToken), | |
90 | End, | |
91 | Eof, | |
223e47cc LB |
92 | } |
93 | ||
1a4d82fc | 94 | impl Token { |
970d7e83 | 95 | pub fn is_eof(&self) -> bool { |
85aaf69f SL |
96 | match *self { |
97 | Token::Eof => true, | |
98 | _ => false, | |
99 | } | |
223e47cc | 100 | } |
970d7e83 LB |
101 | |
102 | pub fn is_hardbreak_tok(&self) -> bool { | |
223e47cc | 103 | match *self { |
85aaf69f | 104 | Token::Break(BreakToken { |
223e47cc LB |
105 | offset: 0, |
106 | blank_space: bs | |
1a4d82fc | 107 | }) if bs == SIZE_INFINITY => |
223e47cc LB |
108 | true, |
109 | _ => | |
110 | false | |
111 | } | |
112 | } | |
113 | } | |
114 | ||
85aaf69f SL |
115 | pub fn tok_str(token: &Token) -> String { |
116 | match *token { | |
117 | Token::String(ref s, len) => format!("STR({},{})", s, len), | |
118 | Token::Break(_) => "BREAK".to_string(), | |
119 | Token::Begin(_) => "BEGIN".to_string(), | |
120 | Token::End => "END".to_string(), | |
121 | Token::Eof => "EOF".to_string() | |
223e47cc LB |
122 | } |
123 | } | |
124 | ||
85aaf69f SL |
125 | pub fn buf_str(toks: &[Token], |
126 | szs: &[isize], | |
127 | left: usize, | |
128 | right: usize, | |
129 | lim: usize) | |
130 | -> String { | |
970d7e83 LB |
131 | let n = toks.len(); |
132 | assert_eq!(n, szs.len()); | |
223e47cc | 133 | let mut i = left; |
1a4d82fc | 134 | let mut l = lim; |
d9579d0f | 135 | let mut s = string::String::from("["); |
85aaf69f SL |
136 | while i != right && l != 0 { |
137 | l -= 1; | |
970d7e83 LB |
138 | if i != left { |
139 | s.push_str(", "); | |
140 | } | |
1a4d82fc JJ |
141 | s.push_str(&format!("{}={}", |
142 | szs[i], | |
c34b1796 | 143 | tok_str(&toks[i]))); |
85aaf69f | 144 | i += 1; |
223e47cc LB |
145 | i %= n; |
146 | } | |
1a4d82fc JJ |
147 | s.push(']'); |
148 | s | |
223e47cc LB |
149 | } |
150 | ||
c34b1796 | 151 | #[derive(Copy, Clone)] |
1a4d82fc JJ |
152 | pub enum PrintStackBreak { |
153 | Fits, | |
154 | Broken(Breaks), | |
155 | } | |
223e47cc | 156 | |
c34b1796 | 157 | #[derive(Copy, Clone)] |
1a4d82fc | 158 | pub struct PrintStackElem { |
85aaf69f | 159 | offset: isize, |
1a4d82fc | 160 | pbreak: PrintStackBreak |
223e47cc LB |
161 | } |
162 | ||
c34b1796 | 163 | const SIZE_INFINITY: isize = 0xffff; |
223e47cc | 164 | |
c34b1796 | 165 | pub fn mk_printer<'a>(out: Box<io::Write+'a>, linewidth: usize) -> Printer<'a> { |
223e47cc LB |
166 | // Yes 3, it makes the ring buffers big enough to never |
167 | // fall behind. | |
85aaf69f | 168 | let n: usize = 3 * linewidth; |
1a4d82fc | 169 | debug!("mk_printer {}", linewidth); |
c1a9b12d | 170 | let token = vec![Token::Eof; n]; |
54a0048b SL |
171 | let size = vec![0; n]; |
172 | let scan_stack = vec![0; n]; | |
1a4d82fc JJ |
173 | Printer { |
174 | out: out, | |
223e47cc | 175 | buf_len: n, |
85aaf69f SL |
176 | margin: linewidth as isize, |
177 | space: linewidth as isize, | |
223e47cc LB |
178 | left: 0, |
179 | right: 0, | |
180 | token: token, | |
181 | size: size, | |
182 | left_total: 0, | |
183 | right_total: 0, | |
184 | scan_stack: scan_stack, | |
185 | scan_stack_empty: true, | |
186 | top: 0, | |
187 | bottom: 0, | |
1a4d82fc | 188 | print_stack: Vec::new(), |
223e47cc LB |
189 | pending_indentation: 0 |
190 | } | |
191 | } | |
192 | ||
193 | ||
1a4d82fc JJ |
194 | /// In case you do not have the paper, here is an explanation of what's going |
195 | /// on. | |
196 | /// | |
197 | /// There is a stream of input tokens flowing through this printer. | |
198 | /// | |
199 | /// The printer buffers up to 3N tokens inside itself, where N is linewidth. | |
200 | /// Yes, linewidth is chars and tokens are multi-char, but in the worst | |
201 | /// case every token worth buffering is 1 char long, so it's ok. | |
202 | /// | |
203 | /// Tokens are String, Break, and Begin/End to delimit blocks. | |
204 | /// | |
205 | /// Begin tokens can carry an offset, saying "how far to indent when you break | |
206 | /// inside here", as well as a flag indicating "consistent" or "inconsistent" | |
207 | /// breaking. Consistent breaking means that after the first break, no attempt | |
208 | /// will be made to flow subsequent breaks together onto lines. Inconsistent | |
209 | /// is the opposite. Inconsistent breaking example would be, say: | |
210 | /// | |
211 | /// foo(hello, there, good, friends) | |
212 | /// | |
213 | /// breaking inconsistently to become | |
214 | /// | |
215 | /// foo(hello, there | |
216 | /// good, friends); | |
217 | /// | |
218 | /// whereas a consistent breaking would yield: | |
219 | /// | |
220 | /// foo(hello, | |
221 | /// there | |
222 | /// good, | |
223 | /// friends); | |
224 | /// | |
225 | /// That is, in the consistent-break blocks we value vertical alignment | |
226 | /// more than the ability to cram stuff onto a line. But in all cases if it | |
227 | /// can make a block a one-liner, it'll do so. | |
228 | /// | |
229 | /// Carrying on with high-level logic: | |
230 | /// | |
231 | /// The buffered tokens go through a ring-buffer, 'tokens'. The 'left' and | |
232 | /// 'right' indices denote the active portion of the ring buffer as well as | |
233 | /// describing hypothetical points-in-the-infinite-stream at most 3N tokens | |
234 | /// apart (i.e. "not wrapped to ring-buffer boundaries"). The paper will switch | |
235 | /// between using 'left' and 'right' terms to denote the wrapped-to-ring-buffer | |
236 | /// and point-in-infinite-stream senses freely. | |
237 | /// | |
238 | /// There is a parallel ring buffer, 'size', that holds the calculated size of | |
239 | /// each token. Why calculated? Because for Begin/End pairs, the "size" | |
240 | /// includes everything between the pair. That is, the "size" of Begin is | |
241 | /// actually the sum of the sizes of everything between Begin and the paired | |
242 | /// End that follows. Since that is arbitrarily far in the future, 'size' is | |
243 | /// being rewritten regularly while the printer runs; in fact most of the | |
244 | /// machinery is here to work out 'size' entries on the fly (and give up when | |
245 | /// they're so obviously over-long that "infinity" is a good enough | |
246 | /// approximation for purposes of line breaking). | |
247 | /// | |
248 | /// The "input side" of the printer is managed as an abstract process called | |
249 | /// SCAN, which uses 'scan_stack', 'scan_stack_empty', 'top' and 'bottom', to | |
250 | /// manage calculating 'size'. SCAN is, in other words, the process of | |
251 | /// calculating 'size' entries. | |
252 | /// | |
253 | /// The "output side" of the printer is managed by an abstract process called | |
254 | /// PRINT, which uses 'print_stack', 'margin' and 'space' to figure out what to | |
255 | /// do with each token/size pair it consumes as it goes. It's trying to consume | |
256 | /// the entire buffered window, but can't output anything until the size is >= | |
257 | /// 0 (sizes are set to negative while they're pending calculation). | |
258 | /// | |
259 | /// So SCAN takes input and buffers tokens and pending calculations, while | |
260 | /// PRINT gobbles up completed calculations and tokens from the buffer. The | |
261 | /// theory is that the two can never get more than 3N tokens apart, because | |
262 | /// once there's "obviously" too much data to fit on a line, in a size | |
263 | /// calculation, SCAN will write "infinity" to the size and let PRINT consume | |
264 | /// it. | |
265 | /// | |
266 | /// In this implementation (following the paper, again) the SCAN process is | |
267 | /// the method called 'pretty_print', and the 'PRINT' process is the method | |
268 | /// called 'print'. | |
c34b1796 AL |
269 | pub struct Printer<'a> { |
270 | pub out: Box<io::Write+'a>, | |
85aaf69f | 271 | buf_len: usize, |
1a4d82fc | 272 | /// Width of lines we're constrained to |
85aaf69f | 273 | margin: isize, |
1a4d82fc | 274 | /// Number of spaces left on line |
85aaf69f | 275 | space: isize, |
1a4d82fc | 276 | /// Index of left side of input stream |
85aaf69f | 277 | left: usize, |
1a4d82fc | 278 | /// Index of right side of input stream |
85aaf69f | 279 | right: usize, |
1a4d82fc JJ |
280 | /// Ring-buffer stream goes through |
281 | token: Vec<Token> , | |
282 | /// Ring-buffer of calculated sizes | |
85aaf69f | 283 | size: Vec<isize> , |
1a4d82fc | 284 | /// Running size of stream "...left" |
85aaf69f | 285 | left_total: isize, |
1a4d82fc | 286 | /// Running size of stream "...right" |
85aaf69f | 287 | right_total: isize, |
1a4d82fc JJ |
288 | /// Pseudo-stack, really a ring too. Holds the |
289 | /// primary-ring-buffers index of the Begin that started the | |
290 | /// current block, possibly with the most recent Break after that | |
291 | /// Begin (if there is any) on top of it. Stuff is flushed off the | |
292 | /// bottom as it becomes irrelevant due to the primary ring-buffer | |
293 | /// advancing. | |
85aaf69f | 294 | scan_stack: Vec<usize> , |
1a4d82fc JJ |
295 | /// Top==bottom disambiguator |
296 | scan_stack_empty: bool, | |
297 | /// Index of top of scan_stack | |
85aaf69f | 298 | top: usize, |
1a4d82fc | 299 | /// Index of bottom of scan_stack |
85aaf69f | 300 | bottom: usize, |
1a4d82fc JJ |
301 | /// Stack of blocks-in-progress being flushed by print |
302 | print_stack: Vec<PrintStackElem> , | |
303 | /// Buffered indentation to avoid writing trailing whitespace | |
85aaf69f | 304 | pending_indentation: isize, |
223e47cc LB |
305 | } |
306 | ||
c34b1796 | 307 | impl<'a> Printer<'a> { |
1a4d82fc JJ |
308 | pub fn last_token(&mut self) -> Token { |
309 | self.token[self.right].clone() | |
310 | } | |
223e47cc | 311 | // be very careful with this! |
1a4d82fc | 312 | pub fn replace_last_token(&mut self, t: Token) { |
970d7e83 LB |
313 | self.token[self.right] = t; |
314 | } | |
c34b1796 | 315 | pub fn pretty_print(&mut self, token: Token) -> io::Result<()> { |
d9579d0f | 316 | debug!("pp Vec<{},{}>", self.left, self.right); |
85aaf69f SL |
317 | match token { |
318 | Token::Eof => { | |
223e47cc LB |
319 | if !self.scan_stack_empty { |
320 | self.check_stack(0); | |
54a0048b | 321 | self.advance_left()?; |
223e47cc LB |
322 | } |
323 | self.indent(0); | |
1a4d82fc | 324 | Ok(()) |
223e47cc | 325 | } |
85aaf69f | 326 | Token::Begin(b) => { |
223e47cc LB |
327 | if self.scan_stack_empty { |
328 | self.left_total = 1; | |
329 | self.right_total = 1; | |
85aaf69f SL |
330 | self.left = 0; |
331 | self.right = 0; | |
223e47cc | 332 | } else { self.advance_right(); } |
d9579d0f | 333 | debug!("pp Begin({})/buffer Vec<{},{}>", |
223e47cc | 334 | b.offset, self.left, self.right); |
85aaf69f | 335 | self.token[self.right] = token; |
223e47cc | 336 | self.size[self.right] = -self.right_total; |
1a4d82fc JJ |
337 | let right = self.right; |
338 | self.scan_push(right); | |
339 | Ok(()) | |
223e47cc | 340 | } |
85aaf69f | 341 | Token::End => { |
223e47cc | 342 | if self.scan_stack_empty { |
d9579d0f | 343 | debug!("pp End/print Vec<{},{}>", self.left, self.right); |
85aaf69f | 344 | self.print(token, 0) |
223e47cc | 345 | } else { |
d9579d0f | 346 | debug!("pp End/buffer Vec<{},{}>", self.left, self.right); |
223e47cc | 347 | self.advance_right(); |
85aaf69f | 348 | self.token[self.right] = token; |
223e47cc | 349 | self.size[self.right] = -1; |
1a4d82fc JJ |
350 | let right = self.right; |
351 | self.scan_push(right); | |
352 | Ok(()) | |
223e47cc LB |
353 | } |
354 | } | |
85aaf69f | 355 | Token::Break(b) => { |
223e47cc LB |
356 | if self.scan_stack_empty { |
357 | self.left_total = 1; | |
358 | self.right_total = 1; | |
85aaf69f SL |
359 | self.left = 0; |
360 | self.right = 0; | |
223e47cc | 361 | } else { self.advance_right(); } |
d9579d0f | 362 | debug!("pp Break({})/buffer Vec<{},{}>", |
223e47cc LB |
363 | b.offset, self.left, self.right); |
364 | self.check_stack(0); | |
1a4d82fc JJ |
365 | let right = self.right; |
366 | self.scan_push(right); | |
85aaf69f | 367 | self.token[self.right] = token; |
223e47cc LB |
368 | self.size[self.right] = -self.right_total; |
369 | self.right_total += b.blank_space; | |
1a4d82fc | 370 | Ok(()) |
223e47cc | 371 | } |
85aaf69f | 372 | Token::String(s, len) => { |
223e47cc | 373 | if self.scan_stack_empty { |
d9579d0f | 374 | debug!("pp String('{}')/print Vec<{},{}>", |
85aaf69f SL |
375 | s, self.left, self.right); |
376 | self.print(Token::String(s, len), len) | |
223e47cc | 377 | } else { |
d9579d0f | 378 | debug!("pp String('{}')/buffer Vec<{},{}>", |
85aaf69f | 379 | s, self.left, self.right); |
223e47cc | 380 | self.advance_right(); |
85aaf69f | 381 | self.token[self.right] = Token::String(s, len); |
223e47cc LB |
382 | self.size[self.right] = len; |
383 | self.right_total += len; | |
1a4d82fc | 384 | self.check_stream() |
223e47cc LB |
385 | } |
386 | } | |
387 | } | |
388 | } | |
c34b1796 | 389 | pub fn check_stream(&mut self) -> io::Result<()> { |
d9579d0f | 390 | debug!("check_stream Vec<{}, {}> with left_total={}, right_total={}", |
223e47cc LB |
391 | self.left, self.right, self.left_total, self.right_total); |
392 | if self.right_total - self.left_total > self.space { | |
1a4d82fc | 393 | debug!("scan window is {}, longer than space on line ({})", |
223e47cc LB |
394 | self.right_total - self.left_total, self.space); |
395 | if !self.scan_stack_empty { | |
396 | if self.left == self.scan_stack[self.bottom] { | |
1a4d82fc JJ |
397 | debug!("setting {} to infinity and popping", self.left); |
398 | let scanned = self.scan_pop_bottom(); | |
399 | self.size[scanned] = SIZE_INFINITY; | |
223e47cc LB |
400 | } |
401 | } | |
54a0048b | 402 | self.advance_left()?; |
1a4d82fc | 403 | if self.left != self.right { |
54a0048b | 404 | self.check_stream()?; |
1a4d82fc | 405 | } |
223e47cc | 406 | } |
1a4d82fc | 407 | Ok(()) |
223e47cc | 408 | } |
85aaf69f | 409 | pub fn scan_push(&mut self, x: usize) { |
1a4d82fc | 410 | debug!("scan_push {}", x); |
223e47cc LB |
411 | if self.scan_stack_empty { |
412 | self.scan_stack_empty = false; | |
413 | } else { | |
85aaf69f | 414 | self.top += 1; |
223e47cc LB |
415 | self.top %= self.buf_len; |
416 | assert!((self.top != self.bottom)); | |
417 | } | |
418 | self.scan_stack[self.top] = x; | |
419 | } | |
85aaf69f | 420 | pub fn scan_pop(&mut self) -> usize { |
223e47cc LB |
421 | assert!((!self.scan_stack_empty)); |
422 | let x = self.scan_stack[self.top]; | |
423 | if self.top == self.bottom { | |
424 | self.scan_stack_empty = true; | |
1a4d82fc | 425 | } else { |
85aaf69f | 426 | self.top += self.buf_len - 1; self.top %= self.buf_len; |
1a4d82fc | 427 | } |
223e47cc LB |
428 | return x; |
429 | } | |
85aaf69f | 430 | pub fn scan_top(&mut self) -> usize { |
223e47cc LB |
431 | assert!((!self.scan_stack_empty)); |
432 | return self.scan_stack[self.top]; | |
433 | } | |
85aaf69f | 434 | pub fn scan_pop_bottom(&mut self) -> usize { |
223e47cc LB |
435 | assert!((!self.scan_stack_empty)); |
436 | let x = self.scan_stack[self.bottom]; | |
437 | if self.top == self.bottom { | |
438 | self.scan_stack_empty = true; | |
1a4d82fc | 439 | } else { |
85aaf69f | 440 | self.bottom += 1; self.bottom %= self.buf_len; |
1a4d82fc | 441 | } |
223e47cc LB |
442 | return x; |
443 | } | |
970d7e83 | 444 | pub fn advance_right(&mut self) { |
85aaf69f | 445 | self.right += 1; |
223e47cc LB |
446 | self.right %= self.buf_len; |
447 | assert!((self.right != self.left)); | |
448 | } | |
c34b1796 | 449 | pub fn advance_left(&mut self) -> io::Result<()> { |
d9579d0f | 450 | debug!("advance_left Vec<{},{}>, sizeof({})={}", self.left, self.right, |
85aaf69f SL |
451 | self.left, self.size[self.left]); |
452 | ||
453 | let mut left_size = self.size[self.left]; | |
454 | ||
455 | while left_size >= 0 { | |
456 | let left = self.token[self.left].clone(); | |
457 | ||
458 | let len = match left { | |
459 | Token::Break(b) => b.blank_space, | |
460 | Token::String(_, len) => { | |
461 | assert_eq!(len, left_size); | |
462 | len | |
463 | } | |
464 | _ => 0 | |
465 | }; | |
466 | ||
54a0048b | 467 | self.print(left, left_size)?; |
85aaf69f SL |
468 | |
469 | self.left_total += len; | |
470 | ||
471 | if self.left == self.right { | |
472 | break; | |
223e47cc | 473 | } |
85aaf69f SL |
474 | |
475 | self.left += 1; | |
476 | self.left %= self.buf_len; | |
477 | ||
478 | left_size = self.size[self.left]; | |
223e47cc | 479 | } |
85aaf69f SL |
480 | |
481 | Ok(()) | |
223e47cc | 482 | } |
85aaf69f | 483 | pub fn check_stack(&mut self, k: isize) { |
223e47cc LB |
484 | if !self.scan_stack_empty { |
485 | let x = self.scan_top(); | |
1a4d82fc | 486 | match self.token[x] { |
85aaf69f | 487 | Token::Begin(_) => { |
1a4d82fc JJ |
488 | if k > 0 { |
489 | let popped = self.scan_pop(); | |
490 | self.size[popped] = self.size[x] + self.right_total; | |
491 | self.check_stack(k - 1); | |
492 | } | |
493 | } | |
85aaf69f | 494 | Token::End => { |
1a4d82fc JJ |
495 | // paper says + not =, but that makes no sense. |
496 | let popped = self.scan_pop(); | |
497 | self.size[popped] = 1; | |
498 | self.check_stack(k + 1); | |
499 | } | |
500 | _ => { | |
501 | let popped = self.scan_pop(); | |
502 | self.size[popped] = self.size[x] + self.right_total; | |
503 | if k > 0 { | |
504 | self.check_stack(k); | |
505 | } | |
223e47cc | 506 | } |
223e47cc LB |
507 | } |
508 | } | |
509 | } | |
c34b1796 | 510 | pub fn print_newline(&mut self, amount: isize) -> io::Result<()> { |
1a4d82fc JJ |
511 | debug!("NEWLINE {}", amount); |
512 | let ret = write!(self.out, "\n"); | |
223e47cc LB |
513 | self.pending_indentation = 0; |
514 | self.indent(amount); | |
1a4d82fc | 515 | return ret; |
223e47cc | 516 | } |
85aaf69f | 517 | pub fn indent(&mut self, amount: isize) { |
1a4d82fc | 518 | debug!("INDENT {}", amount); |
223e47cc LB |
519 | self.pending_indentation += amount; |
520 | } | |
1a4d82fc JJ |
521 | pub fn get_top(&mut self) -> PrintStackElem { |
522 | let print_stack = &mut self.print_stack; | |
223e47cc | 523 | let n = print_stack.len(); |
85aaf69f | 524 | if n != 0 { |
1a4d82fc | 525 | (*print_stack)[n - 1] |
223e47cc | 526 | } else { |
1a4d82fc | 527 | PrintStackElem { |
223e47cc | 528 | offset: 0, |
85aaf69f | 529 | pbreak: PrintStackBreak::Broken(Breaks::Inconsistent) |
223e47cc LB |
530 | } |
531 | } | |
532 | } | |
c34b1796 | 533 | pub fn print_str(&mut self, s: &str) -> io::Result<()> { |
223e47cc | 534 | while self.pending_indentation > 0 { |
54a0048b | 535 | write!(self.out, " ")?; |
223e47cc LB |
536 | self.pending_indentation -= 1; |
537 | } | |
1a4d82fc | 538 | write!(self.out, "{}", s) |
223e47cc | 539 | } |
c34b1796 | 540 | pub fn print(&mut self, token: Token, l: isize) -> io::Result<()> { |
85aaf69f | 541 | debug!("print {} {} (remaining line space={})", tok_str(&token), l, |
223e47cc | 542 | self.space); |
85aaf69f SL |
543 | debug!("{}", buf_str(&self.token, |
544 | &self.size, | |
1a4d82fc JJ |
545 | self.left, |
546 | self.right, | |
547 | 6)); | |
85aaf69f SL |
548 | match token { |
549 | Token::Begin(b) => { | |
1a4d82fc | 550 | if l > self.space { |
223e47cc | 551 | let col = self.margin - self.space + b.offset; |
1a4d82fc JJ |
552 | debug!("print Begin -> push broken block at col {}", col); |
553 | self.print_stack.push(PrintStackElem { | |
223e47cc | 554 | offset: col, |
85aaf69f | 555 | pbreak: PrintStackBreak::Broken(b.breaks) |
223e47cc LB |
556 | }); |
557 | } else { | |
1a4d82fc JJ |
558 | debug!("print Begin -> push fitting block"); |
559 | self.print_stack.push(PrintStackElem { | |
223e47cc | 560 | offset: 0, |
85aaf69f | 561 | pbreak: PrintStackBreak::Fits |
223e47cc LB |
562 | }); |
563 | } | |
1a4d82fc | 564 | Ok(()) |
223e47cc | 565 | } |
85aaf69f | 566 | Token::End => { |
1a4d82fc JJ |
567 | debug!("print End -> pop End"); |
568 | let print_stack = &mut self.print_stack; | |
9346a6ac | 569 | assert!((!print_stack.is_empty())); |
1a4d82fc JJ |
570 | print_stack.pop().unwrap(); |
571 | Ok(()) | |
223e47cc | 572 | } |
85aaf69f | 573 | Token::Break(b) => { |
223e47cc LB |
574 | let top = self.get_top(); |
575 | match top.pbreak { | |
85aaf69f | 576 | PrintStackBreak::Fits => { |
1a4d82fc | 577 | debug!("print Break({}) in fitting block", b.blank_space); |
223e47cc LB |
578 | self.space -= b.blank_space; |
579 | self.indent(b.blank_space); | |
1a4d82fc | 580 | Ok(()) |
223e47cc | 581 | } |
85aaf69f | 582 | PrintStackBreak::Broken(Breaks::Consistent) => { |
1a4d82fc | 583 | debug!("print Break({}+{}) in consistent block", |
223e47cc | 584 | top.offset, b.offset); |
1a4d82fc | 585 | let ret = self.print_newline(top.offset + b.offset); |
223e47cc | 586 | self.space = self.margin - (top.offset + b.offset); |
1a4d82fc | 587 | ret |
223e47cc | 588 | } |
85aaf69f | 589 | PrintStackBreak::Broken(Breaks::Inconsistent) => { |
1a4d82fc JJ |
590 | if l > self.space { |
591 | debug!("print Break({}+{}) w/ newline in inconsistent", | |
223e47cc | 592 | top.offset, b.offset); |
1a4d82fc | 593 | let ret = self.print_newline(top.offset + b.offset); |
223e47cc | 594 | self.space = self.margin - (top.offset + b.offset); |
1a4d82fc | 595 | ret |
223e47cc | 596 | } else { |
1a4d82fc | 597 | debug!("print Break({}) w/o newline in inconsistent", |
223e47cc LB |
598 | b.blank_space); |
599 | self.indent(b.blank_space); | |
600 | self.space -= b.blank_space; | |
1a4d82fc | 601 | Ok(()) |
223e47cc LB |
602 | } |
603 | } | |
604 | } | |
605 | } | |
85aaf69f | 606 | Token::String(s, len) => { |
1a4d82fc JJ |
607 | debug!("print String({})", s); |
608 | assert_eq!(l, len); | |
609 | // assert!(l <= space); | |
223e47cc | 610 | self.space -= len; |
85aaf69f | 611 | self.print_str(&s[..]) |
223e47cc | 612 | } |
85aaf69f | 613 | Token::Eof => { |
1a4d82fc JJ |
614 | // Eof should never get here. |
615 | panic!(); | |
223e47cc LB |
616 | } |
617 | } | |
618 | } | |
619 | } | |
620 | ||
621 | // Convenience functions to talk to the printer. | |
1a4d82fc JJ |
622 | // |
623 | // "raw box" | |
c34b1796 | 624 | pub fn rbox(p: &mut Printer, indent: usize, b: Breaks) -> io::Result<()> { |
85aaf69f SL |
625 | p.pretty_print(Token::Begin(BeginToken { |
626 | offset: indent as isize, | |
223e47cc | 627 | breaks: b |
1a4d82fc | 628 | })) |
223e47cc LB |
629 | } |
630 | ||
c34b1796 | 631 | pub fn ibox(p: &mut Printer, indent: usize) -> io::Result<()> { |
85aaf69f | 632 | rbox(p, indent, Breaks::Inconsistent) |
1a4d82fc | 633 | } |
223e47cc | 634 | |
c34b1796 | 635 | pub fn cbox(p: &mut Printer, indent: usize) -> io::Result<()> { |
85aaf69f | 636 | rbox(p, indent, Breaks::Consistent) |
1a4d82fc | 637 | } |
223e47cc | 638 | |
c34b1796 | 639 | pub fn break_offset(p: &mut Printer, n: usize, off: isize) -> io::Result<()> { |
85aaf69f | 640 | p.pretty_print(Token::Break(BreakToken { |
223e47cc | 641 | offset: off, |
85aaf69f | 642 | blank_space: n as isize |
1a4d82fc | 643 | })) |
223e47cc LB |
644 | } |
645 | ||
c34b1796 | 646 | pub fn end(p: &mut Printer) -> io::Result<()> { |
85aaf69f SL |
647 | p.pretty_print(Token::End) |
648 | } | |
223e47cc | 649 | |
c34b1796 | 650 | pub fn eof(p: &mut Printer) -> io::Result<()> { |
85aaf69f SL |
651 | p.pretty_print(Token::Eof) |
652 | } | |
223e47cc | 653 | |
c34b1796 | 654 | pub fn word(p: &mut Printer, wrd: &str) -> io::Result<()> { |
85aaf69f | 655 | p.pretty_print(Token::String(/* bad */ wrd.to_string(), wrd.len() as isize)) |
223e47cc LB |
656 | } |
657 | ||
c34b1796 | 658 | pub fn huge_word(p: &mut Printer, wrd: &str) -> io::Result<()> { |
85aaf69f | 659 | p.pretty_print(Token::String(/* bad */ wrd.to_string(), SIZE_INFINITY)) |
223e47cc LB |
660 | } |
661 | ||
c34b1796 | 662 | pub fn zero_word(p: &mut Printer, wrd: &str) -> io::Result<()> { |
85aaf69f | 663 | p.pretty_print(Token::String(/* bad */ wrd.to_string(), 0)) |
223e47cc LB |
664 | } |
665 | ||
c34b1796 | 666 | pub fn spaces(p: &mut Printer, n: usize) -> io::Result<()> { |
1a4d82fc JJ |
667 | break_offset(p, n, 0) |
668 | } | |
223e47cc | 669 | |
c34b1796 | 670 | pub fn zerobreak(p: &mut Printer) -> io::Result<()> { |
85aaf69f | 671 | spaces(p, 0) |
1a4d82fc | 672 | } |
223e47cc | 673 | |
c34b1796 | 674 | pub fn space(p: &mut Printer) -> io::Result<()> { |
85aaf69f | 675 | spaces(p, 1) |
1a4d82fc | 676 | } |
223e47cc | 677 | |
c34b1796 | 678 | pub fn hardbreak(p: &mut Printer) -> io::Result<()> { |
85aaf69f | 679 | spaces(p, SIZE_INFINITY as usize) |
1a4d82fc | 680 | } |
223e47cc | 681 | |
85aaf69f SL |
682 | pub fn hardbreak_tok_offset(off: isize) -> Token { |
683 | Token::Break(BreakToken {offset: off, blank_space: SIZE_INFINITY}) | |
223e47cc LB |
684 | } |
685 | ||
85aaf69f SL |
686 | pub fn hardbreak_tok() -> Token { |
687 | hardbreak_tok_offset(0) | |
688 | } |