2019-02-07 02:33:01 +09:00
|
|
|
pub use CommentStyle::*;
|
|
|
|
|
|
|
|
|
|
use crate::ast;
|
|
|
|
|
use crate::source_map::SourceMap;
|
|
|
|
|
use crate::parse::lexer::{is_block_doc_comment, is_pattern_whitespace};
|
|
|
|
|
use crate::parse::lexer::{self, ParseSess, StringReader, TokenAndSpan};
|
|
|
|
|
use crate::print::pprust;
|
2014-11-06 00:05:53 -08:00
|
|
|
|
2017-12-14 08:09:19 +01:00
|
|
|
use syntax_pos::{BytePos, CharPos, Pos, FileName};
|
2019-02-07 02:33:01 +09:00
|
|
|
use log::debug;
|
2012-12-23 17:41:37 -05:00
|
|
|
|
2015-02-26 21:00:43 -08:00
|
|
|
use std::io::Read;
|
2015-01-17 23:33:05 +00:00
|
|
|
use std::usize;
|
2013-05-24 19:35:29 -07:00
|
|
|
|
2016-10-12 20:54:41 +03:00
|
|
|
#[derive(Clone, Copy, PartialEq, Debug)]
|
2014-01-09 15:05:33 +02:00
|
|
|
pub enum CommentStyle {
|
2014-06-09 13:12:30 -07:00
|
|
|
/// No code on either side of each line of the comment
|
|
|
|
|
Isolated,
|
|
|
|
|
/// Code exists to the left of the comment
|
|
|
|
|
Trailing,
|
|
|
|
|
/// Code before /* foo */ and after the comment
|
|
|
|
|
Mixed,
|
|
|
|
|
/// Just a manual blank line "\n\n", for layout
|
|
|
|
|
BlankLine,
|
2012-04-15 03:27:24 -07:00
|
|
|
}
|
|
|
|
|
|
2015-01-03 22:54:18 -05:00
|
|
|
#[derive(Clone)]
|
2014-01-09 15:05:33 +02:00
|
|
|
pub struct Comment {
|
2014-03-27 15:39:48 -07:00
|
|
|
pub style: CommentStyle,
|
2014-05-22 16:57:53 -07:00
|
|
|
pub lines: Vec<String>,
|
2014-03-27 15:39:48 -07:00
|
|
|
pub pos: BytePos,
|
2013-02-21 00:16:31 -08:00
|
|
|
}
|
2012-04-15 03:27:24 -07:00
|
|
|
|
2018-05-31 16:53:30 -06:00
|
|
|
fn is_doc_comment(s: &str) -> bool {
|
2016-01-03 11:14:09 +02:00
|
|
|
(s.starts_with("///") && super::is_doc_comment(s)) || s.starts_with("//!") ||
|
|
|
|
|
(s.starts_with("/**") && is_block_doc_comment(s)) || s.starts_with("/*!")
|
2012-06-30 11:54:54 +01:00
|
|
|
}
|
|
|
|
|
|
2013-07-19 21:51:37 +10:00
|
|
|
pub fn doc_comment_style(comment: &str) -> ast::AttrStyle {
|
2013-03-28 18:39:09 -07:00
|
|
|
assert!(is_doc_comment(comment));
|
2013-05-19 01:07:44 -04:00
|
|
|
if comment.starts_with("//!") || comment.starts_with("/*!") {
|
2015-10-01 18:03:34 +02:00
|
|
|
ast::AttrStyle::Inner
|
2012-06-30 11:54:54 +01:00
|
|
|
} else {
|
2015-10-01 18:03:34 +02:00
|
|
|
ast::AttrStyle::Outer
|
2012-06-30 11:54:54 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-22 16:57:53 -07:00
|
|
|
pub fn strip_doc_comment_decoration(comment: &str) -> String {
|
2012-06-30 11:54:54 +01:00
|
|
|
/// remove whitespace-only lines from the start/end of lines
|
2015-02-13 07:33:44 +00:00
|
|
|
fn vertical_trim(lines: Vec<String>) -> Vec<String> {
|
2015-01-28 01:01:48 +00:00
|
|
|
let mut i = 0;
|
2013-06-04 21:43:41 -07:00
|
|
|
let mut j = lines.len();
|
2013-09-23 14:18:26 -07:00
|
|
|
// first line of all-stars should be omitted
|
2016-01-03 11:14:09 +02:00
|
|
|
if !lines.is_empty() && lines[0].chars().all(|c| c == '*') {
|
2013-09-23 14:18:26 -07:00
|
|
|
i += 1;
|
|
|
|
|
}
|
2018-08-12 15:43:51 +02:00
|
|
|
|
2014-11-27 15:00:50 -05:00
|
|
|
while i < j && lines[i].trim().is_empty() {
|
2013-09-23 14:18:26 -07:00
|
|
|
i += 1;
|
|
|
|
|
}
|
|
|
|
|
// like the first, a last line of all stars should be omitted
|
2016-01-03 11:14:09 +02:00
|
|
|
if j > i &&
|
|
|
|
|
lines[j - 1]
|
|
|
|
|
.chars()
|
|
|
|
|
.skip(1)
|
|
|
|
|
.all(|c| c == '*') {
|
2013-09-23 14:18:26 -07:00
|
|
|
j -= 1;
|
2012-06-30 11:54:54 +01:00
|
|
|
}
|
2018-08-12 15:43:51 +02:00
|
|
|
|
2014-11-27 15:00:50 -05:00
|
|
|
while j > i && lines[j - 1].trim().is_empty() {
|
2013-09-23 14:18:26 -07:00
|
|
|
j -= 1;
|
2012-06-30 11:54:54 +01:00
|
|
|
}
|
2018-08-12 15:43:51 +02:00
|
|
|
|
2017-05-12 20:05:39 +02:00
|
|
|
lines[i..j].to_vec()
|
2012-06-30 11:54:54 +01:00
|
|
|
}
|
|
|
|
|
|
2013-06-14 18:37:29 -04:00
|
|
|
/// remove a "[ \t]*\*" block from each line, if possible
|
2016-01-03 11:14:09 +02:00
|
|
|
fn horizontal_trim(lines: Vec<String>) -> Vec<String> {
|
2015-01-17 23:33:05 +00:00
|
|
|
let mut i = usize::MAX;
|
2013-06-14 18:37:29 -04:00
|
|
|
let mut can_trim = true;
|
|
|
|
|
let mut first = true;
|
2018-08-12 15:43:51 +02:00
|
|
|
|
2015-01-31 12:20:46 -05:00
|
|
|
for line in &lines {
|
2014-11-27 15:00:50 -05:00
|
|
|
for (j, c) in line.chars().enumerate() {
|
2015-02-19 14:36:58 +01:00
|
|
|
if j > i || !"* \t".contains(c) {
|
2013-06-14 18:37:29 -04:00
|
|
|
can_trim = false;
|
2012-06-30 11:54:54 +01:00
|
|
|
break;
|
|
|
|
|
}
|
2013-06-14 18:37:29 -04:00
|
|
|
if c == '*' {
|
|
|
|
|
if first {
|
|
|
|
|
i = j;
|
|
|
|
|
first = false;
|
|
|
|
|
} else if i != j {
|
|
|
|
|
can_trim = false;
|
|
|
|
|
}
|
2012-06-30 11:54:54 +01:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-01-04 23:09:02 -08:00
|
|
|
if i >= line.len() {
|
2013-06-14 18:37:29 -04:00
|
|
|
can_trim = false;
|
|
|
|
|
}
|
|
|
|
|
if !can_trim {
|
|
|
|
|
break;
|
|
|
|
|
}
|
2012-06-30 11:54:54 +01:00
|
|
|
}
|
|
|
|
|
|
2013-06-14 18:37:29 -04:00
|
|
|
if can_trim {
|
2016-01-03 11:14:09 +02:00
|
|
|
lines.iter()
|
|
|
|
|
.map(|line| (&line[i + 1..line.len()]).to_string())
|
|
|
|
|
.collect()
|
2013-06-14 18:37:29 -04:00
|
|
|
} else {
|
|
|
|
|
lines
|
|
|
|
|
}
|
2012-06-30 11:54:54 +01:00
|
|
|
}
|
|
|
|
|
|
2013-09-23 14:18:26 -07:00
|
|
|
// one-line comments lose their prefix
|
2018-08-12 15:43:51 +02:00
|
|
|
const ONELINERS: &[&str] = &["///!", "///", "//!", "//"];
|
|
|
|
|
|
2015-02-27 15:36:53 +01:00
|
|
|
for prefix in ONELINERS {
|
2013-09-23 14:18:26 -07:00
|
|
|
if comment.starts_with(*prefix) {
|
2015-01-07 11:58:31 -05:00
|
|
|
return (&comment[prefix.len()..]).to_string();
|
2013-09-23 14:18:26 -07:00
|
|
|
}
|
2012-06-30 11:54:54 +01:00
|
|
|
}
|
|
|
|
|
|
2013-05-19 01:07:44 -04:00
|
|
|
if comment.starts_with("/*") {
|
2015-01-21 09:15:15 -08:00
|
|
|
let lines = comment[3..comment.len() - 2]
|
2016-01-03 11:14:09 +02:00
|
|
|
.lines()
|
|
|
|
|
.map(|s| s.to_string())
|
|
|
|
|
.collect::<Vec<String>>();
|
2013-06-14 18:37:29 -04:00
|
|
|
|
2012-06-30 11:54:54 +01:00
|
|
|
let lines = vertical_trim(lines);
|
2013-06-14 18:37:29 -04:00
|
|
|
let lines = horizontal_trim(lines);
|
|
|
|
|
|
2015-07-10 08:19:21 -04:00
|
|
|
return lines.join("\n");
|
2012-06-30 11:54:54 +01:00
|
|
|
}
|
|
|
|
|
|
2014-10-09 15:17:22 -04:00
|
|
|
panic!("not a doc-comment: {}", comment);
|
2012-06-30 11:54:54 +01:00
|
|
|
}
|
|
|
|
|
|
2019-02-07 02:33:01 +09:00
|
|
|
fn push_blank_line_comment(rdr: &StringReader<'_>, comments: &mut Vec<Comment>) {
|
2013-10-21 13:08:31 -07:00
|
|
|
debug!(">>> blank-line comment");
|
2014-01-09 15:05:33 +02:00
|
|
|
comments.push(Comment {
|
|
|
|
|
style: BlankLine,
|
2014-03-27 15:14:58 +02:00
|
|
|
lines: Vec::new(),
|
2016-10-04 11:46:54 +11:00
|
|
|
pos: rdr.pos,
|
2013-12-27 12:05:13 -08:00
|
|
|
});
|
2012-04-15 03:27:24 -07:00
|
|
|
}
|
|
|
|
|
|
2019-02-07 02:33:01 +09:00
|
|
|
fn consume_whitespace_counting_blank_lines(
|
|
|
|
|
rdr: &mut StringReader<'_>,
|
|
|
|
|
comments: &mut Vec<Comment>
|
|
|
|
|
) {
|
2016-10-04 11:55:58 +11:00
|
|
|
while is_pattern_whitespace(rdr.ch) && !rdr.is_eof() {
|
2016-10-12 20:54:41 +03:00
|
|
|
if rdr.ch_is('\n') {
|
2013-01-11 21:01:42 -08:00
|
|
|
push_blank_line_comment(rdr, &mut *comments);
|
2012-04-15 03:27:24 -07:00
|
|
|
}
|
2014-05-21 16:57:31 -07:00
|
|
|
rdr.bump();
|
2012-04-15 03:27:24 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-07 02:33:01 +09:00
|
|
|
fn read_shebang_comment(rdr: &mut StringReader<'_>,
|
2016-01-03 11:14:09 +02:00
|
|
|
code_to_the_left: bool,
|
2014-03-27 15:14:58 +02:00
|
|
|
comments: &mut Vec<Comment>) {
|
2013-10-21 13:08:31 -07:00
|
|
|
debug!(">>> shebang comment");
|
2016-10-04 11:46:54 +11:00
|
|
|
let p = rdr.pos;
|
2013-10-21 13:08:31 -07:00
|
|
|
debug!("<<< shebang comment");
|
2014-01-09 15:05:33 +02:00
|
|
|
comments.push(Comment {
|
2016-01-04 21:35:06 +02:00
|
|
|
style: if code_to_the_left { Trailing } else { Isolated },
|
2016-01-03 11:14:09 +02:00
|
|
|
lines: vec![rdr.read_one_line_comment()],
|
|
|
|
|
pos: p,
|
2012-06-30 11:54:54 +01:00
|
|
|
});
|
2012-05-22 17:49:16 -07:00
|
|
|
}
|
|
|
|
|
|
2019-02-07 02:33:01 +09:00
|
|
|
fn read_line_comments(rdr: &mut StringReader<'_>,
|
2016-01-03 11:14:09 +02:00
|
|
|
code_to_the_left: bool,
|
2014-03-27 15:14:58 +02:00
|
|
|
comments: &mut Vec<Comment>) {
|
2013-10-21 13:08:31 -07:00
|
|
|
debug!(">>> line comments");
|
2016-10-04 11:46:54 +11:00
|
|
|
let p = rdr.pos;
|
2014-05-22 16:57:53 -07:00
|
|
|
let mut lines: Vec<String> = Vec::new();
|
2016-10-04 11:55:58 +11:00
|
|
|
while rdr.ch_is('/') && rdr.nextch_is('/') {
|
2014-05-21 16:57:31 -07:00
|
|
|
let line = rdr.read_one_line_comment();
|
2013-10-21 13:08:31 -07:00
|
|
|
debug!("{}", line);
|
2014-05-07 16:33:43 -07:00
|
|
|
// Doc comments are not put in comments.
|
2015-02-18 14:48:57 -05:00
|
|
|
if is_doc_comment(&line[..]) {
|
2012-06-30 11:54:54 +01:00
|
|
|
break;
|
|
|
|
|
}
|
2012-09-26 17:33:34 -07:00
|
|
|
lines.push(line);
|
2014-05-21 16:57:31 -07:00
|
|
|
rdr.consume_non_eol_whitespace();
|
2012-04-15 03:27:24 -07:00
|
|
|
}
|
2013-10-21 13:08:31 -07:00
|
|
|
debug!("<<< line comments");
|
2012-06-30 11:54:54 +01:00
|
|
|
if !lines.is_empty() {
|
2014-01-09 15:05:33 +02:00
|
|
|
comments.push(Comment {
|
2016-01-04 21:35:06 +02:00
|
|
|
style: if code_to_the_left { Trailing } else { Isolated },
|
2017-08-06 22:54:09 -07:00
|
|
|
lines,
|
2016-01-03 11:14:09 +02:00
|
|
|
pos: p,
|
2012-06-30 11:54:54 +01:00
|
|
|
});
|
|
|
|
|
}
|
2012-04-15 03:27:24 -07:00
|
|
|
}
|
|
|
|
|
|
2014-06-09 13:12:30 -07:00
|
|
|
/// Returns None if the first col chars of s contain a non-whitespace char.
|
|
|
|
|
/// Otherwise returns Some(k) where k is first char offset after that leading
|
|
|
|
|
/// whitespace. Note k may be outside bounds of s.
|
2015-01-17 23:33:05 +00:00
|
|
|
fn all_whitespace(s: &str, col: CharPos) -> Option<usize> {
|
2018-10-27 21:41:26 +09:00
|
|
|
let mut idx = 0;
|
|
|
|
|
for (i, ch) in s.char_indices().take(col.to_usize()) {
|
2015-03-10 16:29:02 -07:00
|
|
|
if !ch.is_whitespace() {
|
2013-05-27 12:08:37 +02:00
|
|
|
return None;
|
|
|
|
|
}
|
2018-12-05 12:15:28 +09:00
|
|
|
idx = i + ch.len_utf8();
|
2012-08-01 17:30:05 -07:00
|
|
|
}
|
2018-10-27 21:41:26 +09:00
|
|
|
Some(idx)
|
2012-04-15 03:27:24 -07:00
|
|
|
}
|
|
|
|
|
|
2016-01-03 11:14:09 +02:00
|
|
|
fn trim_whitespace_prefix_and_push_line(lines: &mut Vec<String>, s: String, col: CharPos) {
|
2013-05-09 13:27:24 -07:00
|
|
|
let len = s.len();
|
2015-02-18 14:48:57 -05:00
|
|
|
let s1 = match all_whitespace(&s[..], col) {
|
2013-05-27 12:08:37 +02:00
|
|
|
Some(col) => {
|
|
|
|
|
if col < len {
|
2018-10-27 21:41:26 +09:00
|
|
|
s[col..len].to_string()
|
2014-05-07 16:33:43 -07:00
|
|
|
} else {
|
2018-08-23 10:14:52 +02:00
|
|
|
String::new()
|
2014-05-07 16:33:43 -07:00
|
|
|
}
|
2013-05-27 12:08:37 +02:00
|
|
|
}
|
|
|
|
|
None => s,
|
|
|
|
|
};
|
2013-10-21 13:08:31 -07:00
|
|
|
debug!("pushing line: {}", s1);
|
2012-09-26 17:33:34 -07:00
|
|
|
lines.push(s1);
|
2012-04-15 03:27:24 -07:00
|
|
|
}
|
|
|
|
|
|
2019-02-07 02:33:01 +09:00
|
|
|
fn read_block_comment(rdr: &mut StringReader<'_>,
|
2013-02-04 14:02:01 -08:00
|
|
|
code_to_the_left: bool,
|
2016-01-03 11:14:09 +02:00
|
|
|
comments: &mut Vec<Comment>) {
|
2013-10-21 13:08:31 -07:00
|
|
|
debug!(">>> block comment");
|
2016-10-04 11:46:54 +11:00
|
|
|
let p = rdr.pos;
|
2014-05-22 16:57:53 -07:00
|
|
|
let mut lines: Vec<String> = Vec::new();
|
2018-05-08 19:58:54 +10:00
|
|
|
|
|
|
|
|
// Count the number of chars since the start of the line by rescanning.
|
2018-10-27 21:41:26 +09:00
|
|
|
let src_index = rdr.src_index(rdr.source_file.line_begin_pos(rdr.pos));
|
2018-05-08 19:58:54 +10:00
|
|
|
let end_src_index = rdr.src_index(rdr.pos);
|
2018-06-28 10:45:57 +02:00
|
|
|
assert!(src_index <= end_src_index,
|
|
|
|
|
"src_index={}, end_src_index={}, line_begin_pos={}",
|
2018-08-18 12:13:56 +02:00
|
|
|
src_index, end_src_index, rdr.source_file.line_begin_pos(rdr.pos).to_u32());
|
2018-08-12 15:43:51 +02:00
|
|
|
|
2018-10-27 21:41:26 +09:00
|
|
|
let col = CharPos(rdr.src[src_index..end_src_index].chars().count());
|
2018-05-08 19:58:54 +10:00
|
|
|
|
2014-05-21 16:57:31 -07:00
|
|
|
rdr.bump();
|
|
|
|
|
rdr.bump();
|
2012-06-30 11:54:54 +01:00
|
|
|
|
2015-04-16 21:19:50 -07:00
|
|
|
let mut curr_line = String::from("/*");
|
2013-02-13 11:37:07 +09:00
|
|
|
|
2012-06-30 11:54:54 +01:00
|
|
|
// doc-comments are not really comments, they are attributes
|
2016-10-04 11:55:58 +11:00
|
|
|
if (rdr.ch_is('*') && !rdr.nextch_is('*')) || rdr.ch_is('!') {
|
|
|
|
|
while !(rdr.ch_is('*') && rdr.nextch_is('/')) && !rdr.is_eof() {
|
|
|
|
|
curr_line.push(rdr.ch.unwrap());
|
2014-05-21 16:57:31 -07:00
|
|
|
rdr.bump();
|
2012-06-30 11:54:54 +01:00
|
|
|
}
|
2014-05-21 16:57:31 -07:00
|
|
|
if !rdr.is_eof() {
|
2013-06-11 19:13:42 -07:00
|
|
|
curr_line.push_str("*/");
|
2014-05-21 16:57:31 -07:00
|
|
|
rdr.bump();
|
|
|
|
|
rdr.bump();
|
2012-06-30 11:54:54 +01:00
|
|
|
}
|
2015-02-18 14:48:57 -05:00
|
|
|
if is_block_doc_comment(&curr_line[..]) {
|
2016-01-03 11:14:09 +02:00
|
|
|
return;
|
2014-04-02 16:54:22 -07:00
|
|
|
}
|
2015-02-19 14:36:58 +01:00
|
|
|
assert!(!curr_line.contains('\n'));
|
2014-05-07 16:33:43 -07:00
|
|
|
lines.push(curr_line);
|
2013-02-13 11:37:07 +09:00
|
|
|
} else {
|
2015-01-18 00:18:19 +00:00
|
|
|
let mut level: isize = 1;
|
2013-02-13 11:37:07 +09:00
|
|
|
while level > 0 {
|
2013-10-21 13:08:31 -07:00
|
|
|
debug!("=== block comment level {}", level);
|
2014-05-21 16:57:31 -07:00
|
|
|
if rdr.is_eof() {
|
2018-01-21 12:47:58 +01:00
|
|
|
rdr.fatal("unterminated block comment").raise();
|
2013-02-13 11:37:07 +09:00
|
|
|
}
|
2016-10-04 11:55:58 +11:00
|
|
|
if rdr.ch_is('\n') {
|
2016-01-03 11:14:09 +02:00
|
|
|
trim_whitespace_prefix_and_push_line(&mut lines, curr_line, col);
|
2014-05-22 16:57:53 -07:00
|
|
|
curr_line = String::new();
|
2014-05-21 16:57:31 -07:00
|
|
|
rdr.bump();
|
2012-04-15 03:27:24 -07:00
|
|
|
} else {
|
2016-10-04 11:55:58 +11:00
|
|
|
curr_line.push(rdr.ch.unwrap());
|
|
|
|
|
if rdr.ch_is('/') && rdr.nextch_is('*') {
|
2014-05-21 16:57:31 -07:00
|
|
|
rdr.bump();
|
|
|
|
|
rdr.bump();
|
2014-10-14 23:05:01 -07:00
|
|
|
curr_line.push('*');
|
2013-02-13 11:37:07 +09:00
|
|
|
level += 1;
|
|
|
|
|
} else {
|
2016-10-04 11:55:58 +11:00
|
|
|
if rdr.ch_is('*') && rdr.nextch_is('/') {
|
2014-05-21 16:57:31 -07:00
|
|
|
rdr.bump();
|
|
|
|
|
rdr.bump();
|
2014-10-14 23:05:01 -07:00
|
|
|
curr_line.push('/');
|
2013-02-13 11:37:07 +09:00
|
|
|
level -= 1;
|
2016-01-03 11:14:09 +02:00
|
|
|
} else {
|
|
|
|
|
rdr.bump();
|
|
|
|
|
}
|
2013-02-13 11:37:07 +09:00
|
|
|
}
|
2012-04-15 03:27:24 -07:00
|
|
|
}
|
|
|
|
|
}
|
2015-03-24 16:54:09 -07:00
|
|
|
if !curr_line.is_empty() {
|
2016-01-03 11:14:09 +02:00
|
|
|
trim_whitespace_prefix_and_push_line(&mut lines, curr_line, col);
|
2013-02-13 11:37:07 +09:00
|
|
|
}
|
2012-04-15 03:27:24 -07:00
|
|
|
}
|
2013-02-13 11:37:07 +09:00
|
|
|
|
2016-01-03 11:14:09 +02:00
|
|
|
let mut style = if code_to_the_left {
|
|
|
|
|
Trailing
|
|
|
|
|
} else {
|
|
|
|
|
Isolated
|
|
|
|
|
};
|
2014-05-21 16:57:31 -07:00
|
|
|
rdr.consume_non_eol_whitespace();
|
2016-10-04 11:55:58 +11:00
|
|
|
if !rdr.is_eof() && !rdr.ch_is('\n') && lines.len() == 1 {
|
2014-01-09 15:05:33 +02:00
|
|
|
style = Mixed;
|
2012-04-15 03:27:24 -07:00
|
|
|
}
|
2013-10-21 13:08:31 -07:00
|
|
|
debug!("<<< block comment");
|
2016-01-03 11:14:09 +02:00
|
|
|
comments.push(Comment {
|
2017-08-06 22:54:09 -07:00
|
|
|
style,
|
|
|
|
|
lines,
|
2016-01-03 11:14:09 +02:00
|
|
|
pos: p,
|
|
|
|
|
});
|
2012-04-15 03:27:24 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2019-02-07 02:33:01 +09:00
|
|
|
fn consume_comment(rdr: &mut StringReader<'_>,
|
2016-10-12 20:54:41 +03:00
|
|
|
comments: &mut Vec<Comment>,
|
|
|
|
|
code_to_the_left: &mut bool,
|
|
|
|
|
anything_to_the_left: &mut bool) {
|
2013-10-21 13:08:31 -07:00
|
|
|
debug!(">>> consume comment");
|
2016-10-04 11:55:58 +11:00
|
|
|
if rdr.ch_is('/') && rdr.nextch_is('/') {
|
2016-10-12 20:54:41 +03:00
|
|
|
read_line_comments(rdr, *code_to_the_left, comments);
|
|
|
|
|
*code_to_the_left = false;
|
|
|
|
|
*anything_to_the_left = false;
|
2016-10-04 11:55:58 +11:00
|
|
|
} else if rdr.ch_is('/') && rdr.nextch_is('*') {
|
2016-10-12 20:54:41 +03:00
|
|
|
read_block_comment(rdr, *code_to_the_left, comments);
|
|
|
|
|
*anything_to_the_left = true;
|
2016-10-04 11:55:58 +11:00
|
|
|
} else if rdr.ch_is('#') && rdr.nextch_is('!') {
|
2016-10-12 20:54:41 +03:00
|
|
|
read_shebang_comment(rdr, *code_to_the_left, comments);
|
|
|
|
|
*code_to_the_left = false;
|
|
|
|
|
*anything_to_the_left = false;
|
2016-01-03 11:14:09 +02:00
|
|
|
} else {
|
|
|
|
|
panic!();
|
|
|
|
|
}
|
2013-10-21 13:08:31 -07:00
|
|
|
debug!("<<< consume comment");
|
2012-04-15 03:27:24 -07:00
|
|
|
}
|
|
|
|
|
|
2015-01-03 22:54:18 -05:00
|
|
|
#[derive(Clone)]
|
2014-01-09 15:05:33 +02:00
|
|
|
pub struct Literal {
|
2014-05-22 16:57:53 -07:00
|
|
|
pub lit: String,
|
2014-03-27 15:39:48 -07:00
|
|
|
pub pos: BytePos,
|
2013-02-21 00:16:31 -08:00
|
|
|
}
|
2012-04-15 03:27:24 -07:00
|
|
|
|
2013-04-02 16:44:01 -07:00
|
|
|
// it appears this function is called only from pprust... that's
|
|
|
|
|
// probably not a good thing.
|
2018-07-10 21:06:26 +02:00
|
|
|
pub fn gather_comments_and_literals(sess: &ParseSess, path: FileName, srdr: &mut dyn Read)
|
2018-08-12 15:43:51 +02:00
|
|
|
-> (Vec<Comment>, Vec<Literal>)
|
|
|
|
|
{
|
|
|
|
|
let mut src = String::new();
|
|
|
|
|
srdr.read_to_string(&mut src).unwrap();
|
2018-08-18 12:14:09 +02:00
|
|
|
let cm = SourceMap::new(sess.source_map().path_mapping().clone());
|
2018-08-18 12:13:56 +02:00
|
|
|
let source_file = cm.new_source_file(path, src);
|
|
|
|
|
let mut rdr = lexer::StringReader::new_raw(sess, source_file, None);
|
2012-06-15 09:32:17 -07:00
|
|
|
|
2014-02-28 13:09:09 -08:00
|
|
|
let mut comments: Vec<Comment> = Vec::new();
|
|
|
|
|
let mut literals: Vec<Literal> = Vec::new();
|
2016-10-12 20:54:41 +03:00
|
|
|
let mut code_to_the_left = false; // Only code
|
|
|
|
|
let mut anything_to_the_left = false; // Code or comments
|
2018-08-12 15:43:51 +02:00
|
|
|
|
2014-05-21 16:57:31 -07:00
|
|
|
while !rdr.is_eof() {
|
2012-04-15 03:27:24 -07:00
|
|
|
loop {
|
2016-10-12 20:54:41 +03:00
|
|
|
// Eat all the whitespace and count blank lines.
|
2014-05-21 16:57:31 -07:00
|
|
|
rdr.consume_non_eol_whitespace();
|
2016-10-04 11:55:58 +11:00
|
|
|
if rdr.ch_is('\n') {
|
2016-10-12 20:54:41 +03:00
|
|
|
if anything_to_the_left {
|
|
|
|
|
rdr.bump(); // The line is not blank, do not count.
|
|
|
|
|
}
|
2014-03-27 15:14:58 +02:00
|
|
|
consume_whitespace_counting_blank_lines(&mut rdr, &mut comments);
|
2016-10-12 20:54:41 +03:00
|
|
|
code_to_the_left = false;
|
|
|
|
|
anything_to_the_left = false;
|
2012-04-15 03:27:24 -07:00
|
|
|
}
|
2016-10-12 20:54:41 +03:00
|
|
|
// Eat one comment group
|
|
|
|
|
if rdr.peeking_at_comment() {
|
|
|
|
|
consume_comment(&mut rdr, &mut comments,
|
|
|
|
|
&mut code_to_the_left, &mut anything_to_the_left);
|
|
|
|
|
} else {
|
|
|
|
|
break
|
2012-04-15 03:27:24 -07:00
|
|
|
}
|
|
|
|
|
}
|
2012-06-15 09:32:17 -07:00
|
|
|
|
2016-10-04 11:46:54 +11:00
|
|
|
let bstart = rdr.pos;
|
2012-06-12 10:59:50 -07:00
|
|
|
rdr.next_token();
|
2016-01-03 11:14:09 +02:00
|
|
|
// discard, and look ahead; we're working with internal state
|
2014-10-06 13:36:53 +13:00
|
|
|
let TokenAndSpan { tok, sp } = rdr.peek();
|
2014-10-27 23:33:30 +11:00
|
|
|
if tok.is_lit() {
|
2014-05-21 16:57:31 -07:00
|
|
|
rdr.with_str_from(bstart, |s| {
|
2013-10-21 13:08:31 -07:00
|
|
|
debug!("tok lit: {}", s);
|
2016-01-03 11:14:09 +02:00
|
|
|
literals.push(Literal {
|
|
|
|
|
lit: s.to_string(),
|
2017-07-31 23:04:34 +03:00
|
|
|
pos: sp.lo(),
|
2016-01-03 11:14:09 +02:00
|
|
|
});
|
2013-11-20 16:23:04 -08:00
|
|
|
})
|
2012-04-15 03:27:24 -07:00
|
|
|
} else {
|
2014-10-28 11:05:28 +11:00
|
|
|
debug!("tok: {}", pprust::token_to_string(&tok));
|
2012-04-15 03:27:24 -07:00
|
|
|
}
|
2016-10-12 20:54:41 +03:00
|
|
|
code_to_the_left = true;
|
|
|
|
|
anything_to_the_left = true;
|
2012-04-15 03:27:24 -07:00
|
|
|
}
|
2013-02-21 00:16:31 -08:00
|
|
|
|
|
|
|
|
(comments, literals)
|
2012-04-15 03:27:24 -07:00
|
|
|
}
|
2013-06-14 18:37:29 -04:00
|
|
|
|
|
|
|
|
#[cfg(test)]
|
2015-04-24 17:30:41 +02:00
|
|
|
mod tests {
|
2013-06-14 18:37:29 -04:00
|
|
|
use super::*;
|
|
|
|
|
|
2016-01-03 11:14:09 +02:00
|
|
|
#[test]
|
|
|
|
|
fn test_block_doc_comment_1() {
|
2013-06-14 18:37:29 -04:00
|
|
|
let comment = "/**\n * Test \n ** Test\n * Test\n*/";
|
|
|
|
|
let stripped = strip_doc_comment_decoration(comment);
|
2014-11-27 19:52:53 -05:00
|
|
|
assert_eq!(stripped, " Test \n* Test\n Test");
|
2013-06-14 18:37:29 -04:00
|
|
|
}
|
|
|
|
|
|
2016-01-03 11:14:09 +02:00
|
|
|
#[test]
|
|
|
|
|
fn test_block_doc_comment_2() {
|
2013-06-14 18:37:29 -04:00
|
|
|
let comment = "/**\n * Test\n * Test\n*/";
|
|
|
|
|
let stripped = strip_doc_comment_decoration(comment);
|
2014-11-27 19:52:53 -05:00
|
|
|
assert_eq!(stripped, " Test\n Test");
|
2013-06-14 18:37:29 -04:00
|
|
|
}
|
|
|
|
|
|
2016-01-03 11:14:09 +02:00
|
|
|
#[test]
|
|
|
|
|
fn test_block_doc_comment_3() {
|
2015-01-17 23:55:21 +00:00
|
|
|
let comment = "/**\n let a: *i32;\n *a = 5;\n*/";
|
2013-06-14 18:37:29 -04:00
|
|
|
let stripped = strip_doc_comment_decoration(comment);
|
2015-01-17 23:55:21 +00:00
|
|
|
assert_eq!(stripped, " let a: *i32;\n *a = 5;");
|
2013-06-14 18:37:29 -04:00
|
|
|
}
|
|
|
|
|
|
2016-01-03 11:14:09 +02:00
|
|
|
#[test]
|
|
|
|
|
fn test_block_doc_comment_4() {
|
2013-09-23 14:18:26 -07:00
|
|
|
let comment = "/*******************\n test\n *********************/";
|
2013-06-14 18:37:29 -04:00
|
|
|
let stripped = strip_doc_comment_decoration(comment);
|
2014-11-27 19:52:53 -05:00
|
|
|
assert_eq!(stripped, " test");
|
2013-09-23 14:18:26 -07:00
|
|
|
}
|
|
|
|
|
|
2016-01-03 11:14:09 +02:00
|
|
|
#[test]
|
|
|
|
|
fn test_line_doc_comment() {
|
2013-09-23 14:18:26 -07:00
|
|
|
let stripped = strip_doc_comment_decoration("/// test");
|
2014-11-27 19:52:53 -05:00
|
|
|
assert_eq!(stripped, " test");
|
2013-09-23 14:18:26 -07:00
|
|
|
let stripped = strip_doc_comment_decoration("///! test");
|
2014-11-27 19:52:53 -05:00
|
|
|
assert_eq!(stripped, " test");
|
2013-09-23 14:18:26 -07:00
|
|
|
let stripped = strip_doc_comment_decoration("// test");
|
2014-11-27 19:52:53 -05:00
|
|
|
assert_eq!(stripped, " test");
|
2013-09-23 14:18:26 -07:00
|
|
|
let stripped = strip_doc_comment_decoration("// test");
|
2014-11-27 19:52:53 -05:00
|
|
|
assert_eq!(stripped, " test");
|
2013-09-23 14:18:26 -07:00
|
|
|
let stripped = strip_doc_comment_decoration("///test");
|
2014-11-27 19:52:53 -05:00
|
|
|
assert_eq!(stripped, "test");
|
2013-09-23 14:18:26 -07:00
|
|
|
let stripped = strip_doc_comment_decoration("///!test");
|
2014-11-27 19:52:53 -05:00
|
|
|
assert_eq!(stripped, "test");
|
2013-09-23 14:18:26 -07:00
|
|
|
let stripped = strip_doc_comment_decoration("//test");
|
2014-11-27 19:52:53 -05:00
|
|
|
assert_eq!(stripped, "test");
|
2013-06-14 18:37:29 -04:00
|
|
|
}
|
|
|
|
|
}
|