syntax: Remove io_error usage

This commit is contained in:
Alex Crichton 2014-01-29 17:39:21 -08:00
parent ef00c6a278
commit b211b00d21
7 changed files with 1340 additions and 1037 deletions

View file

@ -189,58 +189,61 @@ impl Level {
}
}
fn print_maybe_styled(msg: &str, color: term::attr::Attr) {
local_data_key!(tls_terminal: ~Option<term::Terminal<StdWriter>>)
fn print_maybe_styled(msg: &str, color: term::attr::Attr) -> io::IoResult<()> {
local_data_key!(tls_terminal: Option<term::Terminal<StdWriter>>)
fn is_stderr_screen() -> bool {
use std::libc;
unsafe { libc::isatty(libc::STDERR_FILENO) != 0 }
}
fn write_pretty<T: Writer>(term: &mut term::Terminal<T>, s: &str, c: term::attr::Attr) {
term.attr(c);
term.write(s.as_bytes());
term.reset();
fn write_pretty<T: Writer>(term: &mut term::Terminal<T>, s: &str,
c: term::attr::Attr) -> io::IoResult<()> {
if_ok!(term.attr(c));
if_ok!(term.write(s.as_bytes()));
if_ok!(term.reset());
Ok(())
}
if is_stderr_screen() {
local_data::get_mut(tls_terminal, |term| {
match term {
Some(term) => {
match **term {
match *term {
Some(ref mut term) => write_pretty(term, msg, color),
None => io::stderr().write(msg.as_bytes())
}
}
None => {
let t = ~match term::Terminal::new(io::stderr()) {
let (t, ret) = match term::Terminal::new(io::stderr()) {
Ok(mut term) => {
write_pretty(&mut term, msg, color);
Some(term)
let r = write_pretty(&mut term, msg, color);
(Some(term), r)
}
Err(_) => {
io::stderr().write(msg.as_bytes());
None
(None, io::stderr().write(msg.as_bytes()))
}
};
local_data::set(tls_terminal, t);
ret
}
}
});
})
} else {
io::stderr().write(msg.as_bytes());
io::stderr().write(msg.as_bytes())
}
}
fn print_diagnostic(topic: &str, lvl: Level, msg: &str) {
let mut stderr = io::stderr();
fn print_diagnostic(topic: &str, lvl: Level, msg: &str) -> io::IoResult<()> {
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()),
term::attr::ForegroundColor(lvl.color()));
print_maybe_styled(format!("{}\n", msg), term::attr::Bold);
if_ok!(print_maybe_styled(format!("{}: ", lvl.to_str()),
term::attr::ForegroundColor(lvl.color())));
if_ok!(print_maybe_styled(format!("{}\n", msg), term::attr::Bold));
Ok(())
}
pub struct DefaultEmitter;
@ -250,20 +253,28 @@ impl Emitter for DefaultEmitter {
cmsp: Option<(&codemap::CodeMap, Span)>,
msg: &str,
lvl: Level) {
match cmsp {
let error = match cmsp {
Some((cm, sp)) => emit(cm, sp, msg, lvl, false),
None => print_diagnostic("", lvl, msg),
};
match error {
Ok(()) => {}
Err(e) => fail!("failed to print diagnostics: {}", e),
}
}
fn custom_emit(&self, cm: &codemap::CodeMap,
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,
msg: &str, lvl: Level, custom: bool) {
msg: &str, lvl: Level, custom: bool) -> io::IoResult<()> {
let ss = cm.span_to_str(sp);
let lines = cm.span_to_lines(sp);
if custom {
@ -272,19 +283,19 @@ fn emit(cm: &codemap::CodeMap, sp: Span,
// the span)
let span_end = Span { lo: sp.hi, hi: sp.hi, expn_info: sp.expn_info};
let ses = cm.span_to_str(span_end);
print_diagnostic(ses, lvl, msg);
custom_highlight_lines(cm, sp, lvl, lines);
if_ok!(print_diagnostic(ses, lvl, msg));
if_ok!(custom_highlight_lines(cm, sp, lvl, lines));
} else {
print_diagnostic(ss, lvl, msg);
highlight_lines(cm, sp, lvl, lines);
if_ok!(print_diagnostic(ss, lvl, msg));
if_ok!(highlight_lines(cm, sp, lvl, lines));
}
print_macro_backtrace(cm, sp);
print_macro_backtrace(cm, sp)
}
fn highlight_lines(cm: &codemap::CodeMap,
sp: Span,
lvl: Level,
lines: &codemap::FileLines) {
lines: &codemap::FileLines) -> io::IoResult<()> {
let fm = lines.file;
let mut err = io::stderr();
let err = &mut err as &mut io::Writer;
@ -297,12 +308,13 @@ fn highlight_lines(cm: &codemap::CodeMap,
}
// Print the offending lines
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 {
let last_line = display_lines[display_lines.len() - 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)
@ -334,7 +346,7 @@ fn highlight_lines(cm: &codemap::CodeMap,
_ => s.push_char(' '),
};
}
write!(err, "{}", s);
if_ok!(write!(err, "{}", s));
let mut s = ~"^";
let hi = cm.lookup_char_pos(sp.hi);
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;
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`:
@ -355,23 +369,23 @@ fn highlight_lines(cm: &codemap::CodeMap,
fn custom_highlight_lines(cm: &codemap::CodeMap,
sp: Span,
lvl: Level,
lines: &codemap::FileLines) {
lines: &codemap::FileLines) -> io::IoResult<()> {
let fm = lines.file;
let mut err = io::stderr();
let err = &mut err as &mut io::Writer;
let lines = lines.lines.as_slice();
if lines.len() > MAX_LINES {
write!(err, "{}:{} {}\n", fm.name,
lines[0] + 1, fm.get_line(lines[0] as int));
write!(err, "...\n");
if_ok!(write!(err, "{}:{} {}\n", fm.name,
lines[0] + 1, fm.get_line(lines[0] as int)));
if_ok!(write!(err, "...\n"));
let last_line = lines[lines.len()-1];
write!(err, "{}:{} {}\n", fm.name,
last_line + 1, fm.get_line(last_line as int));
if_ok!(write!(err, "{}:{} {}\n", fm.name,
last_line + 1, fm.get_line(last_line as int)));
} else {
for line in 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)));
}
}
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 = ~"";
for _ in range(0, skip) { 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() {
let ss = ei.callee.span.as_ref().map_or(~"", |span| cm.span_to_str(*span));
let (pre, post) = match ei.callee.format {
codemap::MacroAttribute => ("#[", "]"),
codemap::MacroBang => ("", "!")
};
print_diagnostic(ss, Note,
format!("in expansion of {}{}{}", pre, ei.callee.name, post));
if_ok!(print_diagnostic(ss, Note,
format!("in expansion of {}{}{}", pre,
ei.callee.name, post)));
let ss = cm.span_to_str(ei.call_site);
print_diagnostic(ss, Note, "expansion site");
print_macro_backtrace(cm, ei.call_site);
if_ok!(print_diagnostic(ss, Note, "expansion site"));
if_ok!(print_macro_backtrace(cm, ei.call_site));
}
Ok(())
}
pub fn expect<T:Clone>(diag: @SpanHandler, opt: Option<T>, msg: || -> ~str)

View file

@ -20,7 +20,6 @@ use parse::token::get_ident_interner;
use parse::token;
use print::pprust;
use std::io;
use std::io::File;
use std::rc::Rc;
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()
};
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) => {
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();
}
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()
};
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) => {
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();
}
Ok(bytes) => {

View file

@ -33,6 +33,11 @@ This API is completely unstable and subject to change.
extern mod extra;
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 interner;
#[cfg(test)]

View file

@ -350,7 +350,8 @@ pub fn gather_comments_and_literals(span_diagnostic:
path: ~str,
srdr: &mut io::Reader)
-> (~[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 filemap = cm.new_filemap(path, src);
let rdr = lexer::new_low_level_string_reader(span_diagnostic, filemap);

View file

@ -19,7 +19,6 @@ use parse::attr::ParserAttr;
use parse::parser::Parser;
use std::cell::RefCell;
use std::io;
use std::io::File;
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),
}
};
let bytes = match io::result(|| File::open(path).read_to_end()) {
let bytes = match File::open(path).read_to_end() {
Ok(bytes) => bytes,
Err(e) => {
err(format!("couldn't read {}: {}", path.display(), e.desc));
err(format!("couldn't read {}: {}", path.display(), e));
unreachable!()
}
};

View file

@ -292,7 +292,7 @@ impl Printer {
pub fn replace_last_token(&mut self, t: Token) {
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);
match t {
Eof => {
@ -301,8 +301,6 @@ impl Printer {
let left = self.token[self.left].clone();
self.advance_left(left, self.size[self.left]);
}
self.indent(0);
}
Begin(b) => {
if self.scan_stack_empty {
self.left_total = 1;
@ -315,17 +313,7 @@ impl Printer {
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.size[self.right] = -1;
self.scan_push(self.right);
Ok(())
}
}
Break(b) => {
@ -359,8 +347,7 @@ impl Printer {
}
}
}
}
pub fn check_stream(&mut self) {
pub fn check_stream(&mut self) -> io::IoResult<()> {
debug!("check_stream ~[{}, {}] with left_total={}, right_total={}",
self.left, self.right, self.left_total, self.right_total);
if self.right_total - self.left_total > self.space {
@ -376,6 +363,7 @@ impl Printer {
self.advance_left(left, self.size[self.left]);
if self.left != self.right { self.check_stream(); }
}
Ok(())
}
pub fn scan_push(&mut self, x: uint) {
debug!("scan_push {}", x);
@ -413,7 +401,7 @@ impl Printer {
self.right %= self.buf_len;
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,
self.left, L);
if L >= 0 {
@ -431,6 +419,9 @@ impl Printer {
let left = self.token[self.left].clone();
self.advance_left(left, self.size[self.left]);
}
ret
} else {
Ok(())
}
}
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);
write!(self.out, "\n");
let ret = write!(self.out, "\n");
self.pending_indentation = 0;
self.indent(amount);
return ret;
}
pub fn indent(&mut self, amount: int) {
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 {
write!(self.out, " ");
if_ok!(write!(self.out, " "));
self.pending_indentation -= 1;
}
write!(self.out, "{}", s);
write!(self.out, "{}", s)
}
pub fn print(&mut self, x: Token, L: int) {
debug!("print {} {} (remaining line space={})", tok_str(x.clone()), L,
@ -509,12 +501,14 @@ impl Printer {
pbreak: Fits
});
}
Ok(())
}
End => {
debug!("print End -> pop End");
let print_stack = &mut self.print_stack;
assert!((print_stack.len() != 0u));
print_stack.pop().unwrap();
Ok(())
}
Break(b) => {
let top = self.get_top();
@ -523,24 +517,28 @@ impl Printer {
debug!("print Break({}) in fitting block", b.blank_space);
self.space -= b.blank_space;
self.indent(b.blank_space);
Ok(())
}
Broken(Consistent) => {
debug!("print Break({}+{}) in consistent block",
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);
ret
}
Broken(Inconsistent) => {
if L > self.space {
debug!("print Break({}+{}) w/ newline in inconsistent",
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);
ret
} else {
debug!("print Break({}) w/o newline in inconsistent",
b.blank_space);
self.indent(b.blank_space);
self.space -= b.blank_space;
Ok(())
}
}
}
@ -550,7 +548,7 @@ impl Printer {
assert_eq!(L, len);
// assert!(L <= space);
self.space -= len;
self.print_str(s);
self.print_str(s)
}
Eof => {
// Eof should never get here.
@ -563,27 +561,31 @@ impl Printer {
// Convenience functions to talk to the printer.
//
// "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 {
offset: indent as int,
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 {
offset: off,
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) {
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));
}
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 {
Break(BreakToken {offset: off, blank_space: SIZE_INFINITY})

File diff suppressed because it is too large Load diff