diff options
| author | Eli Friedman <eli.friedman@gmail.com> | 2015-10-23 19:20:03 -0700 |
|---|---|---|
| committer | Eli Friedman <eli.friedman@gmail.com> | 2015-10-27 20:09:10 -0700 |
| commit | 329e487e582cba7ae1eaa444b0d004cee94ae7c3 (patch) | |
| tree | 50ad1a6a5d43807e543daeadb87cbb0c22014113 /src/libsyntax/parse | |
| parent | de95857129a8bdcc623f57f669b535ddf8a8db6e (diff) | |
| download | rust-329e487e582cba7ae1eaa444b0d004cee94ae7c3.tar.gz rust-329e487e582cba7ae1eaa444b0d004cee94ae7c3.zip | |
Start pushing panics outward in lexer.
Diffstat (limited to 'src/libsyntax/parse')
| -rw-r--r-- | src/libsyntax/parse/lexer/comments.rs | 2 | ||||
| -rw-r--r-- | src/libsyntax/parse/lexer/mod.rs | 70 |
2 files changed, 38 insertions, 34 deletions
diff --git a/src/libsyntax/parse/lexer/comments.rs b/src/libsyntax/parse/lexer/comments.rs index 137996a35ee..e5e2c3a986d 100644 --- a/src/libsyntax/parse/lexer/comments.rs +++ b/src/libsyntax/parse/lexer/comments.rs @@ -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, diff --git a/src/libsyntax/parse/lexer/mod.rs b/src/libsyntax/parse/lexer/mod.rs index 490822b934e..9e38ffe7f0d 100644 --- a/src/libsyntax/parse/lexer/mod.rs +++ b/src/libsyntax/parse/lexer/mod.rs @@ -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; @@ -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; @@ -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) { @@ -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); @@ -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. @@ -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) } @@ -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 @@ -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 @@ -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 { @@ -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; @@ -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); @@ -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 @@ -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; @@ -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; @@ -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; @@ -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)); } } } @@ -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("?") }; @@ -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; @@ -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; @@ -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; |
