我想實現一個簡單的Wiki類標記分析器作爲使用Scala分析器組合器的練習。如何在正則表達式和解析器組合器中限制nestead標記?
我想解決這個問題,所以這裏是我想在第一個版本中實現的:一個簡單的內聯文字標記。
例如,如果輸入字符串爲:
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
允許``
對中的任何字符。
我想知道我應該限制那裏不能嵌套``
並解決這個問題?