antlrantlrworks

ANTLR String LEXER token


I am trying to do a STRING lexer token. My problem is that besides \n, \r, \t any character is himself (for example \c is c). That being said i have the following example:

"This is a valid \
string."
"This is
not valid."
"This is al\so a valid string"

After searching on the internet to no avail for me, i concluded that i must use an @after clause. Unfortunately i don't understand how to do this. If i am not mistaking i can't use a syntactic predicate because this is not a parser rule, it's a lexer rule.


Solution

  • How about something like this:

    STRING
     : '"' ( '\\' ('\\'|'\t'|'\r\n'|'\r'|'\n'|'"') | ~('\\'|'\t'|'\r'|'\n'|'"') )* '"'
     ;
    

    where '\\' ('\\'|'\t'|'\r\n'|'\r'|'\n'|'"') is an escaped slash, tab, line break or quote. And ~('\\'|'\t'|'\r'|'\n'|'"') matches any char other than a slash, tab, line break or quote.