-
Notifications
You must be signed in to change notification settings - Fork 517
Expand file tree
/
Copy pathTestSpanDebug.scala
More file actions
72 lines (65 loc) · 2.28 KB
/
TestSpanDebug.scala
File metadata and controls
72 lines (65 loc) · 2.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import dotty.tools.dotc.ast.untpd
import dotty.tools.dotc.core.Contexts.*
import dotty.tools.dotc.parsing.Parsers.Parser
import dotty.tools.dotc.util.SourceFile
import dotty.tools.dotc.CompilationUnit
import dotty.tools.dotc.core.Contexts.Context
import dotty.tools.dotc.Driver
import dotty.tools.dotc.config.Settings
object TestSpanDebug {
def main(args: Array[String]): Unit = {
val code = """object Test {
var (a, b) = (1, 2)
(a, b) = (3, 4)
}"""
// Setup compiler context
val driver = new Driver
given Context = driver.initCtx.fresh
val source = SourceFile.virtual("test.scala", code)
val unit = CompilationUnit(source)
val parser = Parser(source)
val tree = parser.parse()
def showTree(t: untpd.Tree, indent: Int = 0): Unit = {
val prefix = " " * indent
val span = t.span
val spanStr = if (span.exists) {
val start = span.start
val end = span.end
val content = if (start >= 0 && end <= code.length) {
code.substring(start, end).replace("\n", "\\n")
} else "???"
s"[${start}-${end}] '$content'"
} else "[no span]"
println(s"${prefix}${t.getClass.getSimpleName}: $spanStr")
t match {
case pkg: untpd.PackageDef =>
pkg.stats.foreach(showTree(_, indent + 1))
case mod: untpd.ModuleDef =>
mod.impl.body.foreach(showTree(_, indent + 1))
case vd: untpd.ValDef =>
println(s"${prefix} name: ${vd.name}")
if (vd.tpt != null) showTree(vd.tpt, indent + 1)
if (vd.rhs != null) showTree(vd.rhs, indent + 1)
case asg: untpd.Assign =>
println(s"${prefix} LHS:")
showTree(asg.lhs, indent + 2)
println(s"${prefix} RHS:")
showTree(asg.rhs, indent + 2)
case tuple: untpd.Tuple =>
println(s"${prefix} Elements:")
tuple.trees.foreach(showTree(_, indent + 2))
case _ =>
// Show children
t.productIterator.foreach {
case child: untpd.Tree => showTree(child, indent + 1)
case list: List[_] => list.foreach {
case tree: untpd.Tree => showTree(tree, indent + 1)
case _ =>
}
case _ =>
}
}
}
showTree(tree)
}
}