我如何摆脱 class 文件中 java antlr 的折旧代码?

How do I get rid of depreciated code in this class file for java antlr?

import org.antlr.v4.runtime.*;
import org.antlr.v4.runtime.tree.ParseTree;

import java.io.FileInputStream;
import java.io.InputStream;


public class Calc {
    public static void main(String[] args) throws Exception {
        String inputFile = null;
        if ( args.length>0 ) inputFile = args[0];
        InputStream is = System.in;
        if ( inputFile!=null ) is = new FileInputStream(inputFile);
        ANTLRInputStream input = new ANTLRInputStream(is);
        CalculatorLexer lexer = new CalculatorLexer(input);
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        CalculatorParser parser = new CalculatorParser(tokens);
        ParseTree tree = parser.program(); // parse

        CalcVisitor calcc = new CalcVisitor();
        calcc.visit(tree);
    }

}

据我所知,我很确定 ANTLRFileStream 是被删除的,但我尝试用 CharStreams 替换它,但我尝试的代码和 运行 一直导致错误。我该如何解决这个问题?

尝试这样的事情:


public static void main(String[] args) throws Exception {

  CharStream charStream = args.length > 0 
    ? CharStreams.fromFileName(args[0]) 
    : CharStreams.fromStream(System.in);

  CalculatorLexer lexer = new CalculatorLexer(charStream);
  CommonTokenStream tokens = new CommonTokenStream(lexer);
  CalculatorParser parser = new CalculatorParser(tokens);
  ParseTree tree = parser.program(); // parse

  CalcVisitor calcc = new CalcVisitor();
  calcc.visit(tree);
}