Домой United States USA — software A SonarQube Plugin for Kotlin A SonarQube Plugin for Kotlin

A SonarQube Plugin for Kotlin A SonarQube Plugin for Kotlin

309
0
ПОДЕЛИТЬСЯ

As we start our journey of making a SonarQube plugin for Kotlin, we begin with creating a parser for Kotlin code, including designing the grammar and testing.
Since I started my journey into Kotlin, I wanted to use the same libraries and tools I use in Java. For libraries — Spring Boot, Mockito, etc. — it’s straightforward, as Kotlin is 100% interoperable with Java. For tools, well, it depends. For example, Jenkins works flawlessly, while SonarQube lacks a dedicated plugin. The SonarSource team has limited resources: Kotlin, though on the rise — and even more so since Google I/O 17, is not in their pipe. This post series is about creating such a plugin, and this first post is about parsing Kotlin code.
In the realm of code parsing, ANTLR is a clear leader in the JVM world.
ANTLR (ANother Tool for Language Recognition) is a powerful parser generator for reading, processing, executing, or translating structured text or binary files. It’s widely used to build languages, tools, and frameworks. From a grammar, ANTLR generates a parser that can build and walk parse trees.
ANTLR is able to generate parsing code for any language thanks to a dedicated grammar file. However, creating such a grammar from scratch for regular languages is not trivial. Fortunately, thanks to the power of the community, a grammar for Kotlin already exists on GitHub.
With this existing grammar, ANTLR is able to generate Java parsing code to be used by the SonarQube plugin. The steps are the following:
This should generate a KotlinLexer and a KotlinParser class, as well as several related classes in target/classes. As Maven goes, it also packages them in a JAR named kotlin-1.0-SNAPSHOT.jar in the target folder — and in the local Maven repo as well.
To test the parsing code, one can use the grun command. It’s an alias for the following:
Create the alias manually or install the antlr package via Homebrew on OSX.
With grun, Kotlin code can be parsed then displayed in different ways, textual and graphical. The following expects an input in the console:
After having typed valid Kotlin code, it yields its parse tree in text. By replacing the -tree option with the -gui option, it displays the tree graphically instead. For example, the following tree comes from this snippet:
In order for the JAR to be used later in the SonarQube plugin, it has been deployed on Bintray. In the next post, we will be doing proper code analysis to check for violations.

Continue reading...