2

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?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Achim
  • 23
  • 3
  • possible duplicate of [Allow Whitespace sections ANTLR4](http://stackoverflow.com/questions/29060496/allow-whitespace-sections-antlr4) – CoronA Mar 18 '15 at 06:31

1 Answers1

4

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;
vicsz
  • 9,552
  • 16
  • 69
  • 101