views:

52

answers:

2

I wrote a stub for a grammar (only matches comments so far), and it's giving me the error "syntax error: invalid char literal: <INVALID>". Moreover, i've tracked down the error to being in the following command:

... ~LINE_ENDING* ...
LINE_ENDING  : ( '\n' | '\r' | '\r\n');

Can someone help me fix this?

+1  A: 

Not sure why you get that error (I have no means of testing it myself at the moment). Perhaps the fact you're negating either a single char (\r or \n) or a double char (\r\n) is an issue?

What happens if you try:

SingleLineComment
    : '//' (~LineBreakChar)* (NewLine | EOF)
    ;

LineBreakChar
    : '\r' | '\n'
    ;

NewLine
    : '\r'? '\n' | '\r'
    ;

?

Bart Kiers
This is interesting: i don't get the error when using `~('\r'|'\n')*` so it must be for that reason. I'll have to add a comment and maybe report this or something, thanks for the help!
RCIX
+2  A: 

The ~ operator can only be applied to a set. In a lexer, the elements of a set are characters of an input stream. In other words, you can have this:

~(  'a'..'z'
|   'C'
|   '\r'
|   '\n'
)

But you can't have this because it's a sequence (of two characters) instead of a set.

~('\r\n')

The problem you encountered is an extension of this second case.

280Z28
That makes sense, +1. @RCIX: it would be fair to mark this as the accepted answer.
Bart Kiers
Ah thanks! that makes sense...
RCIX