Chomp zero or more characters if they pass the test.
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.
Chomp zero or more characters if they pass the test.
Remarks
Example
This is commonly useful for chomping whitespace or variable names:
You can also keep track of state. This is useful to support things like escaping characters in strings:
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 interpretletx
as the keywordlet
followed by "spaces" followed by the variablex
. This is why thekeyword
andnumber
parsers peek ahead, making sure they are not followed by anything unexpected.