我想实现一个简单的类似 Wiki 的标记解析器,作为使用 Scala 解析器组合器的练习。
我想一点一点地解决这个问题,所以这是我想在第一个版本中实现的:一个简单的内联文字标记。
例如,如果输入字符串是:
This is a sytax test ``code here`` . Hello ``World``
输出字符串应该是:
This is a sytax test <code>code here</code> . Hello <code>World</code>
我尝试通过使用来解决这个问题RegexParsers
,这就是我现在所做的:
import scala.util.parsing.combinator._
import scala.util.parsing.input._
object TestParser extends RegexParsers
{
override val skipWhitespace = false
def toHTML(s: String) = "<code>" + s.drop(2).dropRight(2) + "</code>"
val words = """(.)""".r
val literal = """\B``(.)*``\B""".r ^^ toHTML
val markup = (literal | words)*
def run(s: String) = parseAll(markup, s) match {
case Success(xs, next) => xs.mkString
case _ => "fail"
}
}
println (TestParser.run("This is a sytax test ``code here`` . Hello ``World``"))
在此代码中,仅包含一个<code>
标记的更简单的输入可以正常工作,例如:
This is a sytax test ``code here``.
变得
This is a sytax test <code>code here</code>.
但是当我用上面的例子运行它时,它会产生
This is a sytax test <code>code here`` . Hello ``World</code>
我认为这是因为我使用的正则表达式:
"""\B``(.)*``\B""".r
允许``
成对的任何字符。
我想知道我是否应该限制无法嵌套``
并解决此问题?