Skip to content

Commit 79a15ac

Browse files
committed
Rewrite doc_comment_contents without macros
1 parent 2cb555c commit 79a15ac

File tree

2 files changed

+31
-68
lines changed

2 files changed

+31
-68
lines changed

src/fallback.rs

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::strnom::{block_comment, skip_whitespace, whitespace, word_break, Cursor, PResult};
1+
use crate::strnom::{block_comment, skip_whitespace, word_break, Cursor, PResult};
22
use crate::{Delimiter, Punct, Spacing, TokenTree};
33
#[cfg(span_locations)]
44
use std::cell::RefCell;
@@ -1435,31 +1435,33 @@ fn doc_comment(input: Cursor) -> PResult<Vec<TokenTree>> {
14351435
Ok((rest, trees))
14361436
}
14371437

1438-
named!(doc_comment_contents -> (&str, bool), alt!(
1439-
do_parse!(
1440-
punct!("//!") >>
1441-
s: take_until_newline_or_eof!() >>
1442-
((s, true))
1443-
)
1444-
|
1445-
do_parse!(
1446-
option!(whitespace) >>
1447-
peek!(tag!("/*!")) >>
1448-
s: block_comment >>
1449-
((s, true))
1450-
)
1451-
|
1452-
do_parse!(
1453-
punct!("///") >>
1454-
not!(tag!("/")) >>
1455-
s: take_until_newline_or_eof!() >>
1456-
((s, false))
1457-
)
1458-
|
1459-
do_parse!(
1460-
option!(whitespace) >>
1461-
peek!(pair!(tag!("/**"), not!(tag!("*")))) >>
1462-
s: block_comment >>
1463-
((s, false))
1464-
)
1465-
));
1438+
fn doc_comment_contents(input: Cursor) -> PResult<(&str, bool)> {
1439+
let input = skip_whitespace(input);
1440+
if input.starts_with("//!") {
1441+
let input = input.advance(3);
1442+
let (input, s) = take_until_newline_or_eof(input);
1443+
Ok((input, (s, true)))
1444+
} else if input.starts_with("/*!") {
1445+
let (input, s) = block_comment(input)?;
1446+
Ok((input, (s, true)))
1447+
} else if input.starts_with("///") {
1448+
let input = input.advance(3);
1449+
if input.starts_with("/") {
1450+
return Err(LexError);
1451+
}
1452+
let (input, s) = take_until_newline_or_eof(input);
1453+
Ok((input, (s, false)))
1454+
} else if input.starts_with("/**") && !input.advance(3).starts_with("*") {
1455+
let (input, s) = block_comment(input)?;
1456+
Ok((input, (s, false)))
1457+
} else {
1458+
Err(LexError)
1459+
}
1460+
}
1461+
1462+
fn take_until_newline_or_eof(input: Cursor) -> (Cursor, &str) {
1463+
match input.find('\n') {
1464+
Some(i) => (input.advance(i), &input.rest[..i]),
1465+
None => (input.advance(input.len()), input.rest),
1466+
}
1467+
}

src/strnom.rs

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -237,15 +237,6 @@ macro_rules! do_parse {
237237
};
238238
}
239239

240-
macro_rules! peek {
241-
($i:expr, $submac:ident!( $($args:tt)* )) => {
242-
match $submac!($i, $($args)*) {
243-
Ok((_, o)) => Ok(($i, o)),
244-
Err(LexError) => Err(LexError),
245-
}
246-
};
247-
}
248-
249240
macro_rules! call {
250241
($i:expr, $fun:expr $(, $args:expr)*) => {
251242
$fun($i $(, $args)*)
@@ -261,36 +252,6 @@ macro_rules! option {
261252
};
262253
}
263254

264-
macro_rules! take_until_newline_or_eof {
265-
($i:expr,) => {
266-
match $i.find('\n') {
267-
Some(i) => Ok(($i.advance(i), &$i.rest[..i])),
268-
None => Ok(($i.advance($i.len()), $i.rest)),
269-
}
270-
};
271-
}
272-
273-
macro_rules! pair {
274-
($i:expr, $first:ident!( $($arg1:tt)* ), $second:ident!( $($arg2:tt)* )) => {
275-
match $first!($i, $($arg1)*) {
276-
Err(LexError) => Err(LexError),
277-
Ok((i, o1)) => match $second!(i, $($arg2)*) {
278-
Err(LexError) => Err(LexError),
279-
Ok((i, o2)) => Ok((i, (o1, o2))),
280-
},
281-
}
282-
};
283-
}
284-
285-
macro_rules! not {
286-
($i:expr, $submac:ident!( $($args:tt)* )) => {
287-
match $submac!($i, $($args)*) {
288-
Ok((_, _)) => Err(LexError),
289-
Err(LexError) => Ok(($i, ())),
290-
}
291-
};
292-
}
293-
294255
macro_rules! tag {
295256
($i:expr, $tag:expr) => {
296257
if $i.starts_with($tag) {

0 commit comments

Comments
 (0)