10

I am trying to port Chris Lambro's ANTLR3 Javascript Grammar to ANTLR4

I am getting the following error,

Rule reference 'LT' is not currently supported in a set

in the following code ~(LT)*

LineComment
    : '//'  ~(LT)* -> skip
    ;

LT  : '\n'      // Line feed.
    | '\r'      // Carriage return.
    | '\u2028'  // Line separator.
    | '\u2029'  // Paragraph separator.
    ;

I need help understanding why I am getting this error, and how I can solve it .

Gautam
  • 7,868
  • 12
  • 64
  • 105

1 Answers1

11

The ~ operator in ANTLR inverts a set of symbols (characters in the lexer, or tokens in the parser). Inside the set, you have a reference to the LT lexer rule, which is not currently supported in ANTLR 4. To resolve the problem, you need to inline the rule reference:

LineComment
    :   '//' ~([\n\r\u2028\u2029])* -> skip
    ;
Sam Harwell
  • 97,721
  • 20
  • 209
  • 280
  • But this is listed as fixed https://github.com/antlr/antlr4/issues/70, Why Can't I use it ? – Gautam May 29 '13 at 08:56
  • 6
    Previously if you had such a rule reference, it would still generate a lexer but the tokens it created would not be correct. The solution was to turn it into an error message instead of letting it fail silently. – Sam Harwell May 29 '13 at 13:14