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
}
Kết quả là: Scala phân tích cú pháp cắt giảm khung cuối cùng
scala> statements.head
res1: String =
libraryDependencies ++= Seq("org.scala-lang" % "scala-compiler" % "2.10.4") map {
(dependency) =>{
dependency
}
tôi mong đợi:
libraryDependencies ++= Seq("org.scala-lang" % "scala-compiler" % "2.10.4") map {
(dependency) =>{
dependency
}
}
Các dấu ngoặc cuối cùng }
(và cuối dòng) là mất tích nếu tôi sử dụng vị trí từ đối tượng Cây: text.substring(t.pos.start, t.pos.end)
Mọi chuyên gia posal làm thế nào để trích xuất tất cả các văn bản từ scala.reflect.api.Trees # Tree đối tượng?
Cập nhật
bị ảnh hưởng phiên bản scala:
- 2.10.6 - cần thiết cho SBT 0.13.x
- 2.11.8
- 2.12.1
Đối Kết quả scala 2.10.6/2.12.1 giống như kết quả ở trên.
Thêm dự án để GitHub
Example project for searching the solution
tôi thấy http : //grokbase.com/t/gg/scala-internals/129smefjxe/reflection-universe-and-range-positions. Tùy chọn '-Yrangepos' là trách nhiệm cho nó. Làm cách nào để thay đổi/đặt tùy chọn khác? –
2.11.6 vẫn bị ảnh hưởng –