]> git.lizzy.rs Git - rust.git/commitdiff
lexer: further slight improvements to lexer errors
authorBenjamin Herr <ben@0x539.de>
Thu, 19 Sep 2013 20:34:31 +0000 (22:34 +0200)
committerBenjamin Herr <ben@0x539.de>
Thu, 19 Sep 2013 21:08:06 +0000 (23:08 +0200)
src/libsyntax/parse/lexer.rs
src/test/compile-fail/lex-illegal-num-char-escape.rs

index 9e5e81c436f9fb7b55cc40333eb242617d3a3eda..d3b0866d4a79c4635ab7325bc78a9f8625406935 100644 (file)
@@ -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) {
@@ -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' {
@@ -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);
@@ -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;
@@ -754,27 +770,25 @@ fn binop(rdr: @mut StringReader, op: token::binop) -> 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;
index 2760371b8e2aa69dfbd16f191fd99bec0d74c53d..858afffb443f38a48c68bccc7d0cc5ca7e634c18 100644 (file)
@@ -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
 ;