在 Scala 中为解析器组合器创建测试

Create tests for parser combinators in Scala

考虑 this 在 Scala 中为术语语言实现解析器的源代码。旨在测试其功能的主要功能定义为 Ñ

def main(args: Array[String]): Unit = {
    val stdin = new java.io.BufferedReader(new java.io.InputStreamReader(System.in))
    val tokens = new lexical.Scanner(stdin.readLine())
    phrase(term)(tokens) match {
      case Success(trees, _) =>
        for (t <- path(trees))
          println(t)
        try {
          print("Big step: ")
          println(eval(trees))
        } catch {
          case TermIsStuck(t) => println("Stuck term: " + t)
        }
      case e =>
        println(e)
    }
  }

但是,我想通过在具体输入上调用 term 解析器来测试部分功能。我如何提供此输入?传递字符串不起作用...

只需将 stdin.readLine() 替换为您想要的输入,因此

phrase(term)(new lexical.Scanner("the string you want to test")) match {
  case Success(trees, _) => ...
  case err => ...
}