title: It it possible with ANTLR4 to determine token types that applicable on some position?
body: <p>I'm trying to build some kind of autocompletion tool on top of ANTLR4 and I've got a problem (probably with my understanding). I'm using <code>ErrorListener</code> and trying to get applicable tokens from <code>RecognitionException</code> object, but this approach is not working all the time.</p>
  
  <p>I have a grammar:</p>
  
  <pre><code>grammar WhereClause;
  
  USER_NAME_COLUMN: 'user_name' ;
  USER_AGE_COLUMN: 'user_age';
  EQ : '=' ;
  LTH : '&lt;' ;
  GTH : '&gt;' ;
  WS : ( ' ' | '\t' )+ -&gt; skip ;
  
  
  stringColumn: USER_NAME_COLUMN ;
  numericColumn: USER_AGE_COLUMN;
  stringRelationalOperator: EQ ;
  numericRelationalOperator: EQ | LTH | GTH ;
  expression: stringColumn stringRelationalOperator stringColumn | numericColumn numericRelationalOperator numericColumn ;
  </code></pre>
  
  <p>And some kind of simple test:</p>
  
  <pre><code>public static void main(String... args) {
      String data = "user_name = user_name";
      for (int i = 1; i &lt;= data.length(); i++) {
          String input = data.substring(0, i);
          System.out.println("===========================");
          System.out.println("&gt;&gt; " + input + "");
          parse(input);
      }
  }
  
  private static void parse(String input) {
      ANTLRInputStream inputStream = new ANTLRInputStream(input);
      WhereClauseLexer lexer = new WhereClauseLexer(inputStream);
      CommonTokenStream tokens = new CommonTokenStream(lexer);
      WhereClauseParser parser = new WhereClauseParser(tokens);
      lexer.removeErrorListeners();
      parser.removeErrorListeners();
      parser.addErrorListener(new ANTLRErrorListener() {
          @Override
          public void syntaxError(Recognizer&lt;?, ?&gt; recognizer, Object o, int i, int i1, String s, RecognitionException e) {
              Vocabulary vocabulary = recognizer.getVocabulary();
              if (e != null) {
                  e.getExpectedTokens().getIntervals().forEach(interval -&gt; {
                      for (int j = interval.a; j &lt;= interval.b; j++) {
                          System.out.println(vocabulary.getDisplayName(j));
                      }
                  });
              }
          }
  
          @Override
          public void reportAmbiguity(Parser parser, DFA dfa, int i, int i1, boolean b, BitSet bitSet, ATNConfigSet atnConfigSet) {}
          @Override
          public void reportAttemptingFullContext(Parser parser, DFA dfa, int i, int i1, BitSet bitSet, ATNConfigSet atnConfigSet) {}
          @Override
          public void reportContextSensitivity(Parser parser, DFA dfa, int i, int i1, int i2, ATNConfigSet atnConfigSet) {}
      });
      parser.expression();
  }
  </code></pre>
  
  <p>And as result I get follow output:</p>
  
  <pre><code>===========================
  &gt;&gt; u
  'user_name'
  'user_age'
  ===========================
  &gt;&gt; us
  'user_name'
  'user_age'
  ===========================
  &gt;&gt; use
  'user_name'
  'user_age'
  ===========================
  &gt;&gt; user
  'user_name'
  'user_age'
  ===========================
  &gt;&gt; user_
  'user_name'
  'user_age'
  ===========================
  &gt;&gt; user_n
  'user_name'
  'user_age'
  ===========================
  &gt;&gt; user_na
  'user_name'
  'user_age'
  ===========================
  &gt;&gt; user_nam
  'user_name'
  'user_age'
  ===========================
  &gt;&gt; user_name
  '='
  ===========================
  &gt;&gt; user_name 
  '='
  ===========================
  &gt;&gt; user_name =
  ===========================
  &gt;&gt; user_name = 
  ===========================
  &gt;&gt; user_name = u
  ===========================
  &gt;&gt; user_name = us
  ===========================
  &gt;&gt; user_name = use
  ===========================
  &gt;&gt; user_name = user
  ===========================
  &gt;&gt; user_name = user_
  ===========================
  &gt;&gt; user_name = user_n
  ===========================
  &gt;&gt; user_name = user_na
  ===========================
  &gt;&gt; user_name = user_nam
  ===========================
  &gt;&gt; user_name = user_name
  </code></pre>
  
  <p>It means that I don't get expected token for right part of equality. Does anybody know the reason? Is it possible to know which token (tokens) should follow input line?</p>