5

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 @?

Lukas Renggli
  • 8,754
  • 23
  • 46
Freewind
  • 193,756
  • 157
  • 432
  • 708

1 Answers1

5

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.

Lukas Renggli
  • 8,754
  • 23
  • 46
  • Can we add an api such as "drop()" which means some matcher should consume the input but will drop the result? So the code can be: `def("invokation", char('@').drop().seq(word().plus().flatten())` – Freewind Jun 23 '13 at 07:58
  • This is certainly doable, but IMHO leads to all kind of weird situations. For example, in the above `seq(...)` the single word would still be wrapped into a list. In general composability would suffer a big deal. That said, there is nothing that prevents you form introducing a drop other new composition operators. – Lukas Renggli Jun 23 '13 at 09:02