any idea how i get kotlin to visit a file, passing...
# compiler
d
any idea how i get kotlin to visit a file, passing in my own visitor?
r
You need to implement
KtVisitorVoid
which I posted in previous thread
d
i understand i need to implement that, that part seems easy enough. I dont know how to tell kotlin to read a file and visit it though
r
I see. Try something like
ktFile.accept(yourMagicVisitor)
d
what class is
ktFile
?
r
If we are talking about file it should be
KtFile
but it’s ok if you use based type
KtElement
d
im lost
do i need to add any specific jars to my classpath?
d
thanks
trying to see which jar i need to add to my project to get it
got it
🆒 1
i guess i need to use a combination of that and this https://github.com/vektory79/kotlin-script-parser-test/blob/master/src/main/java/hello/CompileTest.kt to get what i need
I managed to get this mostly to work. The visitor doesn't do anything though, maybe the compiler isn't doing enough?
What i did in the meantime was to get the functions from the topdowncontext
everything i need is there, but the type parameters for functions aren't resolved properly yet
I wanted to be able to go over a source file and visit the classes, functions, parameters, get their types and line numbers of it all
r
If so you have to implement corresponding visit members in your visitor. You you need resolved semanthic information to get types
d
i tried the visitor and pretty much only one visit method was called
from memory it was visitClassOrObject or something
i probably didn't set up the KtFile properly
r
Probably you forget to visit rest of underlying tree. Just call
super.visitClassOrObject(element)
d
i dont think i did
Copy code
val context = KotlinScriptParser().parse(
        "/Users/darenklamer/projects/toro-gloop/toro-gloop-langs/toro-gloop-kotlin/src/test/resources/Lamp.kt")

    context.files.forEach {
        it.accept(object : KtVisitorVoid() {
            override fun visitFile(file: PsiFile?) {
                println(file)
                super.visitFile(file)
            }

            override fun visitClassOrObject(classOrObject: KtClassOrObject) {
                println(classOrObject)
                super.visitClassOrObject(classOrObject)
            }

            override fun visitClassLiteralExpression(expression: KtClassLiteralExpression) {
                println(expression)
                super.visitClassLiteralExpression(expression)
            }

            override fun visitClassInitializer(initializer: KtClassInitializer) {
                println(initializer)
                super.visitClassInitializer(initializer)
            }

            override fun visitClassInitializer(initializer: KtClassInitializer, data: Void?): Void {
                println(initializer)
                return super.visitClassInitializer(initializer, data)
            }

            override fun visitClass(klass: KtClass) {
                println(klass)
                super.visitClass(klass)
            }

            override fun visitClassBody(classBody: KtClassBody) {
                println(classBody)
                super.visitClassBody(classBody)
            }
        })
    }
only visit file gets called here
r
At first look seems okay. What output do you get? I mean does
println
print something?
Could you try replacing
KtVisitorVoid
with
KtTreeVisitorVoid
? It seems to be a problem because first doesn’t go deep
d
that fixed it. the parameter types in the functions are unresolved but i can live without that for now. thanks for your help!
r
You’re welcome 🙂