I've built a grammar for a DSL and I'd like to display some elements (table names) in some colors. I output HTML from Java.
columnIdentifier :
columnName=Identifier
| tableName=Identifier '.' columnName=Identifier
;
Identifier : Letter LetterOrDigit* ;
fragment Letter : [a-zA-Z_];
fragment LetterOrDigit : [a-zA-Z0-9_];
WS : [ \t\r\n\u000C]+ -> skip ;
I thought about using an AbstractParseTreeVisitor to return all elements as-is, except those I want to highlight which would be returned as <span class="tablename" >theoriginaltext</span>
. Is it a good approach?
Note the whitespaces are dismissed before they are sent to the parser, correct? So if I rebuilt the output using an AbstractParseTreeVisitor, I can't rebuild the spaces.
I assume there's a canonical way of doing syntax highlighting with ANTLR4. It's difficult to find information about this because searches often return results about highlighting the Antlr4 files in Eclipse/IDEA.