Q:

It it possible with ANTLR4 to determine token types that applicable on some position?

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 ErrorListener and trying to get applicable tokens from RecognitionException object, but this approach is not working all the time.
I have a grammar:
grammar WhereClause;

USER_NAME_COLUMN: 'user_name' ;
USER_AGE_COLUMN: 'user_age';
EQ : '=' ;
LTH : '<' ;
GTH : '>' ;
WS : ( ' ' | '\t' )+ -> skip ;

stringColumn: USER_NAME_COLUMN ;
numericColumn: USER_AGE_COLUMN;
stringRelationalOperator: EQ ;
numericRelationalOperator: EQ | LTH | GTH ;
expression: stringColumn stringRelationalOperator stringColumn | numericColumn numericRelationalOperator numericColumn ;

And some kind of simple test:
public static void main(String... args) {
    String data = "user_name = user_name";
    for (int i = 1; i <= data.length(); i++) {
        String input = data.substring(0, i);
        System.out.println("===========================");
        System.out.println(">> " + 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<?, ?> recognizer, Object o, int i, int i1, String s, RecognitionException e) {
            Vocabulary vocabulary = recognizer.getVocabulary();
            if (e != null) {
                e.getExpectedTokens().getIntervals().forEach(interval -> {
                    for (int j = interval.a; j <= 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();
}

And as result I get follow output:
===========================
>> u
'user_name'
'user_age'
===========================
>> us
'user_name'
'user_age'
===========================
>> use
'user_name'
'user_age'
===========================
>> user
'user_name'
'user_age'
===========================
>> user_
'user_name'
'user_age'
===========================
>> user_n
'user_name'
'user_age'
===========================
>> user_na
'user_name'
'user_age'
===========================
>> user_nam
'user_name'
'user_age'
===========================
>> user_name
'='
===========================
>> user_name 
'='
===========================
>> user_name =
===========================
>> user_name = 
===========================
>> user_name = u
===========================
>> user_name = us
===========================
>> user_name = use
===========================
>> user_name = user
===========================
>> user_name = user_
===========================
>> user_name = user_n
===========================
>> user_name = user_na
===========================
>> user_name = user_nam
===========================
>> user_name = user_name

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?

A:

Using the error position for code completion is not going to work well. What if the caret position and the error positon don't concure? Also, this simple approach will only give you expected keyword tokens, but usually you want more (e.g. all available variables at the given position). So you need a symbol table, you need a way to determine the type of symbols expected at a given position etc.
Using a parser to get code completion candidates is not working well. Keep in mind a parser visits a single path that matches the input, but you need all possible paths.
In this blog post I described a possible approach using ANTLR3 and I'm working on one for ANTLR4. Another attempt is published by Federico Tomassetti. It still only returns keywords, but at least it doesn't use the parser for that.
Here is a discussion of Terence Parr about providing a function that returns all follow sets: https://github.com/antlr/antlr4/issues/1428

