I am creating a set of syntax highlighters for an application. The languages include:

  • HTML (specifically HTML5)
  • JavaScript
  • PHP
  • CSS (specifically CSS3)

The problem lies in the way the classes need to be organized. Since this is a Qt application, each language has its own class which derives from QSyntaxHighlighter. Each class overrides highlightBlock() which is responsible for coloring the supplied block of text.

JavaScript code can be contained in .js files as well as inside <script></script> tags. Similarly CSS styles can be contained in .css files as well as inside <style></style> HTML tags. To top it all off, HTML content can be found in both .html and .php files.

Each language has its own file extension and therefore requires its own class. However, since certain languages can be embedded within one another, there needs to be a way to avoid duplicating code. In other words, it makes no sense having the JavaScript parsing code inside the HTML highlighting class and the JavaScript highlighting class.

What would be an ideal way of organizing the code? Here are some options that come to mind:

  • Abstract the parsing code completely. This option would involve removing the parsing code for each language from the syntax highlighting class for that language. Then the parsing code could easily invoke other parsers as necessary. This option sounds good but would be very complex.
  • Use inheritance. This option doesn't sound like a good idea, but I thought it was worth mentioning anyway. In this case, I would have a PHP_SyntaxHighlighter class that derives from HTML_SyntaxHighlighter which in turn derives from both JS_SyntaxHighlighter and CSS_SyntaxHighlighter. The idea is that the container classes (like HTML) could invoke the overridden method in the base class when necessary. This option involves multiple inheritance and therefore will not work with Qt - thus, it's not really a true option.
  • Create instances of the other classes and invoke the highlightBlock() function. This option would involve constructing instances of other classes and then invoking their syntax highlighting function for the given language. This is probably quite difficult to implement as well and would involve a lot of hacking.

So these are the options I'm looking at. I welcome any suggestions, pointers, or tips for organizing these classes.

1 Answer 1


So, it seems that highlightBlock() is your entry point. I'd keep each class deriving directly from QSyntaxHighlighter and overriding highlightBlock() with the needed code. Then,

  1. the HTML highlighter should invoke CSS, JavaScript, and PHP highlighters inside their respective open and close tags, while it should use HTML rules outside
  2. the CSS highlighter should invoke the PHP highlighter inside its open and close tags, while it should use CSS rules outside
  3. the JavaScript highlighter should invoke the PHP highlighter inside its open and close tags, while it should use Javascript rules outside
  4. the PHP highlighter can focus on its own rules only

The problem when highlighting embeddable languages like PHP is that the hosting language can be any.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.