38
38
*/
39
39
package com .oracle .graal .python .parser .antlr ;
40
40
41
- import com .oracle .graal .python .runtime .PythonParser .PIncompleteSourceException ;
42
- import com .oracle .truffle .api .CompilerDirectives .TruffleBoundary ;
43
-
44
41
import org .antlr .v4 .runtime .BaseErrorListener ;
45
42
import org .antlr .v4 .runtime .RecognitionException ;
46
43
import org .antlr .v4 .runtime .Recognizer ;
44
+ import org .antlr .v4 .runtime .Token ;
47
45
import org .antlr .v4 .runtime .misc .IntervalSet ;
48
46
47
+ import com .oracle .graal .python .runtime .PythonParser .PIncompleteSourceException ;
48
+ import com .oracle .truffle .api .CompilerDirectives .TruffleBoundary ;
49
+
49
50
/**
50
51
* An error listener that immediately bails out of the parse (does not recover) and throws a runtime
51
52
* exception with a descriptive error message.
@@ -73,7 +74,9 @@ public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol,
73
74
throw handleRecognitionException ;
74
75
}
75
76
}
76
-
77
+ if (offendingSymbol instanceof Token ) {
78
+ throw new RuntimeException (entireMessage , new EmptyRecognitionException (entireMessage , recognizer , (Token ) offendingSymbol ));
79
+ }
77
80
throw new RuntimeException (entireMessage , e );
78
81
}
79
82
@@ -83,4 +86,19 @@ private static PIncompleteSourceException handleRecognitionException(IntervalSet
83
86
}
84
87
return null ;
85
88
}
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
+ }
86
104
}
0 commit comments