1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
use crate::*;
use peekmore::{PeekMore, PeekMoreIterator};
use std::io::{self, BufRead};
use utf8_chars::{BufReadCharsExt, Chars};
pub struct CommentIterator<'r> {
iter: PeekMoreIterator<Chars<'r, dyn BufRead + 'r>>,
cursor: Cursor,
trackers: &'static [[&'static str; 2]],
excluded: &'static [[&'static str; 2]],
escape: Option<char>,
active: Option<usize>,
excluded_active: Option<usize>,
escaped: bool,
text: String,
start: Cursor,
}
impl<'r> CommentIterator<'r> {
pub fn from_extension(reader: &'r mut dyn BufRead, extension: &str) -> Option<Self> {
match extension {
// C-style: // line comments, /* */ block comments, "..." strings
"rs" | "js" | "ts" | "jsx" | "tsx" | "c" | "h" | "cpp" | "cc" | "cxx" | "hpp"
| "java" | "go" | "swift" | "kt" | "kts" | "cs" | "scala" | "dart" | "php" | "css"
| "scss" | "less" | "proto" | "glsl" | "hlsl" => Some(Self::new(
reader,
&[["//", "\n"], ["/*", "*/"]],
&[["\"", "\""]],
Some('\\'),
)),
// Python: # line comments, triple/single/double-quoted strings
"py" | "pyi" => Some(Self::new(
reader,
&[["#", "\n"]],
&[
["\"\"\"", "\"\"\""],
["'''", "'''"],
["\"", "\""],
["'", "'"],
],
Some('\\'),
)),
// Shell-family: # line comments, single- and double-quoted strings.
// (Single-quoted strings in sh/bash don't actually support escaping,
// but sharing the escape char here is a harmless simplification.)
"sh" | "bash" | "zsh" | "fish" | "rb" | "pl" | "pm" | "yaml" | "yml" | "toml"
| "ini" | "cfg" | "r" | "R" | "make" | "mk" | "dockerfile" | "nim" | "elixir"
| "ex" | "exs" => Some(Self::new(
reader,
&[["#", "\n"]],
&[["\"", "\""], ["'", "'"]],
Some('\\'),
)),
// HTML/XML: only block comments, no string-exclusion needed for
// comment detection since <!-- --> can't legally nest inside tags
// in a way that matters here.
"html" | "htm" | "xml" | "svg" | "xhtml" | "vue" => {
Some(Self::new(reader, &[["<!--", "-->"]], &[], None))
}
// SQL: -- line comments, /* */ block comments, '...' strings
// (SQL strings use '' to escape a literal quote, not backslash,
// so no escape char is passed here — see note below).
"sql" => Some(Self::new(
reader,
&[["--", "\n"], ["/*", "*/"]],
&[["'", "'"]],
None,
)),
// Lua: -- line comments, --[[ ]] block comments, "..."/'...' strings
"lua" => Some(Self::new(
reader,
&[["--[[", "]]"], ["--", "\n"]],
&[["\"", "\""], ["'", "'"]],
Some('\\'),
)),
// Haskell: -- line comments, {- -} block comments, "..." strings
"hs" | "lhs" => Some(Self::new(
reader,
&[["--", "\n"], ["{-", "-}"]],
&[["\"", "\""]],
Some('\\'),
)),
_ => None,
}
}
pub fn new(
reader: &'r mut dyn BufRead,
trackers: &'static [[&'static str; 2]],
excluded: &'static [[&'static str; 2]],
escape: Option<char>,
) -> Self {
Self {
iter: reader.chars().peekmore(),
cursor: Cursor::start(),
trackers,
excluded,
escape,
active: None,
excluded_active: None,
escaped: false,
text: String::new(),
start: Cursor::start(),
}
}
fn try_match(&mut self, first: char, pattern: &str) -> bool {
let mut chars = pattern.chars();
match chars.next() {
Some(c) if c == first => {}
_ => return false,
}
for (i, c) in chars.clone().enumerate() {
if !matches!(self.iter.peek_nth(i), Some(&Ok::<char, io::Error>(c2)) if c2 == c) {
return false;
}
}
for _ in chars {
if let Some(Ok(ch)) = self.iter.next() {
self.cursor.advance_ch(ch);
}
}
true
}
}
impl<'r> Iterator for CommentIterator<'r> {
type Item = Result<Spanned<String>, io::Error>;
fn next(&mut self) -> Option<Self::Item> {
while let Some(ch) = self.iter.next() {
let ch = match ch {
Ok(c) => c,
Err(e) => return Some(Err(e)),
};
if self.escaped {
self.escaped = false;
if self.active.is_some() {
self.text.push(ch);
}
self.cursor.advance_ch(ch);
continue;
}
if let Some(esc) = self.escape
&& ch == esc
{
self.escaped = true;
if self.active.is_some() {
self.text.push(ch);
}
self.cursor.advance_ch(ch);
continue;
}
match self.active {
None => {
if let Some(i) = self.excluded_active {
if self.try_match(ch, self.excluded[i][1]) {
self.excluded_active = None;
}
self.cursor.advance_ch(ch);
continue;
}
let mut matched_excluded = None;
for (i, pair) in self.excluded.iter().enumerate() {
if self.try_match(ch, pair[0]) {
matched_excluded = Some(i);
break;
}
}
if let Some(i) = matched_excluded {
self.excluded_active = Some(i);
self.cursor.advance_ch(ch);
continue;
}
let mut matched = None;
for (i, tracker) in self.trackers.iter().enumerate() {
if self.try_match(ch, tracker[0]) {
matched = Some(i);
break;
}
}
self.cursor.advance_ch(ch);
if let Some(i) = matched {
self.active = Some(i);
// start *after* the opening marker has been fully consumed
self.start = self.cursor;
}
}
Some(i) => {
// cursor position *before* attempting to match the closing marker
let before_closing = self.cursor;
if self.try_match(ch, self.trackers[i][1]) {
self.cursor.advance_ch(ch);
self.active = None;
let text = std::mem::take(&mut self.text);
let span = Span::new(self.start, before_closing);
return Some(Ok(text.into_spanned(span)));
} else {
self.text.push(ch);
self.cursor.advance_ch(ch);
}
}
}
}
None
}
}
#[cfg(test)]
mod tests {
use std::io::BufReader;
use super::*;
#[test]
fn test_rs_comment_iterator() {
let source = r#"
// This is a comment
/* This is another comment */
"#;
let mut reader = BufReader::new(source.as_bytes());
let mut iter = CommentIterator::from_extension(&mut reader, "rs").unwrap();
assert_eq!(
iter.next().unwrap().unwrap(),
" This is a comment"
.to_string()
.into_spanned(Span::new(Cursor::new(2, 11, 11), Cursor::new(2, 29, 29)))
);
assert_eq!(
iter.next().unwrap().unwrap(),
" This is another comment "
.to_string()
.into_spanned(Span::new(Cursor::new(3, 11, 40), Cursor::new(3, 36, 65)))
);
}
}