Skip to content

Commit e69a611

Browse files
committed
using the error message handler for all syntax parser errors
- unittest to check to syntaxerror
1 parent 7ff280d commit e69a611

File tree

3 files changed

+44
-48
lines changed

3 files changed

+44
-48
lines changed

graalpython/com.oracle.graal.python.test/src/tests/test_parser.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,16 @@ def x(*, a=None, b):
6565
assert globs["retval"] == (None, 42)
6666

6767

68+
def test_syntax_error_simple():
69+
globs = {}
70+
was_exception = False
71+
try:
72+
exec("""c = a += 3""", globs)
73+
except SyntaxError:
74+
was_exception = True
75+
assert was_exception
76+
77+
6878
def test_lambda_no_args_with_nested_lambdas():
6979
no_err = True
7080
try:

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/parser/PythonErrorStrategy.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040

4141
import org.antlr.v4.runtime.DefaultErrorStrategy;
4242
import org.antlr.v4.runtime.InputMismatchException;
43+
import org.antlr.v4.runtime.NoViableAltException;
4344
import org.antlr.v4.runtime.Parser;
4445
import org.antlr.v4.runtime.ParserRuleContext;
4546
import org.antlr.v4.runtime.RecognitionException;
@@ -85,8 +86,7 @@ private String getTokeLineText(Parser recognizer, Token token) {
8586
return token.getInputStream().getText(Interval.of(start, stop));
8687
}
8788

88-
@Override
89-
protected void reportInputMismatch(Parser recognizer, InputMismatchException e) {
89+
private void handlePythonSyntaxError(Parser recognizer, RecognitionException e) {
9090
Token offendingToken = e.getOffendingToken();
9191
String lineText = getTokeLineText(recognizer, offendingToken);
9292
String errorMarker = new String(new char[offendingToken.getCharPositionInLine() + 1]).replace('\0', ' ') + "^";
@@ -100,4 +100,14 @@ protected void reportInputMismatch(Parser recognizer, InputMismatchException e)
100100
}
101101
recognizer.notifyErrorListeners(offendingToken, pythonSyntaxErrorMessage, re);
102102
}
103+
104+
@Override
105+
protected void reportInputMismatch(Parser recognizer, InputMismatchException e) {
106+
handlePythonSyntaxError(recognizer, e);
107+
}
108+
109+
@Override
110+
protected void reportNoViableAlternative(Parser recognizer, NoViableAltException e) {
111+
handlePythonSyntaxError(recognizer, e);
112+
}
103113
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/parser/PythonParserImpl.java

Lines changed: 22 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,16 @@
2828
import static com.oracle.graal.python.runtime.exception.PythonErrorType.SyntaxError;
2929

3030
import java.util.HashMap;
31-
import java.util.InputMismatchException;
3231
import java.util.Map;
3332
import java.util.regex.Pattern;
3433

3534
import org.antlr.v4.runtime.CharStreams;
3635
import org.antlr.v4.runtime.CodePointCharStream;
37-
import org.antlr.v4.runtime.NoViableAltException;
3836
import org.antlr.v4.runtime.ParserRuleContext;
39-
import org.antlr.v4.runtime.RecognitionException;
40-
import org.antlr.v4.runtime.Token;
4137

4238
import com.oracle.graal.python.PythonLanguage;
4339
import com.oracle.graal.python.nodes.PNode;
40+
import com.oracle.graal.python.parser.antlr.Builder;
4441
import com.oracle.graal.python.parser.antlr.Python3Parser;
4542
import com.oracle.graal.python.runtime.PythonCore;
4643
import com.oracle.graal.python.runtime.PythonParseResult;
@@ -57,14 +54,25 @@
5754
public 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

Comments
 (0)