Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ANTLR: Space indentation?

Tags:

java

antlr

lexer

I want to create a very simple grammar with space indentation. Each line consists of 1 or more words but indentation like python (4 spaces or a tab is one indent) and there is no close for indentation, 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

Rather than read each line, calculate the indentation and build a tree manually is it possible to do all of that in ANTLR grammar? My target language is Java.

like image 950
Elliot Chance Avatar asked Aug 30 '12 21:08

Elliot Chance


1 Answers

This is possible. All you do is define a rule and let it be skipped.

Here you go:

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

Or if you need to recognize \n or \r

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

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

like image 59
Martin Kersten Avatar answered Nov 03 '22 16:11

Martin Kersten