2016-03-19 16 views
2

Scala sunu derleyici API'sini kullanıyorum veya bir parça kaynak kodunun AST'sini almak için locateTree yöntemini kullanıyorum ve daha sonra ham temsilini showRaw(ast) çağrısı aracılığıyla aldım ancak sonuç görünüyor Beklediğim ile karşılaştırıldığında farklı olmak. ÖrneğinScala sunum derleyicisi locateTree yöntem

val tree = q"final def x = 1" 
println(showRaw(tree)) 

Çıkışlar DefDef(Modifiers(FINAL), TermName("x"), List(), List(), TypeTree(), Literal(Constant(1))), aynı kaynaktan sunum derleyici için bir çağrı DefDef(Modifiers(32, , List()), x, List(), List(), TypeTree(), Literal(Constant(1))) üretirken için (diğer bir deyişle x Modifiers parametre listesine TermName ve fark sarılmış değildir dikkat edin). Neden böyle oluyor ve sunum derleyicisindeki benzer davranışı nasıl uygularım?

Düzenleme: scala sürümünü kullanmak zorunda 2.11.8

cevap

0

ağaçlar, farklı yazıldığında ise show doğru:

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

scala> import scala.tools.nsc._ 
import scala.tools.nsc._ 

scala> val ss = new Settings(println) 
ss: scala.tools.nsc.Settings = 
Settings { 
    -d = . 
} 

scala> val g = new interactive.Global(ss, new reporters.ConsoleReporter(ss), "testing") 
g: scala.tools.nsc.interactive.Global = [email protected] 

scala> val tt = g.newUnitParser("final def x = 1").parseStats 
tt: List[g.syntaxAnalyzer.global.Tree] = List(final def x = 1) 

scala> reflect.runtime.universe.show(tt.head) 
res0: String = final def x = 1 

scala> reflect.runtime.universe.showRaw(tt.head) 
res1: String = DefDef(Modifiers(32, , List()), x, List(), List(), TypeTree(), Literal(Constant(1))) 

scala> g.showRaw(tt) 
res2: String = List(DefDef(Modifiers(FINAL), TermName("x"), List(), List(), TypeTree(), Literal(Constant(1)))) 

size böylece, res1 hiç tip emniyet yoktur Ağacın farklı bir evrenden olduğunu anlamadım.

ağaç

için pozisyon ek açıklamaları modifiye biraz farklıdır:

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

scala> import scala.tools.nsc._ 
import scala.tools.nsc._ 

scala> val ss = new Settings(println) 
ss: scala.tools.nsc.Settings = 
Settings { 
    -d = . 
} 

scala> new interactive.Global(ss, new reporters.ConsoleReporter(ss), "testing") 
res0: scala.tools.nsc.interactive.Global = [email protected] 

scala> val cc = res0 
cc: scala.tools.nsc.interactive.Global = [email protected] 

scala> val tt = cc.newUnitParser("final def x = 1").parseStats 
tt: List[cc.syntaxAnalyzer.global.Tree] = List(final def x = 1) 

scala> val cc.DefDef(mods, nam, ps, vs, tpt, rhs) = tt.head 
mods: cc.Modifiers = Modifiers(final, , Map(32 -> RangePosition(<console>, 0, 0, 4), 72 -> RangePosition(<console>, 6, 6, 8))) 
nam: cc.TermName = x 
ps: List[cc.TypeDef] = List() 
vs: List[List[cc.ValDef]] = List() 
tpt: cc.Tree = <type ?> 
rhs: cc.Tree = 1 

aksine:

scala> mods.annotations 
res13: List[reflect.runtime.universe.Tree] = List() 
:

scala> import reflect.runtime._, universe._ 
import reflect.runtime._ 
import universe._ 

scala> val DefDef(mods, nam, ps, vs, tpt, rhs) = q"final def x = 1" 
mods: reflect.runtime.universe.Modifiers = Modifiers(final, , Map()) 
nam: reflect.runtime.universe.TermName = x 
ps: List[reflect.runtime.universe.TypeDef] = List() 
vs: List[List[reflect.runtime.universe.ValDef]] = List() 
tpt: reflect.runtime.universe.Tree = <type ?> 
rhs: reflect.runtime.universe.Tree = 1 

Bu annotations yalnızca açık olanlar gösterir çıkıyor

the doc'un şunu söylediğine değdi, inşaattan ziyade muayene içindir.

+0

Açıklamalar için teşekkürler! – sugakandrey