https://kotlinlang.org logo
#compiler
Title
# compiler
d

Daren Klamer

07/07/2020, 11:09 AM
any idea how i get kotlin to visit a file, passing in my own visitor?
r

Roman Artemev [JB]

07/07/2020, 11:11 AM
You need to implement
KtVisitorVoid
which I posted in previous thread
d

Daren Klamer

07/07/2020, 11:12 AM
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

Roman Artemev [JB]

07/07/2020, 11:13 AM
I see. Try something like
ktFile.accept(yourMagicVisitor)
d

Daren Klamer

07/07/2020, 11:14 AM
what class is
ktFile
?
r

Roman Artemev [JB]

07/07/2020, 11:19 AM
If we are talking about file it should be
KtFile
but it’s ok if you use based type
KtElement
d

Daren Klamer

07/07/2020, 11:20 AM
im lost
do i need to add any specific jars to my classpath?
d

Daren Klamer

07/07/2020, 11:24 AM
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

Roman Artemev [JB]

07/08/2020, 9:40 AM
If so you have to implement corresponding visit members in your visitor. You you need resolved semanthic information to get types
d

Daren Klamer

07/08/2020, 10:16 AM
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

Roman Artemev [JB]

07/08/2020, 10:29 AM
Probably you forget to visit rest of underlying tree. Just call
super.visitClassOrObject(element)
d

Daren Klamer

07/08/2020, 11:26 AM
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

Roman Artemev [JB]

07/08/2020, 1:59 PM
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

Daren Klamer

07/09/2020, 8:32 AM
that fixed it. the parameter types in the functions are unresolved but i can live without that for now. thanks for your help!
r

Roman Artemev [JB]

07/09/2020, 8:42 AM
You’re welcome 🙂
4 Views