Skip to content

Commit

Permalink
Start pushing panics outward in lexer.
Browse files Browse the repository at this point in the history
  • Loading branch information
eefriedman committed Oct 28, 2015
1 parent de95857 commit 329e487
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 34 deletions.
2 changes: 1 addition & 1 deletion src/libsyntax/parse/lexer/comments.rs
Expand Up @@ -270,7 +270,7 @@ fn read_block_comment(rdr: &mut StringReader,
while level > 0 {
debug!("=== block comment level {}", level);
if rdr.is_eof() {
rdr.fatal("unterminated block comment");
panic!(rdr.fatal("unterminated block comment"));
}
if rdr.curr_is('\n') {
trim_whitespace_prefix_and_push_line(&mut lines,
Expand Down
70 changes: 37 additions & 33 deletions src/libsyntax/parse/lexer/mod.rs
Expand Up @@ -11,6 +11,7 @@
use ast;
use codemap::{BytePos, CharPos, CodeMap, Pos, Span};
use codemap;
use diagnostic::FatalError;
use diagnostic::SpanHandler;
use ext::tt::transcribe::tt_next_token;
use parse::token::str_to_ident;
Expand All @@ -30,7 +31,7 @@ pub trait Reader {
fn is_eof(&self) -> bool;
fn next_token(&mut self) -> TokenAndSpan;
/// Report a fatal error with the current span.
fn fatal(&self, &str) -> !;
fn fatal(&self, &str) -> FatalError;
/// Report a non-fatal error with the current span.
fn err(&self, &str);
fn peek(&self) -> TokenAndSpan;
Expand Down Expand Up @@ -86,7 +87,7 @@ impl<'a> Reader for StringReader<'a> {
self.advance_token();
ret_val
}
fn fatal(&self, m: &str) -> ! {
fn fatal(&self, m: &str) -> FatalError {
self.fatal_span(self.peek_span, m)
}
fn err(&self, m: &str) {
Expand All @@ -110,8 +111,8 @@ impl<'a> Reader for TtReader<'a> {
debug!("TtReader: r={:?}", r);
r
}
fn fatal(&self, m: &str) -> ! {
panic!(self.sp_diag.span_fatal(self.cur_span, m));
fn fatal(&self, m: &str) -> FatalError {
self.sp_diag.span_fatal(self.cur_span, m)
}
fn err(&self, m: &str) {
self.sp_diag.span_err(self.cur_span, m);
Expand Down Expand Up @@ -163,8 +164,8 @@ impl<'a> StringReader<'a> {
}

/// Report a fatal lexical error with a given span.
pub fn fatal_span(&self, sp: Span, m: &str) -> ! {
panic!(self.span_diagnostic.span_fatal(sp, m))
pub fn fatal_span(&self, sp: Span, m: &str) -> FatalError {
self.span_diagnostic.span_fatal(sp, m)
}

/// Report a lexical error with a given span.
Expand All @@ -178,7 +179,7 @@ impl<'a> StringReader<'a> {
}

/// Report a fatal error spanning [`from_pos`, `to_pos`).
fn fatal_span_(&self, from_pos: BytePos, to_pos: BytePos, m: &str) -> ! {
fn fatal_span_(&self, from_pos: BytePos, to_pos: BytePos, m: &str) -> FatalError {
self.fatal_span(codemap::mk_sp(from_pos, to_pos), m)
}

Expand All @@ -194,11 +195,11 @@ impl<'a> StringReader<'a> {

/// Report a lexical error spanning [`from_pos`, `to_pos`), appending an
/// escaped character to the error message
fn fatal_span_char(&self, from_pos: BytePos, to_pos: BytePos, m: &str, c: char) -> ! {
fn fatal_span_char(&self, from_pos: BytePos, to_pos: BytePos, m: &str, c: char) -> FatalError {
let mut m = m.to_string();
m.push_str(": ");
for c in c.escape_default() { m.push(c) }
self.fatal_span_(from_pos, to_pos, &m[..]);
self.fatal_span_(from_pos, to_pos, &m[..])
}

/// Report a lexical error spanning [`from_pos`, `to_pos`), appending an
Expand All @@ -212,12 +213,12 @@ impl<'a> StringReader<'a> {

/// Report a lexical error spanning [`from_pos`, `to_pos`), appending the
/// offending string to the error message
fn fatal_span_verbose(&self, from_pos: BytePos, to_pos: BytePos, mut m: String) -> ! {
fn fatal_span_verbose(&self, from_pos: BytePos, to_pos: BytePos, mut m: String) -> FatalError {
m.push_str(": ");
let from = self.byte_offset(from_pos).to_usize();
let to = self.byte_offset(to_pos).to_usize();
m.push_str(&self.source_text[from..to]);
self.fatal_span_(from_pos, to_pos, &m[..]);
self.fatal_span_(from_pos, to_pos, &m[..])
}

/// Advance peek_tok and peek_span to refer to the next token, and
Expand Down Expand Up @@ -538,7 +539,7 @@ impl<'a> StringReader<'a> {
"unterminated block comment"
};
let last_bpos = self.last_pos;
self.fatal_span_(start_bpos, last_bpos, msg);
panic!(self.fatal_span_(start_bpos, last_bpos, msg));
}
let n = self.curr.unwrap();
match n {
Expand Down Expand Up @@ -682,7 +683,9 @@ impl<'a> StringReader<'a> {
for _ in 0..n_digits {
if self.is_eof() {
let last_bpos = self.last_pos;
self.fatal_span_(start_bpos, last_bpos, "unterminated numeric character escape");
panic!(self.fatal_span_(start_bpos,
last_bpos,
"unterminated numeric character escape"));
}
if self.curr_is(delim) {
let last_bpos = self.last_pos;
Expand Down Expand Up @@ -835,15 +838,15 @@ impl<'a> StringReader<'a> {
let c = match self.curr {
Some(c) => c,
None => {
self.fatal_span_(start_bpos, self.last_pos,
"unterminated unicode escape (found EOF)");
panic!(self.fatal_span_(start_bpos, self.last_pos,
"unterminated unicode escape (found EOF)"));
}
};
accum_int *= 16;
accum_int += c.to_digit(16).unwrap_or_else(|| {
if c == delim {
self.fatal_span_(self.last_pos, self.pos,
"unterminated unicode escape (needed a `}`)");
panic!(self.fatal_span_(self.last_pos, self.pos,
"unterminated unicode escape (needed a `}`)"));
} else {
self.err_span_char(self.last_pos, self.pos,
"invalid character in unicode escape", c);
Expand Down Expand Up @@ -1077,12 +1080,12 @@ impl<'a> StringReader<'a> {
let valid = self.scan_char_or_byte(start, c2, /* ascii_only = */ false, '\'');
if !self.curr_is('\'') {
let last_bpos = self.last_pos;
self.fatal_span_verbose(
panic!(self.fatal_span_verbose(
// Byte offsetting here is okay because the
// character before position `start` is an
// ascii single quote.
start - BytePos(1), last_bpos,
"unterminated character constant".to_string());
"unterminated character constant".to_string()));
}
let id = if valid { self.name_from(start) } else { token::intern("0") };
self.bump(); // advance curr past token
Expand All @@ -1107,7 +1110,9 @@ impl<'a> StringReader<'a> {
while !self.curr_is('"') {
if self.is_eof() {
let last_bpos = self.last_pos;
self.fatal_span_(start_bpos, last_bpos, "unterminated double quote string");
panic!(self.fatal_span_(start_bpos,
last_bpos,
"unterminated double quote string"));
}

let ch_start = self.last_pos;
Expand All @@ -1133,14 +1138,14 @@ impl<'a> StringReader<'a> {

if self.is_eof() {
let last_bpos = self.last_pos;
self.fatal_span_(start_bpos, last_bpos, "unterminated raw string");
panic!(self.fatal_span_(start_bpos, last_bpos, "unterminated raw string"));
} else if !self.curr_is('"') {
let last_bpos = self.last_pos;
let curr_char = self.curr.unwrap();
self.fatal_span_char(start_bpos, last_bpos,
panic!(self.fatal_span_char(start_bpos, last_bpos,
"found invalid character; \
only `#` is allowed in raw string delimitation",
curr_char);
curr_char));
}
self.bump();
let content_start_bpos = self.last_pos;
Expand All @@ -1149,7 +1154,7 @@ impl<'a> StringReader<'a> {
'outer: loop {
if self.is_eof() {
let last_bpos = self.last_pos;
self.fatal_span_(start_bpos, last_bpos, "unterminated raw string");
panic!(self.fatal_span_(start_bpos, last_bpos, "unterminated raw string"));
}
//if self.curr_is('"') {
//content_end_bpos = self.last_pos;
Expand Down Expand Up @@ -1218,7 +1223,7 @@ impl<'a> StringReader<'a> {
c => {
let last_bpos = self.last_pos;
let bpos = self.pos;
self.fatal_span_char(last_bpos, bpos, "unknown start of token", c);
panic!(self.fatal_span_char(last_bpos, bpos, "unknown start of token", c));
}
}
}
Expand Down Expand Up @@ -1271,9 +1276,9 @@ impl<'a> StringReader<'a> {
// character before position `start` are an
// ascii single quote and ascii 'b'.
let last_pos = self.last_pos;
self.fatal_span_verbose(
panic!(self.fatal_span_verbose(
start - BytePos(2), last_pos,
"unterminated byte constant".to_string());
"unterminated byte constant".to_string()));
}

let id = if valid { self.name_from(start) } else { token::intern("?") };
Expand All @@ -1293,8 +1298,7 @@ impl<'a> StringReader<'a> {
while !self.curr_is('"') {
if self.is_eof() {
let last_pos = self.last_pos;
self.fatal_span_(start, last_pos,
"unterminated double quote byte string");
panic!(self.fatal_span_(start, last_pos, "unterminated double quote byte string"));
}

let ch_start = self.last_pos;
Expand All @@ -1318,14 +1322,14 @@ impl<'a> StringReader<'a> {

if self.is_eof() {
let last_pos = self.last_pos;
self.fatal_span_(start_bpos, last_pos, "unterminated raw string");
panic!(self.fatal_span_(start_bpos, last_pos, "unterminated raw string"));
} else if !self.curr_is('"') {
let last_pos = self.last_pos;
let ch = self.curr.unwrap();
self.fatal_span_char(start_bpos, last_pos,
panic!(self.fatal_span_char(start_bpos, last_pos,
"found invalid character; \
only `#` is allowed in raw string delimitation",
ch);
ch));
}
self.bump();
let content_start_bpos = self.last_pos;
Expand All @@ -1334,7 +1338,7 @@ impl<'a> StringReader<'a> {
match self.curr {
None => {
let last_pos = self.last_pos;
self.fatal_span_(start_bpos, last_pos, "unterminated raw string")
panic!(self.fatal_span_(start_bpos, last_pos, "unterminated raw string"))
},
Some('"') => {
content_end_bpos = self.last_pos;
Expand Down

0 comments on commit 329e487

Please sign in to comment.