syntax: Don't import the pp.rs enum variants into the namespace

This commit is contained in:
Erick Tryzelaar
2015-01-13 21:12:39 -08:00
parent 170c4399e6
commit 92b48556e0
2 changed files with 82 additions and 70 deletions

View File

@@ -59,10 +59,6 @@
//! line (which it can't) and so naturally place the content on its own line to //! 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. //! avoid combining it with other lines and making matters even worse.
pub use self::PrintStackBreak::*;
pub use self::Breaks::*;
pub use self::Token::*;
use std::io; use std::io;
use std::string; use std::string;
use std::iter::repeat; use std::iter::repeat;
@@ -87,7 +83,7 @@ pub struct BeginToken {
#[derive(Clone)] #[derive(Clone)]
pub enum Token { pub enum Token {
String(string::String, int), String(String, int),
Break(BreakToken), Break(BreakToken),
Begin(BeginToken), Begin(BeginToken),
End, End,
@@ -96,12 +92,15 @@ pub enum Token {
impl Token { impl Token {
pub fn is_eof(&self) -> bool { pub fn is_eof(&self) -> bool {
match *self { Eof => true, _ => false } match *self {
Token::Eof => true,
_ => false,
}
} }
pub fn is_hardbreak_tok(&self) -> bool { pub fn is_hardbreak_tok(&self) -> bool {
match *self { match *self {
Break(BreakToken { Token::Break(BreakToken {
offset: 0, offset: 0,
blank_space: bs blank_space: bs
}) if bs == SIZE_INFINITY => }) if bs == SIZE_INFINITY =>
@@ -112,22 +111,22 @@ impl Token {
} }
} }
pub fn tok_str(t: Token) -> string::String { pub fn tok_str(token: &Token) -> String {
match t { match *token {
String(s, len) => return format!("STR({},{})", s, len), Token::String(ref s, len) => format!("STR({},{})", s, len),
Break(_) => return "BREAK".to_string(), Token::Break(_) => "BREAK".to_string(),
Begin(_) => return "BEGIN".to_string(), Token::Begin(_) => "BEGIN".to_string(),
End => return "END".to_string(), Token::End => "END".to_string(),
Eof => return "EOF".to_string() Token::Eof => "EOF".to_string()
} }
} }
pub fn buf_str(toks: Vec<Token>, pub fn buf_str(toks: &[Token],
szs: Vec<int>, szs: &[int],
left: uint, left: uint,
right: uint, right: uint,
lim: uint) lim: uint)
-> string::String { -> String {
let n = toks.len(); let n = toks.len();
assert_eq!(n, szs.len()); assert_eq!(n, szs.len());
let mut i = left; let mut i = left;
@@ -140,7 +139,7 @@ pub fn buf_str(toks: Vec<Token>,
} }
s.push_str(&format!("{}={}", s.push_str(&format!("{}={}",
szs[i], szs[i],
tok_str(toks[i].clone()))[]); tok_str(&toks[i]))[]);
i += 1u; i += 1u;
i %= n; i %= n;
} }
@@ -167,7 +166,7 @@ pub fn mk_printer(out: Box<io::Writer+'static>, linewidth: uint) -> Printer {
// fall behind. // fall behind.
let n: uint = 3 * linewidth; let n: uint = 3 * linewidth;
debug!("mk_printer {}", linewidth); debug!("mk_printer {}", linewidth);
let token: Vec<Token> = repeat(Eof).take(n).collect(); let token: Vec<Token> = repeat(Token::Eof).take(n).collect();
let size: Vec<int> = repeat(0i).take(n).collect(); let size: Vec<int> = repeat(0i).take(n).collect();
let scan_stack: Vec<uint> = repeat(0u).take(n).collect(); let scan_stack: Vec<uint> = repeat(0u).take(n).collect();
Printer { Printer {
@@ -312,10 +311,10 @@ impl Printer {
pub fn replace_last_token(&mut self, t: Token) { pub fn replace_last_token(&mut self, t: Token) {
self.token[self.right] = t; self.token[self.right] = t;
} }
pub fn pretty_print(&mut self, t: Token) -> io::IoResult<()> { pub fn pretty_print(&mut self, token: Token) -> io::IoResult<()> {
debug!("pp ~[{},{}]", self.left, self.right); debug!("pp ~[{},{}]", self.left, self.right);
match t { match token {
Eof => { Token::Eof => {
if !self.scan_stack_empty { if !self.scan_stack_empty {
self.check_stack(0); self.check_stack(0);
let left = self.token[self.left].clone(); let left = self.token[self.left].clone();
@@ -325,7 +324,7 @@ impl Printer {
self.indent(0); self.indent(0);
Ok(()) Ok(())
} }
Begin(b) => { Token::Begin(b) => {
if self.scan_stack_empty { if self.scan_stack_empty {
self.left_total = 1; self.left_total = 1;
self.right_total = 1; self.right_total = 1;
@@ -334,27 +333,27 @@ impl Printer {
} else { self.advance_right(); } } else { self.advance_right(); }
debug!("pp Begin({})/buffer ~[{},{}]", debug!("pp Begin({})/buffer ~[{},{}]",
b.offset, self.left, self.right); b.offset, self.left, self.right);
self.token[self.right] = t; self.token[self.right] = token;
self.size[self.right] = -self.right_total; self.size[self.right] = -self.right_total;
let right = self.right; let right = self.right;
self.scan_push(right); self.scan_push(right);
Ok(()) Ok(())
} }
End => { Token::End => {
if self.scan_stack_empty { if self.scan_stack_empty {
debug!("pp End/print ~[{},{}]", self.left, self.right); debug!("pp End/print ~[{},{}]", self.left, self.right);
self.print(t, 0) self.print(token, 0)
} else { } else {
debug!("pp End/buffer ~[{},{}]", self.left, self.right); debug!("pp End/buffer ~[{},{}]", self.left, self.right);
self.advance_right(); self.advance_right();
self.token[self.right] = t; self.token[self.right] = token;
self.size[self.right] = -1; self.size[self.right] = -1;
let right = self.right; let right = self.right;
self.scan_push(right); self.scan_push(right);
Ok(()) Ok(())
} }
} }
Break(b) => { Token::Break(b) => {
if self.scan_stack_empty { if self.scan_stack_empty {
self.left_total = 1; self.left_total = 1;
self.right_total = 1; self.right_total = 1;
@@ -366,21 +365,21 @@ impl Printer {
self.check_stack(0); self.check_stack(0);
let right = self.right; let right = self.right;
self.scan_push(right); self.scan_push(right);
self.token[self.right] = t; self.token[self.right] = token;
self.size[self.right] = -self.right_total; self.size[self.right] = -self.right_total;
self.right_total += b.blank_space; self.right_total += b.blank_space;
Ok(()) Ok(())
} }
String(ref s, len) => { Token::String(ref s, len) => {
if self.scan_stack_empty { if self.scan_stack_empty {
debug!("pp String('{}')/print ~[{},{}]", debug!("pp String('{}')/print ~[{},{}]",
*s, self.left, self.right); *s, self.left, self.right);
self.print(t.clone(), len) self.print(token.clone(), len)
} else { } else {
debug!("pp String('{}')/buffer ~[{},{}]", debug!("pp String('{}')/buffer ~[{},{}]",
*s, self.left, self.right); *s, self.left, self.right);
self.advance_right(); self.advance_right();
self.token[self.right] = t.clone(); self.token[self.right] = token.clone();
self.size[self.right] = len; self.size[self.right] = len;
self.right_total += len; self.right_total += len;
self.check_stream() self.check_stream()
@@ -456,8 +455,8 @@ impl Printer {
if l >= 0 { if l >= 0 {
let ret = self.print(x.clone(), l); let ret = self.print(x.clone(), l);
match x { match x {
Break(b) => self.left_total += b.blank_space, Token::Break(b) => self.left_total += b.blank_space,
String(_, len) => { Token::String(_, len) => {
assert_eq!(len, l); self.left_total += len; assert_eq!(len, l); self.left_total += len;
} }
_ => () _ => ()
@@ -478,14 +477,14 @@ impl Printer {
if !self.scan_stack_empty { if !self.scan_stack_empty {
let x = self.scan_top(); let x = self.scan_top();
match self.token[x] { match self.token[x] {
Begin(_) => { Token::Begin(_) => {
if k > 0 { if k > 0 {
let popped = self.scan_pop(); let popped = self.scan_pop();
self.size[popped] = self.size[x] + self.right_total; self.size[popped] = self.size[x] + self.right_total;
self.check_stack(k - 1); self.check_stack(k - 1);
} }
} }
End => { Token::End => {
// paper says + not =, but that makes no sense. // paper says + not =, but that makes no sense.
let popped = self.scan_pop(); let popped = self.scan_pop();
self.size[popped] = 1; self.size[popped] = 1;
@@ -520,7 +519,7 @@ impl Printer {
} else { } else {
PrintStackElem { PrintStackElem {
offset: 0, offset: 0,
pbreak: Broken(Inconsistent) pbreak: PrintStackBreak::Broken(Breaks::Inconsistent)
} }
} }
} }
@@ -531,56 +530,56 @@ impl Printer {
} }
write!(self.out, "{}", s) write!(self.out, "{}", s)
} }
pub fn print(&mut self, x: Token, l: int) -> io::IoResult<()> { pub fn print(&mut self, token: Token, l: int) -> io::IoResult<()> {
debug!("print {} {} (remaining line space={})", tok_str(x.clone()), l, debug!("print {} {} (remaining line space={})", tok_str(&token), l,
self.space); self.space);
debug!("{}", buf_str(self.token.clone(), debug!("{}", buf_str(&self.token[],
self.size.clone(), &self.size[],
self.left, self.left,
self.right, self.right,
6)); 6));
match x { match token {
Begin(b) => { Token::Begin(b) => {
if l > self.space { if l > self.space {
let col = self.margin - self.space + b.offset; let col = self.margin - self.space + b.offset;
debug!("print Begin -> push broken block at col {}", col); debug!("print Begin -> push broken block at col {}", col);
self.print_stack.push(PrintStackElem { self.print_stack.push(PrintStackElem {
offset: col, offset: col,
pbreak: Broken(b.breaks) pbreak: PrintStackBreak::Broken(b.breaks)
}); });
} else { } else {
debug!("print Begin -> push fitting block"); debug!("print Begin -> push fitting block");
self.print_stack.push(PrintStackElem { self.print_stack.push(PrintStackElem {
offset: 0, offset: 0,
pbreak: Fits pbreak: PrintStackBreak::Fits
}); });
} }
Ok(()) Ok(())
} }
End => { Token::End => {
debug!("print End -> pop End"); debug!("print End -> pop End");
let print_stack = &mut self.print_stack; let print_stack = &mut self.print_stack;
assert!((print_stack.len() != 0u)); assert!((print_stack.len() != 0u));
print_stack.pop().unwrap(); print_stack.pop().unwrap();
Ok(()) Ok(())
} }
Break(b) => { Token::Break(b) => {
let top = self.get_top(); let top = self.get_top();
match top.pbreak { match top.pbreak {
Fits => { PrintStackBreak::Fits => {
debug!("print Break({}) in fitting block", b.blank_space); debug!("print Break({}) in fitting block", b.blank_space);
self.space -= b.blank_space; self.space -= b.blank_space;
self.indent(b.blank_space); self.indent(b.blank_space);
Ok(()) Ok(())
} }
Broken(Consistent) => { PrintStackBreak::Broken(Breaks::Consistent) => {
debug!("print Break({}+{}) in consistent block", debug!("print Break({}+{}) in consistent block",
top.offset, b.offset); top.offset, b.offset);
let ret = self.print_newline(top.offset + b.offset); let ret = self.print_newline(top.offset + b.offset);
self.space = self.margin - (top.offset + b.offset); self.space = self.margin - (top.offset + b.offset);
ret ret
} }
Broken(Inconsistent) => { PrintStackBreak::Broken(Breaks::Inconsistent) => {
if l > self.space { if l > self.space {
debug!("print Break({}+{}) w/ newline in inconsistent", debug!("print Break({}+{}) w/ newline in inconsistent",
top.offset, b.offset); top.offset, b.offset);
@@ -597,14 +596,14 @@ impl Printer {
} }
} }
} }
String(s, len) => { Token::String(s, len) => {
debug!("print String({})", s); debug!("print String({})", s);
assert_eq!(l, len); assert_eq!(l, len);
// assert!(l <= space); // assert!(l <= space);
self.space -= len; self.space -= len;
self.print_str(&s[]) self.print_str(&s[])
} }
Eof => { Token::Eof => {
// Eof should never get here. // Eof should never get here.
panic!(); panic!();
} }
@@ -616,41 +615,45 @@ impl Printer {
// //
// "raw box" // "raw box"
pub fn rbox(p: &mut Printer, indent: uint, b: Breaks) -> io::IoResult<()> { pub fn rbox(p: &mut Printer, indent: uint, b: Breaks) -> io::IoResult<()> {
p.pretty_print(Begin(BeginToken { p.pretty_print(Token::Begin(BeginToken {
offset: indent as int, offset: indent as int,
breaks: b breaks: b
})) }))
} }
pub fn ibox(p: &mut Printer, indent: uint) -> io::IoResult<()> { pub fn ibox(p: &mut Printer, indent: uint) -> io::IoResult<()> {
rbox(p, indent, Inconsistent) rbox(p, indent, Breaks::Inconsistent)
} }
pub fn cbox(p: &mut Printer, indent: uint) -> io::IoResult<()> { pub fn cbox(p: &mut Printer, indent: uint) -> io::IoResult<()> {
rbox(p, indent, Consistent) rbox(p, indent, Breaks::Consistent)
} }
pub fn break_offset(p: &mut Printer, n: uint, off: int) -> io::IoResult<()> { pub fn break_offset(p: &mut Printer, n: uint, off: int) -> io::IoResult<()> {
p.pretty_print(Break(BreakToken { p.pretty_print(Token::Break(BreakToken {
offset: off, offset: off,
blank_space: n as int blank_space: n as int
})) }))
} }
pub fn end(p: &mut Printer) -> io::IoResult<()> { p.pretty_print(End) } pub fn end(p: &mut Printer) -> io::IoResult<()> {
p.pretty_print(Token::End)
}
pub fn eof(p: &mut Printer) -> io::IoResult<()> { p.pretty_print(Eof) } pub fn eof(p: &mut Printer) -> io::IoResult<()> {
p.pretty_print(Token::Eof)
}
pub fn word(p: &mut Printer, wrd: &str) -> io::IoResult<()> { pub fn word(p: &mut Printer, wrd: &str) -> io::IoResult<()> {
p.pretty_print(String(/* bad */ wrd.to_string(), wrd.len() as int)) p.pretty_print(Token::String(/* bad */ wrd.to_string(), wrd.len() as int))
} }
pub fn huge_word(p: &mut Printer, wrd: &str) -> io::IoResult<()> { pub fn huge_word(p: &mut Printer, wrd: &str) -> io::IoResult<()> {
p.pretty_print(String(/* bad */ wrd.to_string(), SIZE_INFINITY)) p.pretty_print(Token::String(/* bad */ wrd.to_string(), SIZE_INFINITY))
} }
pub fn zero_word(p: &mut Printer, wrd: &str) -> io::IoResult<()> { pub fn zero_word(p: &mut Printer, wrd: &str) -> io::IoResult<()> {
p.pretty_print(String(/* bad */ wrd.to_string(), 0)) p.pretty_print(Token::String(/* bad */ wrd.to_string(), 0))
} }
pub fn spaces(p: &mut Printer, n: uint) -> io::IoResult<()> { pub fn spaces(p: &mut Printer, n: uint) -> io::IoResult<()> {
@@ -670,7 +673,9 @@ pub fn hardbreak(p: &mut Printer) -> io::IoResult<()> {
} }
pub fn hardbreak_tok_offset(off: int) -> Token { pub fn hardbreak_tok_offset(off: int) -> Token {
Break(BreakToken {offset: off, blank_space: SIZE_INFINITY}) Token::Break(BreakToken {offset: off, blank_space: SIZE_INFINITY})
} }
pub fn hardbreak_tok() -> Token { return hardbreak_tok_offset(0); } pub fn hardbreak_tok() -> Token {
hardbreak_tok_offset(0)
}

View File

@@ -25,7 +25,8 @@ use parse::token::{self, BinOpToken, Token};
use parse::lexer::comments; use parse::lexer::comments;
use parse; use parse;
use print::pp::{self, break_offset, word, space, zerobreak, hardbreak}; use print::pp::{self, break_offset, word, space, zerobreak, hardbreak};
use print::pp::{Breaks, Consistent, Inconsistent, eof}; use print::pp::{Breaks, eof};
use print::pp::Breaks::{Consistent, Inconsistent};
use ptr::P; use ptr::P;
use std::{ascii, mem}; use std::{ascii, mem};
@@ -459,7 +460,7 @@ fn needs_parentheses(expr: &ast::Expr) -> bool {
impl<'a> State<'a> { impl<'a> State<'a> {
pub fn ibox(&mut self, u: uint) -> IoResult<()> { pub fn ibox(&mut self, u: uint) -> IoResult<()> {
self.boxes.push(pp::Inconsistent); self.boxes.push(pp::Breaks::Inconsistent);
pp::ibox(&mut self.s, u) pp::ibox(&mut self.s, u)
} }
@@ -469,7 +470,7 @@ impl<'a> State<'a> {
} }
pub fn cbox(&mut self, u: uint) -> IoResult<()> { pub fn cbox(&mut self, u: uint) -> IoResult<()> {
self.boxes.push(pp::Consistent); self.boxes.push(pp::Breaks::Consistent);
pp::cbox(&mut self.s, u) pp::cbox(&mut self.s, u)
} }
@@ -531,11 +532,17 @@ impl<'a> State<'a> {
} }
pub fn is_begin(&mut self) -> bool { pub fn is_begin(&mut self) -> bool {
match self.s.last_token() { pp::Begin(_) => true, _ => false } match self.s.last_token() {
pp::Token::Begin(_) => true,
_ => false,
}
} }
pub fn is_end(&mut self) -> bool { pub fn is_end(&mut self) -> bool {
match self.s.last_token() { pp::End => true, _ => false } match self.s.last_token() {
pp::Token::End => true,
_ => false,
}
} }
// is this the beginning of a line? // is this the beginning of a line?
@@ -545,7 +552,7 @@ impl<'a> State<'a> {
pub fn in_cbox(&self) -> bool { pub fn in_cbox(&self) -> bool {
match self.boxes.last() { match self.boxes.last() {
Some(&last_box) => last_box == pp::Consistent, Some(&last_box) => last_box == pp::Breaks::Consistent,
None => false None => false
} }
} }
@@ -2881,7 +2888,7 @@ impl<'a> State<'a> {
comments::BlankLine => { comments::BlankLine => {
// We need to do at least one, possibly two hardbreaks. // We need to do at least one, possibly two hardbreaks.
let is_semi = match self.s.last_token() { let is_semi = match self.s.last_token() {
pp::String(s, _) => ";" == s, pp::Token::String(s, _) => ";" == s,
_ => false _ => false
}; };
if is_semi || self.is_begin() || self.is_end() { if is_semi || self.is_begin() || self.is_end() {