0

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

  • 1
    I removed the "java" and "exception" tags. This is strictly an ANTLR issue. That's out of the way, so... where is your code? And yes, that looks like an ANTLR error message and no, you cannot change them. The error message should be good enough (although I agree is very cryptic. In order for us to help you, you need to share your code with us and how it is used. – hfontanez Mar 07 '22 at 04:54
  • Also, does this answer your question? [ANTLR 4.5 - Mismatched Input 'x' expecting 'x'](https://stackoverflow.com/questions/29777778/antlr-4-5-mismatched-input-x-expecting-x) – hfontanez Mar 07 '22 at 04:56
  • @hfontanez Sorry, I added some of my code, is that what you needed? I can provide more, but my visitor file is well over a thousand lines of dense code, and uploading that here for you to sift through would be a hassle – primevibetime Mar 07 '22 at 05:04

1 Answers1

1

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();
...
Bart Kiers
  • 166,582
  • 36
  • 299
  • 288