Parsica comes with a number of useful parsers for dealing with different types of whitespace and newlines, as well with required or optional whitespace. We recommend browsing
src/space.php to see what is available, so you don't need to build your own parsers for that.
When building a parser for say a language or a file format, you often have specific rules about space. Whitespace can be required or optional, and expressions can be valid or invalid if they contain newlines. All of these are could be valid or invalid depending on your case:
There's too much variation for Parsica to provide a single solution. However, you don't want to litter your code with space parsers everywhere:
This is noisy. And if you want to change the rules about whitespace or build more complex parsers, you have to deal with this problem all the time, making it unmaintainable (or at least annoying).
The idea is to build a space consumer that you can reuse everywhere. The space consumer is a parser combinator that you wrap around another parser, and that returns the output of the inner parser, ignoring whitespace. A typical approach is to consistently ignore space after the thing you're interested in.
Now, all the logic for skipping space is nicely contained in
$token. If we wanted to disallow multiline expressions, we only need to replace
skipHSpace1() in one place.
As an example, here's an excerpt from the JSON parser, using the ws (whitespace) as defined in the JSON spec:
If you have multiple ways of handling space in one parser, you can of course define multiple space consumers and give them relevant names.