Skip to content

Commit

Permalink
lexer: further slight improvements to lexer errors
Browse files Browse the repository at this point in the history
  • Loading branch information
ben0x539 committed Sep 19, 2013
1 parent 8009c97 commit 567c567
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 17 deletions.
46 changes: 30 additions & 16 deletions src/libsyntax/parse/lexer.rs
Expand Up @@ -173,6 +173,22 @@ fn fatal_span_char(rdr: @mut StringReader,
fatal_span(rdr, from_pos, to_pos, m);
}

// report a lexical error spanning [`from_pos`, `to_pos`), appending the
// offending string to the error message
fn fatal_span_verbose(rdr: @mut StringReader,
from_pos: BytePos,
to_pos: BytePos,
m: ~str)
-> ! {
let mut m = m;
m.push_str(": ");
let s = rdr.src.slice(
byte_offset(rdr, from_pos).to_uint(),
byte_offset(rdr, to_pos).to_uint());
m.push_str(s);
fatal_span(rdr, from_pos, to_pos, m);
}

// EFFECT: advance peek_tok and peek_span to refer to the next token.
// EFFECT: update the interner, maybe.
fn string_advance_token(r: @mut StringReader) {
Expand Down Expand Up @@ -390,8 +406,7 @@ fn consume_block_comment(rdr: @mut StringReader)
if res.is_some() { res } else { consume_whitespace_and_comments(rdr) }
}

fn scan_exponent(rdr: @mut StringReader) -> Option<~str> {
let start_bpos = rdr.last_pos;
fn scan_exponent(rdr: @mut StringReader, start_bpos: BytePos) -> Option<~str> {
let mut c = rdr.curr;
let mut rslt = ~"";
if c == 'e' || c == 'E' {
Expand Down Expand Up @@ -507,7 +522,7 @@ fn scan_number(c: char, rdr: @mut StringReader) -> token::Token {
_ => ()
}
}
match scan_exponent(rdr) {
match scan_exponent(rdr, start_bpos) {
Some(ref s) => {
is_float = true;
num_str.push_str(*s);
Expand Down Expand Up @@ -568,7 +583,8 @@ fn scan_numeric_escape(rdr: @mut StringReader, n_hex_digits: uint) -> char {
let n = rdr.curr;
if !is_hex_digit(n) {
fatal_span_char(rdr, rdr.last_pos, rdr.pos,
~"illegal numeric character escape", n);
~"illegal character in numeric character escape",
n);
}
bump(rdr);
accum_int *= 16;
Expand Down Expand Up @@ -754,27 +770,25 @@ fn next_token_inner(rdr: @mut StringReader) -> token::Token {
}
}
if rdr.curr != '\'' {
fatal_span(rdr,
// Byte offsetting here is okay because the character
// before position `start` is an ascii single quote.
start - BytePos(1u),
rdr.last_pos,
~"unterminated character constant");
fatal_span_verbose(rdr,
// Byte offsetting here is okay because the
// character before position `start` is an
// ascii single quote.
start - BytePos(1u),
rdr.last_pos,
~"unterminated character constant");
}
bump(rdr); // advance curr past token
return token::LIT_CHAR(c2 as u32);
}
'"' => {
let mut accum_str = ~"";
let n = rdr.last_pos;
let start_bpos = rdr.last_pos;
bump(rdr);
while rdr.curr != '"' {
if is_eof(rdr) {
do with_str_from(rdr, n) |s| {
fatal_span(rdr, n, rdr.last_pos,
fmt!("unterminated double quote string: %s",
s));
}
fatal_span(rdr, start_bpos, rdr.last_pos,
~"unterminated double quote string");
}

let ch = rdr.curr;
Expand Down
2 changes: 1 addition & 1 deletion src/test/compile-fail/lex-illegal-num-char-escape.rs
Expand Up @@ -9,5 +9,5 @@
// except according to those terms.

static c: char =
'\u539_' //~ ERROR: illegal numeric character escape
'\u539_' //~ ERROR: illegal character in numeric character escape
;

5 comments on commit 567c567

@bors
Copy link
Contributor

@bors bors commented on 567c567 Sep 20, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from alexcrichton
at ben0x539@567c567

@bors
Copy link
Contributor

@bors bors commented on 567c567 Sep 20, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging ben0x539/rust/lexer-error-spans = 567c567 into auto

@bors
Copy link
Contributor

@bors bors commented on 567c567 Sep 20, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ben0x539/rust/lexer-error-spans = 567c567 merged ok, testing candidate = 7f826cb

@bors
Copy link
Contributor

@bors bors commented on 567c567 Sep 20, 2013

@bors
Copy link
Contributor

@bors bors commented on 567c567 Sep 20, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = 7f826cb

Please sign in to comment.