Implmented stack based functionality

This commit is contained in:
Aaron Power 2016-08-16 17:35:22 +01:00
parent e2d3de4a2e
commit e1a4529e2d
2 changed files with 55 additions and 46 deletions

View file

@ -219,37 +219,34 @@ impl Languages {
continue;
}
stats.comments += handle_multi_line();
// if line.starts_with(multi_line) {
// if let Some(multi_line) = has_trailing_comments(line, &language) {
// previous_comment_start = multi_line;
// is_in_comments = true;
// if language.nested {
// comment_depth += 1;
// }
// }
// }
//
//
// if is_in_comments {
// for &(multi_line, multi_line_end) in &language.multi_line {
// if multi_line == previous_comment_start {
// if let Some(pos) = line.find(multi_line_end) {
// if language.nested {
// comment_depth -= 1;
// if comment_depth == 0 {
// is_in_comments = false;
// }
// } else {
// is_in_comments = false;
// }
// }
// }
// }
// stats.comments += 1;
// continue;
// }
//
if line.starts_with(multi_line) {
if let Some(multi_line) = has_trailing_comments(line, &language) {
previous_comment_start = multi_line;
is_in_comments = true;
if language.nested {
comment_depth += 1;
}
}
}
if is_in_comments {
for &(multi_line, multi_line_end) in &language.multi_line {
if multi_line == previous_comment_start {
if let Some(pos) = line.find(multi_line_end) {
if language.nested {
comment_depth -= 1;
if comment_depth == 0 {
is_in_comments = false;
}
} else {
is_in_comments = false;
}
}
}
}
stats.comments += 1;
continue;
}
for single in &language.line_comment {
if line.starts_with(single) {
stats.comments += 1;

View file

@ -1,10 +1,5 @@
use std::cmp;
pub fn handle_multi_line() -> usize {
unreachable!()
}
/// This is used to catch lines like "let x = 5; /* Comment */"
pub fn has_trailing_comments(line: &str, language: &Language) -> Vec<&'static str> {
let line = slice_to_single(line, language);
@ -12,11 +7,11 @@ pub fn has_trailing_comments(line: &str, language: &Language) -> Vec<&'static st
let mut start = None;
let mut stack = vec![];
for &(comment, comment_end) in &language {
start = line.find(comment).and_then(|x| cmp::min(x, start.unwrap_or(|| x)));
for &(comment, comment_end) in &language.multi_line {
start = line.find(comment).and_then(|x| cmp::min(x, start.unwrap_or(x)));
// This should short circuit 99% of languages.
if start != None && !language.nested && language.multi_line.len() == 1 {
if start.is_none() && !language.nested && language.multi_line.len() == 1 {
if let Some(end) = line.rfind(comment_end) {
if let Some(end_check) = line.rfind(comment) {
if end_check > end {
@ -37,17 +32,34 @@ pub fn has_trailing_comments(line: &str, language: &Language) -> Vec<&'static st
};
let mut chars = line[start..].chars();
let mut cont = false;
loop {
let window = chars.as_str();
if window.starts_with(comment) {
if nested {
is_in_comments += 1;
} else {
is_in_comments = 1;
// Prevents counting overlaps like /*/*
if cont {
cont = false;
continue;
}
if let Some(last) = stack.last() {
if window.starts_with(last) {
stack.pop();
cont = true;
continue;
}
}
for &(comment, comment_end) in &language.multi_line {
if window.starts_with(comment) {
if nested {
stack.push(comment_end);
} else if stack.len() == 0 {
stack.push(comment_end);
}
cont = true;
continue;
}
} else if window.starts_with(comment_end) {
is_in_comments = is_in_comments.saturating_sub(1);
}
if chars.next().is_none() {