sv_parser_parser/general/
comments.rs

1use crate::*;
2
3// -----------------------------------------------------------------------------
4
5#[tracable_parser]
6#[packrat_parser]
7pub(crate) fn comment(s: Span) -> IResult<Span, Comment> {
8    alt((one_line_comment, block_comment))(s)
9}
10
11#[tracable_parser]
12#[packrat_parser]
13pub(crate) fn one_line_comment(s: Span) -> IResult<Span, Comment> {
14    let (s, a) = tag("//")(s)?;
15    let (s, b) = opt(is_not("\n"))(s)?;
16    let (s, c) = opt(tag("\n"))(s)?;
17    let a = if let Some(b) = b {
18        concat(a, b).unwrap()
19    } else {
20        a
21    };
22    let a = if let Some(c) = c {
23        concat(a, c).unwrap()
24    } else {
25        a
26    };
27    Ok((
28        s,
29        Comment {
30            nodes: (into_locate(a),),
31        },
32    ))
33}
34
35#[tracable_parser]
36#[packrat_parser]
37pub(crate) fn block_comment(s: Span) -> IResult<Span, Comment> {
38    let (s, a) = tag("/*")(s)?;
39    let (s, b) = many0(alt((
40        is_not("*"),
41        terminated(tag("*"), peek(not(tag("/")))),
42    )))(s)?;
43    let (s, c) = tag("*/")(s)?;
44    let mut a = a;
45    for b in b {
46        a = concat(a, b).unwrap();
47    }
48    let a = concat(a, c).unwrap();
49    Ok((
50        s,
51        Comment {
52            nodes: (into_locate(a),),
53        },
54    ))
55}