考虑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 => ...
}

10-07 19:30
查看更多