@@ -3,18 +3,107 @@ package dotty.semanticdb
33import scala .tasty .Reflection
44import scala .tasty .file .TastyConsumer
55
6+ import scala .tasty .util .TreeTraverser
7+ import dotty .tools .dotc .tastyreflect
8+ import scala .collection .mutable .HashMap
9+
610class SemanticdbConsumer extends TastyConsumer {
11+ var stack : List [String ] = Nil
12+ val symbolsDefs : HashMap [String , Int ] = HashMap ()
13+ val symbolsVals : HashMap [String , Int ] = HashMap ()
14+
15+ def insertPathDefDef (path : String ): String = {
16+ if (symbolsDefs.contains(path)) {
17+ symbolsDefs += (path -> (symbolsDefs(path) + 1 ))
18+ " +" + (symbolsDefs(path) - 1 )
19+ } else {
20+ symbolsDefs += (path -> 1 )
21+ " "
22+ }
23+ }
24+ def insertPathValDef (path : String ): String = {
25+ if (symbolsVals.contains(path)) {
26+ symbolsVals += (path -> (symbolsVals(path) + 1 ))
27+ " +" + (symbolsVals(path) - 1 )
28+ } else {
29+ symbolsVals += (path -> 1 )
30+ " "
31+ }
32+ }
733
834 final def apply (reflect : Reflection )(root : reflect.Tree ): Unit = {
935 import reflect ._
1036 object Traverser extends TreeTraverser {
1137
12- override def traverseTree (tree : Tree )(implicit ctx : Context ): Unit = tree match {
13- case IsDefinition (tree) =>
14- println(tree.name)
15- super .traverseTree(tree)
16- case tree =>
17- super .traverseTree(tree)
38+ def packageDefToOccurence (term : Term ): String = {
39+ // println(term, term.pos.start, term.pos.end)
40+ val Term .Ident (id) = term
41+ return stack.head + id + " /"
42+ }
43+
44+ override def traverseTree (tree : Tree )(implicit ctx : Context ): Unit = {
45+ val previous_path = stack.head
46+
47+ tree match {
48+ case IsClassDef (body) =>
49+ val ClassDef (name, _, _, _, _) = body
50+ // println("[classdef] ", body)
51+ val path = stack.head + name + " #"
52+ println(path)
53+ stack = path :: stack
54+ super .traverseTree(body)
55+ stack = stack.tail
56+ case IsTypeDef (body) =>
57+ println(" [typedef] " , body)
58+ super .traverseTree(body)
59+ case IsDefDef (body) =>
60+ val DefDef (name, _, _, _, _) = body
61+ val def_atom =
62+ name match {
63+ case " <init>" => " `<init>`"
64+ case _ => name
65+ }
66+ val path_repr = stack.head + def_atom
67+ val path = path_repr + " (" + insertPathDefDef(path_repr) + " )."
68+ println(path)
69+ // println("[defdef] ", body)
70+ stack = path :: stack
71+ super .traverseTree(body)
72+ stack = stack.tail
73+ case IsValDef (body) =>
74+ val ValDef (name, _, _) = body
75+ val path_repr = stack.head + name
76+ val path = path_repr + " (" + insertPathValDef(path_repr) + " )"
77+ println(path)
78+ // println("[defdef] ", body)
79+ stack = path :: stack
80+ super .traverseTree(body)
81+ stack = stack.tail
82+ case IsPackageDef (body) =>
83+ println(" [packagedef] " , body)
84+ super .traverseTree(body)
85+ case IsDefinition (body) =>
86+ println(" [definition] " , body)
87+ super .traverseTree(body)
88+ case IsPackageClause (body) =>
89+ // println(body.pos.start, body.pos.end)
90+ val PackageClause (name, list_tree : List [Tree ]) = body
91+ // println(tree)
92+ val path = packageDefToOccurence(name)
93+ println(path)
94+ stack = path :: stack
95+ // call to traverse tree instead of super.traverseTree to avoid
96+ // skipping this child entirely (super.xx will traverse subtrees)
97+ // list_tree.foreach{traverseTree}
98+ /* case IsTerm(body) =>
99+ //println("[term] ", body)
100+ super.traverseTree(body)*/
101+ // iterating this way will probably make us see terms we don't want
102+ super .traverseTree(body)
103+ stack = stack.tail
104+ case tree =>
105+ super .traverseTree(tree)
106+ }
18107 }
19108
20109 }
0 commit comments