I have an antlr4 grammar designed for an a domain specific language that is embedded into a text template.
There are two modes:
This is how I solved the problem at the end:
The idea is to enable/disable whitespace in a parser rule:
templateBody : {enableWs();} templateChunk* {disableWs();};
So we will have to define enableWs
and disableWs
in our parser base class:
public void enableWs() {
if (_input instanceof MultiChannelTokenStream) {
((MultiChannelTokenStream) _input).enable(HIDDEN);
}
}
public void disableWs() {
if (_input instanceof MultiChannelTokenStream) {
((MultiChannelTokenStream) _input).disable(HIDDEN);
}
}
Now what is this MultiChannelTokenStream
?
CommonTokenStream
which is a token stream reading only from one channel. MultiChannelTokenStream
is a token stream reading from the enabled channels. For implementation I took the source code of CommonTokenStream and replaced each reference to the channel
by channels
(equality comparison gets contains comparison)An example implementation with the grammar above could be found at antlr4multichannel