2828import static com .oracle .graal .python .runtime .exception .PythonErrorType .SyntaxError ;
2929
3030import java .util .HashMap ;
31- import java .util .InputMismatchException ;
3231import java .util .Map ;
3332import java .util .regex .Pattern ;
3433
3534import org .antlr .v4 .runtime .CharStreams ;
3635import org .antlr .v4 .runtime .CodePointCharStream ;
37- import org .antlr .v4 .runtime .NoViableAltException ;
3836import org .antlr .v4 .runtime .ParserRuleContext ;
39- import org .antlr .v4 .runtime .RecognitionException ;
40- import org .antlr .v4 .runtime .Token ;
4137
4238import com .oracle .graal .python .PythonLanguage ;
4339import com .oracle .graal .python .nodes .PNode ;
40+ import com .oracle .graal .python .parser .antlr .Builder ;
4441import com .oracle .graal .python .parser .antlr .Python3Parser ;
4542import com .oracle .graal .python .runtime .PythonCore ;
4643import com .oracle .graal .python .runtime .PythonParseResult ;
5754public final class PythonParserImpl implements PythonParser {
5855 private static final Map <String , ParserRuleContext > cachedParseTrees = new HashMap <>();
5956
57+ private static Python3Parser getPython3Parser (CodePointCharStream fromString ) {
58+ Python3Parser parser = new Builder .Parser (fromString ).build ();
59+ parser .setErrorHandler (new PythonErrorStrategy ());
60+ return parser ;
61+ }
62+
63+ private static Python3Parser getPython3Parser (String string ) {
64+ Python3Parser parser = new Builder .Parser (string ).build ();
65+ parser .setErrorHandler (new PythonErrorStrategy ());
66+ return parser ;
67+ }
68+
6069 @ TruffleBoundary
6170 private static ParserRuleContext preParseWithAntlr (PythonCore core , Source source ) {
6271 String path = source .getURI ().toString ();
6372 String [] pathParts = path .split (Pattern .quote (PythonCore .FILE_SEPARATOR ));
6473 String fileDirAndName = pathParts [pathParts .length - 2 ] + PythonCore .FILE_SEPARATOR + pathParts [pathParts .length - 1 ];
6574 CodePointCharStream fromString = CharStreams .fromString (source .getCharacters ().toString (), fileDirAndName );
66- Python3Parser parser = new com .oracle .graal .python .parser .antlr .Builder .Parser (fromString ).build ();
67- parser .setErrorHandler (new PythonErrorStrategy ());
75+ Python3Parser parser = getPython3Parser (fromString );
6876 ParserRuleContext input ;
6977 if (!core .isInitialized ()) {
7078 input = cachedParseTrees .get (fileDirAndName );
@@ -84,23 +92,11 @@ private static ParserRuleContext preParseWithAntlr(PythonCore core, Source sourc
8492 parser .reset ();
8593 input = parser .eval_input ();
8694 } catch (Throwable e2 ) {
87- int line = -1 ;
88- int column = -1 ;
8995 if (source .isInteractive () && e instanceof PIncompleteSourceException ) {
9096 ((PIncompleteSourceException ) e ).setSource (source );
9197 throw e ;
92- } else if (e instanceof RecognitionException ) {
93- Token token = ((RecognitionException ) e ).getOffendingToken ();
94- line = token .getLine ();
95- column = token .getCharPositionInLine ();
96- } else if (e .getCause () instanceof RecognitionException ) {
97- Token token = ((RecognitionException ) e .getCause ()).getOffendingToken ();
98- line = token .getLine ();
99- column = token .getCharPositionInLine ();
100- } else {
101- throw core .raise (SyntaxError , e .getMessage ());
10298 }
103- throw core .raise (SyntaxError , getLocation ( source , line ), "invalid syntax: line %d, column %d. " , line , column );
99+ throw core .raise (SyntaxError , e . getMessage () );
104100 }
105101 }
106102 }
@@ -109,7 +105,7 @@ private static ParserRuleContext preParseWithAntlr(PythonCore core, Source sourc
109105
110106 @ TruffleBoundary
111107 private static ParserRuleContext preParseInlineWithAntlr (PythonCore core , Source source ) {
112- Python3Parser parser = new com . oracle . graal . python . parser . antlr . Builder . Parser (source .getCharacters ().toString ()). build ( );
108+ Python3Parser parser = getPython3Parser (source .getCharacters ().toString ());
113109 ParserRuleContext input ;
114110 try {
115111 input = parser .single_input ();
@@ -118,20 +114,7 @@ private static ParserRuleContext preParseInlineWithAntlr(PythonCore core, Source
118114 parser .reset ();
119115 input = parser .eval_input ();
120116 } catch (Throwable e2 ) {
121- int line = -1 ;
122- int column = -1 ;
123- if (e instanceof RecognitionException ) {
124- Token token = ((RecognitionException ) e ).getOffendingToken ();
125- line = token .getLine ();
126- column = token .getCharPositionInLine ();
127- } else if (e .getCause () instanceof NoViableAltException ) {
128- Token token = ((NoViableAltException ) e .getCause ()).getOffendingToken ();
129- line = token .getLine ();
130- column = token .getCharPositionInLine ();
131- } else {
132- throw core .raise (SyntaxError , e .getMessage ());
133- }
134- throw core .raise (SyntaxError , getLocation (source , line ), "invalid syntax: line %d, column %d. " , line , column );
117+ throw core .raise (SyntaxError , e .getMessage ());
135118 }
136119 }
137120 return input ;
@@ -166,7 +149,7 @@ public PNode parseInline(PythonCore core, Source source, Frame curFrame) {
166149 @ Override
167150 @ TruffleBoundary
168151 public PythonParseResult parseEval (PythonCore core , String expression , String filename ) {
169- Python3Parser parser = new com . oracle . graal . python . parser . antlr . Builder . Parser (expression ). build ( );
152+ Python3Parser parser = getPython3Parser (expression );
170153 ParserRuleContext input ;
171154 try {
172155 input = parser .eval_input ();
@@ -181,7 +164,7 @@ public PythonParseResult parseEval(PythonCore core, String expression, String fi
181164 @ Override
182165 @ TruffleBoundary
183166 public PythonParseResult parseExec (PythonCore core , String expression , String filename ) {
184- Python3Parser parser = new com . oracle . graal . python . parser . antlr . Builder . Parser (expression ). build ( );
167+ Python3Parser parser = getPython3Parser (expression );
185168 ParserRuleContext input ;
186169 try {
187170 input = parser .file_input ();
@@ -195,7 +178,7 @@ public PythonParseResult parseExec(PythonCore core, String expression, String fi
195178 @ Override
196179 @ TruffleBoundary
197180 public PythonParseResult parseSingle (PythonCore core , String expression , String filename ) {
198- Python3Parser parser = new com . oracle . graal . python . parser . antlr . Builder . Parser (expression ). build ( );
181+ Python3Parser parser = getPython3Parser (expression );
199182 ParserRuleContext input ;
200183 try {
201184 input = parser .single_input ();
@@ -209,7 +192,7 @@ public PythonParseResult parseSingle(PythonCore core, String expression, String
209192 @ Override
210193 @ TruffleBoundary
211194 public boolean isIdentifier (PythonCore core , String snippet ) {
212- Python3Parser parser = new com . oracle . graal . python . parser . antlr . Builder . Parser (snippet ). build ( );
195+ Python3Parser parser = getPython3Parser (snippet );
213196 Python3Parser .AtomContext input ;
214197 try {
215198 input = parser .atom ();
@@ -220,14 +203,7 @@ public boolean isIdentifier(PythonCore core, String snippet) {
220203 }
221204
222205 private static PException handleParserError (PythonCore core , Throwable e ) {
223- if (e instanceof RecognitionException ) {
224- Token token = ((RecognitionException ) e ).getOffendingToken ();
225- int line = token .getLine ();
226- int column = token .getCharPositionInLine ();
227- return core .raise (SyntaxError , "parser error at %d:%d\n %s" , line , column , e .toString ());
228- } else {
229- return core .raise (SyntaxError , e .getMessage ());
230- }
206+ return core .raise (SyntaxError , e .getMessage ());
231207 }
232208
233209 private static PythonParseResult translateParseResult (PythonCore core , String name , ParserRuleContext input , Source source ) {
0 commit comments