aboutsummaryrefslogtreecommitdiff
path: root/src/dotty/tools/dotc/ast/Positioned.scala
diff options
context:
space:
mode:
authorMartin Odersky <odersky@gmail.com>2016-03-06 18:47:34 +0100
committerMartin Odersky <odersky@gmail.com>2016-03-12 16:08:36 +0100
commitad483d8c3c9dc70cad9f463efd025bab7e07b882 (patch)
tree9932f0a5f01114f6fc87274a7a636c03c1aa9337 /src/dotty/tools/dotc/ast/Positioned.scala
parent12d895587444d5d59e7d75dfaf7b85deb61e99e0 (diff)
downloaddotty-ad483d8c3c9dc70cad9f463efd025bab7e07b882.tar.gz
dotty-ad483d8c3c9dc70cad9f463efd025bab7e07b882.tar.bz2
dotty-ad483d8c3c9dc70cad9f463efd025bab7e07b882.zip
Add functionality to navigate ASTs
Map typed to corresponding untyped trees.
Diffstat (limited to 'src/dotty/tools/dotc/ast/Positioned.scala')
-rw-r--r--src/dotty/tools/dotc/ast/Positioned.scala67
1 files changed, 35 insertions, 32 deletions
diff --git a/src/dotty/tools/dotc/ast/Positioned.scala b/src/dotty/tools/dotc/ast/Positioned.scala
index e0bd6c75a..f9a775c98 100644
--- a/src/dotty/tools/dotc/ast/Positioned.scala
+++ b/src/dotty/tools/dotc/ast/Positioned.scala
@@ -58,17 +58,42 @@ abstract class Positioned extends DotClass with Product {
* and transitively visit their children.
*/
private def setChildPositions(pos: Position): Unit = {
- def deepSetPos(x: Any): Unit = x match {
- case p: Positioned =>
- if (!p.pos.exists) p.setPos(pos)
- case xs: List[_] =>
- xs foreach deepSetPos
- case _ =>
- }
var n = productArity
- while (n > 0) {
- n -= 1
- deepSetPos(productElement(n))
+ var elems: List[Any] = Nil
+ var end = pos.end
+ var outstanding: List[Positioned] = Nil
+ def fillIn(ps: List[Positioned], start: Int, end: Int): Unit = ps match {
+ case p :: ps1 =>
+ p.setPos(Position(start, end))
+ fillIn(ps1, end, end)
+ case nil =>
+ }
+ while (true) {
+ var nextElem: Any = null
+ if (elems.nonEmpty) {
+ nextElem = elems.head
+ elems = elems.tail
+ }
+ else if (n > 0) {
+ n = n - 1
+ nextElem = productElement(n)
+ }
+ else {
+ fillIn(outstanding, pos.start, end)
+ return
+ }
+ nextElem match {
+ case p: Positioned =>
+ if (p.pos.exists) {
+ fillIn(outstanding, p.pos.end, end)
+ outstanding = Nil
+ end = p.pos.start
+ }
+ else outstanding = p :: outstanding
+ case xs: List[_] =>
+ elems = if (elems.isEmpty) xs else xs ::: elems
+ case _ =>
+ }
}
}
@@ -114,26 +139,4 @@ abstract class Positioned extends DotClass with Product {
found
}
}
-
- /** The path from this node to `that` node, represented
- * as a list starting with `this`, ending with`that` where
- * every node is a child of its predecessor.
- * Nil if no such path exists.
- */
- def pathTo(that: Positioned): List[Positioned] = {
- def childPath(it: Iterator[Any]): List[Positioned] =
- if (it.hasNext) {
- val cpath = it.next match {
- case x: Positioned => x.pathTo(that)
- case xs: List[_] => childPath(xs.iterator)
- case _ => Nil
- }
- if (cpath.nonEmpty) cpath else childPath(it)
- } else Nil
- if (this eq that) this :: Nil
- else if (this.envelope contains that.pos) {
- val cpath = childPath(productIterator)
- if (cpath.nonEmpty) this :: cpath else Nil
- } else Nil
- }
}