Skip to content

Commit 6b73d90

Browse files
numberpicosminbasca
authored andcommitted
fix empty RecogitionException to get syntax error position
1 parent 4636249 commit 6b73d90

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/parser/antlr/DescriptiveBailErrorListener.java

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,15 @@
3838
*/
3939
package com.oracle.graal.python.parser.antlr;
4040

41-
import com.oracle.graal.python.runtime.PythonParser.PIncompleteSourceException;
42-
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
43-
4441
import org.antlr.v4.runtime.BaseErrorListener;
4542
import org.antlr.v4.runtime.RecognitionException;
4643
import org.antlr.v4.runtime.Recognizer;
44+
import org.antlr.v4.runtime.Token;
4745
import org.antlr.v4.runtime.misc.IntervalSet;
4846

47+
import com.oracle.graal.python.runtime.PythonParser.PIncompleteSourceException;
48+
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
49+
4950
/**
5051
* An error listener that immediately bails out of the parse (does not recover) and throws a runtime
5152
* exception with a descriptive error message.
@@ -73,7 +74,9 @@ public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol,
7374
throw handleRecognitionException;
7475
}
7576
}
76-
77+
if (offendingSymbol instanceof Token) {
78+
throw new RuntimeException(entireMessage, new EmptyRecognitionException(entireMessage, recognizer, (Token) offendingSymbol));
79+
}
7780
throw new RuntimeException(entireMessage, e);
7881
}
7982

@@ -83,4 +86,19 @@ private static PIncompleteSourceException handleRecognitionException(IntervalSet
8386
}
8487
return null;
8588
}
89+
90+
private static class EmptyRecognitionException extends RecognitionException {
91+
private static final long serialVersionUID = 1L;
92+
private Token offendingToken;
93+
94+
public EmptyRecognitionException(String message, Recognizer<?, ?> recognizer, Token offendingToken) {
95+
super(message, recognizer, offendingToken.getInputStream(), null);
96+
this.offendingToken = offendingToken;
97+
}
98+
99+
@Override
100+
public Token getOffendingToken() {
101+
return offendingToken;
102+
}
103+
}
86104
}

0 commit comments

Comments
 (0)