1 //! Basic syntax highlighting functionality.
3 //! This module uses libsyntax's lexer to provide token-based highlighting for
4 //! the HTML documentation generated by rustdoc.
6 //! Use the `render_with_highlighting` to highlight some rust code.
8 use crate::html::escape::Escape;
10 use std::fmt::Display;
12 use std::io::prelude::*;
14 use rustc_parse::lexer;
15 use rustc_span::source_map::SourceMap;
16 use rustc_span::symbol::{kw, sym};
17 use rustc_span::{FileName, Span};
18 use syntax::sess::ParseSess;
19 use syntax::token::{self, Token};
21 /// Highlights `src`, returning the HTML output.
22 pub fn render_with_highlighting(
25 extension: Option<&str>,
26 tooltip: Option<(&str, &str)>,
28 debug!("highlighting: ================\n{}\n==============", src);
29 let mut out = Vec::new();
30 if let Some((tooltip, class)) = tooltip {
33 "<div class='information'><div class='tooltip {}'>ⓘ<span \
34 class='tooltiptext'>{}</span></div></div>",
40 let sess = ParseSess::with_silent_emitter();
43 .new_source_file(FileName::Custom(String::from("rustdoc-highlighting")), src.to_owned());
44 let highlight_result = {
45 let lexer = lexer::StringReader::new(&sess, fm, None);
46 let mut classifier = Classifier::new(lexer, sess.source_map());
48 let mut highlighted_source = vec![];
49 if classifier.write_source(&mut highlighted_source).is_err() {
52 Ok(String::from_utf8_lossy(&highlighted_source).into_owned())
56 match highlight_result {
57 Ok(highlighted_source) => {
58 write_header(class, &mut out).unwrap();
59 write!(out, "{}", highlighted_source).unwrap();
60 if let Some(extension) = extension {
61 write!(out, "{}", extension).unwrap();
63 write_footer(&mut out).unwrap();
66 // If errors are encountered while trying to highlight, just emit
67 // the unhighlighted source.
68 write!(out, "<pre><code>{}</code></pre>", Escape(src)).unwrap();
72 String::from_utf8_lossy(&out[..]).into_owned()
75 /// Processes a program (nested in the internal `lexer`), classifying strings of
76 /// text by highlighting category (`Class`). Calls out to a `Writer` to write
77 /// each span of text in sequence.
78 struct Classifier<'a> {
79 lexer: lexer::StringReader<'a>,
80 peek_token: Option<Token>,
81 source_map: &'a SourceMap,
83 // State of the classifier.
86 in_macro_nonterminal: bool,
89 /// How a span of text is classified. Mostly corresponds to token kinds.
90 #[derive(Clone, Copy, Debug, Eq, PartialEq)]
97 // Keywords that do pointer/reference stuff.
113 /// Trait that controls writing the output of syntax highlighting. Users should
114 /// implement this trait to customize writing output.
116 /// The classifier will call into the `Writer` implementation as it finds spans
117 /// of text to highlight. Exactly how that text should be highlighted is up to
118 /// the implementation.
120 /// Called when we start processing a span of text that should be highlighted.
121 /// The `Class` argument specifies how it should be highlighted.
122 fn enter_span(&mut self, _: Class) -> io::Result<()>;
124 /// Called at the end of a span of highlighted text.
125 fn exit_span(&mut self) -> io::Result<()>;
127 /// Called for a span of text. If the text should be highlighted differently from the
128 /// surrounding text, then the `Class` argument will be a value other than `None`.
130 /// The following sequences of callbacks are equivalent:
132 /// enter_span(Foo), string("text", None), exit_span()
133 /// string("text", Foo)
135 /// The latter can be thought of as a shorthand for the former, which is
137 fn string<T: Display>(&mut self, text: T, klass: Class) -> io::Result<()>;
140 // Implement `Writer` for anthing that can be written to, this just implements
141 // the default rustdoc behaviour.
142 impl<U: Write> Writer for U {
143 fn string<T: Display>(&mut self, text: T, klass: Class) -> io::Result<()> {
145 Class::None => write!(self, "{}", text),
146 klass => write!(self, "<span class=\"{}\">{}</span>", klass.rustdoc_class(), text),
150 fn enter_span(&mut self, klass: Class) -> io::Result<()> {
151 write!(self, "<span class=\"{}\">", klass.rustdoc_class())
154 fn exit_span(&mut self) -> io::Result<()> {
155 write!(self, "</span>")
159 enum HighlightError {
164 impl From<io::Error> for HighlightError {
165 fn from(err: io::Error) -> Self {
166 HighlightError::IoError(err)
170 impl<'a> Classifier<'a> {
171 fn new(lexer: lexer::StringReader<'a>, source_map: &'a SourceMap) -> Classifier<'a> {
178 in_macro_nonterminal: false,
182 /// Gets the next token out of the lexer.
183 fn try_next_token(&mut self) -> Result<Token, HighlightError> {
184 if let Some(token) = self.peek_token.take() {
187 let token = self.lexer.next_token();
188 if let token::Unknown(..) = &token.kind {
189 return Err(HighlightError::LexError);
194 fn peek(&mut self) -> Result<&Token, HighlightError> {
195 if self.peek_token.is_none() {
196 let token = self.lexer.next_token();
197 if let token::Unknown(..) = &token.kind {
198 return Err(HighlightError::LexError);
200 self.peek_token = Some(token);
202 Ok(self.peek_token.as_ref().unwrap())
205 /// Exhausts the `lexer` writing the output into `out`.
207 /// The general structure for this method is to iterate over each token,
208 /// possibly giving it an HTML span with a class specifying what flavor of token
209 /// is used. All source code emission is done as slices from the source map,
210 /// not from the tokens themselves, in order to stay true to the original
212 fn write_source<W: Writer>(&mut self, out: &mut W) -> Result<(), HighlightError> {
214 let next = self.try_next_token()?;
215 if next == token::Eof {
219 self.write_token(out, next)?;
225 // Handles an individual token from the lexer.
226 fn write_token<W: Writer>(&mut self, out: &mut W, token: Token) -> Result<(), HighlightError> {
227 let klass = match token.kind {
228 token::Shebang(s) => {
229 out.string(Escape(&s.as_str()), Class::None)?;
233 token::Whitespace | token::Unknown(..) => Class::None,
234 token::Comment => Class::Comment,
235 token::DocComment(..) => Class::DocComment,
237 // If this '&' or '*' token is followed by a non-whitespace token, assume that it's the
238 // reference or dereference operator or a reference or pointer type, instead of the
239 // bit-and or multiplication operator.
240 token::BinOp(token::And) | token::BinOp(token::Star)
241 if self.peek()? != &token::Whitespace =>
246 // Consider this as part of a macro invocation if there was a
247 // leading identifier.
248 token::Not if self.in_macro => {
249 self.in_macro = false;
267 | token::FatArrow => Class::Op,
269 // Miscellaneous, no highlighting.
279 | token::OpenDelim(_)
280 | token::CloseDelim(token::Brace)
281 | token::CloseDelim(token::Paren)
282 | token::CloseDelim(token::NoDelim) => Class::None,
284 token::Question => Class::QuestionMark,
287 if self.peek()?.is_ident() {
288 self.in_macro_nonterminal = true;
289 Class::MacroNonTerminal
295 // This might be the start of an attribute. We're going to want to
296 // continue highlighting it as an attribute until the ending ']' is
297 // seen, so skip out early. Down below we terminate the attribute
298 // span when we see the ']'.
300 // We can't be sure that our # begins an attribute (it could
301 // just be appearing in a macro) until we read either `#![` or
302 // `#[` from the input stream.
304 // We don't want to start highlighting as an attribute until
305 // we're confident there is going to be a ] coming up, as
306 // otherwise # tokens in macros highlight the rest of the input
309 // Case 1: #![inner_attribute]
310 if self.peek()? == &token::Not {
311 self.try_next_token()?; // NOTE: consumes `!` token!
312 if self.peek()? == &token::OpenDelim(token::Bracket) {
313 self.in_attribute = true;
314 out.enter_span(Class::Attribute)?;
316 out.string("#", Class::None)?;
317 out.string("!", Class::None)?;
321 // Case 2: #[outer_attribute]
322 if self.peek()? == &token::OpenDelim(token::Bracket) {
323 self.in_attribute = true;
324 out.enter_span(Class::Attribute)?;
326 out.string("#", Class::None)?;
329 token::CloseDelim(token::Bracket) => {
330 if self.in_attribute {
331 self.in_attribute = false;
332 out.string("]", Class::None)?;
340 token::Literal(lit) => {
347 | token::ByteStrRaw(..)
349 | token::StrRaw(..) => Class::String,
352 token::Integer | token::Float => Class::Number,
354 token::Bool => panic!("literal token contains `Lit::Bool`"),
358 // Keywords are also included in the identifier set.
359 token::Ident(name, is_raw) => match name {
360 kw::Ref | kw::Mut if !is_raw => Class::RefKeyWord,
362 kw::SelfLower | kw::SelfUpper => Class::Self_,
363 kw::False | kw::True if !is_raw => Class::Bool,
365 sym::Option | sym::Result => Class::PreludeTy,
366 sym::Some | sym::None | sym::Ok | sym::Err => Class::PreludeVal,
368 _ if token.is_reserved_ident() => Class::KeyWord,
371 if self.in_macro_nonterminal {
372 self.in_macro_nonterminal = false;
373 Class::MacroNonTerminal
374 } else if self.peek()? == &token::Not {
375 self.in_macro = true;
383 token::Lifetime(..) => Class::Lifetime,
386 | token::Interpolated(..)
389 | token::SingleQuote => Class::None,
392 // Anything that didn't return above is the simple case where we the
393 // class just spans a single token, so we can use the `string` method.
394 out.string(Escape(&self.snip(token.span)), klass)?;
399 // Helper function to get a snippet from the source_map.
400 fn snip(&self, sp: Span) -> String {
401 self.source_map.span_to_snippet(sp).unwrap()
406 /// Returns the css class expected by rustdoc for each `Class`.
407 fn rustdoc_class(self) -> &'static str {
410 Class::Comment => "comment",
411 Class::DocComment => "doccomment",
412 Class::Attribute => "attribute",
413 Class::KeyWord => "kw",
414 Class::RefKeyWord => "kw-2",
415 Class::Self_ => "self",
417 Class::Macro => "macro",
418 Class::MacroNonTerminal => "macro-nonterminal",
419 Class::String => "string",
420 Class::Number => "number",
421 Class::Bool => "bool-val",
422 Class::Ident => "ident",
423 Class::Lifetime => "lifetime",
424 Class::PreludeTy => "prelude-ty",
425 Class::PreludeVal => "prelude-val",
426 Class::QuestionMark => "question-mark",
431 fn write_header(class: Option<&str>, out: &mut dyn Write) -> io::Result<()> {
432 write!(out, "<div class=\"example-wrap\"><pre class=\"rust {}\">\n", class.unwrap_or(""))
435 fn write_footer(out: &mut dyn Write) -> io::Result<()> {
436 write!(out, "</pre></div>\n")