how to parse kotlin code?

2019-06-17 04:40发布

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?

3条回答
虎瘦雄心在
2楼-- · 2019-06-17 04:58

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楼-- · 2019-06-17 05:17

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>
查看更多
淡お忘
4楼-- · 2019-06-17 05:21

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

查看更多
登录 后发表回答