Skip to content

Commit 8009c97

Browse files
committed
lexer: report problematic chars verbatim or as escape sequence
... instead of giving their numeric codepoint, following the lead of fdaae34. So the error message for, say, '\_' mentions _ instead of 95, and '\●' now mentions \u25cf.
1 parent 1019177 commit 8009c97

File tree

1 file changed

+23
-12
lines changed

1 file changed

+23
-12
lines changed

src/libsyntax/parse/lexer.rs

+23-12
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,20 @@ fn fatal_span(rdr: @mut StringReader,
159159
rdr.fatal(m);
160160
}
161161

162+
// report a lexical error spanning [`from_pos`, `to_pos`), appending an
163+
// escaped character to the error message
164+
fn fatal_span_char(rdr: @mut StringReader,
165+
from_pos: BytePos,
166+
to_pos: BytePos,
167+
m: ~str,
168+
c: char)
169+
-> ! {
170+
let mut m = m;
171+
m.push_str(": ");
172+
char::escape_default(c, |c| m.push_char(c));
173+
fatal_span(rdr, from_pos, to_pos, m);
174+
}
175+
162176
// EFFECT: advance peek_tok and peek_span to refer to the next token.
163177
// EFFECT: update the interner, maybe.
164178
fn string_advance_token(r: @mut StringReader) {
@@ -553,9 +567,8 @@ fn scan_numeric_escape(rdr: @mut StringReader, n_hex_digits: uint) -> char {
553567
while i != 0u {
554568
let n = rdr.curr;
555569
if !is_hex_digit(n) {
556-
fatal_span(rdr, rdr.last_pos, rdr.pos,
557-
fmt!("illegal numeric character escape: %d",
558-
n as int));
570+
fatal_span_char(rdr, rdr.last_pos, rdr.pos,
571+
~"illegal numeric character escape", n);
559572
}
560573
bump(rdr);
561574
accum_int *= 16;
@@ -565,7 +578,7 @@ fn scan_numeric_escape(rdr: @mut StringReader, n_hex_digits: uint) -> char {
565578
match char::from_u32(accum_int as u32) {
566579
Some(x) => x,
567580
None => fatal_span(rdr, start_bpos, rdr.last_pos,
568-
fmt!("illegal numeric character escape"))
581+
~"illegal numeric character escape")
569582
}
570583
}
571584

@@ -735,8 +748,8 @@ fn next_token_inner(rdr: @mut StringReader) -> token::Token {
735748
'u' => { c2 = scan_numeric_escape(rdr, 4u); }
736749
'U' => { c2 = scan_numeric_escape(rdr, 8u); }
737750
c2 => {
738-
fatal_span(rdr, escaped_pos, rdr.last_pos,
739-
fmt!("unknown character escape: %d", c2 as int));
751+
fatal_span_char(rdr, escaped_pos, rdr.last_pos,
752+
~"unknown character escape", c2);
740753
}
741754
}
742755
}
@@ -790,8 +803,8 @@ fn next_token_inner(rdr: @mut StringReader) -> token::Token {
790803
accum_str.push_char(scan_numeric_escape(rdr, 8u));
791804
}
792805
c2 => {
793-
fatal_span(rdr, escaped_pos, rdr.last_pos,
794-
fmt!("unknown string escape: %d", c2 as int));
806+
fatal_span_char(rdr, escaped_pos, rdr.last_pos,
807+
~"unknown string escape", c2);
795808
}
796809
}
797810
}
@@ -827,10 +840,8 @@ fn next_token_inner(rdr: @mut StringReader) -> token::Token {
827840
'^' => { return binop(rdr, token::CARET); }
828841
'%' => { return binop(rdr, token::PERCENT); }
829842
c => {
830-
let mut cs = ~"";
831-
char::escape_default(c, |c| cs.push_char(c));
832-
fatal_span(rdr, rdr.last_pos, rdr.pos,
833-
fmt!("unknown start of token: %s", cs));
843+
fatal_span_char(rdr, rdr.last_pos, rdr.pos,
844+
~"unknown start of token", c);
834845
}
835846
}
836847
}

0 commit comments

Comments
 (0)