scyllax_parser/
comment.rs

1use nom::{
2    branch::alt,
3    bytes::complete::{is_not, tag, take_until},
4    combinator::value,
5    sequence::{delimited, pair},
6    IResult,
7};
8
9// matches a cql comment
10// - `-- end of line comment`
11// - `/* block comment */` (can be multiline)
12// - `// end of line comment`
13pub fn parse_comment(input: &str) -> IResult<&str, ()> {
14    alt((
15        parse_line_comment,
16        parse_block_comment,
17        parse_line_comment_slash_slash,
18    ))(input)
19}
20
21fn parse_line_comment(input: &str) -> IResult<&str, ()> {
22    value(
23        (), // Output is thrown away.
24        pair(tag("--"), is_not("\n\r")),
25    )(input)
26}
27
28fn parse_block_comment(input: &str) -> IResult<&str, ()> {
29    value(
30        (), // Output is thrown away.
31        delimited(tag("/*"), take_until("*/"), tag("*/")),
32    )(input)
33}
34
35fn parse_line_comment_slash_slash(input: &str) -> IResult<&str, ()> {
36    value(
37        (), // Output is thrown away.
38        pair(tag("//"), is_not("\n\r")),
39    )(input)
40}
41
42#[cfg(test)]
43mod tests {
44    use super::*;
45    use pretty_assertions::assert_eq;
46
47    #[test]
48    fn test_start_of_line_comment() {
49        let input = "-- start of line comment";
50        let result = parse_comment(input);
51        assert_eq!(result, Ok(("", ())));
52    }
53
54    #[test]
55    fn test_block_comment() {
56        let input = "/* block comment */";
57        let result = parse_comment(input);
58        assert_eq!(result, Ok(("", ())));
59    }
60
61    #[test]
62    fn test_end_of_line_comment() {
63        let input = "// end of _ line comment";
64        let result = parse_comment(input);
65        assert_eq!(result, Ok(("", ())));
66    }
67}