|
2 | 2 |
|
3 | 3 | import java.util.ArrayList; |
4 | 4 | import java.util.List; |
| 5 | +import org.antlr.v4.runtime.BaseErrorListener; |
5 | 6 | import org.antlr.v4.runtime.CharStreams; |
6 | 7 | import org.antlr.v4.runtime.CommonTokenStream; |
| 8 | +import org.antlr.v4.runtime.RecognitionException; |
| 9 | +import org.antlr.v4.runtime.Recognizer; |
7 | 10 | import org.piccode.antlr4.PiccodeScriptLexer; |
8 | 11 | import org.piccode.antlr4.PiccodeScriptParser; |
9 | 12 | import org.piccode.ast.Ast; |
@@ -88,10 +91,16 @@ public static StatementList program(String file, String code) { |
88 | 91 | code = sanitizeSourceFile(code); |
89 | 92 | } |
90 | 93 |
|
| 94 | + var err = new SyntaxError(file); |
| 95 | + |
91 | 96 | var lexer = new PiccodeScriptLexer(CharStreams.fromString(code)); |
92 | | - var parser = new PiccodeScriptParser(new CommonTokenStream(lexer)); |
93 | 97 | lexer.removeErrorListeners(); |
| 98 | + lexer.addErrorListener(err); |
| 99 | + |
| 100 | + |
| 101 | + var parser = new PiccodeScriptParser(new CommonTokenStream(lexer)); |
94 | 102 | parser.removeErrorListeners(); |
| 103 | + parser.addErrorListener(err); |
95 | 104 |
|
96 | 105 | var visitor = new PiccodeVisitor(file); |
97 | 106 |
|
@@ -134,4 +143,19 @@ private static void addSystemFunctions() { |
134 | 143 | PiccodeFileModule.addFunctions(); |
135 | 144 | } |
136 | 145 |
|
| 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 | + } |
137 | 161 | } |
0 commit comments