我试图解析两个Ints和一些元素的输入和结束:
import scala.util.parsing.combinator.JavaTokenParsers
class X extends JavaTokenParsers {
lazy val elems = elem("wrong elem", "#WB-" contains _)
lazy val lists = repsep(rep(elems), ",")
lazy val p1 = int ~ int ~ lists
lazy val p2 = int ~ int ~ (whiteSpace ~> lists)
def go[A](p: Parser[A]) = parseAll(p, "1 2 WB#,---,BBB") match {
case NoSuccess(msg, _) => sys.error(msg)
case _ =>
}
lazy val int: Parser[Int] =
wholeNumber ^^ {
try _.toInt catch {
case e: NumberFormatException => sys.error("invalid number")
}
}
}
方法go
中给出了示例输入。 Ints和末尾的元素必须用空格分隔。但这只适用于Ints而不适用于元素。当我输入
val x = new X
x go x.p1
我收到以下错误:
java.lang.RuntimeException: string matching regex `\z' expected but `W' found
但是当我输入
时x go x.p1
我明白了:
java.lang.RuntimeException: string matching regex `\s+' expected but `W' found
最后我想要一个Parser[Int ~ Int ~ List[List[Char]]]
。为什么在elem
前插入空格不起作用?我怎样才能使这段代码生效?
答案 0 :(得分:2)
只需用RegEx Parser替换elems:
import scala.util.parsing.combinator.JavaTokenParsers
class X extends JavaTokenParsers {
lazy val elems = "[#WB-]".r
lazy val lists = repsep(rep(elems), ",")
lazy val p1 = int ~ int ~ lists
def go[A](p: Parser[A]) = parseAll(p, "1 2 WB#,---,BBB") match {
case NoSuccess(msg, _) => sys.error(msg)
case _ =>
}
lazy val int: Parser[Int] =
wholeNumber ^^ {
try _.toInt catch {
case e: NumberFormatException => sys.error("invalid number")
}
}
}
我删除了p2,因为现在没用了