How do I link SSLR and SonarQube?

Hi everyone.

I’m currently trying to develop a Sonar plugin for a new language, and I’m looking to use SSLR. I’ve written a lexer for now and am not using a grammar as I’m just looking to process some basic metrics like number of LOC, or number of comments.

How do I link SSLR to SonarQube ? I know I’m supposed to use dependencies, but I don’t know how/where.

Thanks for your help.

Hi Rémi,

Here’s some documentation: Plugin basics / Building your plugin

It describes the dependency that you can use to communicate with SonarQube:


And there’s some registered extensions about metrics in the plugin class ExamplePlugin.

1 Like

Hi Alban,

Thank you so much for answering my question.

I seem to have trouble understanding how SSLR works. Is it attached to the Example Plugin, or to SonarQube itself ?

Should I try to get the number of LOC through SSLR or through the plugin ?

Thanks again for your much appreciated help.

SonarQube and SSLR are not linked together.

SonarQube exposes an API (sonar-plugin-api) so that analyzers can feed it with analysis results. An analyzer typically needs to parse code for a given language: in a lot of cases, the analyzer transforms the analyzed code into an abstract syntax tree (AST) to work on it and produce some useful data (metrics, coding rule violations…) which can be fed to SonarQube API.

There are different ways to build an AST for a given language. One way is to use SSLR.
SonarQube plugins don’t have to use SSLR, and SSLR can be used for things than writing SonarQube plugins. SonarQube API and SSLR don’t depend on each other.

I suggest that you have a look at sonar-flex or sonar-lua.

To answer your question about LOC: your analyzer can use SSLR to parse code and identify which parts of the code are code and which parts are comments, and you should feed the metric to SonarQube API (see for example how sonar-flex feeds NCLOC).

1 Like

Hi Pierre-Yves,

Thank you for you very much needed and appreciated help. I got that SSLR and SonarQube do not depend on each other, but SSLR seems like the easiest way to implement a Sonar plugin for a new language !

The exemples you gave me are going to be very useful since they seem to use the architecture of the plugin I’m currently trying to build.

I’ll come back to you if I have more questions or post another topic if needed. Thanks again. :slight_smile:

EDIT : If that’s not inappropriate : I’m wondering where to go from the sonar-flex plugin : it seems a bit complicated for me to edit in order to reach what I’m trying to do. Is there a way I could strip it down to a minimum in order to start and build it up from there ?

I don’t think you should try to strip down sonar-flex.
I suggest that you first try to have a parser which reads a string and counts the LOC.
When you have that, take some inspiration from FlexSquidSensor and xoo plugin to plug your parser to SonarQube API.
It will be hard to help you unless you have more precise questions.

1 Like

Hi Pierre-Yves. Again, thank you very much for your help. I will try to describe my first goal as precisely as I can in order for it to be clear to you.

What I’m trying to do is develop a SonarQube plugin for a pretty uncommon language (CELLman). I’d like to start with feeding basic metrics to the SonarQube API, like LOC or number of comment lines. I figured using SSLR (or any other language recognition tool) would be a good idea in order to recognize that new language, but perhaps I don’t need it for those early goals I set.

I have trouble with knowing what to do first. Where do I start ?

  • Do I use the example plugin as a skeleton for mine ?
  • What basic parts do I need in order to compute those metrics ? Just a lexer, a parser and a line visitor ?

Thanks again.

I think that you only need 3 classes:

  • your lexer
  • 1 class implementing org.sonar.api.batch.sensor.Sensor which iterates over the files you want to analyse and for each one of them runs the lexer and saves a LOC metric.
  • 1 class implementing org.sonar.api.Plugin which only purpose is to declare the Sensor as an extension (see how FlexPlugin declares FlexSquidSensor as well as other extensions).