views:

39

answers:

1

I'm trying to write a string matching rule in ANTLRWorks, and i need to match either escaped quotes or any non quote character. I can match escaped quotes but I'm having trouble with the other part: ~'\'' | ~'\"' will end up matching everything and ~'\'\"' seems to be ignored by the grammar generator (at least the visual display). What sequence of characters will get me what i want?

+1  A: 

Try something like this:

StringLiteral
    :    '"' (EscapeSequence | StringChar)* '"'
    ;

EscapeSequence
    :    '\\' ('"' | '\\')
    ;

StringChar
    :    ~('"' | '\\')
    ;
Bart Kiers
I ended up going with a non-greedy anychar consumption, but this ought to do nicely to answer the question.
RCIX