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_SyntaxHighlighterclass that derives fromHTML_SyntaxHighlighterwhich in turn derives from bothJS_SyntaxHighlighterandCSS_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.