score:0

Accepted answer

This problem is very weird. When you call | and both sides are failures, the side where the failure happened last is selected, ties favoring the left-sided one.

When you try to parse directly with giverRole, it produces the result you expect. If you add a successful match before the failure, though, it produces the result you are seeing.

The reason is rather subtle -- I only found it out by sprinkling log statements on all parsers. To understand it, you must understand how does RegexParser skip spaces. Specifically, spaces are skipped on accept. Because failure doesn't call accept, it doesn't skip spaces.

While the failure of kwIs happens on b, as the space as skipped, the failure of failure happens on the space after If. Here:

If bla blablaa
   ^ kwIs fails here
  ^ failure fails here

Therefore, the error message on kwIs gets precedence by the rule I mentioned.

You can get around this problem by making the parser skip the spaces without matching anything. It is important that this pattern always match, or you'll get an even more confusing error message. Here's a suggestion I think works:

"\\b|$".r ~ failure("is expected")

Another solution is to use acceptIf or acceptMatch instead of using the implicit regex accept, in which case you can provide a tailored error message.


Related Query

More Query from same tag