Thursday, 15 March 2012

conditional - skipping whitespace in ANTLR v4 depending on context -


In my ANTLR 4 grammar, I would normally like to leave the empty space so that it can keep grammar as simple as possible. For this purpose, I use the laser rule WS: [\ t \ r \ n] + - - Skip; .

However, there may be some sections in the input, where there are whitespace cases. There are an example tables that are either tab-delimited or for which to count the number of spaces it needs to be written in which column is written.

If I can stop leaving the white space between some start and ending symbols ( table {...} ), that would be correct. Is this possible?

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

Take a look at context-oriented tokens with lexical mode. This is more deeply covered in the book "Definitive ANTLR 4" - Chapter 12. I think you should be able to pull it out.

Announce a rule that "Space spacing mode", and return to the default one.

Open: '& lt;' - & gt; Mode (SKIP_SPACES); Mode: SKIP_SPACES; Close: '& gt;' - & gt; Mode (DEFAULT_MODE); WS: [\ t \ r \ n] + - & gt; Skip;

No comments:

Post a Comment