gpt4 book ai didi

Scala 解析器削减最后一个括号

转载 作者:行者123 更新时间:2023-12-03 17:49:09 24 4
gpt4 key购买 nike

Welcome to Scala 2.12.1 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_121).
Type in expressions for evaluation. Or try :help.

scala> :paste
// Entering paste mode (ctrl-D to finish)

import scala.reflect.runtime._
import scala.reflect.runtime.universe._
import scala.tools.reflect.ToolBox

val mirror = universe.runtimeMirror(universe.getClass.getClassLoader)
val toolbox = mirror.mkToolBox(options = "-Yrangepos")
val text =
"""
|libraryDependencies ++= Seq("org.scala-lang" % "scala-compiler" % "2.10.4") map {
| (dependency) =>{
| dependency
| }
|}
""".stripMargin
val parsed = toolbox.parse(text)

val parsedTrees = parsed match {
case Block(stmt, expr) =>
stmt :+ expr
case t: Tree =>
Seq(t)
}

val statements = parsedTrees.map { (t: Tree) =>
text.substring(t.pos.start, t.pos.end)
}


// Exiting paste mode, now interpreting.

import scala.reflect.runtime._
import scala.reflect.runtime.universe._
import scala.tools.reflect.ToolBox
mirror: reflect.runtime.universe.Mirror = JavaMirror with primordial classloader with boot classpath...
scala> statements.head
res0: String =
libraryDependencies ++= Seq("org.scala-lang" % "scala-compiler" % "2.10.4") map {
(dependency) =>{
dependency
}

结果是:
scala> statements.head
res1: String =
libraryDependencies ++= Seq("org.scala-lang" % "scala-compiler" % "2.10.4") map {
(dependency) =>{
dependency
}

我期望:
libraryDependencies ++= Seq("org.scala-lang" % "scala-compiler" % "2.10.4") map {
(dependency) =>{
dependency
}
}

最后一个括号 }如果我使用来自 Tree 对象的位置,(和行尾)会丢失: text.substring(t.pos.start, t.pos.end)
任何建议如何从 scala.reflect.api.Trees#Tree 对象中提取所有文本?

更新

受影响的Scala版本:
  • 2.10.6 - sbt 0.13.x 需要
  • 2.11.8
  • 2.12.7

  • 对于 scala 2.10.6/2.12.7 结果与上面的输出相同。

    将项目添加到 github

    Example project for searching the solution

    最佳答案

    只是为了将问题从未回答列表中移出,可以引用为其预订的问题:

    https://issues.scala-lang.org/browse/SI-8859

    关于Scala 解析器削减最后一个括号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25547149/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com