title: ANTLR4 pushMode, popMode, mode
body: <p>I have a grammar that fails when I use 'pushMode' and 'popMode' but works when I use 'mode'. </p>
  
  <p>This grammar construct that works:</p>
  
  <pre><code>TAG: '{'                                -&gt; pushMode( TAG_MODE ), skip;
  TEXT: ~[{]+;
  
  mode TAG_MODE;
       TAG_COMMENT: '*'                   -&gt; skip, mode( COMMENT_MODE );
  
  mode COMMENT_MODE;
       END_COMMENT:  .*? '*}'             -&gt; skip, popMode;
       COMMENT_TEXT: .                    -&gt; type( SYNTAX_ERROR ), popMode;
  </code></pre>
  
  <p>Now if I change the TAG_COMMENT to use 'pushMode' and 'popMode' instead it fails an I loose everything from the comment tag onwards.</p>
  
  <p>This grammar construct that fails:</p>
  
  <pre><code>TAG: '{'                                -&gt; pushMode( TAG_MODE ), skip;
  TEXT: ~[{]+;
  
  mode TAG_MODE;
       TAG_COMMENT: '*'                   -&gt; skip, pushMode( COMMENT_MODE ), popMode;
  
  mode COMMENT_MODE;
       END_COMMENT:  .*? '*}'             -&gt; skip, popMode;
       COMMENT_TEXT: .                    -&gt; type( SYNTAX_ERROR ), popMode;
  </code></pre>
  
  <p>Can anybody explain the difference to me; in my mind they should be functionally equivalent except that it second method uses one more level of lexer stack.</p>
  
  <p>I would actually prefer to use the failing construct as it's too easy to get the lexer stack screwed up using 'mode'(goto) whereas if everything uses 'pushMode' and 'popMode' it's much easier to keep the lexer stack in order.</p>
  
  <p>Also is it ok to use an empty set to exit a mode?
  Something like:</p>
  
  <pre><code>mode MODE1;
       TAG_IDENT:  IDENT;
       TAG_EMPTY:           -&gt; popMode; // Hopefully exits and doesn't consume a token
  </code></pre>
  
  <p>Any thoughts?</p>