Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support comment syntax #6

Open
1 task
wakame-tech opened this issue Mar 22, 2022 · 1 comment
Open
1 task

Support comment syntax #6

wakame-tech opened this issue Mar 22, 2022 · 1 comment
Labels
enhancement New feature or request

Comments

@wakame-tech
Copy link
Member

overview

  • support comment syntax (// comment, /* comment */)
    • need 2-pass parse?
@wakame-tech wakame-tech added the enhancement New feature or request label Mar 22, 2022
@hamadakafu
Copy link
Contributor

hamadakafu commented Mar 22, 2022

https://github.com/Geal/nom/blob/main/doc/nom_recipes.md#comments

  • ok: many0(alt((..., multispace1)))

  • bad: many0(alt((..., multispace0)))

  • bad: many0(alt(multispace0, ...)

  • bad: many1(alt((..., multispace0)))

  • bad: many1(alt((multispace0, ...)))

  • ok: FnMut(&'a str) ...

  • bad: Fn(&'a str)

/// multispace0の代わりにparse_commentを使うべき
pub fn ws<'a, F: 'a, O, E: ParseError<&'a str>>(
    inner: F,
) -> impl FnMut(&'a str) -> IResult<&'a str, O, E>
where
    F: FnMut(&'a str) -> IResult<&'a str, O, E>,
{
    delimited(
        many0(alt((parse_comments, value((), multispace1)))),
        inner,
        many0(alt((parse_comments, value((), multispace1)))),
    )
}

fn parse_comments<'a, E: ParseError<&'a str>>(s: &'a str) -> IResult<&str, (), E> {
    alt((parse_comment_oneline, parse_comment_multiline))(s)
}

// ファイルの末尾に改行なしのコメントが書けるので
// 末尾の改行は消費しないことに注意する
fn parse_comment_oneline<'a, E: ParseError<&'a str>>(s: &'a str) -> IResult<&str, (), E> {
    value((), pair(tag("//"), is_not("\n")))(s)
}

fn parse_comment_multiline<'a, E: ParseError<&'a str>>(s: &'a str) -> IResult<&str, (), E> {
    value(
        (), // Output is thrown away.
        tuple((tag("/*"), take_until("*/"), tag("*/"))),
    )(s)
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants