33
33
34
34
import org .antlr .v4 .runtime .CharStreams ;
35
35
import org .antlr .v4 .runtime .CodePointCharStream ;
36
- import org .antlr .v4 .runtime .NoViableAltException ;
37
36
import org .antlr .v4 .runtime .ParserRuleContext ;
38
- import org .antlr .v4 .runtime .RecognitionException ;
39
- import org .antlr .v4 .runtime .Token ;
40
37
41
38
import com .oracle .graal .python .PythonLanguage ;
42
39
import com .oracle .graal .python .nodes .PNode ;
40
+ import com .oracle .graal .python .parser .antlr .Builder ;
43
41
import com .oracle .graal .python .parser .antlr .Python3Parser ;
44
42
import com .oracle .graal .python .runtime .PythonCore ;
45
43
import com .oracle .graal .python .runtime .PythonParseResult ;
56
54
public final class PythonParserImpl implements PythonParser {
57
55
private static final Map <String , ParserRuleContext > cachedParseTrees = new HashMap <>();
58
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
+
59
69
@ TruffleBoundary
60
70
private static ParserRuleContext preParseWithAntlr (PythonCore core , Source source ) {
61
71
String path = source .getURI ().toString ();
62
72
String [] pathParts = path .split (Pattern .quote (PythonCore .FILE_SEPARATOR ));
63
73
String fileDirAndName = pathParts [pathParts .length - 2 ] + PythonCore .FILE_SEPARATOR + pathParts [pathParts .length - 1 ];
64
74
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 );
66
76
ParserRuleContext input ;
67
77
if (!core .isInitialized ()) {
68
78
input = cachedParseTrees .get (fileDirAndName );
@@ -82,23 +92,12 @@ private static ParserRuleContext preParseWithAntlr(PythonCore core, Source sourc
82
92
parser .reset ();
83
93
input = parser .eval_input ();
84
94
} catch (Throwable e2 ) {
85
- int line = -1 ;
86
- int column = -1 ;
87
95
if (source .isInteractive () && e instanceof PIncompleteSourceException ) {
88
96
((PIncompleteSourceException ) e ).setSource (source );
89
97
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 ());
100
98
}
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 ());
102
101
}
103
102
}
104
103
}
@@ -107,7 +106,7 @@ private static ParserRuleContext preParseWithAntlr(PythonCore core, Source sourc
107
106
108
107
@ TruffleBoundary
109
108
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 ());
111
110
ParserRuleContext input ;
112
111
try {
113
112
input = parser .single_input ();
@@ -116,20 +115,8 @@ private static ParserRuleContext preParseInlineWithAntlr(PythonCore core, Source
116
115
parser .reset ();
117
116
input = parser .eval_input ();
118
117
} 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 ());
133
120
}
134
121
}
135
122
return input ;
@@ -164,7 +151,7 @@ public PNode parseInline(PythonCore core, Source source, Frame curFrame) {
164
151
@ Override
165
152
@ TruffleBoundary
166
153
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 );
168
155
ParserRuleContext input ;
169
156
try {
170
157
input = parser .eval_input ();
@@ -179,7 +166,7 @@ public PythonParseResult parseEval(PythonCore core, String expression, String fi
179
166
@ Override
180
167
@ TruffleBoundary
181
168
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 );
183
170
ParserRuleContext input ;
184
171
try {
185
172
input = parser .file_input ();
@@ -193,7 +180,7 @@ public PythonParseResult parseExec(PythonCore core, String expression, String fi
193
180
@ Override
194
181
@ TruffleBoundary
195
182
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 );
197
184
ParserRuleContext input ;
198
185
try {
199
186
input = parser .single_input ();
@@ -207,7 +194,7 @@ public PythonParseResult parseSingle(PythonCore core, String expression, String
207
194
@ Override
208
195
@ TruffleBoundary
209
196
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 );
211
198
Python3Parser .AtomContext input ;
212
199
try {
213
200
input = parser .atom ();
@@ -218,14 +205,7 @@ public boolean isIdentifier(PythonCore core, String snippet) {
218
205
}
219
206
220
207
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 ());
229
209
}
230
210
231
211
private static PythonParseResult translateParseResult (PythonCore core , String name , ParserRuleContext input , Source source ) {
0 commit comments