I'm trying to write a parser in Scala that gradually builds up a concrete type hierarchy. I started with:
private def word = regex(new Regex("[a-zA-Z][a-zA-Z0-9-]*"))
private def quicktoken: Parser[Quicktoken] = "/" ~> word <~ "/" <~ (space?) ^^ { new Quicktoken(_) }
which is fine. /hello/ will get parsed to a quicktoken
Now I want to add the quicktoken to a composite expression. I have a class
class MatchTokenPart(word:String,quicktoken:RewriteWord){
}
I would have thought that I could write...
private def matchTokenPartContent: Parser[MatchTokenPart] = word<~equals~quicktoken ^^ { case word~quicktoken => new MatchTokenPart(word, quicktoken)}
but it doesn't work. It says that word is of type Option[String] and quicktoken of type String. What am I missing?
Another precedence issue: a <~ b ~ c
is interpreted as a <~ (b ~ c)
, not (a <~ b) ~ c
. This is because infix operators starting with <
have lower precedence than ones starting with ~
, (see the list in 6.12.3 of the language specification).
You want (word <~ equals) ~ quicktoken
, so you'll need to supply the parentheses.