Skip to content

Commit 3eef083

Browse files
committed
libsyntax/pp: minor modernizations
* implement Display on Token instead of custom tok_str() fn * use expression returns * remove redundant parens in asserts * remove "/* bad */" comments that appear to be related to early changes in memory management * and a few individual idiomatic changes
1 parent 2a815a2 commit 3eef083

File tree

1 file changed

+32
-40
lines changed

1 file changed

+32
-40
lines changed

src/libsyntax/print/pp.rs

Lines changed: 32 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@
6161
//! line (which it can't) and so naturally place the content on its own line to
6262
//! avoid combining it with other lines and making matters even worse.
6363
64+
use std::fmt;
6465
use std::io;
65-
use std::string;
6666

6767
#[derive(Clone, Copy, PartialEq)]
6868
pub enum Breaks {
@@ -112,35 +112,30 @@ impl Token {
112112
}
113113
}
114114

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()
115+
impl fmt::Display for Token {
116+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
117+
match *self {
118+
Token::String(ref s, len) => write!(f, "STR({},{})", s, len),
119+
Token::Break(_) => f.write_str("BREAK"),
120+
Token::Begin(_) => f.write_str("BEGIN"),
121+
Token::End => f.write_str("END"),
122+
Token::Eof => f.write_str("EOF"),
123+
}
122124
}
123125
}
124126

125-
pub fn buf_str(toks: &[Token],
126-
szs: &[isize],
127-
left: usize,
128-
right: usize,
129-
lim: usize)
130-
-> String {
127+
fn buf_str(toks: &[Token], szs: &[isize], left: usize, right: usize, lim: usize) -> String {
131128
let n = toks.len();
132129
assert_eq!(n, szs.len());
133130
let mut i = left;
134131
let mut l = lim;
135-
let mut s = string::String::from("[");
132+
let mut s = String::from("[");
136133
while i != right && l != 0 {
137134
l -= 1;
138135
if i != left {
139136
s.push_str(", ");
140137
}
141-
s.push_str(&format!("{}={}",
142-
szs[i],
143-
tok_str(&toks[i])));
138+
s.push_str(&format!("{}={}", szs[i], &toks[i]));
144139
i += 1;
145140
i %= n;
146141
}
@@ -413,38 +408,38 @@ impl<'a> Printer<'a> {
413408
} else {
414409
self.top += 1;
415410
self.top %= self.buf_len;
416-
assert!((self.top != self.bottom));
411+
assert!(self.top != self.bottom);
417412
}
418413
self.scan_stack[self.top] = x;
419414
}
420415
pub fn scan_pop(&mut self) -> usize {
421-
assert!((!self.scan_stack_empty));
416+
assert!(!self.scan_stack_empty);
422417
let x = self.scan_stack[self.top];
423418
if self.top == self.bottom {
424419
self.scan_stack_empty = true;
425420
} else {
426421
self.top += self.buf_len - 1; self.top %= self.buf_len;
427422
}
428-
return x;
423+
x
429424
}
430425
pub fn scan_top(&mut self) -> usize {
431-
assert!((!self.scan_stack_empty));
432-
return self.scan_stack[self.top];
426+
assert!(!self.scan_stack_empty);
427+
self.scan_stack[self.top]
433428
}
434429
pub fn scan_pop_bottom(&mut self) -> usize {
435-
assert!((!self.scan_stack_empty));
430+
assert!(!self.scan_stack_empty);
436431
let x = self.scan_stack[self.bottom];
437432
if self.top == self.bottom {
438433
self.scan_stack_empty = true;
439434
} else {
440435
self.bottom += 1; self.bottom %= self.buf_len;
441436
}
442-
return x;
437+
x
443438
}
444439
pub fn advance_right(&mut self) {
445440
self.right += 1;
446441
self.right %= self.buf_len;
447-
assert!((self.right != self.left));
442+
assert!(self.right != self.left);
448443
}
449444
pub fn advance_left(&mut self) -> io::Result<()> {
450445
debug!("advance_left Vec<{},{}>, sizeof({})={}", self.left, self.right,
@@ -512,19 +507,16 @@ impl<'a> Printer<'a> {
512507
let ret = write!(self.out, "\n");
513508
self.pending_indentation = 0;
514509
self.indent(amount);
515-
return ret;
510+
ret
516511
}
517512
pub fn indent(&mut self, amount: isize) {
518513
debug!("INDENT {}", amount);
519514
self.pending_indentation += amount;
520515
}
521516
pub fn get_top(&mut self) -> PrintStackElem {
522-
let print_stack = &mut self.print_stack;
523-
let n = print_stack.len();
524-
if n != 0 {
525-
(*print_stack)[n - 1]
526-
} else {
527-
PrintStackElem {
517+
match self.print_stack.last() {
518+
Some(el) => *el,
519+
None => PrintStackElem {
528520
offset: 0,
529521
pbreak: PrintStackBreak::Broken(Breaks::Inconsistent)
530522
}
@@ -538,7 +530,7 @@ impl<'a> Printer<'a> {
538530
write!(self.out, "{}", s)
539531
}
540532
pub fn print(&mut self, token: Token, l: isize) -> io::Result<()> {
541-
debug!("print {} {} (remaining line space={})", tok_str(&token), l,
533+
debug!("print {} {} (remaining line space={})", token, l,
542534
self.space);
543535
debug!("{}", buf_str(&self.token,
544536
&self.size,
@@ -566,7 +558,7 @@ impl<'a> Printer<'a> {
566558
Token::End => {
567559
debug!("print End -> pop End");
568560
let print_stack = &mut self.print_stack;
569-
assert!((!print_stack.is_empty()));
561+
assert!(!print_stack.is_empty());
570562
print_stack.pop().unwrap();
571563
Ok(())
572564
}
@@ -603,12 +595,12 @@ impl<'a> Printer<'a> {
603595
}
604596
}
605597
}
606-
Token::String(s, len) => {
598+
Token::String(ref s, len) => {
607599
debug!("print String({})", s);
608600
assert_eq!(l, len);
609601
// assert!(l <= space);
610602
self.space -= len;
611-
self.print_str(&s[..])
603+
self.print_str(s)
612604
}
613605
Token::Eof => {
614606
// Eof should never get here.
@@ -652,15 +644,15 @@ pub fn eof(p: &mut Printer) -> io::Result<()> {
652644
}
653645

654646
pub fn word(p: &mut Printer, wrd: &str) -> io::Result<()> {
655-
p.pretty_print(Token::String(/* bad */ wrd.to_string(), wrd.len() as isize))
647+
p.pretty_print(Token::String(wrd.to_string(), wrd.len() as isize))
656648
}
657649

658650
pub fn huge_word(p: &mut Printer, wrd: &str) -> io::Result<()> {
659-
p.pretty_print(Token::String(/* bad */ wrd.to_string(), SIZE_INFINITY))
651+
p.pretty_print(Token::String(wrd.to_string(), SIZE_INFINITY))
660652
}
661653

662654
pub fn zero_word(p: &mut Printer, wrd: &str) -> io::Result<()> {
663-
p.pretty_print(Token::String(/* bad */ wrd.to_string(), 0))
655+
p.pretty_print(Token::String(wrd.to_string(), 0))
664656
}
665657

666658
pub fn spaces(p: &mut Printer, n: usize) -> io::Result<()> {

0 commit comments

Comments
 (0)