InputMismatchException 在哪里抛出?

发布于 2025-01-12 03:44:30 字数 3408 浏览 1 评论 0原文

当我在错误的位置使用某个标记执行我的程序时,它会抛出 InputMismatchException ,说出类似的内容,

line 21:0 mismatched input '#' expecting {'in', '||', '&&', '==', '!=', '>=', '<=', '^', '>', '<', '+', '-', '*', '/', '%', '[', ';', '?'}

这对于我正在开发的语言来说是一个可怕的错误消息,所以我想要改变它,但我找不到它的来源,我知道为什么会抛出错误,但我找不到抛出 InputMismatchException 的实际 java 代码行,我不认为它在任何地方我的项目,所以我假设它位于antlr4运行时的某个地方,有没有办法禁用这些错误消息,或者至少更改它们?

编辑:

我的语法(相关部分)如下:

grammar Q;

parse
 : header? ( allImport ';' )*? block EOF
 ;

block
 : ( statement | functionDecl )* ( Return expression ';' )?
 ;

statement
 : functionCall ';'
 | ifStatement
 | forStatement | forInStatement
 | whileStatement
 | tryCatchStatement
 | mainFunctionStatement
 | addWebServerTextStatement ';'
 | reAssignment ';'
 | classStatement
 | constructorStatement ';'
 | windowAddCompStatement ';'
 | windowRenderStatement ';'
 | fileWriteStatement ';'
 | verifyFileStatement ';'
 | objFunctionCall (';')?
 | objCreateStatement ';'
 | osExecStatement ';'
 | anonymousFunction
 | hereStatement ';'
 ;

importStatement 访问方法的一个示例是:

    @Override
    public QValue visitImportStatement(ImportStatementContext ctx) {

        StringBuilder path = new StringBuilder();
        StringBuilder text = new StringBuilder();

        for (TerminalNode o : ctx.Identifier()) {
            path.append("/").append(o.getText());
        }

        for (TerminalNode o : ctx.Identifier()) {
            text.append(".").append(o.getText());
        }

        if (lang.allLibs.contains(text.toString().replace(".q.", "").toLowerCase(Locale.ROOT))) {
            lang.parse(text.toString());
            return QValue.VOID;
        }

        for (File f : lang.parsed) {
            Path currentRelativePath = Paths.get("");
            String currentPath = currentRelativePath.toAbsolutePath().toString();

            File file = new File(currentPath + "/" + path + ".l");
            if (f.getPath().equals(file.getPath())) {
                return null;
            }
        }

        QLexer lexer = null;
        Path currentRelativePath = Paths.get("");
        String currentPath = currentRelativePath.toAbsolutePath().toString();

        File file = new File(currentPath + "/" + path + ".l");
        lang.parsed.add(file);

        try {

            lexer = new QLexer(CharStreams.fromFileName(currentPath + "/" + path + ".l"));
        } catch (IOException e) {
            throw new Problem("Library or File not found: " + path, ctx);
        }
        QParser parser = new QParser(new CommonTokenStream(lexer));
        parser.setBuildParseTree(true);
        ParseTree tree = parser.parse();

        Scope s = new Scope(lang.scope, false);
        Visitor v = new Visitor(s, new HashMap<>());

        v.visit(tree);

        return QValue.VOID;
    }

由于我的 g4 文件中的 parse 规则,>import 语句必须出现在任何其他内容之前(除了标头语句),因此这样做会引发错误

class Main

    #import src.main.QFiles.aLib;

    fn main()

        try
            std::ln("orih");
        onflaw

        end

        new Object as o();
        o::set("val");
        std::ln(o::get());

        std::ln("itj");

    end

end

并且,正如预期的那样,它会引发 InputMismatchException,但这不在任何情况下我的代码

When I execute my program with a certain token in the wrong spot, it throws the InputMismatchException, saying something along the lines of

line 21:0 mismatched input '#' expecting {'in', '||', '&&', '==', '!=', '>=', '<=', '^', '>', '<', '+', '-', '*', '/', '%', '[', ';', '?'}

