2012-04-15 03:27:24 -07:00
|
|
|
import io::reader_util;
|
2012-05-22 17:49:16 -07:00
|
|
|
import io::println;//XXXXXXXXxxx
|
2012-04-15 03:27:24 -07:00
|
|
|
import util::interner;
|
2012-07-09 16:01:07 -07:00
|
|
|
import lexer::{string_reader, bump, is_eof, nextch,
|
|
|
|
is_whitespace, get_str_from, string_reader_as_reader, reader};
|
2012-04-15 03:27:24 -07:00
|
|
|
|
2012-04-17 19:34:44 -07:00
|
|
|
export cmnt;
|
|
|
|
export lit;
|
|
|
|
export cmnt_style;
|
|
|
|
export gather_comments_and_literals;
|
2012-06-30 11:54:54 +01:00
|
|
|
export is_doc_comment, doc_comment_style, strip_doc_comment_decoration;
|
2012-07-06 19:06:58 -07:00
|
|
|
export isolated, trailing, mixed, blank_line;
|
2012-04-17 19:34:44 -07:00
|
|
|
|
2012-04-15 03:27:24 -07:00
|
|
|
enum cmnt_style {
|
|
|
|
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
|
|
|
|
blank_line, // Just a manual blank line "\n\n", for layout
|
|
|
|
}
|
|
|
|
|
2012-07-13 22:57:48 -07:00
|
|
|
type cmnt = {style: cmnt_style, lines: ~[~str], pos: uint};
|
2012-04-15 03:27:24 -07:00
|
|
|
|
2012-07-13 22:57:48 -07:00
|
|
|
fn is_doc_comment(s: ~str) -> bool {
|
|
|
|
s.starts_with(~"///") ||
|
|
|
|
s.starts_with(~"//!") ||
|
|
|
|
s.starts_with(~"/**") ||
|
|
|
|
s.starts_with(~"/*!")
|
2012-06-30 11:54:54 +01:00
|
|
|
}
|
|
|
|
|
2012-07-13 22:57:48 -07:00
|
|
|
fn doc_comment_style(comment: ~str) -> ast::attr_style {
|
2012-06-30 11:54:54 +01:00
|
|
|
assert is_doc_comment(comment);
|
2012-07-13 22:57:48 -07:00
|
|
|
if comment.starts_with(~"//!") || comment.starts_with(~"/*!") {
|
2012-06-30 11:54:54 +01:00
|
|
|
ast::attr_inner
|
|
|
|
} else {
|
|
|
|
ast::attr_outer
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-13 22:57:48 -07:00
|
|
|
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
|
2012-07-13 22:57:48 -07:00
|
|
|
fn vertical_trim(lines: ~[~str]) -> ~[~str] {
|
2012-06-30 11:54:54 +01:00
|
|
|
let mut i = 0u, j = lines.len();
|
|
|
|
while i < j && lines[i].trim().is_empty() {
|
|
|
|
i += 1u;
|
|
|
|
}
|
|
|
|
while j > i && lines[j - 1u].trim().is_empty() {
|
|
|
|
j -= 1u;
|
|
|
|
}
|
2012-08-01 17:30:05 -07:00
|
|
|
return lines.slice(i, j);
|
2012-06-30 11:54:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// drop leftmost columns that contain only values in chars
|
2012-07-13 22:57:48 -07:00
|
|
|
fn block_trim(lines: ~[~str], chars: ~str, max: option<uint>) -> ~[~str] {
|
2012-06-30 11:54:54 +01:00
|
|
|
|
|
|
|
let mut i = max.get_default(uint::max_value);
|
2012-07-02 14:44:31 -07:00
|
|
|
for lines.each |line| {
|
2012-06-30 11:54:54 +01:00
|
|
|
if line.trim().is_empty() {
|
2012-07-09 14:37:48 -07:00
|
|
|
again;
|
2012-06-30 11:54:54 +01:00
|
|
|
}
|
2012-07-02 14:44:31 -07:00
|
|
|
for line.each_chari |j, c| {
|
2012-06-30 11:54:54 +01:00
|
|
|
if j >= i {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if !chars.contains_char(c) {
|
|
|
|
i = j;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-01 17:30:05 -07:00
|
|
|
return do lines.map |line| {
|
2012-06-30 11:54:54 +01:00
|
|
|
let chars = str::chars(line);
|
|
|
|
if i > chars.len() {
|
2012-07-13 22:57:48 -07:00
|
|
|
~""
|
2012-06-30 11:54:54 +01:00
|
|
|
} else {
|
|
|
|
str::from_chars(chars.slice(i, chars.len()))
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2012-07-13 22:57:48 -07:00
|
|
|
if comment.starts_with(~"//") {
|
2012-08-01 17:30:05 -07:00
|
|
|
return comment.slice(3u, comment.len()).trim();
|
2012-06-30 11:54:54 +01:00
|
|
|
}
|
|
|
|
|
2012-07-13 22:57:48 -07:00
|
|
|
if comment.starts_with(~"/*") {
|
2012-06-30 11:54:54 +01:00
|
|
|
let lines = str::lines_any(comment.slice(3u, comment.len() - 2u));
|
|
|
|
let lines = vertical_trim(lines);
|
2012-07-13 22:57:48 -07:00
|
|
|
let lines = block_trim(lines, ~"\t ", none);
|
|
|
|
let lines = block_trim(lines, ~"*", some(1u));
|
|
|
|
let lines = block_trim(lines, ~"\t ", none);
|
2012-08-01 17:30:05 -07:00
|
|
|
return str::connect(lines, ~"\n");
|
2012-06-30 11:54:54 +01:00
|
|
|
}
|
|
|
|
|
2012-07-13 22:57:48 -07:00
|
|
|
fail ~"not a doc-comment: " + comment;
|
2012-06-30 11:54:54 +01:00
|
|
|
}
|
|
|
|
|
2012-07-13 22:57:48 -07:00
|
|
|
fn read_to_eol(rdr: string_reader) -> ~str {
|
|
|
|
let mut val = ~"";
|
2012-05-30 11:36:30 -07:00
|
|
|
while rdr.curr != '\n' && !is_eof(rdr) {
|
2012-04-15 03:27:24 -07:00
|
|
|
str::push_char(val, rdr.curr);
|
2012-05-30 11:36:30 -07:00
|
|
|
bump(rdr);
|
2012-04-15 03:27:24 -07:00
|
|
|
}
|
2012-05-30 11:36:30 -07:00
|
|
|
if rdr.curr == '\n' { bump(rdr); }
|
2012-08-01 17:30:05 -07:00
|
|
|
return val;
|
2012-04-15 03:27:24 -07:00
|
|
|
}
|
|
|
|
|
2012-07-13 22:57:48 -07:00
|
|
|
fn read_one_line_comment(rdr: string_reader) -> ~str {
|
2012-04-15 03:27:24 -07:00
|
|
|
let val = read_to_eol(rdr);
|
2012-05-22 17:49:16 -07:00
|
|
|
assert ((val[0] == '/' as u8 && val[1] == '/' as u8) ||
|
|
|
|
(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
|
|
|
}
|
|
|
|
|
2012-05-30 11:36:30 -07:00
|
|
|
fn consume_non_eol_whitespace(rdr: string_reader) {
|
|
|
|
while is_whitespace(rdr.curr) && rdr.curr != '\n' && !is_eof(rdr) {
|
|
|
|
bump(rdr);
|
2012-04-15 03:27:24 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-29 16:26:56 -07:00
|
|
|
fn push_blank_line_comment(rdr: string_reader, &comments: ~[cmnt]) {
|
2012-07-30 16:01:07 -07:00
|
|
|
debug!{">>> blank-line comment"};
|
2012-07-13 22:57:48 -07:00
|
|
|
let v: ~[~str] = ~[];
|
2012-06-26 00:39:18 -07:00
|
|
|
vec::push(comments, {style: blank_line, lines: v, pos: rdr.chpos});
|
2012-04-15 03:27:24 -07:00
|
|
|
}
|
|
|
|
|
2012-05-30 11:36:30 -07:00
|
|
|
fn consume_whitespace_counting_blank_lines(rdr: string_reader,
|
2012-06-29 16:26:56 -07:00
|
|
|
&comments: ~[cmnt]) {
|
2012-05-30 11:36:30 -07:00
|
|
|
while is_whitespace(rdr.curr) && !is_eof(rdr) {
|
2012-04-15 03:27:24 -07:00
|
|
|
if rdr.col == 0u && rdr.curr == '\n' {
|
|
|
|
push_blank_line_comment(rdr, comments);
|
|
|
|
}
|
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
|
|
|
|
|
|
|
fn read_shebang_comment(rdr: string_reader, code_to_the_left: bool,
|
2012-07-11 16:49:02 -07:00
|
|
|
&comments: ~[cmnt]) {
|
2012-07-30 16:01:07 -07:00
|
|
|
debug!{">>> shebang comment"};
|
2012-05-22 17:49:16 -07:00
|
|
|
let p = rdr.chpos;
|
2012-07-30 16:01:07 -07:00
|
|
|
debug!{"<<< shebang comment"};
|
2012-06-30 11:54:54 +01:00
|
|
|
vec::push(comments, {
|
|
|
|
style: if code_to_the_left { trailing } else { isolated },
|
2012-07-02 14:44:31 -07:00
|
|
|
lines: ~[read_one_line_comment(rdr)],
|
2012-06-30 11:54:54 +01:00
|
|
|
pos: p
|
|
|
|
});
|
2012-05-22 17:49:16 -07:00
|
|
|
}
|
|
|
|
|
2012-06-30 11:54:54 +01:00
|
|
|
fn read_line_comments(rdr: string_reader, code_to_the_left: bool,
|
2012-07-11 16:49:02 -07:00
|
|
|
&comments: ~[cmnt]) {
|
2012-07-30 16:01:07 -07:00
|
|
|
debug!{">>> line comments"};
|
2012-04-15 03:27:24 -07:00
|
|
|
let p = rdr.chpos;
|
2012-07-13 22:57:48 -07:00
|
|
|
let mut lines: ~[~str] = ~[];
|
2012-05-30 11:36:30 -07:00
|
|
|
while rdr.curr == '/' && nextch(rdr) == '/' {
|
2012-04-15 03:27:24 -07:00
|
|
|
let line = read_one_line_comment(rdr);
|
|
|
|
log(debug, line);
|
2012-06-30 11:54:54 +01:00
|
|
|
if is_doc_comment(line) { // doc-comments are not put in comments
|
|
|
|
break;
|
|
|
|
}
|
2012-06-26 00:39:18 -07:00
|
|
|
vec::push(lines, line);
|
2012-04-15 03:27:24 -07:00
|
|
|
consume_non_eol_whitespace(rdr);
|
|
|
|
}
|
2012-07-30 16:01:07 -07:00
|
|
|
debug!{"<<< line comments"};
|
2012-06-30 11:54:54 +01:00
|
|
|
if !lines.is_empty() {
|
|
|
|
vec::push(comments, {
|
|
|
|
style: if code_to_the_left { trailing } else { isolated },
|
|
|
|
lines: lines,
|
|
|
|
pos: p
|
|
|
|
});
|
|
|
|
}
|
2012-04-15 03:27:24 -07:00
|
|
|
}
|
|
|
|
|
2012-07-13 22:57:48 -07:00
|
|
|
fn all_whitespace(s: ~str, begin: uint, end: uint) -> bool {
|
2012-04-15 03:27:24 -07:00
|
|
|
let mut i: uint = begin;
|
2012-08-01 17:30:05 -07:00
|
|
|
while i != end {
|
|
|
|
if !is_whitespace(s[i] as char) { return false; } i += 1u;
|
|
|
|
}
|
|
|
|
return true;
|
2012-04-15 03:27:24 -07:00
|
|
|
}
|
|
|
|
|
2012-07-13 22:57:48 -07:00
|
|
|
fn trim_whitespace_prefix_and_push_line(&lines: ~[~str],
|
|
|
|
s: ~str, col: uint) unsafe {
|
2012-04-15 03:27:24 -07:00
|
|
|
let mut s1;
|
2012-04-18 17:02:00 -07:00
|
|
|
let len = str::len(s);
|
|
|
|
if all_whitespace(s, 0u, uint::min(len, col)) {
|
|
|
|
if col < len {
|
|
|
|
s1 = str::slice(s, col, len);
|
2012-07-13 22:57:48 -07:00
|
|
|
} else { s1 = ~""; }
|
2012-04-15 03:27:24 -07:00
|
|
|
} else { s1 = s; }
|
2012-07-13 22:57:48 -07:00
|
|
|
log(debug, ~"pushing line: " + s1);
|
2012-06-26 00:39:18 -07:00
|
|
|
vec::push(lines, s1);
|
2012-04-15 03:27:24 -07:00
|
|
|
}
|
|
|
|
|
2012-06-30 11:54:54 +01:00
|
|
|
fn read_block_comment(rdr: string_reader, code_to_the_left: bool,
|
2012-07-11 16:49:02 -07:00
|
|
|
&comments: ~[cmnt]) {
|
2012-07-30 16:01:07 -07:00
|
|
|
debug!{">>> block comment"};
|
2012-04-15 03:27:24 -07:00
|
|
|
let p = rdr.chpos;
|
2012-07-13 22:57:48 -07:00
|
|
|
let mut lines: ~[~str] = ~[];
|
2012-04-15 03:27:24 -07:00
|
|
|
let mut col: uint = rdr.col;
|
2012-05-30 11:36:30 -07:00
|
|
|
bump(rdr);
|
|
|
|
bump(rdr);
|
2012-06-30 11:54:54 +01:00
|
|
|
|
|
|
|
// doc-comments are not really comments, they are attributes
|
|
|
|
if rdr.curr == '*' || rdr.curr == '!' {
|
|
|
|
while !(rdr.curr == '*' && nextch(rdr) == '/') && !is_eof(rdr) {
|
|
|
|
bump(rdr);
|
|
|
|
}
|
|
|
|
if !is_eof(rdr) {
|
|
|
|
bump(rdr);
|
|
|
|
bump(rdr);
|
|
|
|
}
|
2012-08-01 17:30:05 -07:00
|
|
|
return;
|
2012-06-30 11:54:54 +01:00
|
|
|
}
|
|
|
|
|
2012-07-13 22:57:48 -07:00
|
|
|
let mut curr_line = ~"/*";
|
2012-04-15 03:27:24 -07:00
|
|
|
let mut level: int = 1;
|
|
|
|
while level > 0 {
|
2012-07-30 16:01:07 -07:00
|
|
|
debug!{"=== block comment level %d", level};
|
2012-07-13 22:57:48 -07:00
|
|
|
if is_eof(rdr) {(rdr as reader).fatal(~"unterminated block comment");}
|
2012-04-15 03:27:24 -07:00
|
|
|
if rdr.curr == '\n' {
|
|
|
|
trim_whitespace_prefix_and_push_line(lines, curr_line, col);
|
2012-07-13 22:57:48 -07:00
|
|
|
curr_line = ~"";
|
2012-05-30 11:36:30 -07:00
|
|
|
bump(rdr);
|
2012-04-15 03:27:24 -07:00
|
|
|
} else {
|
|
|
|
str::push_char(curr_line, rdr.curr);
|
2012-05-30 11:36:30 -07:00
|
|
|
if rdr.curr == '/' && nextch(rdr) == '*' {
|
|
|
|
bump(rdr);
|
|
|
|
bump(rdr);
|
2012-07-13 22:57:48 -07:00
|
|
|
curr_line += ~"*";
|
2012-04-15 03:27:24 -07:00
|
|
|
level += 1;
|
|
|
|
} else {
|
2012-05-30 11:36:30 -07:00
|
|
|
if rdr.curr == '*' && nextch(rdr) == '/' {
|
|
|
|
bump(rdr);
|
|
|
|
bump(rdr);
|
2012-07-13 22:57:48 -07:00
|
|
|
curr_line += ~"/";
|
2012-04-15 03:27:24 -07:00
|
|
|
level -= 1;
|
2012-05-30 11:36:30 -07:00
|
|
|
} else { bump(rdr); }
|
2012-04-15 03:27:24 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if str::len(curr_line) != 0u {
|
|
|
|
trim_whitespace_prefix_and_push_line(lines, curr_line, col);
|
|
|
|
}
|
|
|
|
let mut style = if code_to_the_left { trailing } else { isolated };
|
|
|
|
consume_non_eol_whitespace(rdr);
|
2012-05-30 11:36:30 -07:00
|
|
|
if !is_eof(rdr) && rdr.curr != '\n' && vec::len(lines) == 1u {
|
2012-04-15 03:27:24 -07:00
|
|
|
style = mixed;
|
|
|
|
}
|
2012-07-30 16:01:07 -07:00
|
|
|
debug!{"<<< block comment"};
|
2012-06-30 11:54:54 +01:00
|
|
|
vec::push(comments, {style: style, lines: lines, pos: p});
|
2012-04-15 03:27:24 -07:00
|
|
|
}
|
|
|
|
|
2012-05-30 11:36:30 -07:00
|
|
|
fn peeking_at_comment(rdr: string_reader) -> bool {
|
2012-08-01 17:30:05 -07:00
|
|
|
return ((rdr.curr == '/' && nextch(rdr) == '/') ||
|
2012-05-30 11:36:30 -07:00
|
|
|
(rdr.curr == '/' && nextch(rdr) == '*')) ||
|
|
|
|
(rdr.curr == '#' && nextch(rdr) == '!');
|
2012-04-15 03:27:24 -07:00
|
|
|
}
|
|
|
|
|
2012-05-30 11:36:30 -07:00
|
|
|
fn consume_comment(rdr: string_reader, code_to_the_left: bool,
|
2012-06-29 16:26:56 -07:00
|
|
|
&comments: ~[cmnt]) {
|
2012-07-30 16:01:07 -07:00
|
|
|
debug!{">>> consume comment"};
|
2012-05-30 11:36:30 -07:00
|
|
|
if rdr.curr == '/' && nextch(rdr) == '/' {
|
2012-06-30 11:54:54 +01:00
|
|
|
read_line_comments(rdr, code_to_the_left, comments);
|
2012-05-30 11:36:30 -07:00
|
|
|
} else if rdr.curr == '/' && nextch(rdr) == '*' {
|
2012-06-30 11:54:54 +01:00
|
|
|
read_block_comment(rdr, code_to_the_left, comments);
|
2012-05-30 11:36:30 -07:00
|
|
|
} else if rdr.curr == '#' && nextch(rdr) == '!' {
|
2012-06-30 11:54:54 +01:00
|
|
|
read_shebang_comment(rdr, code_to_the_left, comments);
|
2012-04-15 03:27:24 -07:00
|
|
|
} else { fail; }
|
2012-07-30 16:01:07 -07:00
|
|
|
debug!{"<<< consume comment"};
|
2012-04-15 03:27:24 -07:00
|
|
|
}
|
|
|
|
|
2012-07-13 22:57:48 -07:00
|
|
|
type lit = {lit: ~str, pos: uint};
|
2012-04-15 03:27:24 -07:00
|
|
|
|
2012-04-15 03:57:24 -07:00
|
|
|
fn gather_comments_and_literals(span_diagnostic: diagnostic::span_handler,
|
2012-07-13 22:57:48 -07:00
|
|
|
path: ~str,
|
2012-04-15 03:27:24 -07:00
|
|
|
srdr: io::reader) ->
|
2012-06-29 16:26:56 -07:00
|
|
|
{cmnts: ~[cmnt], lits: ~[lit]} {
|
2012-04-30 11:52:07 -07:00
|
|
|
let src = @str::from_bytes(srdr.read_whole_stream());
|
2012-07-13 22:57:48 -07:00
|
|
|
let itr = @interner::mk::<@~str>(
|
2012-06-30 16:19:07 -07:00
|
|
|
|x| str::hash(*x),
|
|
|
|
|x,y| str::eq(*x, *y)
|
2012-06-09 00:53:34 -07:00
|
|
|
);
|
2012-06-15 09:32:17 -07:00
|
|
|
let rdr = lexer::new_low_level_string_reader
|
|
|
|
(span_diagnostic, codemap::new_filemap(path, src, 0u, 0u), itr);
|
|
|
|
|
2012-06-29 16:26:56 -07:00
|
|
|
let mut comments: ~[cmnt] = ~[];
|
|
|
|
let mut literals: ~[lit] = ~[];
|
2012-04-15 03:27:24 -07:00
|
|
|
let mut first_read: bool = true;
|
2012-05-30 11:36:30 -07:00
|
|
|
while !is_eof(rdr) {
|
2012-04-15 03:27:24 -07:00
|
|
|
loop {
|
|
|
|
let mut code_to_the_left = !first_read;
|
|
|
|
consume_non_eol_whitespace(rdr);
|
|
|
|
if rdr.curr == '\n' {
|
|
|
|
code_to_the_left = false;
|
|
|
|
consume_whitespace_counting_blank_lines(rdr, comments);
|
|
|
|
}
|
|
|
|
while peeking_at_comment(rdr) {
|
|
|
|
consume_comment(rdr, code_to_the_left, comments);
|
|
|
|
consume_whitespace_counting_blank_lines(rdr, comments);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2012-06-15 09:32:17 -07:00
|
|
|
|
|
|
|
|
|
|
|
let bstart = rdr.pos;
|
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
|
2012-06-12 10:59:50 -07:00
|
|
|
let {tok: tok, sp: sp} = rdr.peek();
|
2012-06-15 09:32:17 -07:00
|
|
|
if token::is_lit(tok) {
|
|
|
|
let s = get_str_from(rdr, bstart);
|
2012-06-14 22:02:50 -07:00
|
|
|
vec::push(literals, {lit: s, pos: sp.lo});
|
2012-07-13 22:57:48 -07:00
|
|
|
log(debug, ~"tok lit: " + s);
|
2012-04-15 03:27:24 -07:00
|
|
|
} else {
|
2012-07-13 22:57:48 -07:00
|
|
|
log(debug, ~"tok: " + token::to_str(*rdr.interner, tok));
|
2012-04-15 03:27:24 -07:00
|
|
|
}
|
|
|
|
first_read = false;
|
|
|
|
}
|
2012-08-01 17:30:05 -07:00
|
|
|
return {cmnts: comments, lits: literals};
|
2012-04-15 03:27:24 -07:00
|
|
|
}
|