11

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?

4
  • Every Tool/IDE has a "find in path" function. Or is this something that needs to be generated automatically? Commented Sep 8, 2017 at 12:20
  • @thomas-kleßen What do you mean? I'm developping a code analyse plugin, I need something more powerfull than a simple character search. Please tell-me than you're not the one who low-vote me ! Commented Sep 8, 2017 at 22:07
  • No. It wasn't me. I just asked what usecase you have in mind. Commented Sep 9, 2017 at 19:25
  • I have a project that wraps the official Kotlin parser at github.com/cretz/kastree Commented Nov 28, 2018 at 21:07

6 Answers 6

8

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>
Sign up to request clarification or add additional context in comments.

Comments

3

Besides the tools mentioned in other answers, here is another one someone may find useful:

https://github.com/kotlinx/ast

Comments

1

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.

1 Comment

Except if it's in a string. Both seems pretty common in English.
1

You can try Kastree -- simple library to manipulate Kotlin source code as a set of AST objects.

https://github.com/cretz/kastree

Comments

0

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

1 Comment

Yep, I use a similar stuff to developp a lib who do it: github.com/sarahBuisson/kotlin-parser
0

I have not yet written a Kotlin language grammar for it.

But I have implemented a parser in Kotlin, id that is any use.

It is Kotlin common code, so should work for any target platform.

There is an article about it here, and the code is on github.

https://medium.com/@dr.david.h.akehurst/agl-your-dsl-in-the-web-c9f54595691b

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.