YourReflection
YourReflection

Reputation: 395

Process whitespace in specific ANTLR parser rule but skip it in all other cases

maybe someone can help me on this. I have written an ANTLR grammar which in short looks like this:

grammar myGrammar;
file: mystring+;
mystring: name EQUALITYSIGN DOUBLEQUOTE stringVal DOUBLEQUOTE SEMICOLON;
stringVal: (CHAR | INT)*;
name: CHAR | INT;
EQUALITYSIGN : '=';
DOUBLEQUOTE: '"';
SEMICOLON: ';';
WHITESPACE : ' ' -> skip;
NEWLINE : '\r'? '\n' -> skip;
CHAR : ~[ \t\r\n\\\[\]";=]+;

I have different parser rules for other data types but considering the String type, I would like to NOT skip the whitespaces.

For instance, an input might look like this:

string1 = " a ";

In this case, I want the whitespaces before and after the "a" to be preserved. So, anything inside the double quotes, shall be preserved as it is.

I have tried to do it with defined channels but apparently, I am doing something wrong.

Any ideas?

Thanx in advance!

Upvotes: 0

Views: 831

Answers (1)

Bart Kiers
Bart Kiers

Reputation: 170148

You need to make stringVal a lexer rule:

STRING: '"' ~'"'* '"';

and remove DOUBLEQUOTE.

Upvotes: 1

Related Questions