Flavien PERIER
Flavien PERIER

Reputation: 48

Analyze a string between quotation marks containing single quotation marks with ANTLR

I would like to be able to parse a string like "test ' test" with ANTLR4

My grammar :

QUOTE: ('\''|'"');
STRING: QUOTE .*? QUOTE;

My parser :

matcher: STRING;

On another post the following possibility is proposed : Handling scope for single and double quote strings in ANTLR4

grammar :

SQUOTE: '\'';
DQUOTE: '"';

STRING: (SQUOTE .*? SQUOTE | DQUOTE .*? DQUOTE );

However, this possibility creates redundancy and consequently risks during maintenance

That's why I would like to know if there was a possibility of finding an equivalent of the regex:

("|').*?(\1)

That is to say, to automatically search for the same type of closing tag as the opening tag

Upvotes: 1

Views: 704

Answers (1)

Bart Kiers
Bart Kiers

Reputation: 170308

No, ("|').*?(\1) isn't possible. I'd also not do .*?, but something like this:

STRING
 : '"' ~["]* '"'
 | '\'' ~[']* '\''
 ;

Or, if you allow escaped characters:

STRING
 : '"' ( ~["]* | '\\' . )* '"'
 | '\'' ( ~[']* | '\\' . ) '\''
 ;

Also note that ~["]* also matches line breaks. If you don't want that, do this: ~["\r\n]*

Upvotes: 3

Related Questions