Search code examples
parsingdartpetitparser

How to consume but not capture some chars, in PetitParser?


I defined a rule:

def("invokation", char('@').word().plus().flatten());

For "@who", it will match and get @who as result.

How to ask it just return who without @?


Solution

  • Not sure if your question is about PetitParser for Java or Dart?

    In any case, you need to connect char('@') and word().plus().flatten() to a sequence. Then you pick the second element of the list resulting list, ignoring the first character.

    In Java this looks like this:

    def("invokation", character('@')
        .seq(word().plus().flatten())
        .map(Functions.nthOfList(1));
    

    And in Dart this is:

    def("invokation", char('@')
        .seq(word().plus().flatten())
        .pick(1));
    

    Btw, I just committed an improvement to PetitParser for Java so that you can use pick(int) in Java too.