I'm fairly new to ANTLR, and I've run into a problem.
I have a grammar I'm trying to write for a language that includes single-line comments and language directives that begin with the same comment identifier. For example:
--This is a comment. What follows is a directive with a parameter
--directive:param
A directive will always be in that format - two dash characters followed by a command, a colon, and a single parameter.
I would like to have the lexer ignore an actual comment (send it to the hidden channel), but tokenize the directives. I have the following lexer rules:
DCOMMAND : DATABASE;
fragment DATABASE : D A T A B A S E;
fragment COMMENTSTART : '--';
LINE_COMMENT : COMMENTSTART ~(DCOMMAND|('\n'|'\r')*) {$channel=HIDDEN;};
fragment A : ('a'|'A');
fragment B : ('b'|'B');
fragment C : ('c'|'C');
fragment D : ('d'|'D');
....
There's only one directive for now: 'database'. The DCOMMAND token will eventually represent several keywords potentially. The problem is that my lexer is always shoving anything that starts with '--' into the hidden channel. How do I make the LINE_COMMENT token not match directives? Or will I have to move comment handling into the parser?
--This:
? In other words, is--
followed by an identifier followed by:
always reserved for directives? – Sam Harwell-- This:
and--This also:
should NOT be directives, however... note the whitespace. – DuckPuppy