[Qt-creator] SyntaxHighlighting + Bison/FLex

Nikolai Kosjar nikolai.kosjar at qt.io
Thu Nov 23 09:19:31 CET 2017


On 11/23/2017 07:29 AM, JeremY Larrieu wrote:
> Hello,
> 
> I'm working on making a plugin to support a specific language: Anubis.
> 
> I've started the highlighting part and I saw that QtCreator is making 
> syntax highlighting line by line instead of making it "globally" for the 
> whole file.
> 
> I just wanna know, how can I use the Bison/Flex files, used to check 
> syntax for Anubis language, to make syntax highlighting in QtCreator.
> Knowing that most of the tokens declared in the Flex file can be 
> multiline, making a lexer, working line by line, able to detect those 
> tokens is hard and the code is too "verbose".
> 
> I've tried to adapt what I've found in Bison/Flex files to make a syntax 
> highlighter, but it's really painful and it makes further updates harder.
> If I was able to apply highlighting on the whole file (not line by 
> line), it were simpler and I could use it to make some other 
> functionalities at the same time: code completion, code analysis, symbol 
> detection, ...
> 
> Do you have an idea on how I can make syntax highlighting without 
> rewriting a full lexer ?
> 
> Thanks in advance.
> 
> Jeremy

Hi!

I guess you've found TextEditor::SyntaxHighlighter, which does the line 
by line highlighting with highlightBlock(). For C++ and multi-line 
tokens (e.g. C comments) we call our custom lexer for the current line 
with the lexer state from the line before. If flex provides such a state 
based yylex() equivalent, this might work.

The alternative is to use 
SemanticHighlighter::incrementalApplyExtraAdditionalFormats(). With this 
one, you can for example start parsing in a worker thread and provide 
the highlighting information as a stream of 
TextEditor::HighlightingResult items.

Nikolai



More information about the Qt-creator mailing list