Search code examples
dartpetitparser

How to create a parser which means any char not in ['(',')','{','}'], in PetitParserDart?


I want to define a parser which accept any char except ['(', ')', '{', '}'] in PetitParserDart.

I tried:

char('(').not() & char(')').not() & char('{').not() & char('}')

I'm not sure if it's correct, and is it any simple way to do this? (something like chars('(){}').neg()) ?


Solution

  • This matches anything, but the characters listed after the caret ^. It is the character class of all characters without the listed ones:

    pattern('^(){}');
    

    This also works (note the .not() on the last character, and the any() to actually consume the character):

    char('(').not() & char(')').not() & char('{').not() & char('}').not() & any()
    

    And this one works as well:

    anyIn('(){}').neg()
    

    Which is equivalent to:

    (anyIn('(){}').not() & any()).pick(1)
    

    And another alternative is:

    (char('(') | char(')') | char('{') | char('}')).neg()
    

    Except for the second example, all examples return the parsed character (this can be easily fixed, but I wanted to stay close to your question). The first example is probably the easiest to understand, but depending on context you might prefer one of the alternatives.