3333
3434import org .antlr .v4 .runtime .CharStreams ;
3535import org .antlr .v4 .runtime .CodePointCharStream ;
36+ import org .antlr .v4 .runtime .NoViableAltException ;
3637import org .antlr .v4 .runtime .ParserRuleContext ;
38+ import org .antlr .v4 .runtime .RecognitionException ;
39+ import org .antlr .v4 .runtime .Token ;
3740
3841import com .oracle .graal .python .PythonLanguage ;
3942import com .oracle .graal .python .nodes .PNode ;
@@ -92,11 +95,20 @@ private static ParserRuleContext preParseWithAntlr(PythonCore core, Source sourc
9295 parser .reset ();
9396 input = parser .eval_input ();
9497 } catch (Throwable e2 ) {
98+ int line ;
9599 if (source .isInteractive () && e instanceof PIncompleteSourceException ) {
96100 ((PIncompleteSourceException ) e ).setSource (source );
97101 throw e ;
102+ } else if (e instanceof RecognitionException ) {
103+ Token token = ((RecognitionException ) e ).getOffendingToken ();
104+ line = token .getLine ();
105+ } else if (e .getCause () instanceof NoViableAltException ) {
106+ Token token = ((NoViableAltException ) e .getCause ()).getOffendingToken ();
107+ line = token .getLine ();
108+ } else {
109+ throw core .raise (SyntaxError , e .getMessage ());
98110 }
99- throw core .raise (SyntaxError , e .getMessage ());
111+ throw core .raise (SyntaxError , getLocation ( source , line ), e .getMessage ());
100112 }
101113 }
102114 }
@@ -114,7 +126,17 @@ private static ParserRuleContext preParseInlineWithAntlr(PythonCore core, Source
114126 parser .reset ();
115127 input = parser .eval_input ();
116128 } catch (Throwable e2 ) {
117- throw core .raise (SyntaxError , e .getMessage ());
129+ int line ;
130+ if (e instanceof RecognitionException ) {
131+ Token token = ((RecognitionException ) e ).getOffendingToken ();
132+ line = token .getLine ();
133+ } else if (e .getCause () instanceof NoViableAltException ) {
134+ Token token = ((NoViableAltException ) e .getCause ()).getOffendingToken ();
135+ line = token .getLine ();
136+ } else {
137+ throw core .raise (SyntaxError , e .getMessage ());
138+ }
139+ throw core .raise (SyntaxError , getLocation (source , line ), e .getMessage ());
118140 }
119141 }
120142 return input ;
0 commit comments