I use the normal whitespace separation into the hidden channel but I have one rule where I would like to include any whitespace for later processing but any example I have found requires some very strange manual coding.
Is there no easy option to read from multiple channels like the option to put the whitespace there from the beginning.
Ex. this is the WhiteSpace lexer rule
WS : ( ' '
| '\t'
| '\r'
| '\n'
) {$channel=HIDDEN;}
;
And this is my rule where I would like to include whitespace
raw : '{'? (~('{'))*;
Basically it's a catch all rule to capture any content that does not match other rules to be processed by another pattern and therefore I need the original stream.
I was hoping for a {$channel==DEFAULT || $channel==HIDDEN}
syntax example but cannot find any.
My target will be C# but I can rewrite Java examples if required.
In
Antler 4
I'm using a simple solution. I didn't test it inAntlr 3
. It's C# but you can translate it to Java easily.Change
parser1.g4
as next:Change
lexer1.g4
as next:Extend
Parser1
class as next:Extend
Lexer1
class as next:Now the
Main
function as next:AFAIK, that is not possible. However, you could extend the
UnbufferedTokenStream
to change thechannel
during parsing. You can't use theCommonTokenStream
since it buffers a variable amount of tokens (and there can be tokens in the buffer that are on the wrong channel!). Note that you need at least ANTLR 3.3: in previous versions theUnbufferedTokenStream
wasn't included yet.Let's say you want to parse (and display) either lower- or upper case letters. Upper case letters are put on the
HIDDEN
channel, so by deafult, only lower case letters will be parsed. However, when the parser stumbles upon a lower case"q"
, we want to change to theHIDDEN
channel. Once parsing on theHIDDEN
channel, we want the"Q"
to bring us back to theDEFAULT_CHANNEL
again.So when parsing the source
"aAbBcqCdDQeE"
, first"a"
,"b"
and"c"
are printed, then the channel is changed, then"C"
and"D"
get printed, then the channel is changed again, and finally"e"
is printed to the console.Here's an ANTLR grammar that does this:
ChannelDemo.g
And here's the custom token stream class:
ChangeableChannelTokenStream.java
And a small Main class to test it all:
Main.java
Finally, generate a lexer/parser (1), compile all source files (2) and run the Main class (3):
1
2
3 (*nix)
3 (Windows)
which will cause the following to be printed to the console:
EDIT
You can include the class in your grammar file like this:
The parser that gets generated from the grammar above will enable reading from all channels when it encounters a
"*"
. So when parsing"aAbB*cCdDeE"
:the following gets printed:
Maybe you should consider making whitespace a part of your gramar instead. But why clutter up your gramar with such unimportant information? Well, because it is NOT unimportant. The newline has meaining in certain contexts. When you want IDE support, e.g. from visual studio language server, you need to specify a language grammer without all the bells and whistles of low-level ANTLR customization.