3333
3434import org .antlr .v4 .runtime .CharStreams ;
3535import org .antlr .v4 .runtime .CodePointCharStream ;
36- import org .antlr .v4 .runtime .NoViableAltException ;
3736import org .antlr .v4 .runtime .ParserRuleContext ;
38- import org .antlr .v4 .runtime .RecognitionException ;
39- import org .antlr .v4 .runtime .Token ;
4037
4138import com .oracle .graal .python .PythonLanguage ;
4239import com .oracle .graal .python .nodes .PNode ;
40+ import com .oracle .graal .python .parser .antlr .Builder ;
4341import com .oracle .graal .python .parser .antlr .Python3Parser ;
4442import com .oracle .graal .python .runtime .PythonCore ;
4543import com .oracle .graal .python .runtime .PythonParseResult ;
5654public final class PythonParserImpl implements PythonParser {
5755 private static final Map <String , ParserRuleContext > cachedParseTrees = new HashMap <>();
5856
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+
5969 @ TruffleBoundary
6070 private static ParserRuleContext preParseWithAntlr (PythonCore core , Source source ) {
6171 String path = source .getURI ().toString ();
6272 String [] pathParts = path .split (Pattern .quote (PythonCore .FILE_SEPARATOR ));
6373 String fileDirAndName = pathParts [pathParts .length - 2 ] + PythonCore .FILE_SEPARATOR + pathParts [pathParts .length - 1 ];
6474 CodePointCharStream fromString = CharStreams .fromString (source .getCharacters ().toString (), fileDirAndName );
65- Python3Parser parser = new com . oracle . graal . python . parser . antlr . Builder . Parser (fromString ). build ( );
75+ Python3Parser parser = getPython3Parser (fromString );
6676 ParserRuleContext input ;
6777 if (!core .isInitialized ()) {
6878 input = cachedParseTrees .get (fileDirAndName );
@@ -82,23 +92,12 @@ private static ParserRuleContext preParseWithAntlr(PythonCore core, Source sourc
8292 parser .reset ();
8393 input = parser .eval_input ();
8494 } catch (Throwable e2 ) {
85- int line = -1 ;
86- int column = -1 ;
8795 if (source .isInteractive () && e instanceof PIncompleteSourceException ) {
8896 ((PIncompleteSourceException ) e ).setSource (source );
8997 throw e ;
90- } else if (e instanceof RecognitionException ) {
91- Token token = ((RecognitionException ) e ).getOffendingToken ();
92- line = token .getLine ();
93- column = token .getCharPositionInLine ();
94- } else if (e .getCause () instanceof RecognitionException ) {
95- Token token = ((RecognitionException ) e .getCause ()).getOffendingToken ();
96- line = token .getLine ();
97- column = token .getCharPositionInLine ();
98- } else {
99- throw core .raise (SyntaxError , e .getMessage ());
10098 }
101- throw core .raise (SyntaxError , getLocation (source , line ), "invalid syntax: line %d, column %d. " , line , column );
99+ Node location = getLocation (source , PythonErrorStrategy .getLine (e ));
100+ throw core .raise (SyntaxError , location , e .getMessage ());
102101 }
103102 }
104103 }
@@ -107,7 +106,7 @@ private static ParserRuleContext preParseWithAntlr(PythonCore core, Source sourc
107106
108107 @ TruffleBoundary
109108 private static ParserRuleContext preParseInlineWithAntlr (PythonCore core , Source source ) {
110- Python3Parser parser = new com . oracle . graal . python . parser . antlr . Builder . Parser (source .getCharacters ().toString ()). build ( );
109+ Python3Parser parser = getPython3Parser (source .getCharacters ().toString ());
111110 ParserRuleContext input ;
112111 try {
113112 input = parser .single_input ();
@@ -116,20 +115,8 @@ private static ParserRuleContext preParseInlineWithAntlr(PythonCore core, Source
116115 parser .reset ();
117116 input = parser .eval_input ();
118117 } catch (Throwable e2 ) {
119- int line = -1 ;
120- int column = -1 ;
121- if (e instanceof RecognitionException ) {
122- Token token = ((RecognitionException ) e ).getOffendingToken ();
123- line = token .getLine ();
124- column = token .getCharPositionInLine ();
125- } else if (e .getCause () instanceof NoViableAltException ) {
126- Token token = ((NoViableAltException ) e .getCause ()).getOffendingToken ();
127- line = token .getLine ();
128- column = token .getCharPositionInLine ();
129- } else {
130- throw core .raise (SyntaxError , e .getMessage ());
131- }
132- throw core .raise (SyntaxError , getLocation (source , line ), "invalid syntax: line %d, column %d. " , line , column );
118+ Node location = getLocation (source , PythonErrorStrategy .getLine (e ));
119+ throw core .raise (SyntaxError , location , e .getMessage ());
133120 }
134121 }
135122 return input ;
@@ -164,7 +151,7 @@ public PNode parseInline(PythonCore core, Source source, Frame curFrame) {
164151 @ Override
165152 @ TruffleBoundary
166153 public PythonParseResult parseEval (PythonCore core , String expression , String filename ) {
167- Python3Parser parser = new com . oracle . graal . python . parser . antlr . Builder . Parser (expression ). build ( );
154+ Python3Parser parser = getPython3Parser (expression );
168155 ParserRuleContext input ;
169156 try {
170157 input = parser .eval_input ();
@@ -179,7 +166,7 @@ public PythonParseResult parseEval(PythonCore core, String expression, String fi
179166 @ Override
180167 @ TruffleBoundary
181168 public PythonParseResult parseExec (PythonCore core , String expression , String filename ) {
182- Python3Parser parser = new com . oracle . graal . python . parser . antlr . Builder . Parser (expression ). build ( );
169+ Python3Parser parser = getPython3Parser (expression );
183170 ParserRuleContext input ;
184171 try {
185172 input = parser .file_input ();
@@ -193,7 +180,7 @@ public PythonParseResult parseExec(PythonCore core, String expression, String fi
193180 @ Override
194181 @ TruffleBoundary
195182 public PythonParseResult parseSingle (PythonCore core , String expression , String filename ) {
196- Python3Parser parser = new com . oracle . graal . python . parser . antlr . Builder . Parser (expression ). build ( );
183+ Python3Parser parser = getPython3Parser (expression );
197184 ParserRuleContext input ;
198185 try {
199186 input = parser .single_input ();
@@ -207,7 +194,7 @@ public PythonParseResult parseSingle(PythonCore core, String expression, String
207194 @ Override
208195 @ TruffleBoundary
209196 public boolean isIdentifier (PythonCore core , String snippet ) {
210- Python3Parser parser = new com . oracle . graal . python . parser . antlr . Builder . Parser (snippet ). build ( );
197+ Python3Parser parser = getPython3Parser (snippet );
211198 Python3Parser .AtomContext input ;
212199 try {
213200 input = parser .atom ();
@@ -218,14 +205,7 @@ public boolean isIdentifier(PythonCore core, String snippet) {
218205 }
219206
220207 private static PException handleParserError (PythonCore core , Throwable e ) {
221- if (e instanceof RecognitionException ) {
222- Token token = ((RecognitionException ) e ).getOffendingToken ();
223- int line = token .getLine ();
224- int column = token .getCharPositionInLine ();
225- return core .raise (SyntaxError , "parser error at %d:%d\n %s" , line , column , e .toString ());
226- } else {
227- return core .raise (SyntaxError , e .getMessage ());
228- }
208+ return core .raise (SyntaxError , e .getMessage ());
229209 }
230210
231211 private static PythonParseResult translateParseResult (PythonCore core , String name , ParserRuleContext input , Source source ) {
0 commit comments