Rule reference is not currently supported in a set in ANTLR4 Grammar
Asked Answered
P

1

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 .

Precancel answered 28/5, 2013 at 11:24 Comment(2)
I'm interested in an antlr4 grammar for JS too. Did you get very far?Peper
I have a grammar that parses all JS expressions, functionCalls and if-else statements. I will try to opensource the grammar If possiblePrecancel
C
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
    ;
Concurrence answered 28/5, 2013 at 11:44 Comment(2)
But this is listed as fixed github.com/antlr/antlr4/issues/70, Why Can't I use it ?Precancel
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.Concurrence

© 2022 - 2024 — McMap. All rights reserved.