首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >InputMismatchException在哪里抛出?

InputMismatchException在哪里抛出?
EN

Stack Overflow用户
提问于 2022-03-07 04:37:00
回答 1查看 199关注 0票数 0

当我在错误的地方使用某个标记执行我的程序时,它会抛出InputMismatchException,并按照

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

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

编辑:

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

代码语言:javascript
复制
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访问方法的一个例子是:

代码语言:javascript
复制
    @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;
    }

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

代码语言:javascript
复制
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,但这不在我的任何代码中

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-03-07 07:09:47

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

代码语言:javascript
复制
...
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();
...
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71376476

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档