我有一个大文件,正在尝试使用 Java 中的 Antlr 进行解析,我想显示进度。
看起来可以执行以下操作:
CommonTokenStream tokens = new CommonTokenStream(lexer);
int maxTokenIndex = tokens.size();
然后在 ParseTreeListener
中使用 maxTokenIndex
,如下所示:
public void exitMyRule(MyRuleContext context) {
int tokenIndex = context.start.getTokenIndex();
myReportProgress(tokenIndex, maxTokenIndex);
}
后半部分似乎有效。我得到的 tokenIndex
值不断增加。但是,tokens.size() 返回 0。这使得无法衡量我取得了多少进展。
有没有一个好的方法来估计我的进度?
最佳答案
以下内容似乎有效。
File file = getFile();
ANTLRInputStream input = new ANTLRInputStream(new FileReader(file));
ProgressMonitor progress = new ProgressMonitor(null,
"Loading " + file.getName(),
null,
0,
input.size());
然后扩展 MyGrammarBaseListener
@Override
public void exitMyRule(MyRuleContext context) {
progress.setProgress(context.stop.getStopIndex());
}
关于java - 报告解析器的进度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19571512/