]> git.proxmox.com Git - rustc.git/blame - src/libsyntax/print/pp.rs
Imported Upstream version 1.11.0+dfsg1
[rustc.git] / src / libsyntax / print / pp.rs
CommitLineData
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
a7813a04
XL
64use std::collections::VecDeque;
65use std::fmt;
c34b1796 66use std::io;
970d7e83 67
1a4d82fc
JJ
68#[derive(Clone, Copy, PartialEq)]
69pub enum Breaks {
70 Consistent,
71 Inconsistent,
72}
223e47cc 73
1a4d82fc
JJ
74#[derive(Clone, Copy)]
75pub struct BreakToken {
85aaf69f
SL
76 offset: isize,
77 blank_space: isize
223e47cc
LB
78}
79
1a4d82fc
JJ
80#[derive(Clone, Copy)]
81pub struct BeginToken {
85aaf69f 82 offset: isize,
1a4d82fc 83 breaks: Breaks
223e47cc
LB
84}
85
1a4d82fc
JJ
86#[derive(Clone)]
87pub enum Token {
85aaf69f 88 String(String, isize),
1a4d82fc
JJ
89 Break(BreakToken),
90 Begin(BeginToken),
91 End,
92 Eof,
223e47cc
LB
93}
94
1a4d82fc 95impl Token {
970d7e83 96 pub fn is_eof(&self) -> bool {
85aaf69f
SL
97 match *self {
98 Token::Eof => true,
99 _ => false,
100 }
223e47cc 101 }
970d7e83
LB
102
103 pub fn is_hardbreak_tok(&self) -> bool {
223e47cc 104 match *self {
85aaf69f 105 Token::Break(BreakToken {
223e47cc
LB
106 offset: 0,
107 blank_space: bs
1a4d82fc 108 }) if bs == SIZE_INFINITY =>
223e47cc
LB
109 true,
110 _ =>
111 false
112 }
113 }
114}
115
a7813a04
XL
116impl fmt::Display for Token {
117 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
118 match *self {
119 Token::String(ref s, len) => write!(f, "STR({},{})", s, len),
120 Token::Break(_) => f.write_str("BREAK"),
121 Token::Begin(_) => f.write_str("BEGIN"),
122 Token::End => f.write_str("END"),
123 Token::Eof => f.write_str("EOF"),
124 }
223e47cc
LB
125 }
126}
127
a7813a04 128fn buf_str(toks: &[Token], szs: &[isize], left: usize, right: usize, lim: usize) -> String {
970d7e83
LB
129 let n = toks.len();
130 assert_eq!(n, szs.len());
223e47cc 131 let mut i = left;
1a4d82fc 132 let mut l = lim;
a7813a04 133 let mut s = String::from("[");
85aaf69f
SL
134 while i != right && l != 0 {
135 l -= 1;
970d7e83
LB
136 if i != left {
137 s.push_str(", ");
138 }
a7813a04 139 s.push_str(&format!("{}={}", szs[i], &toks[i]));
85aaf69f 140 i += 1;
223e47cc
LB
141 i %= n;
142 }
1a4d82fc
JJ
143 s.push(']');
144 s
223e47cc
LB
145}
146
c34b1796 147#[derive(Copy, Clone)]
1a4d82fc
JJ
148pub enum PrintStackBreak {
149 Fits,
150 Broken(Breaks),
151}
223e47cc 152
c34b1796 153#[derive(Copy, Clone)]
1a4d82fc 154pub struct PrintStackElem {
85aaf69f 155 offset: isize,
1a4d82fc 156 pbreak: PrintStackBreak
223e47cc
LB
157}
158
c34b1796 159const SIZE_INFINITY: isize = 0xffff;
223e47cc 160
c34b1796 161pub fn mk_printer<'a>(out: Box<io::Write+'a>, linewidth: usize) -> Printer<'a> {
3157f602 162 // Yes 55, it makes the ring buffers big enough to never
223e47cc 163 // fall behind.
3157f602 164 let n: usize = 55 * linewidth;
1a4d82fc 165 debug!("mk_printer {}", linewidth);
c1a9b12d 166 let token = vec![Token::Eof; n];
54a0048b 167 let size = vec![0; n];
a7813a04 168 let scan_stack = VecDeque::with_capacity(n);
1a4d82fc
JJ
169 Printer {
170 out: out,
223e47cc 171 buf_len: n,
85aaf69f
SL
172 margin: linewidth as isize,
173 space: linewidth as isize,
223e47cc
LB
174 left: 0,
175 right: 0,
176 token: token,
177 size: size,
178 left_total: 0,
179 right_total: 0,
180 scan_stack: scan_stack,
1a4d82fc 181 print_stack: Vec::new(),
223e47cc
LB
182 pending_indentation: 0
183 }
184}
185
186
1a4d82fc
JJ
187/// In case you do not have the paper, here is an explanation of what's going
188/// on.
189///
190/// There is a stream of input tokens flowing through this printer.
191///
192/// The printer buffers up to 3N tokens inside itself, where N is linewidth.
193/// Yes, linewidth is chars and tokens are multi-char, but in the worst
194/// case every token worth buffering is 1 char long, so it's ok.
195///
196/// Tokens are String, Break, and Begin/End to delimit blocks.
197///
198/// Begin tokens can carry an offset, saying "how far to indent when you break
199/// inside here", as well as a flag indicating "consistent" or "inconsistent"
200/// breaking. Consistent breaking means that after the first break, no attempt
201/// will be made to flow subsequent breaks together onto lines. Inconsistent
202/// is the opposite. Inconsistent breaking example would be, say:
203///
204/// foo(hello, there, good, friends)
205///
206/// breaking inconsistently to become
207///
208/// foo(hello, there
209/// good, friends);
210///
211/// whereas a consistent breaking would yield:
212///
213/// foo(hello,
214/// there
215/// good,
216/// friends);
217///
218/// That is, in the consistent-break blocks we value vertical alignment
219/// more than the ability to cram stuff onto a line. But in all cases if it
220/// can make a block a one-liner, it'll do so.
221///
222/// Carrying on with high-level logic:
223///
224/// The buffered tokens go through a ring-buffer, 'tokens'. The 'left' and
225/// 'right' indices denote the active portion of the ring buffer as well as
226/// describing hypothetical points-in-the-infinite-stream at most 3N tokens
227/// apart (i.e. "not wrapped to ring-buffer boundaries"). The paper will switch
228/// between using 'left' and 'right' terms to denote the wrapped-to-ring-buffer
229/// and point-in-infinite-stream senses freely.
230///
231/// There is a parallel ring buffer, 'size', that holds the calculated size of
232/// each token. Why calculated? Because for Begin/End pairs, the "size"
233/// includes everything between the pair. That is, the "size" of Begin is
234/// actually the sum of the sizes of everything between Begin and the paired
235/// End that follows. Since that is arbitrarily far in the future, 'size' is
236/// being rewritten regularly while the printer runs; in fact most of the
237/// machinery is here to work out 'size' entries on the fly (and give up when
238/// they're so obviously over-long that "infinity" is a good enough
239/// approximation for purposes of line breaking).
240///
241/// The "input side" of the printer is managed as an abstract process called
a7813a04
XL
242/// SCAN, which uses 'scan_stack', to manage calculating 'size'. SCAN is, in
243/// other words, the process of calculating 'size' entries.
1a4d82fc
JJ
244///
245/// The "output side" of the printer is managed by an abstract process called
246/// PRINT, which uses 'print_stack', 'margin' and 'space' to figure out what to
247/// do with each token/size pair it consumes as it goes. It's trying to consume
248/// the entire buffered window, but can't output anything until the size is >=
249/// 0 (sizes are set to negative while they're pending calculation).
250///
251/// So SCAN takes input and buffers tokens and pending calculations, while
252/// PRINT gobbles up completed calculations and tokens from the buffer. The
253/// theory is that the two can never get more than 3N tokens apart, because
254/// once there's "obviously" too much data to fit on a line, in a size
255/// calculation, SCAN will write "infinity" to the size and let PRINT consume
256/// it.
257///
258/// In this implementation (following the paper, again) the SCAN process is
259/// the method called 'pretty_print', and the 'PRINT' process is the method
260/// called 'print'.
c34b1796
AL
261pub struct Printer<'a> {
262 pub out: Box<io::Write+'a>,
85aaf69f 263 buf_len: usize,
1a4d82fc 264 /// Width of lines we're constrained to
85aaf69f 265 margin: isize,
1a4d82fc 266 /// Number of spaces left on line
85aaf69f 267 space: isize,
1a4d82fc 268 /// Index of left side of input stream
85aaf69f 269 left: usize,
1a4d82fc 270 /// Index of right side of input stream
85aaf69f 271 right: usize,
1a4d82fc
JJ
272 /// Ring-buffer stream goes through
273 token: Vec<Token> ,
274 /// Ring-buffer of calculated sizes
85aaf69f 275 size: Vec<isize> ,
1a4d82fc 276 /// Running size of stream "...left"
85aaf69f 277 left_total: isize,
1a4d82fc 278 /// Running size of stream "...right"
85aaf69f 279 right_total: isize,
1a4d82fc
JJ
280 /// Pseudo-stack, really a ring too. Holds the
281 /// primary-ring-buffers index of the Begin that started the
282 /// current block, possibly with the most recent Break after that
283 /// Begin (if there is any) on top of it. Stuff is flushed off the
284 /// bottom as it becomes irrelevant due to the primary ring-buffer
285 /// advancing.
a7813a04 286 scan_stack: VecDeque<usize> ,
1a4d82fc
JJ
287 /// Stack of blocks-in-progress being flushed by print
288 print_stack: Vec<PrintStackElem> ,
289 /// Buffered indentation to avoid writing trailing whitespace
85aaf69f 290 pending_indentation: isize,
223e47cc
LB
291}
292
c34b1796 293impl<'a> Printer<'a> {
1a4d82fc
JJ
294 pub fn last_token(&mut self) -> Token {
295 self.token[self.right].clone()
296 }
223e47cc 297 // be very careful with this!
1a4d82fc 298 pub fn replace_last_token(&mut self, t: Token) {
970d7e83
LB
299 self.token[self.right] = t;
300 }
c34b1796 301 pub fn pretty_print(&mut self, token: Token) -> io::Result<()> {
d9579d0f 302 debug!("pp Vec<{},{}>", self.left, self.right);
85aaf69f
SL
303 match token {
304 Token::Eof => {
a7813a04 305 if !self.scan_stack.is_empty() {
223e47cc 306 self.check_stack(0);
54a0048b 307 self.advance_left()?;
223e47cc
LB
308 }
309 self.indent(0);
1a4d82fc 310 Ok(())
223e47cc 311 }
85aaf69f 312 Token::Begin(b) => {
a7813a04 313 if self.scan_stack.is_empty() {
223e47cc
LB
314 self.left_total = 1;
315 self.right_total = 1;
85aaf69f
SL
316 self.left = 0;
317 self.right = 0;
223e47cc 318 } else { self.advance_right(); }
d9579d0f 319 debug!("pp Begin({})/buffer Vec<{},{}>",
223e47cc 320 b.offset, self.left, self.right);
85aaf69f 321 self.token[self.right] = token;
223e47cc 322 self.size[self.right] = -self.right_total;
1a4d82fc
JJ
323 let right = self.right;
324 self.scan_push(right);
325 Ok(())
223e47cc 326 }
85aaf69f 327 Token::End => {
a7813a04 328 if self.scan_stack.is_empty() {
d9579d0f 329 debug!("pp End/print Vec<{},{}>", self.left, self.right);
85aaf69f 330 self.print(token, 0)
223e47cc 331 } else {
d9579d0f 332 debug!("pp End/buffer Vec<{},{}>", self.left, self.right);
223e47cc 333 self.advance_right();
85aaf69f 334 self.token[self.right] = token;
223e47cc 335 self.size[self.right] = -1;
1a4d82fc
JJ
336 let right = self.right;
337 self.scan_push(right);
338 Ok(())
223e47cc
LB
339 }
340 }
85aaf69f 341 Token::Break(b) => {
a7813a04 342 if self.scan_stack.is_empty() {
223e47cc
LB
343 self.left_total = 1;
344 self.right_total = 1;
85aaf69f
SL
345 self.left = 0;
346 self.right = 0;
223e47cc 347 } else { self.advance_right(); }
d9579d0f 348 debug!("pp Break({})/buffer Vec<{},{}>",
223e47cc
LB
349 b.offset, self.left, self.right);
350 self.check_stack(0);
1a4d82fc
JJ
351 let right = self.right;
352 self.scan_push(right);
85aaf69f 353 self.token[self.right] = token;
223e47cc
LB
354 self.size[self.right] = -self.right_total;
355 self.right_total += b.blank_space;
1a4d82fc 356 Ok(())
223e47cc 357 }
85aaf69f 358 Token::String(s, len) => {
a7813a04 359 if self.scan_stack.is_empty() {
d9579d0f 360 debug!("pp String('{}')/print Vec<{},{}>",
85aaf69f
SL
361 s, self.left, self.right);
362 self.print(Token::String(s, len), len)
223e47cc 363 } else {
d9579d0f 364 debug!("pp String('{}')/buffer Vec<{},{}>",
85aaf69f 365 s, self.left, self.right);
223e47cc 366 self.advance_right();
85aaf69f 367 self.token[self.right] = Token::String(s, len);
223e47cc
LB
368 self.size[self.right] = len;
369 self.right_total += len;
1a4d82fc 370 self.check_stream()
223e47cc
LB
371 }
372 }
373 }
374 }
c34b1796 375 pub fn check_stream(&mut self) -> io::Result<()> {
d9579d0f 376 debug!("check_stream Vec<{}, {}> with left_total={}, right_total={}",
223e47cc
LB
377 self.left, self.right, self.left_total, self.right_total);
378 if self.right_total - self.left_total > self.space {
1a4d82fc 379 debug!("scan window is {}, longer than space on line ({})",
223e47cc 380 self.right_total - self.left_total, self.space);
a7813a04
XL
381 if Some(&self.left) == self.scan_stack.back() {
382 debug!("setting {} to infinity and popping", self.left);
383 let scanned = self.scan_pop_bottom();
384 self.size[scanned] = SIZE_INFINITY;
223e47cc 385 }
54a0048b 386 self.advance_left()?;
1a4d82fc 387 if self.left != self.right {
54a0048b 388 self.check_stream()?;
1a4d82fc 389 }
223e47cc 390 }
1a4d82fc 391 Ok(())
223e47cc 392 }
85aaf69f 393 pub fn scan_push(&mut self, x: usize) {
1a4d82fc 394 debug!("scan_push {}", x);
a7813a04 395 self.scan_stack.push_front(x);
223e47cc 396 }
85aaf69f 397 pub fn scan_pop(&mut self) -> usize {
a7813a04 398 self.scan_stack.pop_front().unwrap()
223e47cc 399 }
85aaf69f 400 pub fn scan_top(&mut self) -> usize {
a7813a04 401 *self.scan_stack.front().unwrap()
223e47cc 402 }
85aaf69f 403 pub fn scan_pop_bottom(&mut self) -> usize {
a7813a04 404 self.scan_stack.pop_back().unwrap()
223e47cc 405 }
970d7e83 406 pub fn advance_right(&mut self) {
85aaf69f 407 self.right += 1;
223e47cc 408 self.right %= self.buf_len;
a7813a04 409 assert!(self.right != self.left);
223e47cc 410 }
c34b1796 411 pub fn advance_left(&mut self) -> io::Result<()> {
d9579d0f 412 debug!("advance_left Vec<{},{}>, sizeof({})={}", self.left, self.right,
85aaf69f
SL
413 self.left, self.size[self.left]);
414
415 let mut left_size = self.size[self.left];
416
417 while left_size >= 0 {
418 let left = self.token[self.left].clone();
419
420 let len = match left {
421 Token::Break(b) => b.blank_space,
422 Token::String(_, len) => {
423 assert_eq!(len, left_size);
424 len
425 }
426 _ => 0
427 };
428
54a0048b 429 self.print(left, left_size)?;
85aaf69f
SL
430
431 self.left_total += len;
432
433 if self.left == self.right {
434 break;
223e47cc 435 }
85aaf69f
SL
436
437 self.left += 1;
438 self.left %= self.buf_len;
439
440 left_size = self.size[self.left];
223e47cc 441 }
85aaf69f
SL
442
443 Ok(())
223e47cc 444 }
85aaf69f 445 pub fn check_stack(&mut self, k: isize) {
a7813a04 446 if !self.scan_stack.is_empty() {
223e47cc 447 let x = self.scan_top();
1a4d82fc 448 match self.token[x] {
85aaf69f 449 Token::Begin(_) => {
1a4d82fc
JJ
450 if k > 0 {
451 let popped = self.scan_pop();
452 self.size[popped] = self.size[x] + self.right_total;
453 self.check_stack(k - 1);
454 }
455 }
85aaf69f 456 Token::End => {
1a4d82fc
JJ
457 // paper says + not =, but that makes no sense.
458 let popped = self.scan_pop();
459 self.size[popped] = 1;
460 self.check_stack(k + 1);
461 }
462 _ => {
463 let popped = self.scan_pop();
464 self.size[popped] = self.size[x] + self.right_total;
465 if k > 0 {
466 self.check_stack(k);
467 }
223e47cc 468 }
223e47cc
LB
469 }
470 }
471 }
c34b1796 472 pub fn print_newline(&mut self, amount: isize) -> io::Result<()> {
1a4d82fc
JJ
473 debug!("NEWLINE {}", amount);
474 let ret = write!(self.out, "\n");
223e47cc
LB
475 self.pending_indentation = 0;
476 self.indent(amount);
a7813a04 477 ret
223e47cc 478 }
85aaf69f 479 pub fn indent(&mut self, amount: isize) {
1a4d82fc 480 debug!("INDENT {}", amount);
223e47cc
LB
481 self.pending_indentation += amount;
482 }
1a4d82fc 483 pub fn get_top(&mut self) -> PrintStackElem {
a7813a04
XL
484 match self.print_stack.last() {
485 Some(el) => *el,
486 None => PrintStackElem {
223e47cc 487 offset: 0,
85aaf69f 488 pbreak: PrintStackBreak::Broken(Breaks::Inconsistent)
223e47cc
LB
489 }
490 }
491 }
c34b1796 492 pub fn print_str(&mut self, s: &str) -> io::Result<()> {
223e47cc 493 while self.pending_indentation > 0 {
54a0048b 494 write!(self.out, " ")?;
223e47cc
LB
495 self.pending_indentation -= 1;
496 }
1a4d82fc 497 write!(self.out, "{}", s)
223e47cc 498 }
c34b1796 499 pub fn print(&mut self, token: Token, l: isize) -> io::Result<()> {
a7813a04 500 debug!("print {} {} (remaining line space={})", token, l,
223e47cc 501 self.space);
85aaf69f
SL
502 debug!("{}", buf_str(&self.token,
503 &self.size,
1a4d82fc
JJ
504 self.left,
505 self.right,
506 6));
85aaf69f
SL
507 match token {
508 Token::Begin(b) => {
1a4d82fc 509 if l > self.space {
223e47cc 510 let col = self.margin - self.space + b.offset;
1a4d82fc
JJ
511 debug!("print Begin -> push broken block at col {}", col);
512 self.print_stack.push(PrintStackElem {
223e47cc 513 offset: col,
85aaf69f 514 pbreak: PrintStackBreak::Broken(b.breaks)
223e47cc
LB
515 });
516 } else {
1a4d82fc
JJ
517 debug!("print Begin -> push fitting block");
518 self.print_stack.push(PrintStackElem {
223e47cc 519 offset: 0,
85aaf69f 520 pbreak: PrintStackBreak::Fits
223e47cc
LB
521 });
522 }
1a4d82fc 523 Ok(())
223e47cc 524 }
85aaf69f 525 Token::End => {
1a4d82fc
JJ
526 debug!("print End -> pop End");
527 let print_stack = &mut self.print_stack;
a7813a04 528 assert!(!print_stack.is_empty());
1a4d82fc
JJ
529 print_stack.pop().unwrap();
530 Ok(())
223e47cc 531 }
85aaf69f 532 Token::Break(b) => {
223e47cc
LB
533 let top = self.get_top();
534 match top.pbreak {
85aaf69f 535 PrintStackBreak::Fits => {
1a4d82fc 536 debug!("print Break({}) in fitting block", b.blank_space);
223e47cc
LB
537 self.space -= b.blank_space;
538 self.indent(b.blank_space);
1a4d82fc 539 Ok(())
223e47cc 540 }
85aaf69f 541 PrintStackBreak::Broken(Breaks::Consistent) => {
1a4d82fc 542 debug!("print Break({}+{}) in consistent block",
223e47cc 543 top.offset, b.offset);
1a4d82fc 544 let ret = self.print_newline(top.offset + b.offset);
223e47cc 545 self.space = self.margin - (top.offset + b.offset);
1a4d82fc 546 ret
223e47cc 547 }
85aaf69f 548 PrintStackBreak::Broken(Breaks::Inconsistent) => {
1a4d82fc
JJ
549 if l > self.space {
550 debug!("print Break({}+{}) w/ newline in inconsistent",
223e47cc 551 top.offset, b.offset);
1a4d82fc 552 let ret = self.print_newline(top.offset + b.offset);
223e47cc 553 self.space = self.margin - (top.offset + b.offset);
1a4d82fc 554 ret
223e47cc 555 } else {
1a4d82fc 556 debug!("print Break({}) w/o newline in inconsistent",
223e47cc
LB
557 b.blank_space);
558 self.indent(b.blank_space);
559 self.space -= b.blank_space;
1a4d82fc 560 Ok(())
223e47cc
LB
561 }
562 }
563 }
564 }
a7813a04 565 Token::String(ref s, len) => {
1a4d82fc
JJ
566 debug!("print String({})", s);
567 assert_eq!(l, len);
568 // assert!(l <= space);
223e47cc 569 self.space -= len;
a7813a04 570 self.print_str(s)
223e47cc 571 }
85aaf69f 572 Token::Eof => {
1a4d82fc
JJ
573 // Eof should never get here.
574 panic!();
223e47cc
LB
575 }
576 }
577 }
578}
579
580// Convenience functions to talk to the printer.
1a4d82fc
JJ
581//
582// "raw box"
c34b1796 583pub fn rbox(p: &mut Printer, indent: usize, b: Breaks) -> io::Result<()> {
85aaf69f
SL
584 p.pretty_print(Token::Begin(BeginToken {
585 offset: indent as isize,
223e47cc 586 breaks: b
1a4d82fc 587 }))
223e47cc
LB
588}
589
c34b1796 590pub fn ibox(p: &mut Printer, indent: usize) -> io::Result<()> {
85aaf69f 591 rbox(p, indent, Breaks::Inconsistent)
1a4d82fc 592}
223e47cc 593
c34b1796 594pub fn cbox(p: &mut Printer, indent: usize) -> io::Result<()> {
85aaf69f 595 rbox(p, indent, Breaks::Consistent)
1a4d82fc 596}
223e47cc 597
c34b1796 598pub fn break_offset(p: &mut Printer, n: usize, off: isize) -> io::Result<()> {
85aaf69f 599 p.pretty_print(Token::Break(BreakToken {
223e47cc 600 offset: off,
85aaf69f 601 blank_space: n as isize
1a4d82fc 602 }))
223e47cc
LB
603}
604
c34b1796 605pub fn end(p: &mut Printer) -> io::Result<()> {
85aaf69f
SL
606 p.pretty_print(Token::End)
607}
223e47cc 608
c34b1796 609pub fn eof(p: &mut Printer) -> io::Result<()> {
85aaf69f
SL
610 p.pretty_print(Token::Eof)
611}
223e47cc 612
c34b1796 613pub fn word(p: &mut Printer, wrd: &str) -> io::Result<()> {
a7813a04 614 p.pretty_print(Token::String(wrd.to_string(), wrd.len() as isize))
223e47cc
LB
615}
616
c34b1796 617pub fn huge_word(p: &mut Printer, wrd: &str) -> io::Result<()> {
a7813a04 618 p.pretty_print(Token::String(wrd.to_string(), SIZE_INFINITY))
223e47cc
LB
619}
620
c34b1796 621pub fn zero_word(p: &mut Printer, wrd: &str) -> io::Result<()> {
a7813a04 622 p.pretty_print(Token::String(wrd.to_string(), 0))
223e47cc
LB
623}
624
c34b1796 625pub fn spaces(p: &mut Printer, n: usize) -> io::Result<()> {
1a4d82fc
JJ
626 break_offset(p, n, 0)
627}
223e47cc 628
c34b1796 629pub fn zerobreak(p: &mut Printer) -> io::Result<()> {
85aaf69f 630 spaces(p, 0)
1a4d82fc 631}
223e47cc 632
c34b1796 633pub fn space(p: &mut Printer) -> io::Result<()> {
85aaf69f 634 spaces(p, 1)
1a4d82fc 635}
223e47cc 636
c34b1796 637pub fn hardbreak(p: &mut Printer) -> io::Result<()> {
85aaf69f 638 spaces(p, SIZE_INFINITY as usize)
1a4d82fc 639}
223e47cc 640
85aaf69f
SL
641pub fn hardbreak_tok_offset(off: isize) -> Token {
642 Token::Break(BreakToken {offset: off, blank_space: SIZE_INFINITY})
223e47cc
LB
643}
644
85aaf69f
SL
645pub fn hardbreak_tok() -> Token {
646 hardbreak_tok_offset(0)
647}