Lexer modes and imported grammars - status?

23 views
Skip to first unread message

Thomas Beale

unread,
Nov 2, 2015, 11:07:13 AM11/2/15
to antlr-discussion
I can see that there has been some confusion for a couple of years on lexer modes + imported grammars. Does anyone know whether importing a lexer grammar with modes works or not?

My case is of the following structure:

~~~~~~~~~~ my_lang.g4 ~~~~~~~~~~
parser grammar my_lang;
import my_lang_lexer_rules;

top_rule: 'xxx' meta_data? A_SECTION 'yyy' B_SECTION 'zzz';
meta_data: '(' meta_data_item  (';' meta_data_item )* ')' ;  // *** whitespace should be allowed here

meta_data_item:
Meta_data_tag_adl_version '=' VERSION_ID
    | etc;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


~~~~~~~~~~ my_lang_lexer_rules.g4 ~~~~~~~~~~
lexer grammar my_lang_lexer_rules;
import common_patterns;

A_SECTION: 'aaaa' -> PushMode (ModeA);
B_SECTION: 'bbbb' -> PushMode (ModeB);

mode ModeA;
ExitA: 'AAAA' -> popMode;
AContent: .+? SECTION_END_PATTERN -> more ;

mode ModeB;
ExitB: 'BBBB' -> popMode;
BContent: .+? SECTION_END_PATTERN -> more ;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


~~~~~~~~~~ common_patterns.g4 ~~~~~~~~~~
lexer grammar common_patterns;

WS         : [ \t\r]+    -> skip ;
LINE : '\n' -> skip ; // increment line count
ETC....
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


Whitespace is not being matched, and this means rules like the one marked near the top don't work. The WS matching rule is in the 3rd grammar; it appears that either the 3rd grammar rules are just ignored, or else they are being counted as being in ModeB, the second mode of the 2nd grammar. 

What are the precise rules for modes, importing? When does a 'mode' section end? At a file boundary? Are imported lexer files always added to the DEFAULT_MODE lexer rule set?

thanks for any pointers

- thomas
Reply all
Reply to author
Forward
0 new messages