1
votes

If I have simple parser defs like this:

  def term: Parser[String] = """[a-zA-Z"']+""".r ^^ { _.toString }
  def intWhole: Parser[String] = wholeNumber ^^ { w => w }

  def simpleTerm: Parser[String] = term >> { 
    case t:String => failure("Oops!") 
  }

If I parse against simpleTerm (with any string) it fails as expected with my "Oops!" message.

Now if I add this:

  def repTerm: Parser[Unit] = rep(simpleTerm | intWhole) ^^ { _ => Unit }

If I now parse against repTerm, again with just a non-numeric character string, what I'd hope to happen is have it fail with the same "Oops!" message--basically an aborted parse. What happens instead is that I get no error at all; just the returned Unit.

Is there a way to make parsing stop once it hits a failure, and return that failure, during a rep() clause?

1

1 Answers

0
votes

Looked at the code. There's a difference how rep() handles failure vs errors. failure() just tells a repeating sequence to stop, i.e. end of repeating clause. It's not a breakage necessarily. err() means something broke, and a rep() clause does propagate errors and stop further parsing.

Changing my failure() in code above to err() produces the desired result of stopping further parsing.