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
use crate::parsers::{ParseResult, Span};
use nom::bytes::complete::take_while;
use nom::character::complete::{char, multispace0};
use nom::combinator::opt;
use nom::combinator::value;
use nom::sequence::{pair, terminated};
use nom::Parser;
/// Parses a comment and swallows trailing whitespace / newline.
///
/// Accepts empty comments such as `;` followed immediately by `\n` or `\r\n`.
///
/// ## Example
///
/// Comments are parsed and trailing whitespace is suppressed:
///
/// ```
/// # use pddl::parsers::{ignore_eol_comment};
/// let input = "; nothing\n\t; and this line\n\tthen more";
///
/// let (remainder, comment) = ignore_eol_comment(input).unwrap();
///
/// assert_eq!(comment, ());
/// assert_eq!(remainder.to_ascii_lowercase().trim(), "then more");
/// ```
///
/// If no comments are found, the text parses as expected, although leading whitespace is still suppressed.
///
/// ```
/// # use pddl::parsers::{ignore_eol_comment};
/// let input = "then more";
///
/// let (remainder, comment) = ignore_eol_comment(input).unwrap();
///
/// assert_eq!(comment, ());
/// assert_eq!(remainder.to_ascii_lowercase().trim(), "then more");
/// ```
pub fn ignore_eol_comment<'a, S: Into<Span<'a>>>(input: S) -> ParseResult<'a, ()> {
value(
(), // Output is thrown away.
opt(terminated(
pair(char(';'), take_while(|c: char| c != '\n' && c != '\r')),
(multispace0, opt(ignore_eol_comment)),
)),
)
.parse(input.into())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn single_line() {
let input = "; nothing";
let (remainder, comment) = ignore_eol_comment(input).unwrap();
assert_eq!(comment, ());
assert!(remainder.is_empty());
}
#[test]
fn precedes_text() {
let input = "; nothing\n\r\tlast line";
let (remainder, comment) = ignore_eol_comment(input).unwrap();
assert_eq!(comment, ());
assert_eq!(remainder.to_ascii_lowercase(), "last line");
}
}