Skip to content

Commit 329e487

Browse files
committed
Start pushing panics outward in lexer.
1 parent de95857 commit 329e487

File tree

2 files changed

+38
-34
lines changed

2 files changed

+38
-34
lines changed

src/libsyntax/parse/lexer/comments.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ fn read_block_comment(rdr: &mut StringReader,
270270
while level > 0 {
271271
debug!("=== block comment level {}", level);
272272
if rdr.is_eof() {
273-
rdr.fatal("unterminated block comment");
273+
panic!(rdr.fatal("unterminated block comment"));
274274
}
275275
if rdr.curr_is('\n') {
276276
trim_whitespace_prefix_and_push_line(&mut lines,

src/libsyntax/parse/lexer/mod.rs

+37-33
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use ast;
1212
use codemap::{BytePos, CharPos, CodeMap, Pos, Span};
1313
use codemap;
14+
use diagnostic::FatalError;
1415
use diagnostic::SpanHandler;
1516
use ext::tt::transcribe::tt_next_token;
1617
use parse::token::str_to_ident;
@@ -30,7 +31,7 @@ pub trait Reader {
3031
fn is_eof(&self) -> bool;
3132
fn next_token(&mut self) -> TokenAndSpan;
3233
/// Report a fatal error with the current span.
33-
fn fatal(&self, &str) -> !;
34+
fn fatal(&self, &str) -> FatalError;
3435
/// Report a non-fatal error with the current span.
3536
fn err(&self, &str);
3637
fn peek(&self) -> TokenAndSpan;
@@ -86,7 +87,7 @@ impl<'a> Reader for StringReader<'a> {
8687
self.advance_token();
8788
ret_val
8889
}
89-
fn fatal(&self, m: &str) -> ! {
90+
fn fatal(&self, m: &str) -> FatalError {
9091
self.fatal_span(self.peek_span, m)
9192
}
9293
fn err(&self, m: &str) {
@@ -110,8 +111,8 @@ impl<'a> Reader for TtReader<'a> {
110111
debug!("TtReader: r={:?}", r);
111112
r
112113
}
113-
fn fatal(&self, m: &str) -> ! {
114-
panic!(self.sp_diag.span_fatal(self.cur_span, m));
114+
fn fatal(&self, m: &str) -> FatalError {
115+
self.sp_diag.span_fatal(self.cur_span, m)
115116
}
116117
fn err(&self, m: &str) {
117118
self.sp_diag.span_err(self.cur_span, m);
@@ -163,8 +164,8 @@ impl<'a> StringReader<'a> {
163164
}
164165

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

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

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

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

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

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

213214
/// Report a lexical error spanning [`from_pos`, `to_pos`), appending the
214215
/// offending string to the error message
215-
fn fatal_span_verbose(&self, from_pos: BytePos, to_pos: BytePos, mut m: String) -> ! {
216+
fn fatal_span_verbose(&self, from_pos: BytePos, to_pos: BytePos, mut m: String) -> FatalError {
216217
m.push_str(": ");
217218
let from = self.byte_offset(from_pos).to_usize();
218219
let to = self.byte_offset(to_pos).to_usize();
219220
m.push_str(&self.source_text[from..to]);
220-
self.fatal_span_(from_pos, to_pos, &m[..]);
221+
self.fatal_span_(from_pos, to_pos, &m[..])
221222
}
222223

223224
/// Advance peek_tok and peek_span to refer to the next token, and
@@ -538,7 +539,7 @@ impl<'a> StringReader<'a> {
538539
"unterminated block comment"
539540
};
540541
let last_bpos = self.last_pos;
541-
self.fatal_span_(start_bpos, last_bpos, msg);
542+
panic!(self.fatal_span_(start_bpos, last_bpos, msg));
542543
}
543544
let n = self.curr.unwrap();
544545
match n {
@@ -682,7 +683,9 @@ impl<'a> StringReader<'a> {
682683
for _ in 0..n_digits {
683684
if self.is_eof() {
684685
let last_bpos = self.last_pos;
685-
self.fatal_span_(start_bpos, last_bpos, "unterminated numeric character escape");
686+
panic!(self.fatal_span_(start_bpos,
687+
last_bpos,
688+
"unterminated numeric character escape"));
686689
}
687690
if self.curr_is(delim) {
688691
let last_bpos = self.last_pos;
@@ -835,15 +838,15 @@ impl<'a> StringReader<'a> {
835838
let c = match self.curr {
836839
Some(c) => c,
837840
None => {
838-
self.fatal_span_(start_bpos, self.last_pos,
839-
"unterminated unicode escape (found EOF)");
841+
panic!(self.fatal_span_(start_bpos, self.last_pos,
842+
"unterminated unicode escape (found EOF)"));
840843
}
841844
};
842845
accum_int *= 16;
843846
accum_int += c.to_digit(16).unwrap_or_else(|| {
844847
if c == delim {
845-
self.fatal_span_(self.last_pos, self.pos,
846-
"unterminated unicode escape (needed a `}`)");
848+
panic!(self.fatal_span_(self.last_pos, self.pos,
849+
"unterminated unicode escape (needed a `}`)"));
847850
} else {
848851
self.err_span_char(self.last_pos, self.pos,
849852
"invalid character in unicode escape", c);
@@ -1077,12 +1080,12 @@ impl<'a> StringReader<'a> {
10771080
let valid = self.scan_char_or_byte(start, c2, /* ascii_only = */ false, '\'');
10781081
if !self.curr_is('\'') {
10791082
let last_bpos = self.last_pos;
1080-
self.fatal_span_verbose(
1083+
panic!(self.fatal_span_verbose(
10811084
// Byte offsetting here is okay because the
10821085
// character before position `start` is an
10831086
// ascii single quote.
10841087
start - BytePos(1), last_bpos,
1085-
"unterminated character constant".to_string());
1088+
"unterminated character constant".to_string()));
10861089
}
10871090
let id = if valid { self.name_from(start) } else { token::intern("0") };
10881091
self.bump(); // advance curr past token
@@ -1107,7 +1110,9 @@ impl<'a> StringReader<'a> {
11071110
while !self.curr_is('"') {
11081111
if self.is_eof() {
11091112
let last_bpos = self.last_pos;
1110-
self.fatal_span_(start_bpos, last_bpos, "unterminated double quote string");
1113+
panic!(self.fatal_span_(start_bpos,
1114+
last_bpos,
1115+
"unterminated double quote string"));
11111116
}
11121117

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

11341139
if self.is_eof() {
11351140
let last_bpos = self.last_pos;
1136-
self.fatal_span_(start_bpos, last_bpos, "unterminated raw string");
1141+
panic!(self.fatal_span_(start_bpos, last_bpos, "unterminated raw string"));
11371142
} else if !self.curr_is('"') {
11381143
let last_bpos = self.last_pos;
11391144
let curr_char = self.curr.unwrap();
1140-
self.fatal_span_char(start_bpos, last_bpos,
1145+
panic!(self.fatal_span_char(start_bpos, last_bpos,
11411146
"found invalid character; \
11421147
only `#` is allowed in raw string delimitation",
1143-
curr_char);
1148+
curr_char));
11441149
}
11451150
self.bump();
11461151
let content_start_bpos = self.last_pos;
@@ -1149,7 +1154,7 @@ impl<'a> StringReader<'a> {
11491154
'outer: loop {
11501155
if self.is_eof() {
11511156
let last_bpos = self.last_pos;
1152-
self.fatal_span_(start_bpos, last_bpos, "unterminated raw string");
1157+
panic!(self.fatal_span_(start_bpos, last_bpos, "unterminated raw string"));
11531158
}
11541159
//if self.curr_is('"') {
11551160
//content_end_bpos = self.last_pos;
@@ -1218,7 +1223,7 @@ impl<'a> StringReader<'a> {
12181223
c => {
12191224
let last_bpos = self.last_pos;
12201225
let bpos = self.pos;
1221-
self.fatal_span_char(last_bpos, bpos, "unknown start of token", c);
1226+
panic!(self.fatal_span_char(last_bpos, bpos, "unknown start of token", c));
12221227
}
12231228
}
12241229
}
@@ -1271,9 +1276,9 @@ impl<'a> StringReader<'a> {
12711276
// character before position `start` are an
12721277
// ascii single quote and ascii 'b'.
12731278
let last_pos = self.last_pos;
1274-
self.fatal_span_verbose(
1279+
panic!(self.fatal_span_verbose(
12751280
start - BytePos(2), last_pos,
1276-
"unterminated byte constant".to_string());
1281+
"unterminated byte constant".to_string()));
12771282
}
12781283

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

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

13191323
if self.is_eof() {
13201324
let last_pos = self.last_pos;
1321-
self.fatal_span_(start_bpos, last_pos, "unterminated raw string");
1325+
panic!(self.fatal_span_(start_bpos, last_pos, "unterminated raw string"));
13221326
} else if !self.curr_is('"') {
13231327
let last_pos = self.last_pos;
13241328
let ch = self.curr.unwrap();
1325-
self.fatal_span_char(start_bpos, last_pos,
1329+
panic!(self.fatal_span_char(start_bpos, last_pos,
13261330
"found invalid character; \
13271331
only `#` is allowed in raw string delimitation",
1328-
ch);
1332+
ch));
13291333
}
13301334
self.bump();
13311335
let content_start_bpos = self.last_pos;
@@ -1334,7 +1338,7 @@ impl<'a> StringReader<'a> {
13341338
match self.curr {
13351339
None => {
13361340
let last_pos = self.last_pos;
1337-
self.fatal_span_(start_bpos, last_pos, "unterminated raw string")
1341+
panic!(self.fatal_span_(start_bpos, last_pos, "unterminated raw string"))
13381342
},
13391343
Some('"') => {
13401344
content_end_bpos = self.last_pos;

0 commit comments

Comments
 (0)