我应该使用 RegexParsers、StandardTokenParsers 还是这些都适合解析这种语法?语法示例可以在 here 中找到。 .
最佳答案
我会使用正则表达式。它简化了一些事情,并使其余的事情标准化。
def process(src: scala.io.Source) {
import scala.util.matching.Regex
val FilePattern = """(.*) ''(.*)''"""
val OriginalFile = new Regex("--- "+FilePattern, "path", "timestamp")
val NewFile = new Regex("+++ "+FilePattern, "path", "timestamp")
val Chunk = new Regex("""@@ -(\d+),(\d+) +(\d+),(\d+) @@""", "orgStarting", "orgSize", "newStarting", "newSize")
val AddedLine = """+(.*)""".r
val RemovedLine = """-(.*)""".r
val UnchangedLine = """ (.*)""".r
src.getLines() foreach {
case OriginalFile(path, timestamp) => println("Original file: "+path)
case NewFile(path, timestamp) => println("New file: "+path)
case Chunk(l1, s1, l2, s2) => println("Modifying %d lines at line %d, to %d lines at %d" format (s1, l1, s2, l2))
case AddedLine(line) => println("Adding line "+line)
case RemovedLine(line) => println("Removing line "+line)
case UnchangedLine(line) => println("Keeping line "+line)
}
}
关于parsing - 如何编写统一 diff 语法的解析器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3560073/