add custom error listener

This commit is contained in:
Boolean-true 2024-06-30 22:38:20 +02:00
parent d9cf560331
commit 245dc147ec
2 changed files with 27 additions and 0 deletions

View File

@ -25,9 +25,19 @@ public class Compiler {
List<Class> classes = new ArrayList<>();
for (String fromSource : fromSources) {
CharStream input = CharStreams.fromString(fromSource);
DecafLexer lexer = new DecafLexer(input);
//add custom error listener
lexer.removeErrorListeners();
lexer.addErrorListener(ThrowingErrorListener.INSTANCE);
CommonTokenStream tokens = new CommonTokenStream(lexer);
DecafParser parser = new DecafParser(tokens);
//add custom error listener
parser.removeErrorListeners();
parser.addErrorListener(ThrowingErrorListener.INSTANCE);
DecafParser.ClassContext tree = parser.class_(); //Parsen
classes.add(ASTGenerator.generateAST(tree));
}

View File

@ -0,0 +1,17 @@
package de.maishai;
import org.antlr.v4.runtime.BaseErrorListener;
import org.antlr.v4.runtime.RecognitionException;
import org.antlr.v4.runtime.Recognizer;
import org.antlr.v4.runtime.misc.ParseCancellationException;
public class ThrowingErrorListener extends BaseErrorListener {
public static final ThrowingErrorListener INSTANCE = new ThrowingErrorListener();
@Override
public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine, String msg, RecognitionException e)
throws ParseCancellationException {
throw new ParseCancellationException("Error in line " + line + ":" + charPositionInLine + " " + msg);
}
}