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