Handling errors in ANTLR4 JavaScript
Asked Answered
O

2

5

I am using ANTLR4 JavaScript to create a sort of a web IDE for a custom language.

It all works great, apart from the fact that errors are logged to the console and I can't figure out a way to redirect those errors somewhere I can present them to the users.

At the moment, they are shown on the JS console like this:

JS Console

Could anyone point me on the right direction (which file I need to edit, etc)?

Overwrite answered 16/5, 2015 at 13:7 Comment(2)
Cannot speak directly to the Javascript implementation, but for Java there are Parser.removeErrorListeners(), that removes the default ConsoleErrorListener, and Parser.addErrorListener(....), to add back your own custom error listener. Do this after creating the Parser and before running it.Subdebutante
@Subdebutante Thanks, that helped me figure it out! Can you post it as an answer so I can accept it?Overwrite
S
5

Cannot speak directly to the Javascript implementation, but for Java there are:

Parser.removeErrorListeners() // removes the default ConsoleErrorListener Parser.addErrorListener(....) // add back a custom error listener

Do this after creating the Parser and before running it.

Subdebutante answered 18/5, 2015 at 4:54 Comment(0)
T
4

You can do this by implementing the antlr4.error.ErrorListener interface and providing one of the interface methods such as syntaxError to be invoked upon each error.

class ExprErrorListener extends antlr4.error.ErrorListener {
  syntaxError(recognizer, offendingSymbol, line, column, msg, err) {
    ...
  }
}

Disable the default error listener and enable the custom listener with:

parser.removeErrorListeners();
parser.addErrorListener(new ExprErrorListener());

Note that you can skip the class and pass in an object with the syntaxError function available. Here's a minimal, complete example on the Expr.g4 grammar:

const antlr4 = require("antlr4");
const {ExprLexer} = require("./parser/ExprLexer");
const {ExprParser} = require("./parser/ExprParser");

const expression = "2 + 8 * 9 - \n";
const input = new antlr4.InputStream(expression);
const lexer = new ExprLexer(input);
const tokens = new antlr4.CommonTokenStream(lexer);

const parser = new ExprParser(tokens);
parser.buildParseTrees = true;
parser.removeErrorListeners();
parser.addErrorListener({
  syntaxError: (recognizer, offendingSymbol, line, column, msg, err) => {
    console.error(`${offendingSymbol} line ${line}, col ${column}: ${msg}`);
  }
});

const tree = parser.prog();

Gives:

[@6,12:12='\n',<10>,1:12] line 1, col 12: mismatched input '\n' expecting {'(', ID, INT}

See also error handlers.

Takashi answered 25/3, 2020 at 0:10 Comment(2)
What is @6? How is that useful to a user?Laquitalar
@Laquitalar I'm not sure offhand -- it's probably something to do with the grammar, but it's irrelevant as far as this answer is concerned. I'm just dumping whatever Antlr passes to the callback to show that it works. It's up to you to do whatever you need with that information.Takashi

© 2022 - 2024 — McMap. All rights reserved.