Skip to main content

px_wsdom_ts_parse/parser/
comment.rs

1use winnow::{
2    ascii::{line_ending, multispace0, not_line_ending},
3    combinator::{alt, delimited, repeat},
4    token::take_until0,
5    PResult, Parser,
6};
7
8use super::util::Parsable;
9
10#[derive(Debug, Clone, PartialEq, Eq)]
11pub struct Comment<'a> {
12    pub source: &'a str,
13}
14
15impl<'a> Parsable<'a> for Comment<'a> {
16    fn parse(input: &mut &'a str) -> PResult<Self> {
17        alt((
18            ("/*", take_until0("*/"), "*/").recognize(),
19            ("//", not_line_ending, line_ending).recognize(),
20        ))
21        .parse_next(input)
22        .map(|source| Self { source })
23    }
24}
25
26#[derive(Debug, Clone, PartialEq, Eq)]
27pub struct WithComment<'a, T> {
28    pub comment: Vec<Comment<'a>>,
29    pub data: T,
30}
31
32impl<'a, T: Parsable<'a>> Parsable<'a> for WithComment<'a, T> {
33    fn parse(input: &mut &'a str) -> PResult<Self> {
34        (
35            repeat(0.., delimited(multispace0, Comment::parse, multispace0)),
36            T::parse,
37        )
38            .map(|(comment, data)| Self { comment, data })
39            .parse_next(input)
40    }
41}