Skip to content

Commit 64cd645

Browse files
committed
Split up pretty_print and print.
`pretty_print` takes a `Token` and `match`es on it. But the particular `Token` kind is known at each call site, so this commit splits it into five functions: `pretty_print_eof`, `pretty_print_begin`, etc. This commit also does likewise with `print`, though there is one callsite for `print` where the `Token` kind isn't known, so a generic `print` has to stay (but it now just calls out to the various `print_*` functions).
1 parent 787959c commit 64cd645

File tree

1 file changed

+152
-150
lines changed

1 file changed

+152
-150
lines changed

src/libsyntax/print/pp.rs

+152-150
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,9 @@
140140
//! calculation, SCAN will write "infinity" to the size and let PRINT consume
141141
//! it.
142142
//!
143-
//! In this implementation (following the paper, again) the SCAN process is
144-
//! the method called `Printer::pretty_print`, and the 'PRINT' process is the method
145-
//! called `Printer::print`.
143+
//! In this implementation (following the paper, again) the SCAN process is the
144+
//! methods called `Printer::pretty_print_*`, and the 'PRINT' process is the
145+
//! method called `Printer::print`.
146146
147147
use std::collections::VecDeque;
148148
use std::fmt;
@@ -319,78 +319,77 @@ impl<'a> Printer<'a> {
319319
self.buf[self.right].token = t;
320320
}
321321

