Reading multi-line value in ANTLR before special character appears
How can I read multiline text in ANTLR until a special character appears. As in the text below: -
@Description("
Hi There I am.
")
I need to read it as - > @Description and value -> "Hello, I am" .
I tried this with the following grammar
KEY
: '@' [a-zA-Z] (~[(\r\n] | '\\)')*
;
VALUE
: '(' ~[\r\n]*
;
I have tried many variants of VALUE grammar but no luck.
source to share
You are probably confused by the lexer / parser separation. You only provided one example, but I can do the following:
declaration: KEY '(' STRING ')' ; KEY : '@' [a-zA-Z]+ ; STRING: '"' (~'"')* '"' ; WS: [ \t\r\n] -> skip;
declaration
is the parser rule. It consists of KEY
( @
followed by letters), an open parenthesis, STRING
(any text between quotes), and a closing parenthesis. KEY
and STRING
- lexer rules.
Keep in mind that the rule STRING
above will not allow you to avoid symbols. If you need to avoid a quote with a backslash (as well as a backslash with a backslash), use the following rule instead:
STRING: '"' ('\\' ["\\] | ~'"')* '"'
source to share