Read multiline value in ANTLR untill a special symbol occurs

127 views Asked by At

How can i read a multiline text in ANTLR untill a special symbol occurs. Like as in below text:-

@Description("
Hi There I am.
")

I need to read it as key -> @Description and value -> "Hi There I am".

I tried it with following grammar

KEY
 : '@' [a-zA-Z] (~[(\r\n] | '\\)')*
 ;


VALUE
 : '(' ~[\r\n]*
 ;

I tried many variations of VALUE grammar but no luck.

1

There are 1 answers

0
Lucas Trzesniewski On BEST ANSWER

You're likely confused by the lexer/parser separation here. You did only provide a single example, but I can infer the following:

declaration: KEY '(' STRING ')' ;
KEY : '@' [a-zA-Z]+ ;
STRING: '"' (~'"')* '"' ;
WS: [ \t\r\n] -> skip;

declaration is a parser rule. It consists of a KEY (@ followed by letters), an opening parenthesis, a STRING (any text between quotes), and a closing parenthesis. KEY and STRING are the lexer rules.

Be aware that the STRING rule above won't let you escape characters. If you need to be able to escape a quote with a backslash (and also a backslash with a backslash), use the following rule instead:

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