Import ANTLR4 lexical grammar that uses different modes

I am trying to import a lexer grammar into another lexer grammar. The imported grammar uses different modes (in XMLLexer INSIDE and PROC_INSTR modes).

lexer grammar HTMLLexer;
import XMLLexer;

      

When compiling, I get an error that the variable corresponding to the mode name is not defined.

As a concrete example, I define HTMLLexer on top of XMLLexer (from the antlr4 book) and get the following error:

C:\Users\<user>\AppData\Local\Temp\TestRigTask-1360839400637\HTMLLexer.java:143: 
  error: cannot find symbol
  case 6: more(); pushMode(PROC_INSTR);  break;
                         ^
  symbol:   variable PROC_INSTR
  location: class HTMLLexer
1 error

      

I can overcome this error by updating the modes in the top-level lexis with additional tokens, but then the imported tokens inside other modes, like the default, are not recognized.

The parser works if I embed the imported part in the definition of the main vocabulary, but I would like to use the import function to have a clean separation. (Not for the xml / html example, but in a different case.)

Can I get it to work using imports or is this an antlr4 limitation?

+3


source to share


1 answer


Importing multi-mode lexer grammars is not yet supported in ANTLR 4. The following issue will track the progress of this function:



Support for importing multi-mode lexer grammars

+4


source







All Articles