]> git.lizzy.rs Git - rust.git/commitdiff
Start pushing panics outward in lexer.
authorEli Friedman <eli.friedman@gmail.com>
Sat, 24 Oct 2015 02:20:03 +0000 (19:20 -0700)
committerEli Friedman <eli.friedman@gmail.com>
Wed, 28 Oct 2015 03:09:10 +0000 (20:09 -0700)
src/libsyntax/parse/lexer/comments.rs
src/libsyntax/parse/lexer/mod.rs

index 137996a35ee8f57cb119e3def16dbbe1fa8be729..e5e2c3a986db374b76b03711b51daf16f9eabbee 100644 (file)
@@ -270,7 +270,7 @@ fn read_block_comment(rdr: &mut StringReader,
         while level > 0 {
             debug!("=== block comment level {}", level);
             if rdr.is_eof() {
-                rdr.fatal("unterminated block comment");
+                panic!(rdr.fatal("unterminated block comment"));
             }
             if rdr.curr_is('\n') {
                 trim_whitespace_prefix_and_push_line(&mut lines,
index 490822b934e5ed314a9d68946499f9bb4f31600f..9e38ffe7f0dc423c062fa15803c42cb89d26afbc 100644 (file)
@@ -11,6 +11,7 @@
 use ast;
 use codemap::{BytePos, CharPos, CodeMap, Pos, Span};
 use codemap;
+use diagnostic::FatalError;
 use diagnostic::SpanHandler;
 use ext::tt::transcribe::tt_next_token;
 use parse::token::str_to_ident;
@@ -30,7 +31,7 @@ pub trait Reader {
     fn is_eof(&self) -> bool;
     fn next_token(&mut self) -> TokenAndSpan;
     /// Report a fatal error with the current span.
-    fn fatal(&self, &str) -> !;
+    fn fatal(&self, &str) -> FatalError;
     /// Report a non-fatal error with the current span.
     fn err(&self, &str);
     fn peek(&self) -> TokenAndSpan;
@@ -86,7 +87,7 @@ fn next_token(&mut self) -> TokenAndSpan {
         self.advance_token();
         ret_val
     }
-    fn fatal(&self, m: &str) -> ! {
+    fn fatal(&self, m: &str) -> FatalError {
         self.fatal_span(self.peek_span, m)
     }
     fn err(&self, m: &str) {
@@ -110,8 +111,8 @@ fn next_token(&mut self) -> TokenAndSpan {
         debug!("TtReader: r={:?}", r);
         r
     }
-    fn fatal(&self, m: &str) -> ! {
-        panic!(self.sp_diag.span_fatal(self.cur_span, m));
+    fn fatal(&self, m: &str) -> FatalError {
+        self.sp_diag.span_fatal(self.cur_span, m)
     }
     fn err(&self, m: &str) {
         self.sp_diag.span_err(self.cur_span, m);
@@ -163,8 +164,8 @@ pub fn curr_is(&self, c: char) -> bool {
     }
 
     /// Report a fatal lexical error with a given span.
-    pub fn fatal_span(&self, sp: Span, m: &str) -> ! {
-        panic!(self.span_diagnostic.span_fatal(sp, m))
+    pub fn fatal_span(&self, sp: Span, m: &str) -> FatalError {
+        self.span_diagnostic.span_fatal(sp, m)
     }
 
     /// Report a lexical error with a given span.
@@ -178,7 +179,7 @@ pub fn help_span(&self, sp: Span, m: &str) {
     }
 
     /// Report a fatal error spanning [`from_pos`, `to_pos`).
-    fn fatal_span_(&self, from_pos: BytePos, to_pos: BytePos, m: &str) -> ! {
+    fn fatal_span_(&self, from_pos: BytePos, to_pos: BytePos, m: &str) -> FatalError {
         self.fatal_span(codemap::mk_sp(from_pos, to_pos), m)
     }
 
@@ -194,11 +195,11 @@ fn help_span_(&self, from_pos: BytePos, to_pos: BytePos, m: &str) {
 
     /// Report a lexical error spanning [`from_pos`, `to_pos`), appending an
     /// escaped character to the error message
-    fn fatal_span_char(&self, from_pos: BytePos, to_pos: BytePos, m: &str, c: char) -> ! {
+    fn fatal_span_char(&self, from_pos: BytePos, to_pos: BytePos, m: &str, c: char) -> FatalError {
         let mut m = m.to_string();
         m.push_str(": ");
         for c in c.escape_default() { m.push(c) }
-        self.fatal_span_(from_pos, to_pos, &m[..]);
+        self.fatal_span_(from_pos, to_pos, &m[..])
     }
 
     /// Report a lexical error spanning [`from_pos`, `to_pos`), appending an
@@ -212,12 +213,12 @@ fn err_span_char(&self, from_pos: BytePos, to_pos: BytePos, m: &str, c: char) {
 
     /// Report a lexical error spanning [`from_pos`, `to_pos`), appending the
     /// offending string to the error message
-    fn fatal_span_verbose(&self, from_pos: BytePos, to_pos: BytePos, mut m: String) -> ! {
+    fn fatal_span_verbose(&self, from_pos: BytePos, to_pos: BytePos, mut m: String) -> FatalError {
         m.push_str(": ");
         let from = self.byte_offset(from_pos).to_usize();
         let to = self.byte_offset(to_pos).to_usize();
         m.push_str(&self.source_text[from..to]);
-        self.fatal_span_(from_pos, to_pos, &m[..]);
+        self.fatal_span_(from_pos, to_pos, &m[..])
     }
 
     /// Advance peek_tok and peek_span to refer to the next token, and
@@ -538,7 +539,7 @@ fn scan_block_comment(&mut self) -> Option<TokenAndSpan> {
                     "unterminated block comment"
                 };
                 let last_bpos = self.last_pos;
-                self.fatal_span_(start_bpos, last_bpos, msg);
+                panic!(self.fatal_span_(start_bpos, last_bpos, msg));
             }
             let n = self.curr.unwrap();
             match n {
@@ -682,7 +683,9 @@ fn scan_hex_digits(&mut self,
         for _ in 0..n_digits {
             if self.is_eof() {
                 let last_bpos = self.last_pos;
-                self.fatal_span_(start_bpos, last_bpos, "unterminated numeric character escape");
+                panic!(self.fatal_span_(start_bpos,
+                                        last_bpos,
+                                        "unterminated numeric character escape"));
             }
             if self.curr_is(delim) {
                 let last_bpos = self.last_pos;
@@ -835,15 +838,15 @@ fn scan_unicode_escape(&mut self, delim: char) -> bool {
             let c = match self.curr {
                 Some(c) => c,
                 None => {
-                    self.fatal_span_(start_bpos, self.last_pos,
-                                     "unterminated unicode escape (found EOF)");
+                    panic!(self.fatal_span_(start_bpos, self.last_pos,
+                                            "unterminated unicode escape (found EOF)"));
                 }
             };
             accum_int *= 16;
             accum_int += c.to_digit(16).unwrap_or_else(|| {
                 if c == delim {
-                    self.fatal_span_(self.last_pos, self.pos,
-                                     "unterminated unicode escape (needed a `}`)");
+                    panic!(self.fatal_span_(self.last_pos, self.pos,
+                                            "unterminated unicode escape (needed a `}`)"));
                 } else {
                     self.err_span_char(self.last_pos, self.pos,
                                    "invalid character in unicode escape", c);
@@ -1077,12 +1080,12 @@ fn next_token_inner(&mut self) -> token::Token {
             let valid = self.scan_char_or_byte(start, c2, /* ascii_only = */ false, '\'');
             if !self.curr_is('\'') {
                 let last_bpos = self.last_pos;
-                self.fatal_span_verbose(
+                panic!(self.fatal_span_verbose(
                                    // Byte offsetting here is okay because the
                                    // character before position `start` is an
                                    // ascii single quote.
                                    start - BytePos(1), last_bpos,
-                                   "unterminated character constant".to_string());
+                                   "unterminated character constant".to_string()));
             }
             let id = if valid { self.name_from(start) } else { token::intern("0") };
             self.bump(); // advance curr past token
@@ -1107,7 +1110,9 @@ fn next_token_inner(&mut self) -> token::Token {
             while !self.curr_is('"') {
                 if self.is_eof() {
                     let last_bpos = self.last_pos;
-                    self.fatal_span_(start_bpos, last_bpos, "unterminated double quote string");
+                    panic!(self.fatal_span_(start_bpos,
+                                            last_bpos,
+                                            "unterminated double quote string"));
                 }
 
                 let ch_start = self.last_pos;
@@ -1133,14 +1138,14 @@ fn next_token_inner(&mut self) -> token::Token {
 
             if self.is_eof() {
                 let last_bpos = self.last_pos;
-                self.fatal_span_(start_bpos, last_bpos, "unterminated raw string");
+                panic!(self.fatal_span_(start_bpos, last_bpos, "unterminated raw string"));
             } else if !self.curr_is('"') {
                 let last_bpos = self.last_pos;
                 let curr_char = self.curr.unwrap();
-                self.fatal_span_char(start_bpos, last_bpos,
+                panic!(self.fatal_span_char(start_bpos, last_bpos,
                                 "found invalid character; \
                                  only `#` is allowed in raw string delimitation",
-                                curr_char);
+                                curr_char));
             }
             self.bump();
             let content_start_bpos = self.last_pos;
@@ -1149,7 +1154,7 @@ fn next_token_inner(&mut self) -> token::Token {
             'outer: loop {
                 if self.is_eof() {
                     let last_bpos = self.last_pos;
-                    self.fatal_span_(start_bpos, last_bpos, "unterminated raw string");
+                    panic!(self.fatal_span_(start_bpos, last_bpos, "unterminated raw string"));
                 }
                 //if self.curr_is('"') {
                     //content_end_bpos = self.last_pos;
@@ -1218,7 +1223,7 @@ fn next_token_inner(&mut self) -> token::Token {
           c => {
               let last_bpos = self.last_pos;
               let bpos = self.pos;
-              self.fatal_span_char(last_bpos, bpos, "unknown start of token", c);
+              panic!(self.fatal_span_char(last_bpos, bpos, "unknown start of token", c));
           }
         }
     }
@@ -1271,9 +1276,9 @@ fn scan_byte(&mut self) -> token::Lit {
             // character before position `start` are an
             // ascii single quote and ascii 'b'.
             let last_pos = self.last_pos;
-            self.fatal_span_verbose(
+            panic!(self.fatal_span_verbose(
                 start - BytePos(2), last_pos,
-                "unterminated byte constant".to_string());
+                "unterminated byte constant".to_string()));
         }
 
         let id = if valid { self.name_from(start) } else { token::intern("?") };
@@ -1293,8 +1298,7 @@ fn scan_byte_string(&mut self) -> token::Lit {
         while !self.curr_is('"') {
             if self.is_eof() {
                 let last_pos = self.last_pos;
-                self.fatal_span_(start, last_pos,
-                                  "unterminated double quote byte string");
+                panic!(self.fatal_span_(start, last_pos, "unterminated double quote byte string"));
             }
 
             let ch_start = self.last_pos;
@@ -1318,14 +1322,14 @@ fn scan_raw_byte_string(&mut self) -> token::Lit {
 
         if self.is_eof() {
             let last_pos = self.last_pos;
-            self.fatal_span_(start_bpos, last_pos, "unterminated raw string");
+            panic!(self.fatal_span_(start_bpos, last_pos, "unterminated raw string"));
         } else if !self.curr_is('"') {
             let last_pos = self.last_pos;
             let ch = self.curr.unwrap();
-            self.fatal_span_char(start_bpos, last_pos,
+            panic!(self.fatal_span_char(start_bpos, last_pos,
                             "found invalid character; \
                              only `#` is allowed in raw string delimitation",
-                            ch);
+                            ch));
         }
         self.bump();
         let content_start_bpos = self.last_pos;
@@ -1334,7 +1338,7 @@ fn scan_raw_byte_string(&mut self) -> token::Lit {
             match self.curr {
                 None => {
                     let last_pos = self.last_pos;
-                    self.fatal_span_(start_bpos, last_pos, "unterminated raw string")
+                    panic!(self.fatal_span_(start_bpos, last_pos, "unterminated raw string"))
                 },
                 Some('"') => {
                     content_end_bpos = self.last_pos;