p.magalhaes
p.magalhaes

Reputation: 8364

ANTLR4 - Whitespace as separator

I have the following grammar. My grammar needs to accept these kind of statements:

X IN (10,20,30)

X IN (10 20 30)

expr
   : expr IN '(' constant_list ')'  #InExp
    ;
    
constant_list
    : constant ((',') constant)*
    ;
    
constant
    : numeric_constant
    | character_constant
    ;
    
SPACE
    : [ \t\r\n] -> skip
    ;

I just tried to edit the rule constant_list to:

constant_list
    : constant ((','|SPACE) constant)*
    ;

But the spaces ate being ignored. How can I handle this situation? Do I need to use the Hidden Channel?

Upvotes: 1

Views: 1068

Answers (2)

Bart Kiers
Bart Kiers

Reputation: 170138

No, you can't use the SPACE token in your parser rules: they are skipped (discarded from the lexer).

What you could do is this:

constant_list
 : constant (','? constant)*
 ;

Upvotes: 4

p.magalhaes
p.magalhaes

Reputation: 8364

I solved editing the rule:

constant_list
    : constant (',' constant)*
    | constant ( constant)*
    ;

Upvotes: 1

Related Questions