1
votes

In my ANTLR4 grammar, I would like to skip whitespace in general, in order to keep the grammar as simple as possible. For this purpose I use the lexer rule WS : [ \t\r\n]+ -> skip;.

However, there may be certain sections in the input, where whitespace matters. One example are tables that are either tab-delimited or which need to count the spaces to find out which number is written in which column.

If I could switch off skipping the whitespace in between some begin and end symbols (table{ ... }), this would be perfect. Is it possible?

If not, are there other solutions to switch between different lexer rules depending on the context?

1
possible duplicate of Allow Whitespace sections ANTLR4CoronA

1 Answers

3
votes

Take a look at context-senstive tokens with lexical modes. It's covered in more depth in "The Definitive ANTLR 4" book -- Chapter 12. I think you should be able to pull it off with this.

Declare a rule that will change to the "skip spaces mode", and back to the default one.

OPEN: '<' -> mode (SKIP_SPACES);

mode: SKIP_SPACES;
CLOSE: '>' -> mode (DEFAULT_MODE);
WS : [ \t\r\n]+ -> skip;