Dracoflamme
Dracoflamme

Reputation: 57

Handling errors in ANTLR 4

After following the accepted answer's instructions of Handling errors in ANTLR4 question I stuck up with the following error.

CustomErrorListener.java:11: cannot find symbol
symbol : variable REPORT_SYNTAX_ERRORS
location: class CustomErrorListener

I understood that ways to handle errors in ANTLR4 were different from ANTLR3, and based on the aforementioned question and its answers I ended up implementing the following error listener.

public class DescriptiveErrorListener extends BaseErrorListener {
    public static DescriptiveErrorListener INSTANCE = new DescriptiveErrorListener();

    @Override
    public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol,
                        int line, int charPositionInLine,
                        String msg, RecognitionException e)
    {
        if (!REPORT_SYNTAX_ERRORS) {
            return;
        }

        String sourceName = recognizer.getInputStream().getSourceName();
        if (!sourceName.isEmpty()) {
            sourceName = String.format("%s:%d:%d: ", sourceName, line, charPositionInLine);
        }

        System.err.println(sourceName+"line "+line+":"+charPositionInLine+" "+msg);
    }
}

Unfortunately I could not find anything about this REPORT_SYNTAX_ERRORS field anywhere in the ANTLR documentation. Any clue on what this could come from?

Upvotes: 2

Views: 2988

Answers (1)

Sam Harwell
Sam Harwell

Reputation: 99859

It's declared in the same file you copied and pasted the DescriptiveErrorListener class from. Here is the declaration:

private static final boolean REPORT_SYNTAX_ERRORS = true;

When the value is false, the syntaxError method returns without displaying errors.

Upvotes: 1

Related Questions