Skip to content

Commit d7904e5

Browse files
committed
Allow doc comments to be terminated with EOF
Closes #61
1 parent eca28d4 commit d7904e5

File tree

3 files changed

+17
-29
lines changed

3 files changed

+17
-29
lines changed

src/stable.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1179,7 +1179,7 @@ fn op_char(input: Cursor) -> PResult<char> {
11791179
named!(doc_comment -> (), alt!(
11801180
do_parse!(
11811181
punct!("//!") >>
1182-
take_until!("\n") >>
1182+
take_until_newline_or_eof!() >>
11831183
(())
11841184
)
11851185
|
@@ -1193,7 +1193,7 @@ named!(doc_comment -> (), alt!(
11931193
do_parse!(
11941194
punct!("///") >>
11951195
not!(tag!("/")) >>
1196-
take_until!("\n") >>
1196+
take_until_newline_or_eof!() >>
11971197
(())
11981198
)
11991199
|

src/strnom.rs

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -263,34 +263,14 @@ macro_rules! option {
263263
};
264264
}
265265

266-
macro_rules! take_until {
267-
($i:expr, $substr:expr) => {{
268-
if $substr.len() > $i.len() {
269-
Err(LexError)
266+
macro_rules! take_until_newline_or_eof {
267+
($i:expr,) => {{
268+
if $i.len() == 0 {
269+
Ok(($i, ""))
270270
} else {
271-
let substr_vec: Vec<char> = $substr.chars().collect();
272-
let mut window: Vec<char> = vec![];
273-
let mut offset = $i.len();
274-
let mut parsed = false;
275-
for (o, c) in $i.char_indices() {
276-
window.push(c);
277-
if window.len() > substr_vec.len() {
278-
window.remove(0);
279-
}
280-
if window == substr_vec {
281-
parsed = true;
282-
window.pop();
283-
let window_len: usize = window.iter()
284-
.map(|x| x.len_utf8())
285-
.fold(0, |x, y| x + y);
286-
offset = o - window_len;
287-
break;
288-
}
289-
}
290-
if parsed {
291-
Ok(($i.advance(offset), &$i.rest[..offset]))
292-
} else {
293-
Err(LexError)
271+
match $i.find('\n') {
272+
Some(i) => Ok(($i.advance(i), &$i.rest[..i])),
273+
None => Ok(($i.advance($i.len()), ""))
294274
}
295275
}
296276
}};

tests/test.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,5 +175,13 @@ fn tricky_doc_commaent() {
175175
let stream = "/**/".parse::<proc_macro2::TokenStream>().unwrap();
176176
let tokens = stream.into_iter().collect::<Vec<_>>();
177177
assert!(tokens.is_empty(), "not empty -- {:?}", tokens);
178+
179+
let stream = "/// doc".parse::<proc_macro2::TokenStream>().unwrap();
180+
let tokens = stream.into_iter().collect::<Vec<_>>();
181+
assert!(tokens.len() == 1, "not length 1 -- {:?}", tokens);
182+
match tokens[0].kind {
183+
proc_macro2::TokenNode::Literal(_) => {}
184+
_ => panic!("wrong token {:?}", tokens[0]),
185+
}
178186
}
179187

0 commit comments

Comments
 (0)