I am creating a set of syntax highlighters for an application. The languages include:
- HTML (specifically HTML5)
- 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.
.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
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_SyntaxHighlighterclass that derives from
HTML_SyntaxHighlighterwhich in turn derives from both
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.