Search code examples
whitespacedartpetitparser

How to create a parser which is a whitespace but not a line separator, in PetitParser?


There is a built-in whitespace() parser in PetitParserDart, which checks character in:

(9 <= value && value <= 13) || (value == 32) || (value == 160)
  || (value == 5760) || (value == 6158) || (8192 <= value && value <= 8202) || (value == 8232)
  || (value == 8233) || (value == 8239) || (value == 8287) || (value == 12288)

Now I want a custom whitespace parser that is a whitespace() parser, but not accept line separator \n.

How to do that, I don't want to copy and modify the code inside whitespace(). Is there a better way to do this?


Solution

  • There are various ways to do this.

    One way that reuses the existing parser would be:

    char('\n').not().seq(whitespace()).pick(1);
    

    Another way is to create a new character pattern:

    pattern('\t\f\r ');
    

    This does not exactly match all the unicode whitespaces that whitespace() accepts, but likely is enough for most use-cases. Alternatively you can add the unicode ranges as well:

    pattern('\t\f\r \u00a0\u1680\u180e\u2000-\u200a\u2028\u2029\u202f\u205f\u3000');