how to parse kotlin code?

旧巷老猫 提交于 2019-12-10 13:03:03

问题


I need to analyse kotlin files code, to detect the keyword "data" and "?".

The issue is I don't find any libs like JavaParser. I don't need powerfull tools, just something who return me the number of the lines.

Any idea?


回答1:


I use antlr4 to do it. And I create an open source lib: https://github.com/sarahBuisson/kotlin-parser

<dependency
   <groupId>com.github.sarahbuisson</groupId>
   <artifactId>kotlin-parser</artifactId>
</dependency>



回答2:


You would need to either find a Kotlin parser, or write your own.

For this subset of parsing you may be able to cheat, as data is a keyword, so if you see data with non letters each side you know it is the keyword.

For ?, it depends which meaning you want, the elvis one can be detected by the dot else try the nullable meaning if the ? is next to a letter.




回答3:


See this [0] Kotlin code parsing related discussion. They talk about using antlr v4 grammar to parse the Kotlin code.

[0] https://discuss.kotlinlang.org/t/kotlin-parser/1728



来源:https://stackoverflow.com/questions/45956449/how-to-parse-kotlin-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!