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.
ANTLR
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.
Designing the grammar
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:
- Clone the Github repository
git clone git@github.com:antlr/grammars-v4.git
- By default, classes will be generated under the root package, which is discouraged.
To change that default:
- Create a
src/main/antlr4/<fully>/<qualified>/<package>
folder such assrc/main/antlr4/ch/frankel/sonarqube/kotlin/api
- Move the g4 files there
- In the POM, remove the
sourceDirectory
andincludes
bits from theantlr4-maven-plugin
configuration to use the default
- Create a
- Build and install the JAR in the local Maven repo
cd grammars-v4/kotlin mvn install
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.
Testing the parsing code
To test the parsing code, one can use the grun
command.
It’s an alias for the following:
java -Xmx500M -cp "<path/to/antlr/complete/>.jar:$CLASSPATH" org.antlr.v4.Tool
Create the alias manually or install the antlr
package via Homebrew on OSX.
With grun
, Kotlin code can parsed then displayed in different ways, textual and graphical.
The following expects an input in the console:
cd target/classes
grun Kotlin kotlinFile -tree
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:
fun main(args : Array<String>) {
val firstName : String = "Adam"
val name : String? = firstName
print("$name")
}
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.