about summary refs log tree commit diff
path: root/src/librustdoc/html
diff options
context:
space:
mode:
authormitaa <mitaa.ceb@gmail.com>2016-04-25 17:20:32 +0200
committermitaa <mitaa.ceb@gmail.com>2016-04-27 20:48:18 +0200
commit6887202ea3a1d3e3df0c88c07c754defd87b9712 (patch)
tree8482535a9f58e567728140d8be863f1add112ef8 /src/librustdoc/html
parent645dd013ac6b334ab5bf7a7240c2243f4bb590c9 (diff)
downloadrust-6887202ea3a1d3e3df0c88c07c754defd87b9712.tar.gz
rust-6887202ea3a1d3e3df0c88c07c754defd87b9712.zip
Make some fatal lexer errors recoverable
Diffstat (limited to 'src/librustdoc/html')
-rw-r--r--src/librustdoc/html/highlight.rs26
1 files changed, 19 insertions, 7 deletions
diff --git a/src/librustdoc/html/highlight.rs b/src/librustdoc/html/highlight.rs
index 2f0ae540fbf..789239c3ab0 100644
--- a/src/librustdoc/html/highlight.rs
+++ b/src/librustdoc/html/highlight.rs
@@ -29,9 +29,11 @@ pub fn render_with_highlighting(src: &str, class: Option<&str>, id: Option<&str>
 
     let mut out = Vec::new();
     write_header(class, id, &mut out).unwrap();
-    write_source(&sess,
-                 lexer::StringReader::new(&sess.span_diagnostic, fm),
-                 &mut out).unwrap();
+    if let Err(_) = write_source(&sess,
+                                 lexer::StringReader::new(&sess.span_diagnostic, fm),
+                                 &mut out) {
+        return format!("<pre>{}</pre>", src)
+    }
     write_footer(&mut out).unwrap();
     String::from_utf8_lossy(&out[..]).into_owned()
 }
@@ -39,15 +41,15 @@ pub fn render_with_highlighting(src: &str, class: Option<&str>, id: Option<&str>
 /// Highlights `src`, returning the HTML output. Returns only the inner html to
 /// be inserted into an element. C.f., `render_with_highlighting` which includes
 /// an enclosing `<pre>` block.
-pub fn render_inner_with_highlighting(src: &str) -> String {
+pub fn render_inner_with_highlighting(src: &str) -> io::Result<String> {
     let sess = parse::ParseSess::new();
     let fm = sess.codemap().new_filemap("<stdin>".to_string(), src.to_string());
 
     let mut out = Vec::new();
     write_source(&sess,
                  lexer::StringReader::new(&sess.span_diagnostic, fm),
-                 &mut out).unwrap();
-    String::from_utf8_lossy(&out[..]).into_owned()
+                 &mut out)?;
+    Ok(String::from_utf8_lossy(&out[..]).into_owned())
 }
 
 /// Exhausts the `lexer` writing the output into `out`.
@@ -65,7 +67,17 @@ fn write_source(sess: &parse::ParseSess,
     let mut is_macro = false;
     let mut is_macro_nonterminal = false;
     loop {
-        let next = lexer.next_token();
+        let next = match lexer.try_next_token() {
+            Ok(tok) => tok,
+            Err(_) => {
+                lexer.emit_fatal_errors();
+                lexer.span_diagnostic.struct_warn("Backing out of syntax highlighting")
+                                     .note("You probably did not intend to render this \
+                                            as a rust code-block")
+                                     .emit();
+                return Err(io::Error::new(io::ErrorKind::Other, ""))
+            },
+        };
 
         let snip = |sp| sess.codemap().span_to_snippet(sp).unwrap();