28
28
import static com .oracle .graal .python .runtime .exception .PythonErrorType .SyntaxError ;
29
29
30
30
import java .util .HashMap ;
31
- import java .util .InputMismatchException ;
32
31
import java .util .Map ;
33
32
import java .util .regex .Pattern ;
34
33
35
34
import org .antlr .v4 .runtime .CharStreams ;
36
35
import org .antlr .v4 .runtime .CodePointCharStream ;
37
- import org .antlr .v4 .runtime .NoViableAltException ;
38
36
import org .antlr .v4 .runtime .ParserRuleContext ;
39
- import org .antlr .v4 .runtime .RecognitionException ;
40
- import org .antlr .v4 .runtime .Token ;
41
37
42
38
import com .oracle .graal .python .PythonLanguage ;
43
39
import com .oracle .graal .python .nodes .PNode ;
40
+ import com .oracle .graal .python .parser .antlr .Builder ;
44
41
import com .oracle .graal .python .parser .antlr .Python3Parser ;
45
42
import com .oracle .graal .python .runtime .PythonCore ;
46
43
import com .oracle .graal .python .runtime .PythonParseResult ;
57
54
public final class PythonParserImpl implements PythonParser {
58
55
private static final Map <String , ParserRuleContext > cachedParseTrees = new HashMap <>();
59
56
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
+
60
69
@ TruffleBoundary
61
70
private static ParserRuleContext preParseWithAntlr (PythonCore core , Source source ) {
62
71
String path = source .getURI ().toString ();
63
72
String [] pathParts = path .split (Pattern .quote (PythonCore .FILE_SEPARATOR ));
64
73
String fileDirAndName = pathParts [pathParts .length - 2 ] + PythonCore .FILE_SEPARATOR + pathParts [pathParts .length - 1 ];
65
74
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 );
68
76
ParserRuleContext input ;
69
77
if (!core .isInitialized ()) {
70
78
input = cachedParseTrees .get (fileDirAndName );
@@ -84,23 +92,11 @@ private static ParserRuleContext preParseWithAntlr(PythonCore core, Source sourc
84
92
parser .reset ();
85
93
input = parser .eval_input ();
86
94
} catch (Throwable e2 ) {
87
- int line = -1 ;
88
- int column = -1 ;
89
95
if (source .isInteractive () && e instanceof PIncompleteSourceException ) {
90
96
((PIncompleteSourceException ) e ).setSource (source );
91
97
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 ());
102
98
}
103
- throw core .raise (SyntaxError , getLocation ( source , line ), "invalid syntax: line %d, column %d. " , line , column );
99
+ throw core .raise (SyntaxError , e . getMessage () );
104
100
}
105
101
}
106
102
}
@@ -109,7 +105,7 @@ private static ParserRuleContext preParseWithAntlr(PythonCore core, Source sourc
109
105
110
106
@ TruffleBoundary
111
107
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 ());
113
109
ParserRuleContext input ;
114
110
try {
115
111
input = parser .single_input ();
@@ -118,20 +114,7 @@ private static ParserRuleContext preParseInlineWithAntlr(PythonCore core, Source
118
114
parser .reset ();
119
115
input = parser .eval_input ();
120
116
} 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 ());
135
118
}
136
119
}
137
120
return input ;
@@ -166,7 +149,7 @@ public PNode parseInline(PythonCore core, Source source, Frame curFrame) {
166
149
@ Override
167
150
@ TruffleBoundary
168
151
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 );
170
153
ParserRuleContext input ;
171
154
try {
172
155
input = parser .eval_input ();
@@ -181,7 +164,7 @@ public PythonParseResult parseEval(PythonCore core, String expression, String fi
181
164
@ Override
182
165
@ TruffleBoundary
183
166
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 );
185
168
ParserRuleContext input ;
186
169
try {
187
170
input = parser .file_input ();
@@ -195,7 +178,7 @@ public PythonParseResult parseExec(PythonCore core, String expression, String fi
195
178
@ Override
196
179
@ TruffleBoundary
197
180
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 );
199
182
ParserRuleContext input ;
200
183
try {
201
184
input = parser .single_input ();
@@ -209,7 +192,7 @@ public PythonParseResult parseSingle(PythonCore core, String expression, String
209
192
@ Override
210
193
@ TruffleBoundary
211
194
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 );
213
196
Python3Parser .AtomContext input ;
214
197
try {
215
198
input = parser .atom ();
@@ -220,14 +203,7 @@ public boolean isIdentifier(PythonCore core, String snippet) {
220
203
}
221
204
222
205
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 ());
231
207
}
232
208
233
209
private static PythonParseResult translateParseResult (PythonCore core , String name , ParserRuleContext input , Source source ) {
0 commit comments