从 STDIN 读取命令并在输入时执行每个命令

发布于 2024-12-06 17:35:44 字数 1109 浏览 0 评论 0原文

我想调整下面的代码以使用 ANTLRReaderStream,这样我就不必为每一行创建一个新的解析器。但它需要单独处理每一行,我目前不知道如何做,而且我没有看到任何方法来询问解析器是否已准备好数据(或者相当于 String line = stdin.readLine()

主循环:

stdin = new BufferedReader(new InputStreamReader(System.in));

while (true) {
    String line = stdin.readLine();
    if (line == null) {
        System.exit(0);
    }

    processLine(line.trim());
}

处理单行:

public void processLine(String line) throws IOException {
    try {
        QuotaControlCommandsLexer lexer = new QuotaControlCommandsLexer();
        lexer.setCharStream(new ANTLRStringStream(line));           
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        QuotaControlCommandsParser parser = new QuotaControlCommandsParser(tokens);

        Command cmd = parser.command();
        boolean result = cmd.execute();         
        output(result ? "1" : "0");
        stdout.flush();
    }
    catch (RecognitionException e) {
        logger.error("invalid command: " + line);
        output("ERROR: invalid command `" + line + "`");
    }
}

I'd like to adapt the code below to use a ANTLRReaderStream so I don't have to create a new parser for each line. But it needs to process each line individually, which I don't have any idea how to do currently, and I don't see any way to ask the parser whether it has data ready (or whatever would be the equivalent of String line = stdin.readLine().

main loop:

stdin = new BufferedReader(new InputStreamReader(System.in));

while (true) {
    String line = stdin.readLine();
    if (line == null) {
        System.exit(0);
    }

    processLine(line.trim());
}

handle a single line:

public void processLine(String line) throws IOException {
    try {
        QuotaControlCommandsLexer lexer = new QuotaControlCommandsLexer();
        lexer.setCharStream(new ANTLRStringStream(line));           
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        QuotaControlCommandsParser parser = new QuotaControlCommandsParser(tokens);

        Command cmd = parser.command();
        boolean result = cmd.execute();         
        output(result ? "1" : "0");
        stdout.flush();
    }
    catch (RecognitionException e) {
        logger.error("invalid command: " + line);
        output("ERROR: invalid command `" + line + "`");
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

两人的回忆 2024-12-13 17:35:44

如果使用 JDK1.6,我们可以按如下方式执行主循环:

    Console console = System.console();

    if (console != null) {
        String line = null;

        while ((line = console.readLine()) != null) {
            processLine(line.trim());
        }
    } else {
        System.out.println("No console available!!");
    }

If using JDK1.6 we can do the main loop as the following:

    Console console = System.console();

    if (console != null) {
        String line = null;

        while ((line = console.readLine()) != null) {
            processLine(line.trim());
        }
    } else {
        System.out.println("No console available!!");
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文