title: C# ANTLR4 DefaultErrorStrategy or custom error listener does not catch unrecognized characters
body: <p>It's quite strange, but <code>DefaultErrorStrategy</code> does not do anything for catching unrecognized characters from a stream. I tried a custom error strategy, a custom error listener and <code>BailErrorStrategy</code> - no luck here.</p>
  
  <p>My grammar</p>
  
  <pre><code>grammar Polynomial;
  
  parse           : canonical EOF
                  ;
  
  canonical       : polynomial+                                     #canonicalPolynom
                  | polynomial+ EQUAL polynomial+                   #equality
                  ;
  
  polynomial      : SIGN? '(' (polynomial)* ')'                     #parens
                  | monomial                                        #monom
                  ;
  
  monomial        : SIGN? coefficient? VAR ('^' INT)?               #addend
                  | SIGN? coefficient                               #number
                  ;
  
  coefficient             : INT | DEC;
  
  INT                     : ('0'..'9')+;
  DEC                     : INT '.' INT;
  VAR                     : [a-z]+;
  SIGN                    : '+' | '-';
  EQUAL                   : '=';
  WHITESPACE              : (' '|'\t')+ -&gt; skip;
  </code></pre>
  
  <p>and I'm giving an input <code>23*44=12</code> or <code>@1234</code></p>
  
  <p>I'm expecting that my parser throws mismatched token or any kind of exception for a character <code>*</code> or <code>@</code> that is not defined in my grammar.</p>
  
  <p>Instead, my parser just skips <code>*</code> or <code>@</code> and traverse a tree like there are do not exist.</p>
  
  <p>My handler function where I'm calling lexer, parser and that's kind of stuff.</p>
  
  <pre><code>private static (IParseTree tree, string parseErrorMessage) TryParseExpression(string expression)
          {
              ICharStream stream = CharStreams.fromstring(expression);
              ITokenSource lexer = new PolynomialLexer(stream);
  
              ITokenStream tokens = new CommonTokenStream(lexer);
              PolynomialParser parser = new PolynomialParser(tokens);
  
              //parser.ErrorHandler = new PolynomialErrorStrategy(); -&gt; I tried custom error strategy
              //parser.RemoveErrorListeners();
              //parser.AddErrorListener(new PolynomialErrorListener()); -&gt; I tried custom error listener
              parser.BuildParseTree = true;
  
              try
              {
                  var tree = parser.canonical();
                  return (tree, string.Empty);
              }
              catch (RecognitionException re)
              {
                  return (null, re.Message);
              }
              catch (ParseCanceledException pce)
              {
                  return (null, pce.Message);
              }
          }            
  </code></pre>
  
  <p>I tried to add a custom error listener.</p>
  
  <pre><code>public class PolynomialErrorListener : BaseErrorListener
      {
          private const string Eof = "EOF";
  
          public override void SyntaxError(TextWriter output, IRecognizer recognizer, IToken offendingSymbol, int line, int charPositionInLine, string msg,
              RecognitionException e)
          {
              if (msg.Contains(Eof))
              {
                  throw new ParseCanceledException($"{GetSyntaxErrorHeader(charPositionInLine)}. Missing an expression after '=' sign");
              }
  
              if (e is NoViableAltException || e is InputMismatchException)
              {
                  throw new ParseCanceledException($"{GetSyntaxErrorHeader(charPositionInLine)}. Probably, not closed operator");
              }
  
              throw new ParseCanceledException($"{GetSyntaxErrorHeader(charPositionInLine)}. {msg}");
          }
  
          private static string GetSyntaxErrorHeader(int errorPosition)
          {
              return $"Expression is invalid. Input is not valid at {--errorPosition} position";
          }
      }
  </code></pre>
  
  <p>After that, I tried to implement a custom error strategy.</p>
  
  <pre><code>public class PolynomialErrorStrategy : DefaultErrorStrategy
      {
          public override void ReportError(Parser recognizer, RecognitionException e)
          {
              throw e;
          }
  
          public override void Recover(Parser recognizer, RecognitionException e)
          {
              for (ParserRuleContext context = recognizer.Context; context != null; context = (ParserRuleContext) context.Parent) {
                  context.exception = e;
              }
  
              throw new ParseCanceledException(e);
          }
  
          public override IToken RecoverInline(Parser recognizer)
          {
              InputMismatchException e = new InputMismatchException(recognizer);
              for (ParserRuleContext context = recognizer.Context; context != null; context = (ParserRuleContext) context.Parent) {
                  context.exception = e;
              }
  
              throw new ParseCanceledException(e);
          }
  
          protected override void ReportInputMismatch(Parser recognizer, InputMismatchException e)
          {
              string msg = "mismatched input " + GetTokenErrorDisplay(e.OffendingToken);
              // msg += " expecting one of " + e.GetExpectedTokens().ToString(recognizer.());
              RecognitionException ex = new RecognitionException(msg, recognizer, recognizer.InputStream, recognizer.Context);
              throw ex;
          }
  
          protected override void ReportMissingToken(Parser recognizer)
          {
              BeginErrorCondition(recognizer);
              IToken token = recognizer.CurrentToken;
              IntervalSet expecting = GetExpectedTokens(recognizer);
              string msg = "missing " + expecting.ToString() + " at " + GetTokenErrorDisplay(token);
              throw new RecognitionException(msg, recognizer, recognizer.InputStream, recognizer.Context);
          }
      }
  </code></pre>
  
  <p>Is there any flag that I forgot to specify in a parser or I have incorrect grammar?</p>
  
  <p>Funny thing that I'm using ANTLR plugin in my IDE and when I'm testing my grammar in here this plugin correctly responds with <code>line 1:2 token recognition error at: '*'</code></p>
  
  <p>Full source code: <a href="https://github.com/EvgeniyZ/PolynomialCanonicForm" rel="nofollow noreferrer">https://github.com/EvgeniyZ/PolynomialCanonicForm</a></p>
  
  <p>I'm using ANTLR 4.8-complete.jar</p>
  
  <p><strong>Edit</strong></p>
  
  <p>I tried to add to a grammar rule</p>
  
  <pre><code>parse           : canonical EOF
                  ;
  </code></pre>
  
  <p>Still no luck here</p>