There is a trim()
method which will create a parser to trim a string on both side.
How to create one which just trim the left or right?
The following helpers create parsers that trim in all possible ways:
Parser trim(Parser parser, [Parser trimmer]) {
if (trimmer == null) trimmer = whitespace();
return trimmer.star().seq(parser).seq(trimmer.star()).pick(1);
}
Parser trimRight(Parser parser, [Parser trimmer]) {
if (trimmer == null) trimmer = whitespace();
return parser.seq(trimmer.star()).pick(0);
}
Parser trimLeft(Parser parser, [Parser trimmer]) {
if (trimmer == null) trimmer = whitespace();
return trimmer.star().seq(parser).pick(1);
}
The above function trim
results in an equivalent parser to the built-in function Parser.trim
.