Question

Let's say I have the following:

case class Var(s: String)

class MyParser extends JavaTokensParser {

  def variableExpr = "?" ~ identifier ^^ { case "?" ~ id => Var(id) }

  def identifier = //...

}

I want this to accept inputs of the form ?X but not ? X (with a space in between). How would this be expressed?

Thanks!

Was it helpful?

Solution

JavaTokensParser by default allows white spaces between any parsers. You could change this behavior this way:

override def skipWhitespace = false

Now you have to specify all white spaces manually:

def ws: Parser[Seq[Char]] = rep(' ')

def variableExpr = ws ~> "?" ~ identifier ^^ { case "?" ~ id => Var(id) }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top