mirror of
https://github.com/JonathanFleischmann/CompilerULTIMATE.git
synced 2024-12-28 09:08:05 +00:00
add custom error listener
This commit is contained in:
parent
d9cf560331
commit
245dc147ec
@ -25,9 +25,19 @@ public class Compiler {
|
|||||||
List<Class> classes = new ArrayList<>();
|
List<Class> classes = new ArrayList<>();
|
||||||
for (String fromSource : fromSources) {
|
for (String fromSource : fromSources) {
|
||||||
CharStream input = CharStreams.fromString(fromSource);
|
CharStream input = CharStreams.fromString(fromSource);
|
||||||
|
|
||||||
DecafLexer lexer = new DecafLexer(input);
|
DecafLexer lexer = new DecafLexer(input);
|
||||||
|
//add custom error listener
|
||||||
|
lexer.removeErrorListeners();
|
||||||
|
lexer.addErrorListener(ThrowingErrorListener.INSTANCE);
|
||||||
|
|
||||||
CommonTokenStream tokens = new CommonTokenStream(lexer);
|
CommonTokenStream tokens = new CommonTokenStream(lexer);
|
||||||
|
|
||||||
DecafParser parser = new DecafParser(tokens);
|
DecafParser parser = new DecafParser(tokens);
|
||||||
|
//add custom error listener
|
||||||
|
parser.removeErrorListeners();
|
||||||
|
parser.addErrorListener(ThrowingErrorListener.INSTANCE);
|
||||||
|
|
||||||
DecafParser.ClassContext tree = parser.class_(); //Parsen
|
DecafParser.ClassContext tree = parser.class_(); //Parsen
|
||||||
classes.add(ASTGenerator.generateAST(tree));
|
classes.add(ASTGenerator.generateAST(tree));
|
||||||
}
|
}
|
||||||
|
17
src/main/java/de/maishai/ThrowingErrorListener.java
Normal file
17
src/main/java/de/maishai/ThrowingErrorListener.java
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user