-
Notifications
You must be signed in to change notification settings - Fork 23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Best way to explore AST to search for specific patterns? #33
Comments
Hi @gauthier-roebroeck-mox,
(import kotlinx.ast.common.klass.identifierName) Reading the value is not so easy because currently only parsing of top level stuff is implemented. When you have the argument, you can use
you can try to call summary again on all children to get an easier to use ast. please let me know if you have any questions. |
The best I could come up with is this, not very pretty: func.annotations
.mapNotNull { annotation -> annotation.arguments.firstOrNull { it.identifier?.identifier == "topics" } }
.mapNotNull { it.expressions.firstOrNull() }
.flatMap { it.flatten("lineStringContent") }
.flatMap { it.children }
.filter { it.description == "LineStrText" }
.filterIsInstance<DefaultAstTerminal>()
.map { it.text } Is it possible to use something in |
Hi @gauthier-roebroeck-mox, yes, sadly I have only little time to work on this library and I prefer to add new functionality, so there is almost no documentation. TreeFilter is mainly used as an internal API by TreeMapper. This is something like map/flatMap on a AST-Structure. If you are interested in an example: Lines 1518 to 1532 in 8168038
This Code will convert the annotation from your example, parsed into this ast: ast/grammar-kotlin-parser-test/src/commonMain/resources/testdata/Issue33.raw.ast.txt Lines 23 to 257 in 8168038
into this summary: ast/grammar-kotlin-parser-test/src/commonMain/resources/testdata/Issue33.summary.ast.txt Lines 6 to 18 in 8168038
I hope this helps, feel free to ask me any questions. Sadly, I have no time to add functionality to work in a better way with the ast nodes. In my private project (for which I'm developing this library here) I'm doing code generation (using https://github.com/square/kotlinpoet for writing generated code) and kotlinx.ast for parsing the source. The first step after kotlinx.ast is filtering the annotations I'm interested in (and the annotated subjects), I'm storing this information in a set of data classes. This is very similar to the code you provided here. In the last step, I'm converting this data class into generated code using kotlinpoet. |
Hello,
i am trying to migrate an existing project using kastree to this library.
I am struggling to retrieve the content of a parameter of an annotation that is on a method of a class.
I have a Kotlin file that looks like this (excerpt):
What's the best way to get the content of the
topics
argument of the@KafkaListener
annotation ?So far i came up with this. This gives me the members of the class:
This tries to parse the function declaration block. I am faced with each node having a single node in its
children
, over and over, and no good way to get the content of the actual string.I am also not sure that doing a
.filterIsInstance<KlassAnnotation>()
is a good way of filtering the AST, surely there is a better way of doing that, no ?The text was updated successfully, but these errors were encountered: