Search code examples
javaantlrprogressantlr4

Reporting progress of parser


I have a large file that I am trying to parse with Antlr in Java, and I would like to show the progress.

It looked like could do the following:

CommonTokenStream tokens = new CommonTokenStream(lexer);
int maxTokenIndex = tokens.size();

and then use maxTokenIndex in a ParseTreeListener as such:

public void exitMyRule(MyRuleContext context) {
    int tokenIndex = context.start.getTokenIndex();
    myReportProgress(tokenIndex, maxTokenIndex);
}

The second half of that appears to work. I get ever increasing values for tokenIndex. However, tokens.size() is returning 0. This makes it impossible to gauge how much progress I have made.

Is there a good way to get an estimate of how far along I am?


Solution

  • The following appears to work.

    File file = getFile();
    ANTLRInputStream input = new ANTLRInputStream(new FileReader(file));
    ProgressMonitor progress = new ProgressMonitor(null,
                                                   "Loading " + file.getName(),
                                                   null,
                                                   0,
                                                   input.size());
    

    Then extend MyGrammarBaseListener with

    @Override 
    public void exitMyRule(MyRuleContext context) {
        progress.setProgress(context.stop.getStopIndex());
    }