Skip to content

Commit f3ab944

Browse files
committed
Compiler: Added a custom error handler
1 parent 0ecb2d2 commit f3ab944

File tree

6 files changed

+25
-1117
lines changed

6 files changed

+25
-1117
lines changed

src/main/java/org/piccode/backend/Compiler.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22

33
import java.util.ArrayList;
44
import java.util.List;
5+
import org.antlr.v4.runtime.BaseErrorListener;
56
import org.antlr.v4.runtime.CharStreams;
67
import org.antlr.v4.runtime.CommonTokenStream;
8+
import org.antlr.v4.runtime.RecognitionException;
9+
import org.antlr.v4.runtime.Recognizer;
710
import org.piccode.antlr4.PiccodeScriptLexer;
811
import org.piccode.antlr4.PiccodeScriptParser;
912
import org.piccode.ast.Ast;
@@ -88,10 +91,16 @@ public static StatementList program(String file, String code) {
8891
code = sanitizeSourceFile(code);
8992
}
9093

94+
var err = new SyntaxError(file);
95+
9196
var lexer = new PiccodeScriptLexer(CharStreams.fromString(code));
92-
var parser = new PiccodeScriptParser(new CommonTokenStream(lexer));
9397
lexer.removeErrorListeners();
98+
lexer.addErrorListener(err);
99+
100+
101+
var parser = new PiccodeScriptParser(new CommonTokenStream(lexer));
94102
parser.removeErrorListeners();
103+
parser.addErrorListener(err);
95104

96105
var visitor = new PiccodeVisitor(file);
97106

@@ -134,4 +143,19 @@ private static void addSystemFunctions() {
134143
PiccodeFileModule.addFunctions();
135144
}
136145

146+
147+
private static class SyntaxError extends BaseErrorListener {
148+
149+
public String file;
150+
151+
public SyntaxError(String file) {
152+
this.file = file;
153+
}
154+
155+
156+
@Override
157+
public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine, String msg, RecognitionException e) {
158+
throw new PiccodeException(file, line, charPositionInLine, msg);
159+
}
160+
}
137161
}

src/main/java/org/piccode/parser/Lexer.java

Lines changed: 0 additions & 296 deletions
This file was deleted.

0 commit comments

Comments
 (0)