Bug in ANTLR grammar

Oct 15, 2013 at 11:00 AM
Edited Oct 15, 2013 at 11:01 AM
The E3s.g used to generate Jint's parser has:
   fragment RegularExpressionChar
          : ~ ( LineTerminator | BSLASH | DIV )
          | BackslashSequence
          ;
I.e., indicating you can’t have a / inside a regular expression. But as long as it’s inside [ ] it’s fine (and supported by all major browsers), so I think the grammar needs to be:
   fragment RegularExpressionChar
          : ~ ( LineTerminator | BSLASH | DIV )
          | RegularExpressionClass
          | BackslashSequence
          ;

   fragment RegularExpressionClass
          : '[' RegularExpressionClassChar* ']'
          ;

   fragment RegularExpressionClassChar
          : BackslashSequence
          | ~ ( LineTerminator | ']' )
          ;
Oct 15, 2013 at 11:06 PM
You also need:
fragment RegularExpressionFirstChar
: ~ ( LineTerminator | MUL | BSLASH | DIV )
| RegularExpressionClass
| BackslashSequence
;
to ensure that a regular expression starting with [ works (e.g. /[/]/). I regenerated the lexer with ANTLR 3.3 and it seems OK, though oddly I had to add in a 'public' modified before the GrammarRule for 'program'.