|
| 1 | +// Copyright 2014 The Rust Project Developers. See the COPYRIGHT |
| 2 | +// file at the top-level directory of this distribution and at |
| 3 | +// http://rust-lang.org/COPYRIGHT. |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 7 | +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 8 | +// option. This file may not be copied, modified, or distributed |
| 9 | +// except according to those terms. |
| 10 | + |
| 11 | +//! Basic html highlighting functionality |
| 12 | +//! |
| 13 | +//! This module uses libsyntax's lexer to provide token-based highlighting for |
| 14 | +//! the HTML documentation generated by rustdoc. |
| 15 | +
|
| 16 | +use std::str; |
| 17 | +use std::io; |
| 18 | + |
| 19 | +use syntax::parse; |
| 20 | +use syntax::parse::lexer; |
| 21 | +use syntax::diagnostic; |
| 22 | +use syntax::codemap::{BytePos, Span}; |
| 23 | + |
| 24 | +use html::escape::Escape; |
| 25 | + |
| 26 | +use t = syntax::parse::token; |
| 27 | + |
| 28 | +/// Highlights some source code, returning the HTML output. |
| 29 | +pub fn highlight(src: &str) -> ~str { |
| 30 | + let sess = parse::new_parse_sess(); |
| 31 | + let handler = diagnostic::mk_handler(); |
| 32 | + let span_handler = diagnostic::mk_span_handler(handler, sess.cm); |
| 33 | + let fm = parse::string_to_filemap(sess, src.to_owned(), ~"<stdin>"); |
| 34 | +
|
| 35 | + let mut out = io::MemWriter::new(); |
| 36 | + doit(sess, |
| 37 | + lexer::new_string_reader(span_handler, fm), |
| 38 | + &mut out).unwrap(); |
| 39 | + str::from_utf8_lossy(out.unwrap()).into_owned() |
| 40 | +} |
| 41 | +
|
| 42 | +/// Exhausts the `lexer` writing the output into `out`. |
| 43 | +/// |
| 44 | +/// The general structure for this method is to iterate over each token, |
| 45 | +/// possibly giving it an HTML span with a class specifying what flavor of token |
| 46 | +/// it's used. All source code emission is done as slices from the source map, |
| 47 | +/// not from the tokens themselves, in order to stay true to the original |
| 48 | +/// source. |
| 49 | +fn doit(sess: @parse::ParseSess, lexer: lexer::StringReader, |
| 50 | + out: &mut Writer) -> io::IoResult<()> { |
| 51 | + use syntax::parse::lexer::Reader; |
| 52 | +
|
| 53 | + try!(write!(out, "<pre class='rust'>\n")); |
| 54 | + let mut last = BytePos(0); |
| 55 | + let mut is_attribute = false; |
| 56 | + let mut is_macro = false; |
| 57 | + loop { |
| 58 | + let next = lexer.next_token(); |
| 59 | + let test = if next.tok == t::EOF {lexer.pos.get()} else {next.sp.lo}; |
| 60 | + |
| 61 | + // The lexer consumes all whitespace and non-doc-comments when iterating |
| 62 | + // between tokens. If this token isn't directly adjacent to our last |
| 63 | + // token, then we need to emit the whitespace/comment. |
| 64 | + // |
| 65 | + // If the gap has any '/' characters then we consider the whole thing a |
| 66 | + // comment. This will classify some whitespace as a comment, but that |
| 67 | + // doesn't matter too much for syntax highlighting purposes. |
| 68 | + if test > last { |
| 69 | + let snip = sess.cm.span_to_snippet(Span { |
| 70 | + lo: last, |
| 71 | + hi: test, |
| 72 | + expn_info: None, |
| 73 | + }).unwrap(); |
| 74 | + if snip.contains("/") { |
| 75 | + try!(write!(out, "<span class='comment'>{}</span>", |
| 76 | + Escape(snip))); |
| 77 | + } else { |
| 78 | + try!(write!(out, "{}", Escape(snip))); |
| 79 | + } |
| 80 | + } |
| 81 | + last = next.sp.hi; |
| 82 | + if next.tok == t::EOF { break } |
| 83 | + |
| 84 | + let klass = match next.tok { |
| 85 | + // If this '&' token is directly adjacent to another token, assume |
| 86 | + // that it's the address-of operator instead of the and-operator. |
| 87 | + // This allows us to give all pointers their own class (~ and @ are |
| 88 | + // below). |
| 89 | + t::BINOP(t::AND) if lexer.peek().sp.lo == next.sp.hi => "kw-2", |
| 90 | + t::AT | t::TILDE => "kw-2", |
| 91 | + |
| 92 | + // consider this as part of a macro invocation if there was a |
| 93 | + // leading identifier |
| 94 | + t::NOT if is_macro => { is_macro = false; "macro" } |
| 95 | + |
| 96 | + // operators |
| 97 | + t::EQ | t::LT | t::LE | t::EQEQ | t::NE | t::GE | t::GT | |
| 98 | + t::ANDAND | t::OROR | t::NOT | t::BINOP(..) | t::RARROW | |
| 99 | + t::BINOPEQ(..) | t::FAT_ARROW => "op", |
| 100 | + |
| 101 | + // miscellaneous, no highlighting |
| 102 | + t::DOT | t::DOTDOT | t::DOTDOTDOT | t::COMMA | t::SEMI | |
| 103 | + t::COLON | t::MOD_SEP | t::LARROW | t::DARROW | t::LPAREN | |
| 104 | + t::RPAREN | t::LBRACKET | t::LBRACE | t::RBRACE | |
| 105 | + t::DOLLAR => "", |
| 106 | + |
| 107 | + // This is the start of an attribute. We're going to want to |
| 108 | + // continue highlighting it as an attribute until the ending ']' is |
| 109 | + // seen, so skip out early. Down below we terminate the attribute |
| 110 | + // span when we see the ']'. |
| 111 | + t::POUND => { |
| 112 | + is_attribute = true; |
| 113 | + try!(write!(out, r"<span class='attribute'>\#")); |
| 114 | + continue |
| 115 | + } |
| 116 | + t::RBRACKET => { |
| 117 | + if is_attribute { |
| 118 | + is_attribute = false; |
| 119 | + try!(write!(out, "]</span>")); |
| 120 | + continue |
| 121 | + } else { |
| 122 | + "" |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + // text literals |
| 127 | + t::LIT_CHAR(..) | t::LIT_STR(..) | t::LIT_STR_RAW(..) => "string", |
| 128 | + |
| 129 | + // number literals |
| 130 | + t::LIT_INT(..) | t::LIT_UINT(..) | t::LIT_INT_UNSUFFIXED(..) | |
| 131 | + t::LIT_FLOAT(..) | t::LIT_FLOAT_UNSUFFIXED(..) => "number", |
| 132 | + |
| 133 | + // keywords are also included in the identifier set |
| 134 | + t::IDENT(ident, _is_mod_sep) => { |
| 135 | + match t::get_ident(ident).get() { |
| 136 | + "ref" | "mut" => "kw-2", |
| 137 | + |
| 138 | + "self" => "self", |
| 139 | + "false" | "true" => "boolval", |
| 140 | + |
| 141 | + "Option" | "Result" => "prelude-ty", |
| 142 | + "Some" | "None" | "Ok" | "Err" => "prelude-val", |
| 143 | + |
| 144 | + _ if t::is_any_keyword(&next.tok) => "kw", |
| 145 | + _ => { |
| 146 | + if lexer.peek().tok == t::NOT { |
| 147 | + is_macro = true; |
| 148 | + "macro" |
| 149 | + } else { |
| 150 | + "ident" |
| 151 | + } |
| 152 | + } |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + t::LIFETIME(..) => "lifetime", |
| 157 | + t::DOC_COMMENT(..) => "doccomment", |
| 158 | + t::UNDERSCORE | t::EOF | t::INTERPOLATED(..) => "", |
| 159 | + }; |
| 160 | + |
| 161 | + // as mentioned above, use the original source code instead of |
| 162 | + // stringifying this token |
| 163 | + let snip = sess.cm.span_to_snippet(next.sp).unwrap(); |
| 164 | + if klass == "" { |
| 165 | + try!(write!(out, "{}", Escape(snip))); |
| 166 | + } else { |
| 167 | + try!(write!(out, "<span class='{}'>{}</span>", klass, |
| 168 | + Escape(snip))); |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + write!(out, "</pre>\n") |
| 173 | +} |
| 174 | + |
0 commit comments