I wrote the following Parser with the intent of fail-ing on whitespace:
import scala.util.parsing.combinator._
object Foo extends JavaTokenParsers {
val wsTest = not(whiteSpace) // uses whitespace inherited from `RegexParsers`
}
Why is parsing a bunch of whitespace successfull?
scala> Foo.parseAll(Foo.wsTest, " ")
res5: Foo.ParseResult[Unit] = [1.11] parsed: ()
scala> res5.successful
res6: Boolean = true
Looking at Parsers#not from the project, I would've expected a Failure for my above test.
/** Wrap a parser so that its failures and errors become success and
* vice versa -- it never consumes any input.
*/
def not[T](p: => Parser[T]): Parser[Unit] = Parser { in =>
p(in) match {
case Success(_, _) => Failure("Expected failure", in)
case _ => Success((), in)
}
}
notworks correctly. My guess is that the parser skips white spaces by default and you have to disable that. Maybe this helps: stackoverflow.com/questions/3564094/… - KigyoMy guess is that the parser skips white spaces by default- I've observed this behavior with a class extendingJavaTokenParsers. However, I would not have expectedFoo.parseAll(Foo.wsTest, " ")to have succeeded. - Kevin Meredith