322-
pub fn pretty_print(&mut self, token: Token) -> io::Result<()> {
323-
debug!("pp Vec<{},{}>", self.left, self.right);
324-
match token {
325-
Token::Eof => {
326-
if !self.scan_stack.is_empty() {
327-
self.check_stack(0);
328-
self.advance_left()?;
329-
}
330-
self.indent(0);
331-
Ok(())
332-
}
333-
Token::Begin(b) => {
334-
if self.scan_stack.is_empty() {
335-
self.left_total = 1;
336-
self.right_total = 1;
337-
self.left = 0;
338-
self.right = 0;
339-
} else {
340-
self.advance_right();
341-
}
342-
debug!("pp Begin({})/buffer Vec<{},{}>",
343-
b.offset, self.left, self.right);
344-
self.buf[self.right] = BufEntry { token: token, size: -self.right_total };
345-
let right = self.right;
346-
self.scan_push(right);
347-
Ok(())
348-
}
349-
Token::End => {
350-
if self.scan_stack.is_empty() {
351-
debug!("pp End/print Vec<{},{}>", self.left, self.right);
352-
self.print(token, 0)
353-
} else {
354-
debug!("pp End/buffer Vec<{},{}>", self.left, self.right);
355-
self.advance_right();
356-
self.buf[self.right] = BufEntry { token: token, size: -1 };
357-
let right = self.right;
358-
self.scan_push(right);
359-
Ok(())
360-
}
361-
}
362-
Token::Break(b) => {
363-
if self.scan_stack.is_empty() {
364-
self.left_total = 1;
365-
self.right_total = 1;
366-
self.left = 0;
367-
self.right = 0;
368-
} else {
369-
self.advance_right();
370-
}
371-
debug!("pp Break({})/buffer Vec<{},{}>",
372-
b.offset, self.left, self.right);
373-
self.check_stack(0);
374-
let right = self.right;
375-
self.scan_push(right);
376-
self.buf[self.right] = BufEntry { token: token, size: -self.right_total };
377-
self.right_total += b.blank_space;
378-
Ok(())
379-
}
380-
Token::String(s, len) => {
381-
if self.scan_stack.is_empty() {
382-
debug!("pp String('{}')/print Vec<{},{}>",
383-
s, self.left, self.right);
384-
self.print(Token::String(s, len), len)
385-
} else {
386-
debug!("pp String('{}')/buffer Vec<{},{}>",
387-
s, self.left, self.right);
388-
self.advance_right();
389-
self.buf[self.right] = BufEntry { token: Token::String(s, len), size: len };
390-
self.right_total += len;
391-
self.check_stream()
392-
}
393-
}
322+
fn pretty_print_eof(&mut self) -> io::Result<()> {
323+
if !self.scan_stack.is_empty() {
324+
self.check_stack(0);
325+
self.advance_left()?;
326+
}
327+
self.indent(0);
328+
Ok(())
329+
}
330+
331+
fn pretty_print_begin(&mut self, b: BeginToken) -> io::Result<()> {
332+
if self.scan_stack.is_empty() {
333+
self.left_total = 1;
334+
self.right_total = 1;
335+
self.left = 0;
336+
self.right = 0;
337+
} else {
338+
self.advance_right();
339+
}
340+
debug!("pp Begin({})/buffer Vec<{},{}>",
341+
b.offset, self.left, self.right);
342+
self.buf[self.right] = BufEntry { token: Token::Begin(b), size: -self.right_total };
343+
let right = self.right;
344+
self.scan_push(right);
345+
Ok(())
346+
}
347+
348+
fn pretty_print_end(&mut self) -> io::Result<()> {
349+
if self.scan_stack.is_empty() {
350+
debug!("pp End/print Vec<{},{}>", self.left, self.right);
351+
self.print_end()
352+
} else {
353+
debug!("pp End/buffer Vec<{},{}>", self.left, self.right);
354+
self.advance_right();
355+
self.buf[self.right] = BufEntry { token: Token::End, size: -1 };
356+
let right = self.right;
357+
self.scan_push(right);
358+
Ok(())
359+
}
360+
}
361+
362+
fn pretty_print_break(&mut self, b: BreakToken) -> io::Result<()> {
363+
if self.scan_stack.is_empty() {
364+
self.left_total = 1;
365+
self.right_total = 1;
366+
self.left = 0;
367+
self.right = 0;
368+
} else {
369+
self.advance_right();
370+
}
371+
debug!("pp Break({})/buffer Vec<{},{}>",
372+
b.offset, self.left, self.right);
373+
self.check_stack(0);
374+
let right = self.right;
375+
self.scan_push(right);
376+
self.buf[self.right] = BufEntry { token: Token::Break(b), size: -self.right_total };
377+
self.right_total += b.blank_space;
378+
Ok(())
379+
}
380+
381+
fn pretty_print_string(&mut self, s: Cow<'static, str>, len: isize) -> io::Result<()> {
382+
if self.scan_stack.is_empty() {
383+
debug!("pp String('{}')/print Vec<{},{}>",
384+
s, self.left, self.right);
385+
self.print_string(s, len)
386+
} else {
387+
debug!("pp String('{}')/buffer Vec<{},{}>",
388+
s, self.left, self.right);
389+
self.advance_right();
390+
self.buf[self.right] = BufEntry { token: Token::String(s, len), size: len };
391+
self.right_total += len;
392+
self.check_stream()
394393
}
395394
}
396395

@@ -526,7 +525,70 @@ impl<'a> Printer<'a> {
526525
}
527526
}
528527

529-
pub fn print_str(&mut self, s: &str) -> io::Result<()> {
528+
pub fn print_begin(&mut self, b: BeginToken, l: isize) -> io::Result<()> {
529+
if l > self.space {
530+
let col = self.margin - self.space + b.offset;
531+
debug!("print Begin -> push broken block at col {}", col);
532+
self.print_stack.push(PrintStackElem {
533+
offset: col,
534+
pbreak: PrintStackBreak::Broken(b.breaks)
535+
});
536+
} else {
537+
debug!("print Begin -> push fitting block");
538+
self.print_stack.push(PrintStackElem {
539+
offset: 0,
540+
pbreak: PrintStackBreak::Fits
541+
});
542+
}
543+
Ok(())
544+
}
545+
546+
pub fn print_end(&mut self) -> io::Result<()> {
547+
debug!("print End -> pop End");
548+
let print_stack = &mut self.print_stack;
549+
assert!(!print_stack.is_empty());
550+
print_stack.pop().unwrap();
551+
Ok(())
552+
}
553+
554+
pub fn print_break(&mut self, b: BreakToken, l: isize) -> io::Result<()> {
555+
let top = self.get_top();
556+
match top.pbreak {
557+
PrintStackBreak::Fits => {
558+
debug!("print Break({}) in fitting block", b.blank_space);
559+
self.space -= b.blank_space;
560+
self.indent(b.blank_space);
561+
Ok(())
562+
}
563+
PrintStackBreak::Broken(Breaks::Consistent) => {
564+
debug!("print Break({}+{}) in consistent block",
565+
top.offset, b.offset);
566+
let ret = self.print_newline(top.offset + b.offset);
567+
self.space = self.margin - (top.offset + b.offset);
568+
ret
569+
}
570+
PrintStackBreak::Broken(Breaks::Inconsistent) => {
571+
if l > self.space {
572+
debug!("print Break({}+{}) w/ newline in inconsistent",
573+
top.offset, b.offset);
574+
let ret = self.print_newline(top.offset + b.offset);
575+
self.space = self.margin - (top.offset + b.offset);
576+
ret
577+
} else {
578+
debug!("print Break({}) w/o newline in inconsistent",
579+
b.blank_space);
580+
self.indent(b.blank_space);
581+
self.space -= b.blank_space;
582+
Ok(())
583+
}
584+
}
585+
}
586+
}
587+
588+
pub fn print_string(&mut self, s: Cow<'static, str>, len: isize) -> io::Result<()> {
589+
debug!("print String({})", s);
590+
// assert!(len <= space);
591+
self.space -= len;
530592
while self.pending_indentation > 0 {
531593
write!(self.out, " ")?;
532594
self.pending_indentation -= 1;
@@ -542,85 +604,25 @@ impl<'a> Printer<'a> {
542604
self.right,
543605
6));
544606
match token {
545-
Token::Begin(b) => {
546-
if l > self.space {
547-
let col = self.margin - self.space + b.offset;
548-
debug!("print Begin -> push broken block at col {}", col);
549-
self.print_stack.push(PrintStackElem {
550-
offset: col,
551-
pbreak: PrintStackBreak::Broken(b.breaks)
552-
});
553-
} else {
554-
debug!("print Begin -> push fitting block");
555-
self.print_stack.push(PrintStackElem {
556-
offset: 0,
557-
pbreak: PrintStackBreak::Fits
558-
});
559-
}
560-
Ok(())
561-
}
562-
Token::End => {
563-
debug!("print End -> pop End");
564-
let print_stack = &mut self.print_stack;
565-
assert!(!print_stack.is_empty());
566-
print_stack.pop().unwrap();
567-
Ok(())
568-
}
569-
Token::Break(b) => {
570-
let top = self.get_top();
571-
match top.pbreak {
572-
PrintStackBreak::Fits => {
573-
debug!("print Break({}) in fitting block", b.blank_space);
574-
self.space -= b.blank_space;
575-
self.indent(b.blank_space);
576-
Ok(())
577-
}
578-
PrintStackBreak::Broken(Breaks::Consistent) => {
579-
debug!("print Break({}+{}) in consistent block",
580-
top.offset, b.offset);
581-
let ret = self.print_newline(top.offset + b.offset);
582-
self.space = self.margin - (top.offset + b.offset);
583-
ret
584-
}
585-
PrintStackBreak::Broken(Breaks::Inconsistent) => {
586-
if l > self.space {
587-
debug!("print Break({}+{}) w/ newline in inconsistent",
588-
top.offset, b.offset);
589-
let ret = self.print_newline(top.offset + b.offset);
590-
self.space = self.margin - (top.offset + b.offset);
591-
ret
592-
} else {
593-
debug!("print Break({}) w/o newline in inconsistent",
594-
b.blank_space);
595-
self.indent(b.blank_space);
596-
self.space -= b.blank_space;
597-
Ok(())
598-
}
599-
}
600-
}
601-
}
602-
Token::String(ref s, len) => {
603-
debug!("print String({})", s);
604-
assert_eq!(l, len);
605-
// assert!(l <= space);
606-
self.space -= len;
607-
self.print_str(s)
608-
}
609-
Token::Eof => {
610-
// Eof should never get here.
611-
panic!();
607+
Token::Begin(b) => self.print_begin(b, l),
608+
Token::End => self.print_end(),
609+
Token::Break(b) => self.print_break(b, l),
610+
Token::String(s, len) => {
611+
assert_eq!(len, l);
612+
self.print_string(s, len)
612613
}
614+
Token::Eof => panic!(), // Eof should never get here.
613615
}
614616
}
615617

616618
// Convenience functions to talk to the printer.
617619

618620
/// "raw box"
619621
pub fn rbox(&mut self, indent: usize, b: Breaks) -> io::Result<()> {
620-
self.pretty_print(Token::Begin(BeginToken {
622+
self.pretty_print_begin(BeginToken {
621623
offset: indent as isize,
622624
breaks: b
623-
}))
625+
})
624626
}
625627

626628
/// Inconsistent breaking box
@@ -634,24 +636,24 @@ impl<'a> Printer<'a> {
634636
}
635637

636638
pub fn break_offset(&mut self, n: usize, off: isize) -> io::Result<()> {
637-
self.pretty_print(Token::Break(BreakToken {
639+
self.pretty_print_break(BreakToken {
638640
offset: off,
639641
blank_space: n as isize
640-
}))
642+
})
641643
}
642644

643645
pub fn end(&mut self) -> io::Result<()> {
644-
self.pretty_print(Token::End)
646+
self.pretty_print_end()
645647
}
646648

647649
pub fn eof(&mut self) -> io::Result<()> {
648-
self.pretty_print(Token::Eof)
650+
self.pretty_print_eof()
649651
}
650652

651653
pub fn word<S: Into<Cow<'static, str>>>(&mut self, wrd: S) -> io::Result<()> {
652654
let s = wrd.into();
653655
let len = s.len() as isize;
654-
self.pretty_print(Token::String(s, len))
656+
self.pretty_print_string(s, len)
655657
}
656658

657659
fn spaces(&mut self, n: usize) -> io::Result<()> {

0 commit comments

Comments
 (0)