Skip to content

Commit 567c567

Browse files
committed
lexer: further slight improvements to lexer errors
1 parent 8009c97 commit 567c567

File tree

2 files changed

+31
-17
lines changed

2 files changed

+31
-17
lines changed

src/libsyntax/parse/lexer.rs

+30-16
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,22 @@ fn fatal_span_char(rdr: @mut StringReader,
173173
fatal_span(rdr, from_pos, to_pos, m);
174174
}
175175

176+
// report a lexical error spanning [`from_pos`, `to_pos`), appending the
177+
// offending string to the error message
178+
fn fatal_span_verbose(rdr: @mut StringReader,
179+
from_pos: BytePos,
180+
to_pos: BytePos,
181+
m: ~str)
182+
-> ! {
183+
let mut m = m;
184+
m.push_str(": ");
185+
let s = rdr.src.slice(
186+
byte_offset(rdr, from_pos).to_uint(),
187+
byte_offset(rdr, to_pos).to_uint());
188+
m.push_str(s);
189+
fatal_span(rdr, from_pos, to_pos, m);
190+
}
191+
176192
// EFFECT: advance peek_tok and peek_span to refer to the next token.
177193
// EFFECT: update the interner, maybe.
178194
fn string_advance_token(r: @mut StringReader) {
@@ -390,8 +406,7 @@ fn consume_block_comment(rdr: @mut StringReader)
390406
if res.is_some() { res } else { consume_whitespace_and_comments(rdr) }
391407
}
392408

393-
fn scan_exponent(rdr: @mut StringReader) -> Option<~str> {
394-
let start_bpos = rdr.last_pos;
409+
fn scan_exponent(rdr: @mut StringReader, start_bpos: BytePos) -> Option<~str> {
395410
let mut c = rdr.curr;
396411
let mut rslt = ~"";
397412
if c == 'e' || c == 'E' {
@@ -507,7 +522,7 @@ fn scan_number(c: char, rdr: @mut StringReader) -> token::Token {
507522
_ => ()
508523
}
509524
}
510-
match scan_exponent(rdr) {
525+
match scan_exponent(rdr, start_bpos) {
511526
Some(ref s) => {
512527
is_float = true;
513528
num_str.push_str(*s);
@@ -568,7 +583,8 @@ fn scan_numeric_escape(rdr: @mut StringReader, n_hex_digits: uint) -> char {
568583
let n = rdr.curr;
569584
if !is_hex_digit(n) {
570585
fatal_span_char(rdr, rdr.last_pos, rdr.pos,
571-
~"illegal numeric character escape", n);
586+
~"illegal character in numeric character escape",
587+
n);
572588
}
573589
bump(rdr);
574590
accum_int *= 16;
@@ -754,27 +770,25 @@ fn next_token_inner(rdr: @mut StringReader) -> token::Token {
754770
}
755771
}
756772
if rdr.curr != '\'' {
757-
fatal_span(rdr,
758-
// Byte offsetting here is okay because the character
759-
// before position `start` is an ascii single quote.
760-
start - BytePos(1u),
761-
rdr.last_pos,
762-
~"unterminated character constant");
773+
fatal_span_verbose(rdr,
774+
// Byte offsetting here is okay because the
775+
// character before position `start` is an
776+
// ascii single quote.
777+
start - BytePos(1u),
778+
rdr.last_pos,
779+
~"unterminated character constant");
763780
}
764781
bump(rdr); // advance curr past token
765782
return token::LIT_CHAR(c2 as u32);
766783
}
767784
'"' => {
768785
let mut accum_str = ~"";
769-
let n = rdr.last_pos;
786+
let start_bpos = rdr.last_pos;
770787
bump(rdr);
771788
while rdr.curr != '"' {
772789
if is_eof(rdr) {
773-
do with_str_from(rdr, n) |s| {
774-
fatal_span(rdr, n, rdr.last_pos,
775-
fmt!("unterminated double quote string: %s",
776-
s));
777-
}
790+
fatal_span(rdr, start_bpos, rdr.last_pos,
791+
~"unterminated double quote string");
778792
}
779793

780794
let ch = rdr.curr;

src/test/compile-fail/lex-illegal-num-char-escape.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@
99
// except according to those terms.
1010

1111
static c: char =
12-
'\u539_' //~ ERROR: illegal numeric character escape
12+
'\u539_' //~ ERROR: illegal character in numeric character escape
1313
;

0 commit comments

Comments
 (0)