ANTLR: Space indent?

I want to create a very simple indented space grammar. Each line consists of 1 or more words, but indents, such as python (4 spaces or tab - one indent), and no indents for indents, for example:

if something cool occurs do this else otherwise do this loop around something each time doing this and do that say good byte 

Instead of reading each line, calculating the indentation and building the tree manually, can all this be done in the ANTLR grammar? My target language is Java.

+6
source share
1 answer

It is possible. All you do is define a rule and skip it.

Here you go:

 Ignore : (' ' | '\t' | '\n' | '\r')+ {skip();}; 

Or if you need to recognize \ n or \ r

 Ignore : (' ' | '\t')+ {skip();}; 

Add this to your gram and all spaces and tabs will be ignored.

+1
source

Source: https://habr.com/ru/post/924213/


All Articles