title: How to make Antlr4 stop parsing when a rule is completed
body: <p>I have to parse parts of a file where I have expressions like :</p>
  
  <pre><code>garbage garbage garbage
  BEGIN &lt;something&gt; END
  garbage garbage...
  </code></pre>
  
  <p>Here, I want to parse everything between BEGIN and END, keeping the garbage aside.</p>
  
  <p>I tried to write a parser which has rules like :</p>
  
  <pre><code>rule : BEGIN expr END;
  expr : ... ;
  </code></pre>
  
  <p>which correctly parses my expression, if it's the only thing I have in my file. Sadly, when I try to kick the parser when I meet a "BEGIN" in my file, the parser will correctly parse the expression, but will then try to fetch some other token after the "END".</p>
  
  <p>I have read the part abiut fyzzy grammar in ANTLR4 book, but this is not what I want, because the result of the parsing will impact the remaining of the file (basically, the result of the parsing will produce a set of substitutions to apply in the following text).</p>
  
  <p>What I'm looking for is a way to tell the parser to stop after the "END" keyword. I have tried to override the TokenStream to produce a Token.EOF when END is met, with this modified rule set :</p>
  
  <pre><code>rule : BEGIN expr EOF;
  expr : ... ;
  </code></pre>
  
  <p>with code like :</p>
  
  <pre><code>public Token LT(int k) 
  {
      Token token = super.LT( k );
  
      if ( token.getType() == MyParser.END )
      {
          token = new CommonToken(Token.EOF,"");
      }
  
      return token;
  }
  </code></pre>
  
  <p>but in this case, the stream is closed, and I can't anymore use it for the remaining file...</p>