我正在尝试在scala中编写一个简单的解析器,但是当我添加一个重复的令牌时,Scala似乎陷入无限循环......
object RewriteRuleParsers extends RegexParsers {
private def space = regex(new Regex("[ \\n]+"))
private def number = regex(new Regex("[0-9]+"))
private def equals = (space?)~"="~(space?)
private def word = regex(new Regex("[a-zA-Z][a-zA-Z0-9-]*"))
private def string = regex(new Regex("[0-9]+")) >> { len => ":" ~> regex(new Regex(".{" + len + "}")) }
private def matchTokenPartContent: Parser[Any] = (space?)~word~equals~word<~ space?
private def matchTokenPart: Parser[Any] = ((space?) ~> "{" ~> matchTokenPartContent <~ "}"<~ space?)
private def matchTokenParts = (matchTokenPart *)
private def matchToken: Parser[Any] = ("[" ~> matchTokenParts ~ "]")
def parseMatchToken(str: String): ParseResult[Any] = parse(matchToken, str)
}
以及调用它的代码
val parseResult = RewriteRuleParsers.parseMatchToken("[{tag=hello}]")
感谢任何建议