syntax: Remove io_error usage
This commit is contained in:
parent
ef00c6a278
commit
b211b00d21
7 changed files with 1340 additions and 1037 deletions
|
@ -189,58 +189,61 @@ impl Level {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn print_maybe_styled(msg: &str, color: term::attr::Attr) {
|
fn print_maybe_styled(msg: &str, color: term::attr::Attr) -> io::IoResult<()> {
|
||||||
local_data_key!(tls_terminal: ~Option<term::Terminal<StdWriter>>)
|
local_data_key!(tls_terminal: Option<term::Terminal<StdWriter>>)
|
||||||
|
|
||||||
|
|
||||||
fn is_stderr_screen() -> bool {
|
fn is_stderr_screen() -> bool {
|
||||||
use std::libc;
|
use std::libc;
|
||||||
unsafe { libc::isatty(libc::STDERR_FILENO) != 0 }
|
unsafe { libc::isatty(libc::STDERR_FILENO) != 0 }
|
||||||
}
|
}
|
||||||
fn write_pretty<T: Writer>(term: &mut term::Terminal<T>, s: &str, c: term::attr::Attr) {
|
fn write_pretty<T: Writer>(term: &mut term::Terminal<T>, s: &str,
|
||||||
term.attr(c);
|
c: term::attr::Attr) -> io::IoResult<()> {
|
||||||
term.write(s.as_bytes());
|
if_ok!(term.attr(c));
|
||||||
term.reset();
|
if_ok!(term.write(s.as_bytes()));
|
||||||
|
if_ok!(term.reset());
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
if is_stderr_screen() {
|
if is_stderr_screen() {
|
||||||
local_data::get_mut(tls_terminal, |term| {
|
local_data::get_mut(tls_terminal, |term| {
|
||||||
match term {
|
match term {
|
||||||
Some(term) => {
|
Some(term) => {
|
||||||
match **term {
|
match *term {
|
||||||
Some(ref mut term) => write_pretty(term, msg, color),
|
Some(ref mut term) => write_pretty(term, msg, color),
|
||||||
None => io::stderr().write(msg.as_bytes())
|
None => io::stderr().write(msg.as_bytes())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
None => {
|
None => {
|
||||||
let t = ~match term::Terminal::new(io::stderr()) {
|
let (t, ret) = match term::Terminal::new(io::stderr()) {
|
||||||
Ok(mut term) => {
|
Ok(mut term) => {
|
||||||
write_pretty(&mut term, msg, color);
|
let r = write_pretty(&mut term, msg, color);
|
||||||
Some(term)
|
(Some(term), r)
|
||||||
}
|
}
|
||||||
Err(_) => {
|
Err(_) => {
|
||||||
io::stderr().write(msg.as_bytes());
|
(None, io::stderr().write(msg.as_bytes()))
|
||||||
None
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
local_data::set(tls_terminal, t);
|
local_data::set(tls_terminal, t);
|
||||||
|
ret
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
})
|
||||||
} else {
|
} else {
|
||||||
io::stderr().write(msg.as_bytes());
|
io::stderr().write(msg.as_bytes())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn print_diagnostic(topic: &str, lvl: Level, msg: &str) {
|
fn print_diagnostic(topic: &str, lvl: Level, msg: &str) -> io::IoResult<()> {
|
||||||
let mut stderr = io::stderr();
|
|
||||||
|
|
||||||
if !topic.is_empty() {
|
if !topic.is_empty() {
|
||||||
write!(&mut stderr as &mut io::Writer, "{} ", topic);
|
let mut stderr = io::stderr();
|
||||||
|
if_ok!(write!(&mut stderr as &mut io::Writer, "{} ", topic));
|
||||||
}
|
}
|
||||||
|
|
||||||
print_maybe_styled(format!("{}: ", lvl.to_str()),
|
if_ok!(print_maybe_styled(format!("{}: ", lvl.to_str()),
|
||||||
term::attr::ForegroundColor(lvl.color()));
|
term::attr::ForegroundColor(lvl.color())));
|
||||||
print_maybe_styled(format!("{}\n", msg), term::attr::Bold);
|
if_ok!(print_maybe_styled(format!("{}\n", msg), term::attr::Bold));
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct DefaultEmitter;
|
pub struct DefaultEmitter;
|
||||||
|
@ -250,20 +253,28 @@ impl Emitter for DefaultEmitter {
|
||||||
cmsp: Option<(&codemap::CodeMap, Span)>,
|
cmsp: Option<(&codemap::CodeMap, Span)>,
|
||||||
msg: &str,
|
msg: &str,
|
||||||
lvl: Level) {
|
lvl: Level) {
|
||||||
match cmsp {
|
let error = match cmsp {
|
||||||
Some((cm, sp)) => emit(cm, sp, msg, lvl, false),
|
Some((cm, sp)) => emit(cm, sp, msg, lvl, false),
|
||||||
None => print_diagnostic("", lvl, msg),
|
None => print_diagnostic("", lvl, msg),
|
||||||
|
};
|
||||||
|
|
||||||
|
match error {
|
||||||
|
Ok(()) => {}
|
||||||
|
Err(e) => fail!("failed to print diagnostics: {}", e),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn custom_emit(&self, cm: &codemap::CodeMap,
|
fn custom_emit(&self, cm: &codemap::CodeMap,
|
||||||
sp: Span, msg: &str, lvl: Level) {
|
sp: Span, msg: &str, lvl: Level) {
|
||||||
emit(cm, sp, msg, lvl, true);
|
match emit(cm, sp, msg, lvl, true) {
|
||||||
|
Ok(()) => {}
|
||||||
|
Err(e) => fail!("failed to print diagnostics: {}", e),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn emit(cm: &codemap::CodeMap, sp: Span,
|
fn emit(cm: &codemap::CodeMap, sp: Span,
|
||||||
msg: &str, lvl: Level, custom: bool) {
|
msg: &str, lvl: Level, custom: bool) -> io::IoResult<()> {
|
||||||
let ss = cm.span_to_str(sp);
|
let ss = cm.span_to_str(sp);
|
||||||
let lines = cm.span_to_lines(sp);
|
let lines = cm.span_to_lines(sp);
|
||||||
if custom {
|
if custom {
|
||||||
|
@ -272,19 +283,19 @@ fn emit(cm: &codemap::CodeMap, sp: Span,
|
||||||
// the span)
|
// the span)
|
||||||
let span_end = Span { lo: sp.hi, hi: sp.hi, expn_info: sp.expn_info};
|
let span_end = Span { lo: sp.hi, hi: sp.hi, expn_info: sp.expn_info};
|
||||||
let ses = cm.span_to_str(span_end);
|
let ses = cm.span_to_str(span_end);
|
||||||
print_diagnostic(ses, lvl, msg);
|
if_ok!(print_diagnostic(ses, lvl, msg));
|
||||||
custom_highlight_lines(cm, sp, lvl, lines);
|
if_ok!(custom_highlight_lines(cm, sp, lvl, lines));
|
||||||
} else {
|
} else {
|
||||||
print_diagnostic(ss, lvl, msg);
|
if_ok!(print_diagnostic(ss, lvl, msg));
|
||||||
highlight_lines(cm, sp, lvl, lines);
|
if_ok!(highlight_lines(cm, sp, lvl, lines));
|
||||||
}
|
}
|
||||||
print_macro_backtrace(cm, sp);
|
print_macro_backtrace(cm, sp)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn highlight_lines(cm: &codemap::CodeMap,
|
fn highlight_lines(cm: &codemap::CodeMap,
|
||||||
sp: Span,
|
sp: Span,
|
||||||
lvl: Level,
|
lvl: Level,
|
||||||
lines: &codemap::FileLines) {
|
lines: &codemap::FileLines) -> io::IoResult<()> {
|
||||||
let fm = lines.file;
|
let fm = lines.file;
|
||||||
let mut err = io::stderr();
|
let mut err = io::stderr();
|
||||||
let err = &mut err as &mut io::Writer;
|
let err = &mut err as &mut io::Writer;
|
||||||
|
@ -297,12 +308,13 @@ fn highlight_lines(cm: &codemap::CodeMap,
|
||||||
}
|
}
|
||||||
// Print the offending lines
|
// Print the offending lines
|
||||||
for line in display_lines.iter() {
|
for line in display_lines.iter() {
|
||||||
write!(err, "{}:{} {}\n", fm.name, *line + 1, fm.get_line(*line as int));
|
if_ok!(write!(err, "{}:{} {}\n", fm.name, *line + 1,
|
||||||
|
fm.get_line(*line as int)));
|
||||||
}
|
}
|
||||||
if elided {
|
if elided {
|
||||||
let last_line = display_lines[display_lines.len() - 1u];
|
let last_line = display_lines[display_lines.len() - 1u];
|
||||||
let s = format!("{}:{} ", fm.name, last_line + 1u);
|
let s = format!("{}:{} ", fm.name, last_line + 1u);
|
||||||
write!(err, "{0:1$}...\n", "", s.len());
|
if_ok!(write!(err, "{0:1$}...\n", "", s.len()));
|
||||||
}
|
}
|
||||||
|
|
||||||
// FIXME (#3260)
|
// FIXME (#3260)
|
||||||
|
@ -334,7 +346,7 @@ fn highlight_lines(cm: &codemap::CodeMap,
|
||||||
_ => s.push_char(' '),
|
_ => s.push_char(' '),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
write!(err, "{}", s);
|
if_ok!(write!(err, "{}", s));
|
||||||
let mut s = ~"^";
|
let mut s = ~"^";
|
||||||
let hi = cm.lookup_char_pos(sp.hi);
|
let hi = cm.lookup_char_pos(sp.hi);
|
||||||
if hi.col != lo.col {
|
if hi.col != lo.col {
|
||||||
|
@ -342,8 +354,10 @@ fn highlight_lines(cm: &codemap::CodeMap,
|
||||||
let num_squigglies = hi.col.to_uint()-lo.col.to_uint()-1u;
|
let num_squigglies = hi.col.to_uint()-lo.col.to_uint()-1u;
|
||||||
for _ in range(0, num_squigglies) { s.push_char('~'); }
|
for _ in range(0, num_squigglies) { s.push_char('~'); }
|
||||||
}
|
}
|
||||||
print_maybe_styled(s + "\n", term::attr::ForegroundColor(lvl.color()));
|
if_ok!(print_maybe_styled(s + "\n",
|
||||||
|
term::attr::ForegroundColor(lvl.color())));
|
||||||
}
|
}
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
// Here are the differences between this and the normal `highlight_lines`:
|
// Here are the differences between this and the normal `highlight_lines`:
|
||||||
|
@ -355,23 +369,23 @@ fn highlight_lines(cm: &codemap::CodeMap,
|
||||||
fn custom_highlight_lines(cm: &codemap::CodeMap,
|
fn custom_highlight_lines(cm: &codemap::CodeMap,
|
||||||
sp: Span,
|
sp: Span,
|
||||||
lvl: Level,
|
lvl: Level,
|
||||||
lines: &codemap::FileLines) {
|
lines: &codemap::FileLines) -> io::IoResult<()> {
|
||||||
let fm = lines.file;
|
let fm = lines.file;
|
||||||
let mut err = io::stderr();
|
let mut err = io::stderr();
|
||||||
let err = &mut err as &mut io::Writer;
|
let err = &mut err as &mut io::Writer;
|
||||||
|
|
||||||
let lines = lines.lines.as_slice();
|
let lines = lines.lines.as_slice();
|
||||||
if lines.len() > MAX_LINES {
|
if lines.len() > MAX_LINES {
|
||||||
write!(err, "{}:{} {}\n", fm.name,
|
if_ok!(write!(err, "{}:{} {}\n", fm.name,
|
||||||
lines[0] + 1, fm.get_line(lines[0] as int));
|
lines[0] + 1, fm.get_line(lines[0] as int)));
|
||||||
write!(err, "...\n");
|
if_ok!(write!(err, "...\n"));
|
||||||
let last_line = lines[lines.len()-1];
|
let last_line = lines[lines.len()-1];
|
||||||
write!(err, "{}:{} {}\n", fm.name,
|
if_ok!(write!(err, "{}:{} {}\n", fm.name,
|
||||||
last_line + 1, fm.get_line(last_line as int));
|
last_line + 1, fm.get_line(last_line as int)));
|
||||||
} else {
|
} else {
|
||||||
for line in lines.iter() {
|
for line in lines.iter() {
|
||||||
write!(err, "{}:{} {}\n", fm.name,
|
if_ok!(write!(err, "{}:{} {}\n", fm.name,
|
||||||
*line + 1, fm.get_line(*line as int));
|
*line + 1, fm.get_line(*line as int)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let last_line_start = format!("{}:{} ", fm.name, lines[lines.len()-1]+1);
|
let last_line_start = format!("{}:{} ", fm.name, lines[lines.len()-1]+1);
|
||||||
|
@ -381,22 +395,24 @@ fn custom_highlight_lines(cm: &codemap::CodeMap,
|
||||||
let mut s = ~"";
|
let mut s = ~"";
|
||||||
for _ in range(0, skip) { s.push_char(' '); }
|
for _ in range(0, skip) { s.push_char(' '); }
|
||||||
s.push_char('^');
|
s.push_char('^');
|
||||||
print_maybe_styled(s + "\n", term::attr::ForegroundColor(lvl.color()));
|
print_maybe_styled(s + "\n", term::attr::ForegroundColor(lvl.color()))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn print_macro_backtrace(cm: &codemap::CodeMap, sp: Span) {
|
fn print_macro_backtrace(cm: &codemap::CodeMap, sp: Span) -> io::IoResult<()> {
|
||||||
for ei in sp.expn_info.iter() {
|
for ei in sp.expn_info.iter() {
|
||||||
let ss = ei.callee.span.as_ref().map_or(~"", |span| cm.span_to_str(*span));
|
let ss = ei.callee.span.as_ref().map_or(~"", |span| cm.span_to_str(*span));
|
||||||
let (pre, post) = match ei.callee.format {
|
let (pre, post) = match ei.callee.format {
|
||||||
codemap::MacroAttribute => ("#[", "]"),
|
codemap::MacroAttribute => ("#[", "]"),
|
||||||
codemap::MacroBang => ("", "!")
|
codemap::MacroBang => ("", "!")
|
||||||
};
|
};
|
||||||
print_diagnostic(ss, Note,
|
if_ok!(print_diagnostic(ss, Note,
|
||||||
format!("in expansion of {}{}{}", pre, ei.callee.name, post));
|
format!("in expansion of {}{}{}", pre,
|
||||||
|
ei.callee.name, post)));
|
||||||
let ss = cm.span_to_str(ei.call_site);
|
let ss = cm.span_to_str(ei.call_site);
|
||||||
print_diagnostic(ss, Note, "expansion site");
|
if_ok!(print_diagnostic(ss, Note, "expansion site"));
|
||||||
print_macro_backtrace(cm, ei.call_site);
|
if_ok!(print_macro_backtrace(cm, ei.call_site));
|
||||||
}
|
}
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn expect<T:Clone>(diag: @SpanHandler, opt: Option<T>, msg: || -> ~str)
|
pub fn expect<T:Clone>(diag: @SpanHandler, opt: Option<T>, msg: || -> ~str)
|
||||||
|
|
|
@ -20,7 +20,6 @@ use parse::token::get_ident_interner;
|
||||||
use parse::token;
|
use parse::token;
|
||||||
use print::pprust;
|
use print::pprust;
|
||||||
|
|
||||||
use std::io;
|
|
||||||
use std::io::File;
|
use std::io::File;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
use std::str;
|
use std::str;
|
||||||
|
@ -109,9 +108,9 @@ pub fn expand_include_str(cx: &mut ExtCtxt, sp: Span, tts: &[ast::TokenTree])
|
||||||
None => return MacResult::dummy_expr()
|
None => return MacResult::dummy_expr()
|
||||||
};
|
};
|
||||||
let file = res_rel_file(cx, sp, &Path::new(file));
|
let file = res_rel_file(cx, sp, &Path::new(file));
|
||||||
let bytes = match io::result(|| File::open(&file).read_to_end()) {
|
let bytes = match File::open(&file).read_to_end() {
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
cx.span_err(sp, format!("couldn't read {}: {}", file.display(), e.desc));
|
cx.span_err(sp, format!("couldn't read {}: {}", file.display(), e));
|
||||||
return MacResult::dummy_expr();
|
return MacResult::dummy_expr();
|
||||||
}
|
}
|
||||||
Ok(bytes) => bytes,
|
Ok(bytes) => bytes,
|
||||||
|
@ -141,9 +140,9 @@ pub fn expand_include_bin(cx: &mut ExtCtxt, sp: Span, tts: &[ast::TokenTree])
|
||||||
None => return MacResult::dummy_expr()
|
None => return MacResult::dummy_expr()
|
||||||
};
|
};
|
||||||
let file = res_rel_file(cx, sp, &Path::new(file));
|
let file = res_rel_file(cx, sp, &Path::new(file));
|
||||||
match io::result(|| File::open(&file).read_to_end()) {
|
match File::open(&file).read_to_end() {
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
cx.span_err(sp, format!("couldn't read {}: {}", file.display(), e.desc));
|
cx.span_err(sp, format!("couldn't read {}: {}", file.display(), e));
|
||||||
return MacResult::dummy_expr();
|
return MacResult::dummy_expr();
|
||||||
}
|
}
|
||||||
Ok(bytes) => {
|
Ok(bytes) => {
|
||||||
|
|
|
@ -33,6 +33,11 @@ This API is completely unstable and subject to change.
|
||||||
extern mod extra;
|
extern mod extra;
|
||||||
extern mod term;
|
extern mod term;
|
||||||
|
|
||||||
|
#[cfg(stage0)]
|
||||||
|
macro_rules! if_ok (
|
||||||
|
($e:expr) => (match $e { Ok(e) => e, Err(e) => return Err(e) })
|
||||||
|
)
|
||||||
|
|
||||||
pub mod util {
|
pub mod util {
|
||||||
pub mod interner;
|
pub mod interner;
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
|
|
@ -350,7 +350,8 @@ pub fn gather_comments_and_literals(span_diagnostic:
|
||||||
path: ~str,
|
path: ~str,
|
||||||
srdr: &mut io::Reader)
|
srdr: &mut io::Reader)
|
||||||
-> (~[Comment], ~[Literal]) {
|
-> (~[Comment], ~[Literal]) {
|
||||||
let src = str::from_utf8_owned(srdr.read_to_end()).unwrap();
|
let src = srdr.read_to_end().unwrap();
|
||||||
|
let src = str::from_utf8_owned(src).unwrap();
|
||||||
let cm = CodeMap::new();
|
let cm = CodeMap::new();
|
||||||
let filemap = cm.new_filemap(path, src);
|
let filemap = cm.new_filemap(path, src);
|
||||||
let rdr = lexer::new_low_level_string_reader(span_diagnostic, filemap);
|
let rdr = lexer::new_low_level_string_reader(span_diagnostic, filemap);
|
||||||
|
|
|
@ -19,7 +19,6 @@ use parse::attr::ParserAttr;
|
||||||
use parse::parser::Parser;
|
use parse::parser::Parser;
|
||||||
|
|
||||||
use std::cell::RefCell;
|
use std::cell::RefCell;
|
||||||
use std::io;
|
|
||||||
use std::io::File;
|
use std::io::File;
|
||||||
use std::str;
|
use std::str;
|
||||||
|
|
||||||
|
@ -232,10 +231,10 @@ pub fn file_to_filemap(sess: @ParseSess, path: &Path, spanopt: Option<Span>)
|
||||||
None => sess.span_diagnostic.handler().fatal(msg),
|
None => sess.span_diagnostic.handler().fatal(msg),
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
let bytes = match io::result(|| File::open(path).read_to_end()) {
|
let bytes = match File::open(path).read_to_end() {
|
||||||
Ok(bytes) => bytes,
|
Ok(bytes) => bytes,
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
err(format!("couldn't read {}: {}", path.display(), e.desc));
|
err(format!("couldn't read {}: {}", path.display(), e));
|
||||||
unreachable!()
|
unreachable!()
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -292,7 +292,7 @@ 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) {
|
pub fn pretty_print(&mut self, t: Token) -> io::IoResult<()> {
|
||||||
debug!("pp ~[{},{}]", self.left, self.right);
|
debug!("pp ~[{},{}]", self.left, self.right);
|
||||||
match t {
|
match t {
|
||||||
Eof => {
|
Eof => {
|
||||||
|
@ -301,31 +301,19 @@ impl Printer {
|
||||||
let left = self.token[self.left].clone();
|
let left = self.token[self.left].clone();
|
||||||
self.advance_left(left, self.size[self.left]);
|
self.advance_left(left, self.size[self.left]);
|
||||||
}
|
}
|
||||||
self.indent(0);
|
Begin(b) => {
|
||||||
}
|
if self.scan_stack_empty {
|
||||||
Begin(b) => {
|
self.left_total = 1;
|
||||||
if self.scan_stack_empty {
|
self.right_total = 1;
|
||||||
self.left_total = 1;
|
self.left = 0u;
|
||||||
self.right_total = 1;
|
self.right = 0u;
|
||||||
self.left = 0u;
|
} else { self.advance_right(); }
|
||||||
self.right = 0u;
|
debug!("pp Begin({})/buffer ~[{},{}]",
|
||||||
} else { self.advance_right(); }
|
b.offset, self.left, self.right);
|
||||||
debug!("pp Begin({})/buffer ~[{},{}]",
|
|
||||||
b.offset, self.left, self.right);
|
|
||||||
self.token[self.right] = t;
|
|
||||||
self.size[self.right] = -self.right_total;
|
|
||||||
self.scan_push(self.right);
|
|
||||||
}
|
|
||||||
End => {
|
|
||||||
if self.scan_stack_empty {
|
|
||||||
debug!("pp End/print ~[{},{}]", self.left, self.right);
|
|
||||||
self.print(t, 0);
|
|
||||||
} else {
|
|
||||||
debug!("pp End/buffer ~[{},{}]", self.left, self.right);
|
|
||||||
self.advance_right();
|
|
||||||
self.token[self.right] = t;
|
self.token[self.right] = t;
|
||||||
self.size[self.right] = -1;
|
self.size[self.right] = -self.right_total;
|
||||||
self.scan_push(self.right);
|
self.scan_push(self.right);
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Break(b) => {
|
Break(b) => {
|
||||||
|
@ -357,10 +345,9 @@ impl Printer {
|
||||||
self.right_total += len;
|
self.right_total += len;
|
||||||
self.check_stream();
|
self.check_stream();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub fn check_stream(&mut self) {
|
pub fn check_stream(&mut self) -> io::IoResult<()> {
|
||||||
debug!("check_stream ~[{}, {}] with left_total={}, right_total={}",
|
debug!("check_stream ~[{}, {}] with left_total={}, right_total={}",
|
||||||
self.left, self.right, self.left_total, self.right_total);
|
self.left, self.right, self.left_total, self.right_total);
|
||||||
if self.right_total - self.left_total > self.space {
|
if self.right_total - self.left_total > self.space {
|
||||||
|
@ -376,6 +363,7 @@ impl Printer {
|
||||||
self.advance_left(left, self.size[self.left]);
|
self.advance_left(left, self.size[self.left]);
|
||||||
if self.left != self.right { self.check_stream(); }
|
if self.left != self.right { self.check_stream(); }
|
||||||
}
|
}
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
pub fn scan_push(&mut self, x: uint) {
|
pub fn scan_push(&mut self, x: uint) {
|
||||||
debug!("scan_push {}", x);
|
debug!("scan_push {}", x);
|
||||||
|
@ -413,7 +401,7 @@ impl Printer {
|
||||||
self.right %= self.buf_len;
|
self.right %= self.buf_len;
|
||||||
assert!((self.right != self.left));
|
assert!((self.right != self.left));
|
||||||
}
|
}
|
||||||
pub fn advance_left(&mut self, x: Token, L: int) {
|
pub fn advance_left(&mut self, x: Token, L: int) -> io::IoResult<()> {
|
||||||
debug!("advnce_left ~[{},{}], sizeof({})={}", self.left, self.right,
|
debug!("advnce_left ~[{},{}], sizeof({})={}", self.left, self.right,
|
||||||
self.left, L);
|
self.left, L);
|
||||||
if L >= 0 {
|
if L >= 0 {
|
||||||
|
@ -431,6 +419,9 @@ impl Printer {
|
||||||
let left = self.token[self.left].clone();
|
let left = self.token[self.left].clone();
|
||||||
self.advance_left(left, self.size[self.left]);
|
self.advance_left(left, self.size[self.left]);
|
||||||
}
|
}
|
||||||
|
ret
|
||||||
|
} else {
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub fn check_stack(&mut self, k: int) {
|
pub fn check_stack(&mut self, k: int) {
|
||||||
|
@ -456,11 +447,12 @@ impl Printer {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub fn print_newline(&mut self, amount: int) {
|
pub fn print_newline(&mut self, amount: int) -> io::IoResult<()> {
|
||||||
debug!("NEWLINE {}", amount);
|
debug!("NEWLINE {}", amount);
|
||||||
write!(self.out, "\n");
|
let ret = write!(self.out, "\n");
|
||||||
self.pending_indentation = 0;
|
self.pending_indentation = 0;
|
||||||
self.indent(amount);
|
self.indent(amount);
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
pub fn indent(&mut self, amount: int) {
|
pub fn indent(&mut self, amount: int) {
|
||||||
debug!("INDENT {}", amount);
|
debug!("INDENT {}", amount);
|
||||||
|
@ -478,12 +470,12 @@ impl Printer {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub fn print_str(&mut self, s: &str) {
|
pub fn print_str(&mut self, s: &str) -> io::IoResult<()> {
|
||||||
while self.pending_indentation > 0 {
|
while self.pending_indentation > 0 {
|
||||||
write!(self.out, " ");
|
if_ok!(write!(self.out, " "));
|
||||||
self.pending_indentation -= 1;
|
self.pending_indentation -= 1;
|
||||||
}
|
}
|
||||||
write!(self.out, "{}", s);
|
write!(self.out, "{}", s)
|
||||||
}
|
}
|
||||||
pub fn print(&mut self, x: Token, L: int) {
|
pub fn print(&mut self, x: Token, L: int) {
|
||||||
debug!("print {} {} (remaining line space={})", tok_str(x.clone()), L,
|
debug!("print {} {} (remaining line space={})", tok_str(x.clone()), L,
|
||||||
|
@ -509,12 +501,14 @@ impl Printer {
|
||||||
pbreak: Fits
|
pbreak: Fits
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
End => {
|
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(())
|
||||||
}
|
}
|
||||||
Break(b) => {
|
Break(b) => {
|
||||||
let top = self.get_top();
|
let top = self.get_top();
|
||||||
|
@ -523,24 +517,28 @@ impl Printer {
|
||||||
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(())
|
||||||
}
|
}
|
||||||
Broken(Consistent) => {
|
Broken(Consistent) => {
|
||||||
debug!("print Break({}+{}) in consistent block",
|
debug!("print Break({}+{}) in consistent block",
|
||||||
top.offset, b.offset);
|
top.offset, b.offset);
|
||||||
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
|
||||||
}
|
}
|
||||||
Broken(Inconsistent) => {
|
Broken(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);
|
||||||
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
|
||||||
} else {
|
} else {
|
||||||
debug!("print Break({}) w/o newline in inconsistent",
|
debug!("print Break({}) w/o newline in inconsistent",
|
||||||
b.blank_space);
|
b.blank_space);
|
||||||
self.indent(b.blank_space);
|
self.indent(b.blank_space);
|
||||||
self.space -= b.blank_space;
|
self.space -= b.blank_space;
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -550,7 +548,7 @@ impl Printer {
|
||||||
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 => {
|
Eof => {
|
||||||
// Eof should never get here.
|
// Eof should never get here.
|
||||||
|
@ -563,27 +561,31 @@ impl Printer {
|
||||||
// Convenience functions to talk to the printer.
|
// Convenience functions to talk to the printer.
|
||||||
//
|
//
|
||||||
// "raw box"
|
// "raw box"
|
||||||
pub fn rbox(p: &mut Printer, indent: uint, b: Breaks) {
|
pub fn rbox(p: &mut Printer, indent: uint, b: Breaks) -> io::IoResult<()> {
|
||||||
p.pretty_print(Begin(BeginToken {
|
p.pretty_print(Begin(BeginToken {
|
||||||
offset: indent as int,
|
offset: indent as int,
|
||||||
breaks: b
|
breaks: b
|
||||||
}));
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn ibox(p: &mut Printer, indent: uint) { rbox(p, indent, Inconsistent); }
|
pub fn ibox(p: &mut Printer, indent: uint) -> io::IoResult<()> {
|
||||||
|
rbox(p, indent, Inconsistent)
|
||||||
|
}
|
||||||
|
|
||||||
pub fn cbox(p: &mut Printer, indent: uint) { rbox(p, indent, Consistent); }
|
pub fn cbox(p: &mut Printer, indent: uint) -> io::IoResult<()> {
|
||||||
|
rbox(p, indent, Consistent)
|
||||||
|
}
|
||||||
|
|
||||||
pub fn break_offset(p: &mut Printer, n: uint, off: int) {
|
pub fn break_offset(p: &mut Printer, n: uint, off: int) -> io::IoResult<()> {
|
||||||
p.pretty_print(Break(BreakToken {
|
p.pretty_print(Break(BreakToken {
|
||||||
offset: off,
|
offset: off,
|
||||||
blank_space: n as int
|
blank_space: n as int
|
||||||
}));
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn end(p: &mut Printer) { p.pretty_print(End); }
|
pub fn end(p: &mut Printer) -> io::IoResult<()> { p.pretty_print(End) }
|
||||||
|
|
||||||
pub fn eof(p: &mut Printer) { p.pretty_print(Eof); }
|
pub fn eof(p: &mut Printer) -> io::IoResult<()> { p.pretty_print(Eof) }
|
||||||
|
|
||||||
pub fn word(p: &mut Printer, wrd: &str) {
|
pub fn word(p: &mut Printer, wrd: &str) {
|
||||||
p.pretty_print(String(/* bad */ wrd.to_str(), wrd.len() as int));
|
p.pretty_print(String(/* bad */ wrd.to_str(), wrd.len() as int));
|
||||||
|
@ -597,13 +599,21 @@ pub fn zero_word(p: &mut Printer, wrd: &str) {
|
||||||
p.pretty_print(String(/* bad */ wrd.to_str(), 0));
|
p.pretty_print(String(/* bad */ wrd.to_str(), 0));
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn spaces(p: &mut Printer, n: uint) { break_offset(p, n, 0); }
|
pub fn spaces(p: &mut Printer, n: uint) -> io::IoResult<()> {
|
||||||
|
break_offset(p, n, 0)
|
||||||
|
}
|
||||||
|
|
||||||
pub fn zerobreak(p: &mut Printer) { spaces(p, 0u); }
|
pub fn zerobreak(p: &mut Printer) -> io::IoResult<()> {
|
||||||
|
spaces(p, 0u)
|
||||||
|
}
|
||||||
|
|
||||||
pub fn space(p: &mut Printer) { spaces(p, 1u); }
|
pub fn space(p: &mut Printer) -> io::IoResult<()> {
|
||||||
|
spaces(p, 1u)
|
||||||
|
}
|
||||||
|
|
||||||
pub fn hardbreak(p: &mut Printer) { spaces(p, SIZE_INFINITY as uint); }
|
pub fn hardbreak(p: &mut Printer) -> io::IoResult<()> {
|
||||||
|
spaces(p, SIZE_INFINITY as uint)
|
||||||
|
}
|
||||||
|
|
||||||
pub fn hardbreak_tok_offset(off: int) -> Token {
|
pub fn hardbreak_tok_offset(off: int) -> Token {
|
||||||
Break(BreakToken {offset: off, blank_space: SIZE_INFINITY})
|
Break(BreakToken {offset: off, blank_space: SIZE_INFINITY})
|
||||||
|
|
File diff suppressed because it is too large
Load diff
Loading…
Add table
Add a link
Reference in a new issue