1

I can't to do something when a parser fails, in petitparser.

My solution is:

var parser = string("hello").or(
        epsilon().map((_) {
          // do something
        }).seq(failure())
     );

I want to know if there is any better solution?

Freewind
  • 193,756
  • 157
  • 432
  • 708

1 Answers1

2

Yes, this looks reasonable. The more general form

var parser = string("hello")
  .or(failure("Unable to parse hello"))

is quite common.

However, introducing side-effects in parsers is normally not suggested. That said, you could create a function that encapsulates your pattern as follows:

function handleFailure(Parser parser, Function action, String message) {
  return parser.or(epsilon().map(action).failure(message));
}
Lukas Renggli
  • 8,754
  • 23
  • 46
  • Thank you, Lukas! I'm trying to implement nested block parser for this question: http://stackoverflow.com/questions/21784392/how-to-match-the-start-and-end-of-a-block . I used a stack to hold each new start delimiter, and I have to drop the top one if it doesn't get corresponding stop delimiter. That's why I need this `side-effect` – Freewind Feb 24 '14 at 03:38
  • I think the thing with the self-modifying parsers is introducing more problems than it solves. Maybe it is simpler if you implement your own `Parser` subclass, something vaguely similar to the `TrimmingParser`? – Lukas Renggli Feb 24 '14 at 06:37
  • That's a new idea for me. I will try it later – Freewind Feb 24 '14 at 08:51