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