How to find patterns in a changing parse tree (ANTLR4) for code completion?

Hi, I'm currently working on developing a plugin to support a custom language. The grammar is written in ANTLR4. There is a rule in the language called "table_lookup" which is defined as follows:

table_lookup :
TABLE COLON ID L_PAR named_arg_list? R_PAR;

named_arg_list:
named_arg (COMMA named_arg)*;

named_arg:
ID ASSIGN arg;


An arg is a variable or literal. Example table lookup from the language:  myVar = Table:myTableName (myParameter = "string")

I am trying to implement code completion for table names and parameters. The problem is that the parse tree is changing drastically as I type. If I type "myVar = Table:" it doesn't recognize it as a table lookup because I haven't put in both parentheses yet, so it treats it as generic statement consisting of many children (defined in the grammar). As soon as I start typing it breaks off the table name I'm typing into a new statement also consisting of many children.  When I put the parentheses in it groups it back into a table lookup. A simliar issue happens as I'm trying parameters. The parse tree is changing a lot as I type which is making it hard to find patterns to complete too. Right now I've basically muscled it by hardcoding the different cases in, but I might be missing some. Is there a better way to do it? Maybe by changing the grammar or looking for some other pattern?

Thanks.

Please sign in to leave a comment.