2014-01-25 20:37:51 +13:00
|
|
|
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
|
2012-12-03 16:48:01 -08:00
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2012-12-23 17:41:37 -05:00
|
|
|
use ast;
|
2013-03-26 16:38:07 -04:00
|
|
|
use codemap::{BytePos, CharPos, CodeMap, Pos};
|
2012-12-23 17:41:37 -05:00
|
|
|
use diagnostic;
|
2014-01-09 15:05:33 +02:00
|
|
|
use parse::lexer::{is_whitespace, with_str_from, Reader};
|
2014-02-07 01:36:59 +11:00
|
|
|
use parse::lexer::{StringReader, bump, is_eof, nextch_is, TokenAndSpan};
|
2013-02-13 11:37:07 +09:00
|
|
|
use parse::lexer::{is_line_non_doc_comment, is_block_non_doc_comment};
|
2012-12-23 17:41:37 -05:00
|
|
|
use parse::lexer;
|
|
|
|
use parse::token;
|
|
|
|
|
2013-11-10 22:46:32 -08:00
|
|
|
use std::io;
|
2013-06-24 20:40:33 -04:00
|
|
|
use std::str;
|
|
|
|
use std::uint;
|
2014-02-28 12:54:01 -08:00
|
|
|
use std::vec_ng::Vec;
|
2013-05-24 19:35:29 -07:00
|
|
|
|
2013-07-02 12:47:32 -07:00
|
|
|
#[deriving(Clone, Eq)]
|
2014-01-09 15:05:33 +02:00
|
|
|
pub enum CommentStyle {
|
|
|
|
Isolated, // No code on either side of each line of the comment
|
|
|
|
Trailing, // Code exists to the left of the comment
|
|
|
|
Mixed, // Code before /* foo */ and after the comment
|
|
|
|
BlankLine, // Just a manual blank line "\n\n", for layout
|
2012-04-15 03:27:24 -07:00
|
|
|
}
|
|
|
|
|
2013-07-02 12:47:32 -07:00
|
|
|
#[deriving(Clone)]
|
2014-01-09 15:05:33 +02:00
|
|
|
pub struct Comment {
|
|
|
|
style: CommentStyle,
|
2014-02-28 13:09:09 -08:00
|
|
|
lines: Vec<~str> ,
|
2013-02-21 00:16:31 -08:00
|
|
|
pos: BytePos
|
|
|
|
}
|
2012-04-15 03:27:24 -07:00
|
|
|
|
2013-02-11 08:28:41 -08:00
|
|
|
pub fn is_doc_comment(s: &str) -> bool {
|
2013-05-19 01:07:44 -04:00
|
|
|
(s.starts_with("///") && !is_line_non_doc_comment(s)) ||
|
|
|
|
s.starts_with("//!") ||
|
|
|
|
(s.starts_with("/**") && !is_block_non_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("/*!") {
|
2013-07-19 21:51:37 +10:00
|
|
|
ast::AttrInner
|
2012-06-30 11:54:54 +01:00
|
|
|
} else {
|
2013-07-19 21:51:37 +10:00
|
|
|
ast::AttrOuter
|
2012-06-30 11:54:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-11 08:28:41 -08:00
|
|
|
pub fn strip_doc_comment_decoration(comment: &str) -> ~str {
|
2012-06-30 11:54:54 +01:00
|
|
|
/// remove whitespace-only lines from the start/end of lines
|
2014-02-28 13:09:09 -08:00
|
|
|
fn vertical_trim(lines: Vec<~str> ) -> Vec<~str> {
|
2013-06-04 21:43:41 -07:00
|
|
|
let mut i = 0u;
|
|
|
|
let mut j = lines.len();
|
2013-09-23 14:18:26 -07:00
|
|
|
// first line of all-stars should be omitted
|
2014-02-28 12:54:01 -08:00
|
|
|
if lines.len() > 0 && lines.get(0).chars().all(|c| c == '*') {
|
2013-09-23 14:18:26 -07:00
|
|
|
i += 1;
|
|
|
|
}
|
2014-02-28 12:54:01 -08:00
|
|
|
while i < j && lines.get(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
|
2014-02-28 12:54:01 -08:00
|
|
|
if j > i && lines.get(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
|
|
|
}
|
2014-02-28 12:54:01 -08:00
|
|
|
while j > i && lines.get(j - 1).trim().is_empty() {
|
2013-09-23 14:18:26 -07:00
|
|
|
j -= 1;
|
2012-06-30 11:54:54 +01:00
|
|
|
}
|
2014-02-28 12:54:01 -08:00
|
|
|
return lines.slice(i, j).iter().map(|x| (*x).clone()).collect();
|
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
|
2014-02-28 13:09:09 -08:00
|
|
|
fn horizontal_trim(lines: Vec<~str> ) -> Vec<~str> {
|
2014-01-25 20:37:51 +13:00
|
|
|
let mut i = uint::MAX;
|
2013-06-14 18:37:29 -04:00
|
|
|
let mut can_trim = true;
|
|
|
|
let mut first = true;
|
2013-08-03 12:45:23 -04:00
|
|
|
for line in lines.iter() {
|
2013-11-23 11:18:51 +01:00
|
|
|
for (j, c) in line.chars().enumerate() {
|
2013-06-14 18:37:29 -04:00
|
|
|
if j > i || !"* \t".contains_char(c) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2013-06-14 18:37:29 -04:00
|
|
|
if i > line.len() {
|
|
|
|
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 {
|
2013-11-20 16:23:04 -08:00
|
|
|
lines.map(|line| line.slice(i + 1, line.len()).to_owned())
|
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
|
|
|
|
static ONLINERS: &'static [&'static str] = &["///!", "///", "//!", "//"];
|
|
|
|
for prefix in ONLINERS.iter() {
|
|
|
|
if comment.starts_with(*prefix) {
|
|
|
|
return comment.slice_from(prefix.len()).to_owned();
|
|
|
|
}
|
2012-06-30 11:54:54 +01:00
|
|
|
}
|
|
|
|
|
2013-05-19 01:07:44 -04:00
|
|
|
if comment.starts_with("/*") {
|
2013-06-14 01:39:06 +10:00
|
|
|
let lines = comment.slice(3u, comment.len() - 2u)
|
2013-11-23 11:18:51 +01:00
|
|
|
.lines_any()
|
2013-08-09 20:09:47 -07:00
|
|
|
.map(|s| s.to_owned())
|
2014-02-28 13:09:09 -08:00
|
|
|
.collect::<Vec<~str> >();
|
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);
|
|
|
|
|
2013-06-10 23:25:25 +10:00
|
|
|
return lines.connect("\n");
|
2012-06-30 11:54:54 +01:00
|
|
|
}
|
|
|
|
|
2013-10-21 13:08:31 -07:00
|
|
|
fail!("not a doc-comment: {}", comment);
|
2012-06-30 11:54:54 +01:00
|
|
|
}
|
|
|
|
|
2014-02-07 00:38:33 +02:00
|
|
|
fn read_to_eol(rdr: &StringReader) -> ~str {
|
2012-07-13 22:57:48 -07:00
|
|
|
let mut val = ~"";
|
2014-02-07 01:36:59 +11:00
|
|
|
while !rdr.curr_is('\n') && !is_eof(rdr) {
|
|
|
|
val.push_char(rdr.curr.get().unwrap());
|
2012-05-30 11:36:30 -07:00
|
|
|
bump(rdr);
|
2012-04-15 03:27:24 -07:00
|
|
|
}
|
2014-02-07 01:36:59 +11:00
|
|
|
if rdr.curr_is('\n') { bump(rdr); }
|
2012-08-01 17:30:05 -07:00
|
|
|
return val;
|
2012-04-15 03:27:24 -07:00
|
|
|
}
|
|
|
|
|
2014-02-07 00:38:33 +02:00
|
|
|
fn read_one_line_comment(rdr: &StringReader) -> ~str {
|
2012-04-15 03:27:24 -07:00
|
|
|
let val = read_to_eol(rdr);
|
2013-03-28 18:39:09 -07:00
|
|
|
assert!((val[0] == '/' as u8 && val[1] == '/' as u8) ||
|
2013-03-06 13:58:02 -08:00
|
|
|
(val[0] == '#' as u8 && val[1] == '!' as u8));
|
2012-08-01 17:30:05 -07:00
|
|
|
return val;
|
2012-04-15 03:27:24 -07:00
|
|
|
}
|
|
|
|
|
2014-02-07 00:38:33 +02:00
|
|
|
fn consume_non_eol_whitespace(rdr: &StringReader) {
|
2014-02-07 01:36:59 +11:00
|
|
|
while is_whitespace(rdr.curr.get()) && !rdr.curr_is('\n') &&
|
2013-12-27 12:11:30 -08:00
|
|
|
!is_eof(rdr) {
|
2012-05-30 11:36:30 -07:00
|
|
|
bump(rdr);
|
2012-04-15 03:27:24 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-28 13:09:09 -08: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-02-28 13:09:09 -08:00
|
|
|
let v: Vec<~str> = Vec::new();
|
2014-01-09 15:05:33 +02:00
|
|
|
comments.push(Comment {
|
|
|
|
style: BlankLine,
|
2013-12-27 12:05:13 -08:00
|
|
|
lines: v,
|
|
|
|
pos: rdr.last_pos.get(),
|
|
|
|
});
|
2012-04-15 03:27:24 -07:00
|
|
|
}
|
|
|
|
|
2014-02-07 00:38:33 +02:00
|
|
|
fn consume_whitespace_counting_blank_lines(rdr: &StringReader,
|
2014-02-28 13:09:09 -08:00
|
|
|
comments: &mut Vec<Comment> ) {
|
2013-12-27 12:11:30 -08:00
|
|
|
while is_whitespace(rdr.curr.get()) && !is_eof(rdr) {
|
2014-02-07 01:36:59 +11:00
|
|
|
if rdr.col.get() == CharPos(0u) && rdr.curr_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
|
|
|
}
|
2012-05-30 11:36:30 -07:00
|
|
|
bump(rdr);
|
2012-04-15 03:27:24 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-30 11:54:54 +01:00
|
|
|
|
2014-02-07 00:38:33 +02:00
|
|
|
fn read_shebang_comment(rdr: &StringReader, code_to_the_left: bool,
|
2014-02-28 13:09:09 -08:00
|
|
|
comments: &mut Vec<Comment> ) {
|
2013-10-21 13:08:31 -07:00
|
|
|
debug!(">>> shebang comment");
|
2013-12-27 12:05:13 -08:00
|
|
|
let p = rdr.last_pos.get();
|
2013-10-21 13:08:31 -07:00
|
|
|
debug!("<<< shebang comment");
|
2014-01-09 15:05:33 +02:00
|
|
|
comments.push(Comment {
|
|
|
|
style: if code_to_the_left { Trailing } else { Isolated },
|
2014-02-28 13:09:09 -08:00
|
|
|
lines: vec!(read_one_line_comment(rdr)),
|
2012-06-30 11:54:54 +01:00
|
|
|
pos: p
|
|
|
|
});
|
2012-05-22 17:49:16 -07:00
|
|
|
}
|
|
|
|
|
2014-02-07 00:38:33 +02:00
|
|
|
fn read_line_comments(rdr: &StringReader, code_to_the_left: bool,
|
2014-02-28 13:09:09 -08:00
|
|
|
comments: &mut Vec<Comment> ) {
|
2013-10-21 13:08:31 -07:00
|
|
|
debug!(">>> line comments");
|
2013-12-27 12:05:13 -08:00
|
|
|
let p = rdr.last_pos.get();
|
2014-02-28 13:09:09 -08:00
|
|
|
let mut lines: Vec<~str> = Vec::new();
|
2014-02-07 01:36:59 +11:00
|
|
|
while rdr.curr_is('/') && nextch_is(rdr, '/') {
|
2012-04-15 03:27:24 -07:00
|
|
|
let line = read_one_line_comment(rdr);
|
2013-10-21 13:08:31 -07:00
|
|
|
debug!("{}", line);
|
2012-06-30 11:54:54 +01:00
|
|
|
if is_doc_comment(line) { // doc-comments are not put in comments
|
|
|
|
break;
|
|
|
|
}
|
2012-09-26 17:33:34 -07:00
|
|
|
lines.push(line);
|
2012-04-15 03:27:24 -07:00
|
|
|
consume_non_eol_whitespace(rdr);
|
|
|
|
}
|
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 {
|
|
|
|
style: if code_to_the_left { Trailing } else { Isolated },
|
2012-06-30 11:54:54 +01:00
|
|
|
lines: lines,
|
|
|
|
pos: p
|
|
|
|
});
|
|
|
|
}
|
2012-04-15 03:27:24 -07:00
|
|
|
}
|
|
|
|
|
2013-05-27 12:08:37 +02: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.
|
|
|
|
fn all_whitespace(s: &str, col: CharPos) -> Option<uint> {
|
|
|
|
let len = s.len();
|
|
|
|
let mut col = col.to_uint();
|
|
|
|
let mut cursor: uint = 0;
|
|
|
|
while col > 0 && cursor < len {
|
2013-06-14 09:36:03 +02:00
|
|
|
let r: str::CharRange = s.char_range_at(cursor);
|
2013-05-27 12:08:37 +02:00
|
|
|
if !r.ch.is_whitespace() {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
cursor = r.next;
|
|
|
|
col -= 1;
|
2012-08-01 17:30:05 -07:00
|
|
|
}
|
2013-05-27 12:08:37 +02:00
|
|
|
return Some(cursor);
|
2012-04-15 03:27:24 -07:00
|
|
|
}
|
|
|
|
|
2014-02-28 13:09:09 -08:00
|
|
|
fn trim_whitespace_prefix_and_push_line(lines: &mut Vec<~str> ,
|
2012-11-12 19:32:48 -08:00
|
|
|
s: ~str, col: CharPos) {
|
2013-05-09 13:27:24 -07:00
|
|
|
let len = s.len();
|
2013-05-27 12:08:37 +02:00
|
|
|
let s1 = match all_whitespace(s, col) {
|
|
|
|
Some(col) => {
|
|
|
|
if col < len {
|
|
|
|
s.slice(col, len).to_owned()
|
|
|
|
} else { ~"" }
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-02-07 00:38:33 +02:00
|
|
|
fn read_block_comment(rdr: &StringReader,
|
2013-02-04 14:02:01 -08:00
|
|
|
code_to_the_left: bool,
|
2014-02-28 13:09:09 -08:00
|
|
|
comments: &mut Vec<Comment> ) {
|
2013-10-21 13:08:31 -07:00
|
|
|
debug!(">>> block comment");
|
2013-12-27 12:05:13 -08:00
|
|
|
let p = rdr.last_pos.get();
|
2014-02-28 13:09:09 -08:00
|
|
|
let mut lines: Vec<~str> = Vec::new();
|
2013-12-27 12:06:59 -08:00
|
|
|
let col: CharPos = rdr.col.get();
|
2012-05-30 11:36:30 -07:00
|
|
|
bump(rdr);
|
|
|
|
bump(rdr);
|
2012-06-30 11:54:54 +01:00
|
|
|
|
2013-02-13 11:37:07 +09:00
|
|
|
let mut curr_line = ~"/*";
|
|
|
|
|
2012-06-30 11:54:54 +01:00
|
|
|
// doc-comments are not really comments, they are attributes
|
2014-02-27 12:16:18 +11:00
|
|
|
if (rdr.curr_is('*') && !nextch_is(rdr, '*')) || rdr.curr_is('!') {
|
2014-02-07 01:36:59 +11:00
|
|
|
while !(rdr.curr_is('*') && nextch_is(rdr, '/')) && !is_eof(rdr) {
|
|
|
|
curr_line.push_char(rdr.curr.get().unwrap());
|
2012-06-30 11:54:54 +01:00
|
|
|
bump(rdr);
|
|
|
|
}
|
|
|
|
if !is_eof(rdr) {
|
2013-06-11 19:13:42 -07:00
|
|
|
curr_line.push_str("*/");
|
2012-06-30 11:54:54 +01:00
|
|
|
bump(rdr);
|
|
|
|
bump(rdr);
|
|
|
|
}
|
2013-02-13 11:37:07 +09:00
|
|
|
if !is_block_non_doc_comment(curr_line) { return; }
|
2013-03-28 18:39:09 -07:00
|
|
|
assert!(!curr_line.contains_char('\n'));
|
2013-02-13 11:37:07 +09:00
|
|
|
lines.push(curr_line);
|
|
|
|
} else {
|
|
|
|
let mut level: int = 1;
|
|
|
|
while level > 0 {
|
2013-10-21 13:08:31 -07:00
|
|
|
debug!("=== block comment level {}", level);
|
2013-02-13 11:37:07 +09:00
|
|
|
if is_eof(rdr) {
|
2014-02-07 00:38:33 +02:00
|
|
|
rdr.fatal(~"unterminated block comment");
|
2013-02-13 11:37:07 +09:00
|
|
|
}
|
2014-02-07 01:36:59 +11:00
|
|
|
if rdr.curr_is('\n') {
|
2013-02-13 11:37:07 +09:00
|
|
|
trim_whitespace_prefix_and_push_line(&mut lines, curr_line,
|
|
|
|
col);
|
|
|
|
curr_line = ~"";
|
2012-05-30 11:36:30 -07:00
|
|
|
bump(rdr);
|
2012-04-15 03:27:24 -07:00
|
|
|
} else {
|
2014-02-07 01:36:59 +11:00
|
|
|
curr_line.push_char(rdr.curr.get().unwrap());
|
|
|
|
if rdr.curr_is('/') && nextch_is(rdr, '*') {
|
2012-05-30 11:36:30 -07:00
|
|
|
bump(rdr);
|
|
|
|
bump(rdr);
|
2013-06-11 19:13:42 -07:00
|
|
|
curr_line.push_char('*');
|
2013-02-13 11:37:07 +09:00
|
|
|
level += 1;
|
|
|
|
} else {
|
2014-02-07 01:36:59 +11:00
|
|
|
if rdr.curr_is('*') && nextch_is(rdr, '/') {
|
2013-02-13 11:37:07 +09:00
|
|
|
bump(rdr);
|
|
|
|
bump(rdr);
|
2013-06-11 19:13:42 -07:00
|
|
|
curr_line.push_char('/');
|
2013-02-13 11:37:07 +09:00
|
|
|
level -= 1;
|
|
|
|
} else { bump(rdr); }
|
|
|
|
}
|
2012-04-15 03:27:24 -07:00
|
|
|
}
|
|
|
|
}
|
2013-06-10 00:44:58 +10:00
|
|
|
if curr_line.len() != 0 {
|
2013-02-13 11:37:07 +09:00
|
|
|
trim_whitespace_prefix_and_push_line(&mut lines, curr_line, col);
|
|
|
|
}
|
2012-04-15 03:27:24 -07:00
|
|
|
}
|
2013-02-13 11:37:07 +09:00
|
|
|
|
2014-01-09 15:05:33 +02:00
|
|
|
let mut style = if code_to_the_left { Trailing } else { Isolated };
|
2012-04-15 03:27:24 -07:00
|
|
|
consume_non_eol_whitespace(rdr);
|
2014-02-07 01:36:59 +11:00
|
|
|
if !is_eof(rdr) && !rdr.curr_is('\n') && lines.len() == 1u {
|
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");
|
2014-01-09 15:05:33 +02:00
|
|
|
comments.push(Comment {style: style, lines: lines, pos: p});
|
2012-04-15 03:27:24 -07:00
|
|
|
}
|
|
|
|
|
2014-02-07 00:38:33 +02:00
|
|
|
fn peeking_at_comment(rdr: &StringReader) -> bool {
|
2014-02-07 01:36:59 +11:00
|
|
|
return (rdr.curr_is('/') && nextch_is(rdr, '/')) ||
|
|
|
|
(rdr.curr_is('/') && nextch_is(rdr, '*')) ||
|
|
|
|
(rdr.curr_is('#') && nextch_is(rdr, '!'));
|
2012-04-15 03:27:24 -07:00
|
|
|
}
|
|
|
|
|
2014-02-07 00:38:33 +02:00
|
|
|
fn consume_comment(rdr: &StringReader,
|
2013-02-04 14:02:01 -08:00
|
|
|
code_to_the_left: bool,
|
2014-02-28 13:09:09 -08:00
|
|
|
comments: &mut Vec<Comment> ) {
|
2013-10-21 13:08:31 -07:00
|
|
|
debug!(">>> consume comment");
|
2014-02-07 01:36:59 +11:00
|
|
|
if rdr.curr_is('/') && nextch_is(rdr, '/') {
|
2012-06-30 11:54:54 +01:00
|
|
|
read_line_comments(rdr, code_to_the_left, comments);
|
2014-02-07 01:36:59 +11:00
|
|
|
} else if rdr.curr_is('/') && nextch_is(rdr, '*') {
|
2012-06-30 11:54:54 +01:00
|
|
|
read_block_comment(rdr, code_to_the_left, comments);
|
2014-02-07 01:36:59 +11:00
|
|
|
} else if rdr.curr_is('#') && nextch_is(rdr, '!') {
|
2012-06-30 11:54:54 +01:00
|
|
|
read_shebang_comment(rdr, code_to_the_left, comments);
|
2013-10-21 13:08:31 -07:00
|
|
|
} else { fail!(); }
|
|
|
|
debug!("<<< consume comment");
|
2012-04-15 03:27:24 -07:00
|
|
|
}
|
|
|
|
|
2013-07-02 12:47:32 -07:00
|
|
|
#[deriving(Clone)]
|
2014-01-09 15:05:33 +02:00
|
|
|
pub struct Literal {
|
2013-02-21 00:16:31 -08:00
|
|
|
lit: ~str,
|
|
|
|
pos: BytePos
|
|
|
|
}
|
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.
|
2013-03-12 13:00:50 -07:00
|
|
|
pub fn gather_comments_and_literals(span_diagnostic:
|
2013-12-27 13:48:00 -08:00
|
|
|
@diagnostic::SpanHandler,
|
2014-01-15 16:42:51 -08:00
|
|
|
path: ~str,
|
2013-10-25 18:08:45 -07:00
|
|
|
srdr: &mut io::Reader)
|
2014-02-28 13:09:09 -08:00
|
|
|
-> (Vec<Comment> , Vec<Literal> ) {
|
2014-01-29 17:39:21 -08:00
|
|
|
let src = srdr.read_to_end().unwrap();
|
|
|
|
let src = str::from_utf8_owned(src).unwrap();
|
2012-11-16 14:22:09 -08:00
|
|
|
let cm = CodeMap::new();
|
|
|
|
let filemap = cm.new_filemap(path, src);
|
2013-05-21 11:29:03 -07:00
|
|
|
let rdr = lexer::new_low_level_string_reader(span_diagnostic, filemap);
|
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();
|
2012-04-15 03:27:24 -07:00
|
|
|
let mut first_read: bool = true;
|
2014-02-07 00:38:33 +02:00
|
|
|
while !is_eof(&rdr) {
|
2012-04-15 03:27:24 -07:00
|
|
|
loop {
|
|
|
|
let mut code_to_the_left = !first_read;
|
2014-02-07 00:38:33 +02:00
|
|
|
consume_non_eol_whitespace(&rdr);
|
2014-02-07 01:36:59 +11:00
|
|
|
if rdr.curr_is('\n') {
|
2012-04-15 03:27:24 -07:00
|
|
|
code_to_the_left = false;
|
2014-02-07 00:38:33 +02:00
|
|
|
consume_whitespace_counting_blank_lines(&rdr, &mut comments);
|
2012-04-15 03:27:24 -07:00
|
|
|
}
|
2014-02-07 00:38:33 +02:00
|
|
|
while peeking_at_comment(&rdr) {
|
|
|
|
consume_comment(&rdr, code_to_the_left, &mut comments);
|
|
|
|
consume_whitespace_counting_blank_lines(&rdr, &mut comments);
|
2012-04-15 03:27:24 -07:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2012-06-15 09:32:17 -07:00
|
|
|
|
|
|
|
|
2013-12-27 12:05:13 -08:00
|
|
|
let bstart = rdr.last_pos.get();
|
2012-06-12 10:59:50 -07:00
|
|
|
rdr.next_token();
|
2012-06-15 09:32:17 -07:00
|
|
|
//discard, and look ahead; we're working with internal state
|
2013-01-30 09:56:33 -08:00
|
|
|
let TokenAndSpan {tok: tok, sp: sp} = rdr.peek();
|
2013-02-24 17:24:28 -08:00
|
|
|
if token::is_lit(&tok) {
|
2014-02-07 00:38:33 +02:00
|
|
|
with_str_from(&rdr, bstart, |s| {
|
2013-10-21 13:08:31 -07:00
|
|
|
debug!("tok lit: {}", s);
|
2014-01-09 15:05:33 +02:00
|
|
|
literals.push(Literal {lit: s.to_owned(), pos: sp.lo});
|
2013-11-20 16:23:04 -08:00
|
|
|
})
|
2012-04-15 03:27:24 -07:00
|
|
|
} else {
|
2014-02-14 07:07:09 +02:00
|
|
|
debug!("tok: {}", token::to_str(&tok));
|
2012-04-15 03:27:24 -07:00
|
|
|
}
|
|
|
|
first_read = false;
|
|
|
|
}
|
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)]
|
|
|
|
mod test {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test] fn test_block_doc_comment_1() {
|
|
|
|
let comment = "/**\n * Test \n ** Test\n * Test\n*/";
|
|
|
|
let stripped = strip_doc_comment_decoration(comment);
|
2013-09-23 14:18:26 -07:00
|
|
|
assert_eq!(stripped, ~" Test \n* Test\n Test");
|
2013-06-14 18:37:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test] fn test_block_doc_comment_2() {
|
|
|
|
let comment = "/**\n * Test\n * Test\n*/";
|
|
|
|
let stripped = strip_doc_comment_decoration(comment);
|
2013-09-23 14:18:26 -07:00
|
|
|
assert_eq!(stripped, ~" Test\n Test");
|
2013-06-14 18:37:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test] fn test_block_doc_comment_3() {
|
|
|
|
let comment = "/**\n let a: *int;\n *a = 5;\n*/";
|
|
|
|
let stripped = strip_doc_comment_decoration(comment);
|
2013-09-23 14:18:26 -07:00
|
|
|
assert_eq!(stripped, ~" let a: *int;\n *a = 5;");
|
2013-06-14 18:37:29 -04:00
|
|
|
}
|
|
|
|
|
2013-09-23 14:18:26 -07:00
|
|
|
#[test] fn test_block_doc_comment_4() {
|
|
|
|
let comment = "/*******************\n test\n *********************/";
|
2013-06-14 18:37:29 -04:00
|
|
|
let stripped = strip_doc_comment_decoration(comment);
|
2013-09-23 14:18:26 -07:00
|
|
|
assert_eq!(stripped, ~" test");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test] fn test_line_doc_comment() {
|
|
|
|
let stripped = strip_doc_comment_decoration("/// test");
|
|
|
|
assert_eq!(stripped, ~" test");
|
|
|
|
let stripped = strip_doc_comment_decoration("///! test");
|
|
|
|
assert_eq!(stripped, ~" test");
|
|
|
|
let stripped = strip_doc_comment_decoration("// test");
|
|
|
|
assert_eq!(stripped, ~" test");
|
|
|
|
let stripped = strip_doc_comment_decoration("// test");
|
|
|
|
assert_eq!(stripped, ~" test");
|
|
|
|
let stripped = strip_doc_comment_decoration("///test");
|
|
|
|
assert_eq!(stripped, ~"test");
|
|
|
|
let stripped = strip_doc_comment_decoration("///!test");
|
|
|
|
assert_eq!(stripped, ~"test");
|
|
|
|
let stripped = strip_doc_comment_decoration("//test");
|
|
|
|
assert_eq!(stripped, ~"test");
|
2013-06-14 18:37:29 -04:00
|
|
|
}
|
|
|
|
}
|