-
Notifications
You must be signed in to change notification settings - Fork 226
Expand file tree
/
Copy pathParserFacade.java
More file actions
468 lines (431 loc) · 17 KB
/
ParserFacade.java
File metadata and controls
468 lines (431 loc) · 17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
// Copyright (c) Corporation for National Research Initiatives
package org.python.core;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringReader;
import java.nio.ByteBuffer;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CodingErrorAction;
import java.nio.charset.UnsupportedCharsetException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.antlr.runtime.CharStream;
import org.antlr.runtime.CommonTokenStream;
import org.python.antlr.BaseParser;
import org.python.antlr.NoCloseReaderStream;
import org.python.antlr.ParseException;
import org.python.antlr.PythonPartialLexer;
import org.python.antlr.PythonPartialParser;
import org.python.antlr.PythonTokenSource;
import org.python.antlr.PythonTree;
import org.python.antlr.base.mod;
import org.python.core.io.StreamIO;
import org.python.core.io.TextIOInputStream;
import org.python.core.io.UniversalIOWrapper;
import org.python.core.util.StringUtil;
/**
* Facade for the classes in the org.python.antlr package.
*/
public class ParserFacade {
private static int MARK_LIMIT = 100000;
private ParserFacade() {}
private static String getLine(ExpectedEncodingBufferedReader reader, int line) {
if (reader == null) {
return "";
}
String text = null;
try {
for (int i = 0; i < line; i++) {
text = reader.readLine();
}
if (text == null) {
return text;
}
if (reader.encoding != null) {
// The parser used a non-latin encoding: re-encode chars to bytes.
Charset cs = Charset.forName(reader.encoding);
ByteBuffer decoded = cs.encode(text);
text = StringUtil.fromBytes(decoded);
}
return text + "\n";
} catch (IOException ioe) {
}
return text;
}
// if reader != null, reset it
public static PyException fixParseError(ExpectedEncodingBufferedReader reader,
Throwable t,
String filename) {
if (reader != null) {
try {
reader.reset();
} catch (IOException e) {
reader = null;
}
}
if (t instanceof ParseException) {
ParseException e = (ParseException)t;
PythonTree node = (PythonTree)e.node;
int line=e.line;
int col=e.charPositionInLine;
if (node != null) {
line = node.getLineno();
col = node.getCol_offset();
}
String text= getLine(reader, line);
String msg = e.getMessage();
if (e.getType() == Py.IndentationError) {
return new PyIndentationError(msg, line, col, text, filename);
}
PyException pye = new PySyntaxError(msg, line, col, text, filename);
if (e.definite) {
// The error cannot be fixed by reading more input
throw pye;
} else {
// The error might be fixed by reading more input
return pye;
}
} else if (t instanceof CharacterCodingException) {
String msg;
if (reader.encoding == null) {
msg = "Non-ASCII character in file '" + filename + "', but no encoding declared"
+ "; see http://www.python.org/peps/pep-0263.html for details";
} else {
msg = "Illegal character in file '" + filename + "' for encoding '"
+ reader.encoding + "'";
}
throw Py.SyntaxError(msg);
} else {
return Py.JavaError(t);
}
}
/**
* Parse Python source as either an expression (if possible) or module.
*
* Designed for use by a JSR 223 implementation: "the Scripting API does not distinguish
* between scripts which return values and those which do not, nor do they make the
* corresponding distinction between evaluating or executing objects." (SCR.4.2.1)
*/
public static mod parseExpressionOrModule(Reader reader,
String filename,
CompilerFlags cflags) {
ExpectedEncodingBufferedReader bufReader = null;
try {
bufReader = prepBufReader(reader, cflags, filename);
// first, try parsing as an expression
return parse(bufReader, CompileMode.eval, filename, cflags);
} catch (Throwable t) {
if (bufReader == null)
{
throw Py.JavaError(t); // can't do any more
}
try {
// then, try parsing as a module
bufReader.reset();
return parse(bufReader, CompileMode.exec, filename, cflags);
} catch (Throwable tt) {
throw fixParseError(bufReader, tt, filename);
}
}
}
/**
* Internal parser entry point.
*
* Users of this method should call fixParseError on any Throwable thrown
* from it, to translate ParserExceptions into PySyntaxErrors or
* PyIndentationErrors.
*/
private static mod parse(ExpectedEncodingBufferedReader reader,
CompileMode kind,
String filename,
CompilerFlags cflags) throws Throwable {
reader.mark(MARK_LIMIT); // We need the ability to move back on the
// reader, for the benefit of fixParseError and
// validPartialSentence
if (kind != null) {
CharStream cs = new NoCloseReaderStream(reader);
BaseParser parser = new BaseParser(cs, filename, cflags);
return kind.dispatch(parser);
} else {
throw Py.ValueError("parse kind must be eval, exec, or single");
}
}
public static mod parse(Reader reader,
CompileMode kind,
String filename,
CompilerFlags cflags) {
ExpectedEncodingBufferedReader bufReader = null;
try {
bufReader = prepBufReader(reader, cflags, filename);
return parse(bufReader, kind, filename, cflags );
} catch (Throwable t) {
throw fixParseError(bufReader, t, filename);
} finally {
close(bufReader);
}
}
public static mod parse(InputStream stream,
CompileMode kind,
String filename,
CompilerFlags cflags) {
ExpectedEncodingBufferedReader bufReader = null;
try {
// prepBufReader takes care of encoding detection and universal
// newlines:
bufReader = prepBufReader(stream, cflags, filename, false);
return parse(bufReader, kind, filename, cflags );
} catch (Throwable t) {
throw fixParseError(bufReader, t, filename);
} finally {
close(bufReader);
}
}
public static mod parse(String string,
CompileMode kind,
String filename,
CompilerFlags cflags) {
ExpectedEncodingBufferedReader bufReader = null;
try {
bufReader = prepBufReader(string, cflags, filename);
return parse(bufReader, kind, filename, cflags);
} catch (Throwable t) {
throw fixParseError(bufReader, t, filename);
} finally {
close(bufReader);
}
}
public static mod partialParse(String string,
CompileMode kind,
String filename,
CompilerFlags cflags,
boolean stdprompt) {
// XXX: What's the idea of the stdprompt argument?
ExpectedEncodingBufferedReader reader = null;
try {
reader = prepBufReader(string, cflags, filename);
return parse(reader, kind, filename, cflags);
} catch (Throwable t) {
PyException p = fixParseError(reader, t, filename);
if (reader != null && validPartialSentence(reader, kind, filename)) {
return null;
}
throw p;
} finally {
close(reader);
}
}
private static boolean validPartialSentence(BufferedReader bufreader, CompileMode kind, String filename) {
PythonPartialLexer lexer = null;
try {
bufreader.reset();
CharStream cs = new NoCloseReaderStream(bufreader);
lexer = new PythonPartialLexer(cs);
CommonTokenStream tokens = new CommonTokenStream(lexer);
PythonTokenSource indentedSource = new PythonTokenSource(tokens, filename);
tokens = new CommonTokenStream(indentedSource);
PythonPartialParser parser = new PythonPartialParser(tokens);
switch (kind) {
case single:
parser.single_input();
break;
case eval:
parser.eval_input();
break;
default:
return false;
}
} catch (Exception e) {
return lexer.eofWhileNested;
}
return true;
}
private static class ExpectedEncodingBufferedReader extends BufferedReader {
/**
* The encoding from the source file, or null if none was specified and ascii is being used.
*/
public final String encoding;
public ExpectedEncodingBufferedReader(Reader in, String encoding) {
super(in);
this.encoding = encoding;
}
}
private static ExpectedEncodingBufferedReader prepBufReader(Reader reader,
CompilerFlags cflags,
String filename)
throws IOException {
cflags.source_is_utf8 = true;
cflags.encoding = "utf-8";
BufferedReader bufferedReader = new BufferedReader(reader);
bufferedReader.mark(MARK_LIMIT);
if (findEncoding(bufferedReader) != null) {
throw new ParseException("encoding declaration in Unicode string");
}
bufferedReader.reset();
return new ExpectedEncodingBufferedReader(bufferedReader, null);
}
private static ExpectedEncodingBufferedReader prepBufReader(InputStream input,
CompilerFlags cflags,
String filename,
boolean fromString)
throws IOException {
return prepBufReader(input, cflags, filename, fromString, true);
}
private static ExpectedEncodingBufferedReader prepBufReader(InputStream input,
CompilerFlags cflags,
String filename,
boolean fromString,
boolean universalNewlines)
throws IOException {
input = new BufferedInputStream(input);
boolean bom = adjustForBOM(input);
String encoding = readEncoding(input);
if (encoding == null) {
if (bom) {
encoding = "utf-8";
} else if (cflags != null && cflags.encoding != null) {
encoding = cflags.encoding;
}
}
if (cflags.source_is_utf8) {
if (encoding != null) {
throw new ParseException("encoding declaration in Unicode string");
}
encoding = "utf-8";
}
cflags.encoding = encoding;
if (universalNewlines) {
// Enable universal newlines mode on the input
StreamIO rawIO = new StreamIO(input, true);
org.python.core.io.BufferedReader bufferedIO =
new org.python.core.io.BufferedReader(rawIO, 0);
UniversalIOWrapper textIO = new UniversalIOWrapper(bufferedIO);
input = new TextIOInputStream(textIO);
}
Charset cs;
try {
// Use ascii for the raw bytes when no encoding was specified
if (encoding == null) {
if (fromString) {
cs = Charset.forName("ISO-8859-1");
} else {
cs = Charset.forName("ascii");
}
} else {
cs = Charset.forName(encoding);
}
} catch (UnsupportedCharsetException exc) {
throw new PySyntaxError("Unknown encoding: " + encoding, 1, 0, "", filename);
}
CharsetDecoder dec = cs.newDecoder();
dec.onMalformedInput(CodingErrorAction.REPORT);
dec.onUnmappableCharacter(CodingErrorAction.REPORT);
return new ExpectedEncodingBufferedReader(new InputStreamReader(input, dec), encoding);
}
private static ExpectedEncodingBufferedReader prepBufReader(String string,
CompilerFlags cflags,
String filename)
throws IOException {
if (cflags.source_is_utf8) {
return prepBufReader(new StringReader(string), cflags, filename);
}
byte[] stringBytes = StringUtil.toBytes(string);
return prepBufReader(new ByteArrayInputStream(stringBytes), cflags, filename, true, false);
}
/**
* Check for a BOM mark at the beginning of stream. If there is a BOM
* mark, advance the stream passed it. If not, reset() to start at the
* beginning of the stream again.
*
* Only checks for EF BB BF right now, since that is all that CPython 2.5
* Checks.
*
* @return true if a BOM was found and skipped.
* @throws ParseException if only part of a BOM is matched.
*
*/
private static boolean adjustForBOM(InputStream stream) throws IOException {
stream.mark(3);
int ch = stream.read();
if (ch == 0xEF) {
if (stream.read() != 0xBB) {
throw new ParseException("Incomplete BOM at beginning of file");
}
if (stream.read() != 0xBF) {
throw new ParseException("Incomplete BOM at beginning of file");
}
return true;
}
stream.reset();
return false;
}
private static String readEncoding(InputStream stream) throws IOException {
stream.mark(MARK_LIMIT);
String encoding = null;
BufferedReader br = new BufferedReader(new InputStreamReader(stream, "ISO-8859-1"), 512);
encoding = findEncoding(br);
// XXX: reset() can still raise an IOException if a line exceeds our large mark
// limit
stream.reset();
return encodingMap(encoding);
}
/**
* Reads the first two lines of the reader, searching for an encoding
* declaration.
*
* Note that reseting the reader (if needed) is responsibility of the caller.
*
* @return The declared encoding, or null if no encoding declaration is
* found
*/
private static String findEncoding(BufferedReader br)
throws IOException {
String encoding = null;
for (int i = 0; i < 2; i++) {
String strLine = br.readLine();
if (strLine == null) {
break;
}
String result = matchEncoding(strLine);
if (result != null) {
encoding = result;
break;
}
}
return encoding;
}
private static String encodingMap(String encoding) {
if (encoding == null) {
return null;
}
if (encoding.equals("Latin-1") || encoding.equals("latin-1")) {
return "ISO8859_1";
}
return encoding;
}
private static final Pattern pep263EncodingPattern = Pattern.compile("#.*coding[:=]\\s*([-\\w.]+)");
private static String matchEncoding(String inputStr) {
Matcher matcher = pep263EncodingPattern.matcher(inputStr);
boolean matchFound = matcher.find();
if (matchFound && matcher.groupCount() == 1) {
String groupStr = matcher.group(1);
return groupStr;
}
return null;
}
private static void close(BufferedReader reader) {
try {
if (reader != null) {
reader.close();
}
} catch (IOException i) {
// XXX: Log the error?
}
}
}