Function chompWhile

  • Chomp zero or more characters if they pass the test.

    Remarks

    Example

    This is commonly useful for chomping whitespace or variable names:

        const whitespace: P.Parser<P.Unit> = P.chompWhile(
    (c: string) => c == " " || c == "\t" || c == "\n" || c == "\r"
    );

    const elmVar: P.Parser<string> = P.getChompedString(
    P.succeed(P.Unit).skip(
    P.chompIf(Helpers.isLower).skip(
    P.chompWhile((c: string) => Helpers.isAlphaNum(c) || c == "_")
    )
    )
    );

    You can also keep track of state. This is useful to support things like escaping characters in strings:

       const string = A.symbol('"')
    .keep(
    A.chompWhile(
    (c, isEscaped) => [c !== '"' || isEscaped, c === "\\"],
    false
    ).getChompedString()
    ).skip(A.symbol('"');

    Note: a chompWhile parser always succeeds! This can lead to tricky situations, especially if you define your whitespace with it. In that case, you could accidentally interpret letx as the keyword let followed by "spaces" followed by the variable x. This is why the keyword and number parsers peek ahead, making sure they are not followed by anything unexpected.

    Type Parameters

    • A

    Parameters

    • isGood: ((char: string, state: A) => [boolean, A])
        • (char: string, state: A): [boolean, A]
        • Parameters

          • char: string
          • state: A

          Returns [boolean, A]

    • init: A

    Returns Simple.Parser<Symbol>

  • Chomp zero or more characters if they pass the test.

    Remarks

    Example

    This is commonly useful for chomping whitespace or variable names:

        const whitespace: P.Parser<P.Unit> = P.chompWhile(
    (c: string) => c == " " || c == "\t" || c == "\n" || c == "\r"
    );

    const elmVar: P.Parser<string> = P.getChompedString(
    P.succeed(P.Unit).skip(
    P.chompIf(Helpers.isLower).skip(
    P.chompWhile((c: string) => Helpers.isAlphaNum(c) || c == "_")
    )
    )
    );

    You can also keep track of state. This is useful to support things like escaping characters in strings:

       const string = A.symbol('"')
    .keep(
    A.chompWhile(
    (c, isEscaped) => [c !== '"' || isEscaped, c === "\\"],
    false
    ).getChompedString()
    ).skip(A.symbol('"');

    Note: a chompWhile parser always succeeds! This can lead to tricky situations, especially if you define your whitespace with it. In that case, you could accidentally interpret letx as the keyword let followed by "spaces" followed by the variable x. This is why the keyword and number parsers peek ahead, making sure they are not followed by anything unexpected.

    Type Parameters

    • A

    Parameters

    • isGood: ((char: string) => boolean)
        • (char: string): boolean
        • Parameters

          • char: string

          Returns boolean

    Returns Simple.Parser<Symbol>