2013-02-13 11:37:07 +09:00
|
|
|
// Copyright 2012-2013 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;
|
2013-06-08 02:26:52 +02:00
|
|
|
use parse::lexer::{is_whitespace, with_str_from, reader};
|
2013-02-04 14:02:01 -08:00
|
|
|
use parse::lexer::{StringReader, bump, is_eof, nextch, 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-05-17 10:18:35 -07:00
|
|
|
use parse::token::{get_ident_interner};
|
2012-12-23 17:41:37 -05:00
|
|
|
|
2013-06-24 20:40:33 -04:00
|
|
|
use std::io;
|
|
|
|
use std::str;
|
|
|
|
use std::uint;
|
2013-05-24 19:35:29 -07:00
|
|
|
|
2013-03-26 08:04:54 -04:00
|
|
|
#[deriving(Eq)]
|
2013-01-29 13:54:06 -08:00
|
|
|
pub enum cmnt_style {
|
2012-04-15 03:27:24 -07:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2013-02-21 00:16:31 -08:00
|
|
|
pub struct cmnt {
|
|
|
|
style: cmnt_style,
|
|
|
|
lines: ~[~str],
|
|
|
|
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-02-11 08:28:41 -08:00
|
|
|
pub fn doc_comment_style(comment: &str) -> ast::attr_style {
|
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("/*!") {
|
2012-06-30 11:54:54 +01:00
|
|
|
ast::attr_inner
|
|
|
|
} else {
|
|
|
|
ast::attr_outer
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
2012-07-13 22:57:48 -07:00
|
|
|
fn vertical_trim(lines: ~[~str]) -> ~[~str] {
|
2013-06-04 21:43:41 -07:00
|
|
|
let mut i = 0u;
|
|
|
|
let mut j = lines.len();
|
2013-03-21 23:02:27 +01:00
|
|
|
while i < j && lines[i].trim().is_empty() {
|
2012-06-30 11:54:54 +01:00
|
|
|
i += 1u;
|
|
|
|
}
|
2013-03-21 23:02:27 +01:00
|
|
|
while j > i && lines[j - 1u].trim().is_empty() {
|
2012-06-30 11:54:54 +01:00
|
|
|
j -= 1u;
|
|
|
|
}
|
2013-03-21 12:36:21 +01:00
|
|
|
return lines.slice(i, j).to_owned();
|
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
|
|
|
|
fn horizontal_trim(lines: ~[~str]) -> ~[~str] {
|
|
|
|
let mut i = uint::max_value;
|
|
|
|
let mut can_trim = true;
|
|
|
|
let mut first = true;
|
|
|
|
for lines.iter().advance |line| {
|
2013-06-08 22:04:46 +10:00
|
|
|
for line.iter().enumerate().advance |(j, c)| {
|
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 {
|
|
|
|
do lines.map |line| {
|
|
|
|
line.slice(i + 1, line.len()).to_owned()
|
2012-06-30 11:54:54 +01:00
|
|
|
}
|
2013-06-14 18:37:29 -04:00
|
|
|
} else {
|
|
|
|
lines
|
|
|
|
}
|
2012-06-30 11:54:54 +01:00
|
|
|
}
|
|
|
|
|
2013-05-19 01:07:44 -04:00
|
|
|
if comment.starts_with("//") {
|
2013-03-21 22:59:33 +01:00
|
|
|
// FIXME #5475:
|
2013-06-14 18:37:29 -04:00
|
|
|
// return comment.slice(3u, comment.len()).to_owned();
|
|
|
|
let r = comment.slice(3u, comment.len()); return r.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)
|
|
|
|
.any_line_iter()
|
|
|
|
.transform(|s| s.to_owned())
|
|
|
|
.collect::<~[~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-05-06 00:18:51 +02:00
|
|
|
fail!("not a doc-comment: %s", comment);
|
2012-06-30 11:54:54 +01:00
|
|
|
}
|
|
|
|
|
2013-02-04 14:02:01 -08:00
|
|
|
fn read_to_eol(rdr: @mut StringReader) -> ~str {
|
2012-07-13 22:57:48 -07:00
|
|
|
let mut val = ~"";
|
2012-05-30 11:36:30 -07:00
|
|
|
while rdr.curr != '\n' && !is_eof(rdr) {
|
2013-06-10 17:42:24 +10:00
|
|
|
val.push_char(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
|
|
|
}
|
|
|
|
|
2013-02-04 14:02:01 -08:00
|
|
|
fn read_one_line_comment(rdr: @mut 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
|
|
|
}
|
|
|
|
|
2013-02-04 14:02:01 -08:00
|
|
|
fn consume_non_eol_whitespace(rdr: @mut StringReader) {
|
2012-05-30 11:36:30 -07:00
|
|
|
while is_whitespace(rdr.curr) && rdr.curr != '\n' && !is_eof(rdr) {
|
|
|
|
bump(rdr);
|
2012-04-15 03:27:24 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-04 14:02:01 -08:00
|
|
|
fn push_blank_line_comment(rdr: @mut StringReader, comments: &mut ~[cmnt]) {
|
2012-08-22 17:24:52 -07:00
|
|
|
debug!(">>> blank-line comment");
|
2012-07-13 22:57:48 -07:00
|
|
|
let v: ~[~str] = ~[];
|
2013-02-21 00:16:31 -08:00
|
|
|
comments.push(cmnt {style: blank_line, lines: v, pos: rdr.last_pos});
|
2012-04-15 03:27:24 -07:00
|
|
|
}
|
|
|
|
|
2013-02-04 14:02:01 -08:00
|
|
|
fn consume_whitespace_counting_blank_lines(rdr: @mut StringReader,
|
2012-10-05 16:17:10 -07:00
|
|
|
comments: &mut ~[cmnt]) {
|
2012-05-30 11:36:30 -07:00
|
|
|
while is_whitespace(rdr.curr) && !is_eof(rdr) {
|
2012-11-12 19:32:48 -08:00
|
|
|
if rdr.col == CharPos(0u) && rdr.curr == '\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
|
|
|
|
2013-02-04 14:02:01 -08:00
|
|
|
fn read_shebang_comment(rdr: @mut StringReader, code_to_the_left: bool,
|
2012-10-05 16:17:10 -07:00
|
|
|
comments: &mut ~[cmnt]) {
|
2012-08-22 17:24:52 -07:00
|
|
|
debug!(">>> shebang comment");
|
2012-11-16 14:10:17 -08:00
|
|
|
let p = rdr.last_pos;
|
2012-08-22 17:24:52 -07:00
|
|
|
debug!("<<< shebang comment");
|
2013-02-21 00:16:31 -08:00
|
|
|
comments.push(cmnt {
|
2012-06-30 11:54:54 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-02-04 14:02:01 -08:00
|
|
|
fn read_line_comments(rdr: @mut StringReader, code_to_the_left: bool,
|
2012-10-05 16:17:10 -07:00
|
|
|
comments: &mut ~[cmnt]) {
|
2012-08-22 17:24:52 -07:00
|
|
|
debug!(">>> line comments");
|
2012-11-16 14:10:17 -08:00
|
|
|
let p = rdr.last_pos;
|
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);
|
2013-03-08 12:39:42 -08:00
|
|
|
debug!("%s", 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);
|
|
|
|
}
|
2012-08-22 17:24:52 -07:00
|
|
|
debug!("<<< line comments");
|
2012-06-30 11:54:54 +01:00
|
|
|
if !lines.is_empty() {
|
2013-02-21 00:16:31 -08:00
|
|
|
comments.push(cmnt {
|
2012-06-30 11:54:54 +01:00
|
|
|
style: if code_to_the_left { trailing } else { isolated },
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2012-10-05 16:17:10 -07:00
|
|
|
fn trim_whitespace_prefix_and_push_line(lines: &mut ~[~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-03-08 12:39:42 -08:00
|
|
|
debug!("pushing line: %s", s1);
|
2012-09-26 17:33:34 -07:00
|
|
|
lines.push(s1);
|
2012-04-15 03:27:24 -07:00
|
|
|
}
|
|
|
|
|
2013-02-04 14:02:01 -08:00
|
|
|
fn read_block_comment(rdr: @mut StringReader,
|
|
|
|
code_to_the_left: bool,
|
|
|
|
comments: &mut ~[cmnt]) {
|
2012-08-22 17:24:52 -07:00
|
|
|
debug!(">>> block comment");
|
2012-11-16 14:10:17 -08:00
|
|
|
let p = rdr.last_pos;
|
2012-07-13 22:57:48 -07:00
|
|
|
let mut lines: ~[~str] = ~[];
|
2013-04-12 01:10:31 -04:00
|
|
|
let col: CharPos = rdr.col;
|
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
|
|
|
|
if rdr.curr == '*' || rdr.curr == '!' {
|
|
|
|
while !(rdr.curr == '*' && nextch(rdr) == '/') && !is_eof(rdr) {
|
2013-06-10 17:42:24 +10:00
|
|
|
curr_line.push_char(rdr.curr);
|
2012-06-30 11:54:54 +01:00
|
|
|
bump(rdr);
|
|
|
|
}
|
|
|
|
if !is_eof(rdr) {
|
2013-05-24 01:09:11 +09:00
|
|
|
curr_line += "*/";
|
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 {
|
|
|
|
debug!("=== block comment level %d", level);
|
|
|
|
if is_eof(rdr) {
|
2013-03-12 13:00:50 -07:00
|
|
|
(rdr as @reader).fatal(~"unterminated block comment");
|
2013-02-13 11:37:07 +09:00
|
|
|
}
|
|
|
|
if rdr.curr == '\n' {
|
|
|
|
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 {
|
2013-06-10 17:42:24 +10:00
|
|
|
curr_line.push_char(rdr.curr);
|
2013-02-13 11:37:07 +09:00
|
|
|
if rdr.curr == '/' && nextch(rdr) == '*' {
|
2012-05-30 11:36:30 -07:00
|
|
|
bump(rdr);
|
|
|
|
bump(rdr);
|
2013-05-24 01:09:11 +09:00
|
|
|
curr_line += "*";
|
2013-02-13 11:37:07 +09:00
|
|
|
level += 1;
|
|
|
|
} else {
|
|
|
|
if rdr.curr == '*' && nextch(rdr) == '/' {
|
|
|
|
bump(rdr);
|
|
|
|
bump(rdr);
|
2013-05-24 01:09:11 +09:00
|
|
|
curr_line += "/";
|
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
|
|
|
|
2012-04-15 03:27:24 -07:00
|
|
|
let mut style = if code_to_the_left { trailing } else { isolated };
|
|
|
|
consume_non_eol_whitespace(rdr);
|
2013-05-14 18:52:12 +09:00
|
|
|
if !is_eof(rdr) && rdr.curr != '\n' && lines.len() == 1u {
|
2012-04-15 03:27:24 -07:00
|
|
|
style = mixed;
|
|
|
|
}
|
2012-08-22 17:24:52 -07:00
|
|
|
debug!("<<< block comment");
|
2013-02-21 00:16:31 -08:00
|
|
|
comments.push(cmnt {style: style, lines: lines, pos: p});
|
2012-04-15 03:27:24 -07:00
|
|
|
}
|
|
|
|
|
2013-02-04 14:02:01 -08:00
|
|
|
fn peeking_at_comment(rdr: @mut StringReader) -> 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
|
|
|
}
|
|
|
|
|
2013-02-04 14:02:01 -08:00
|
|
|
fn consume_comment(rdr: @mut StringReader,
|
|
|
|
code_to_the_left: bool,
|
2012-10-05 16:17:10 -07:00
|
|
|
comments: &mut ~[cmnt]) {
|
2012-08-22 17:24:52 -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);
|
2013-02-11 19:26:38 -08:00
|
|
|
} else { fail!(); }
|
2012-08-22 17:24:52 -07:00
|
|
|
debug!("<<< consume comment");
|
2012-04-15 03:27:24 -07:00
|
|
|
}
|
|
|
|
|
2013-02-21 00:16:31 -08:00
|
|
|
pub struct lit {
|
|
|
|
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:
|
|
|
|
@diagnostic::span_handler,
|
2013-06-13 03:02:55 +10:00
|
|
|
path: @str,
|
2013-03-12 13:00:50 -07:00
|
|
|
srdr: @io::Reader)
|
|
|
|
-> (~[cmnt], ~[lit]) {
|
2013-06-13 03:02:55 +10:00
|
|
|
let src = str::from_bytes(srdr.read_whole_stream()).to_managed();
|
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
|
|
|
|
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;
|
2012-10-05 16:17:10 -07:00
|
|
|
consume_whitespace_counting_blank_lines(rdr, &mut comments);
|
2012-04-15 03:27:24 -07:00
|
|
|
}
|
|
|
|
while peeking_at_comment(rdr) {
|
2012-10-05 16:17:10 -07:00
|
|
|
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-06-07 20:00:37 +02:00
|
|
|
let bstart = rdr.last_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
|
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) {
|
2013-06-08 02:26:52 +02:00
|
|
|
do with_str_from(rdr, bstart) |s| {
|
|
|
|
debug!("tok lit: %s", s);
|
|
|
|
literals.push(lit {lit: s.to_owned(), pos: sp.lo});
|
|
|
|
}
|
2012-04-15 03:27:24 -07:00
|
|
|
} else {
|
2013-05-17 10:18:35 -07:00
|
|
|
debug!("tok: %s", token::to_str(get_ident_interner(), &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 correct_stripped = " Test \n* Test\n Test";
|
|
|
|
let stripped = strip_doc_comment_decoration(comment);
|
|
|
|
assert_eq!(stripped.slice(0, stripped.len()), correct_stripped);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test] fn test_block_doc_comment_2() {
|
|
|
|
let comment = "/**\n * Test\n * Test\n*/";
|
|
|
|
let correct_stripped = " Test\n Test";
|
|
|
|
let stripped = strip_doc_comment_decoration(comment);
|
|
|
|
assert_eq!(stripped.slice(0, stripped.len()), correct_stripped);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test] fn test_block_doc_comment_3() {
|
|
|
|
let comment = "/**\n let a: *int;\n *a = 5;\n*/";
|
|
|
|
let correct_stripped = " let a: *int;\n *a = 5;";
|
|
|
|
let stripped = strip_doc_comment_decoration(comment);
|
|
|
|
assert_eq!(stripped.slice(0, stripped.len()), correct_stripped);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test] fn test_line_doc_comment() {
|
|
|
|
let comment = "/// Test";
|
|
|
|
let correct_stripped = " Test";
|
|
|
|
let stripped = strip_doc_comment_decoration(comment);
|
|
|
|
assert_eq!(stripped.slice(0, stripped.len()), correct_stripped);
|
|
|
|
}
|
|
|
|
}
|