0
votes

I'm using the JSON grammar from the antlr4 grammar repository to parse JSON files. It works fine and ignores whitespace with the usual rule: WS: [ \t\n\r] + -> skip;

I want to re-use the lexer to identify all tokens for syntax highlighting in eclipse. It requires to specify whitespace tokens as well, but calling JSONLexer.nextToken() skips over them.

Is there a way to not skip whitespace when using the lexer directly?

1

1 Answers

2
votes

Use the following:

WS: [ \t\n\r] + -> channel(HIDDEN);

Instead of discarding whitespace completely, it will still create WS tokens, but they'll end up on a separate channel (named HIDDEN). The parser can only "listen" to a single channel, but the lexer still allows you to retrieve tokens from any channel.