Making a parse error more specific

For the plugin I am working on I would like to be able to throw very specific parse errors.

For example, lets say an if statement in my programming language looks like this:
if (1 < 2) do

If someone were to write the if statement like this:
if (1 < 2)
SonarQube will trigger a generic parse error which shows the line where the parsing has failed.

I would like to be able to say in this parse error that the programmer has forgotten the keyword “do”.
Is there any way to do this? Can I, for example, look at how far the tree has currently been generated and determine based on existing nodes which node is missing?

Hello @Hydracorp,

Well, that won’t be a developer answer, because I’m no one, though I guess it’s tricky to determine what could be expected: a parsing algorithm knows what’s valid after a token and what’s not (which leads to a parse error), though it can’t say what was the intention, ie. several valid tokens might follow one.

Also do you think the effort of developing such a tool, worth the added value? Usually (not always), a code with parse errors is caught before coming into SQ (compiling, building). If there is a parse error, SQ will report it as an issue, and in most cases the developer will easily identify if:

  • there is indeed a parse error, he will get how to solve it
  • there is not and, even though it’s quite rare, it’s the SQ parser which is failing