Function multiComment

  • Parse multi-line comments. So if you wanted to parse Elm whitespace or JS whitespace, you could say:


    const ifProgress =
    <A>(parser: P.Parser<A>) =>
    (offset: number): P.Parser<P.Step<number, P.Unit>> => {
    return P.succeed((x: A) => x)
    .skip(parser)
    .getOffset()
    .map((newOffset) =>
    offset === newOffset ? P.Done(P.Unit) : P.Loop(newOffset)
    );
    };

    const elm: P.Parser<P.Unit> = P.loop(0)(
    ifProgress(
    P.oneOf(
    P.lineComment("//"),
    P.multiComment("/*")("* /")(P.Nestable.Nestable),
    P.spaces
    )
    )
    );

    const js: P.Parser<P.Unit> = P.loop(0)(
    ifProgress(
    P.oneOf(
    P.lineComment("//"),
    P.multiComment("/*")("* /")(P.Nestable.NotNestable),
    P.chompWhile((c) => c == " " || c == "\n" || c == "\r" || c == "\t")
    )
    )
    );

    "* /" is incorrect, it should be "*​/" but that requires me to use a zero with space character, confusing anyone who copies the code as to why the example doesn't work.

    Note: The fact that spaces comes last in the definition of elm is very important! It can succeed without consuming any characters, so if it were the first option, it would always succeed and bypass the others! (Same is true of chompWhile in js.) This possibility of success without consumption is also why we need the ifProgress helper. It detects if there is no more whitespace to consume.

    Parameters

    • open: string

    Returns ((close: string) => ((nestable: Simple.Nestable) => Simple.Parser<Symbol>))