3838 */
3939package 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-
4441import org .antlr .v4 .runtime .BaseErrorListener ;
4542import org .antlr .v4 .runtime .RecognitionException ;
4643import org .antlr .v4 .runtime .Recognizer ;
44+ import org .antlr .v4 .runtime .Token ;
4745import 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