Which is a terrible error message for the language I'm developing, so I'm looking to change it, but I can't find the source of it, I know why the error is being thrown, but I can't find the actual line of java code that throws the InputMismatchException, I don't think its anywhere in my project, so I assume it's somewhere in the antlr4 runtime, is there a way to disable these error messages, or at least change them?

Edit:

My grammar (the relevant parts) are as follows:

grammar Q;

parse
 : header? ( allImport ';' )*? block EOF
 ;

block
 : ( statement | functionDecl )* ( Return expression ';' )?
 ;

statement
 : functionCall ';'
 | ifStatement
 | forStatement | forInStatement
 | whileStatement
 | tryCatchStatement
 | mainFunctionStatement
 | addWebServerTextStatement ';'
 | reAssignment ';'
 | classStatement
 | constructorStatement ';'
 | windowAddCompStatement ';'
 | windowRenderStatement ';'
 | fileWriteStatement ';'
 | verifyFileStatement ';'
 | objFunctionCall (';')?
 | objCreateStatement ';'
 | osExecStatement ';'
 | anonymousFunction
 | hereStatement ';'
 ;

And an example of the importStatement visit method is:

    @Override
    public QValue visitImportStatement(ImportStatementContext ctx) {

        StringBuilder path = new StringBuilder();
        StringBuilder text = new StringBuilder();

        for (TerminalNode o : ctx.Identifier()) {
            path.append("/").append(o.getText());
        }

        for (TerminalNode o : ctx.Identifier()) {
            text.append(".").append(o.getText());
        }

        if (lang.allLibs.contains(text.toString().replace(".q.", "").toLowerCase(Locale.ROOT))) {
            lang.parse(text.toString());
            return QValue.VOID;
        }

        for (File f : lang.parsed) {
            Path currentRelativePath = Paths.get("");
            String currentPath = currentRelativePath.toAbsolutePath().toString();

            File file = new File(currentPath + "/" + path + ".l");
            if (f.getPath().equals(file.getPath())) {
                return null;
            }
        }

        QLexer lexer = null;
        Path currentRelativePath = Paths.get("");
        String currentPath = currentRelativePath.toAbsolutePath().toString();

        File file = new File(currentPath + "/" + path + ".l");
        lang.parsed.add(file);

        try {

            lexer = new QLexer(CharStreams.fromFileName(currentPath + "/" + path + ".l"));
        } catch (IOException e) {
            throw new Problem("Library or File not found: " + path, ctx);
        }
        QParser parser = new QParser(new CommonTokenStream(lexer));
        parser.setBuildParseTree(true);
        ParseTree tree = parser.parse();

        Scope s = new Scope(lang.scope, false);
        Visitor v = new Visitor(s, new HashMap<>());

        v.visit(tree);

        return QValue.VOID;
    }

Because of the parse rule in my g4 file, the import statement MUST come before any other thing (aside from a header statement), so doing this would throw an error

class Main

    #import src.main.QFiles.aLib;

    fn main()

        try
            std::ln("orih");
        onflaw

        end

        new Object as o();
        o::set("val");
        std::ln(o::get());

        std::ln("itj");

    end

end

And, as expected, it throws an InputMismatchException, but that's not in any of my code

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

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

发布评论

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

评论(1

美煞众生 2025-01-19 03:44:30

您可以删除默认的错误策略并实施您自己的:

...
QParser parser = new QParser(new CommonTokenStream(lexer));

parser.removeErrorListeners();

parser.addErrorListener(new BaseErrorListener() {
  @Override
  public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine, String msg, RecognitionException e) {
    throw new RuntimeException("Your own message here", e);
  }
});

ParseTree tree = parser.parse();
...

You can remove the default error strategy and implement your own:

...
QParser parser = new QParser(new CommonTokenStream(lexer));

parser.removeErrorListeners();

parser.addErrorListener(new BaseErrorListener() {
  @Override
  public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine, String msg, RecognitionException e) {
    throw new RuntimeException("Your own message here", e);
  }
});

ParseTree tree = parser.parse();
...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文