I'm using ANTLR3 (C runtime) to parse a text file: The problem is that I usually want to recognize newline characters, but at some points in the grammar I want to ignore them.
My first approach was to dynamically from the grammar set a boolean value that was used in the lexer rule for NEWLINE to set its channel to HIDDEN or DEFAULT.
That didn't work as ANTLR3 first lexes all the tokens and builds the tokenstream, and then the grammar walks the stream.
Now I'm wondering if it's possible to dynamically (using parser grammar predicates) tell the tokenstream to start/stop listening to a specific token channel in addition to the default one like this:
NEWLINE: ('\r'? '\n')+ {$channel=CH_WSPACE;}
expr
@init {
INPUT.disableChannel(CH_WSPACE);
}
@after {
INPUT.enableChannel(CH_WSPACE);
} :
...rhs of the rule....
;
Do I need to write my own TokenStream? Is there a better approach to my problem?
Thanks a lot in advance!