2013-10-24 43 views
1

我有一個很大的文件,我試圖用Java中的Antlr解析,我想顯示進度。解析器的報告進度

它看起來像可以做到以下幾點:

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

,然後用maxTokenIndexParseTreeListener這樣:

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

的是,下半年似乎工作。我獲得了tokenIndex的增加值。但是,tokens.size()正在返回0.這使得我無法衡量我已經取得了多大進展。

有沒有很好的方法來估計我有多遠?

回答

1

以下顯示工作。

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

然後用

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