Skip to content

Commit ce97ceb

Browse files
committed
SyntaxError, try to return a source location in most cases.
1 parent c652a8c commit ce97ceb

File tree

2 files changed

+16
-24
lines changed

2 files changed

+16
-24
lines changed

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,18 @@ public void recover(Parser recognizer, RecognitionException e) {
5656
super.recover(recognizer, e);
5757
}
5858

59+
static int getLine(Throwable e) {
60+
if (e instanceof RecognitionException) {
61+
return ((RecognitionException) e).getOffendingToken().getLine();
62+
} else {
63+
Throwable cause = e.getCause();
64+
if (cause instanceof RecognitionException) {
65+
return ((RecognitionException) cause).getOffendingToken().getLine();
66+
}
67+
return -1;
68+
}
69+
}
70+
5971
private static String getTokeLineText(Parser recognizer, Token token) {
6072
TokenStream tokenStream = recognizer.getTokenStream();
6173
int index = token.getTokenIndex();

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

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,7 @@
3333

3434
import org.antlr.v4.runtime.CharStreams;
3535
import org.antlr.v4.runtime.CodePointCharStream;
36-
import org.antlr.v4.runtime.NoViableAltException;
3736
import org.antlr.v4.runtime.ParserRuleContext;
38-
import org.antlr.v4.runtime.RecognitionException;
39-
import org.antlr.v4.runtime.Token;
4037

4138
import com.oracle.graal.python.PythonLanguage;
4239
import com.oracle.graal.python.nodes.PNode;
@@ -95,20 +92,12 @@ private static ParserRuleContext preParseWithAntlr(PythonCore core, Source sourc
9592
parser.reset();
9693
input = parser.eval_input();
9794
} catch (Throwable e2) {
98-
int line;
9995
if (source.isInteractive() && e instanceof PIncompleteSourceException) {
10096
((PIncompleteSourceException) e).setSource(source);
10197
throw e;
102-
} else if (e instanceof RecognitionException) {
103-
Token token = ((RecognitionException) e).getOffendingToken();
104-
line = token.getLine();
105-
} else if (e.getCause() instanceof NoViableAltException) {
106-
Token token = ((NoViableAltException) e.getCause()).getOffendingToken();
107-
line = token.getLine();
108-
} else {
109-
throw core.raise(SyntaxError, e.getMessage());
11098
}
111-
throw core.raise(SyntaxError, getLocation(source, line), e.getMessage());
99+
Node location = getLocation(source, PythonErrorStrategy.getLine(e));
100+
throw core.raise(SyntaxError, location, e.getMessage());
112101
}
113102
}
114103
}
@@ -126,17 +115,8 @@ private static ParserRuleContext preParseInlineWithAntlr(PythonCore core, Source
126115
parser.reset();
127116
input = parser.eval_input();
128117
} catch (Throwable e2) {
129-
int line;
130-
if (e instanceof RecognitionException) {
131-
Token token = ((RecognitionException) e).getOffendingToken();
132-
line = token.getLine();
133-
} else if (e.getCause() instanceof NoViableAltException) {
134-
Token token = ((NoViableAltException) e.getCause()).getOffendingToken();
135-
line = token.getLine();
136-
} else {
137-
throw core.raise(SyntaxError, e.getMessage());
138-
}
139-
throw core.raise(SyntaxError, getLocation(source, line), e.getMessage());
118+
Node location = getLocation(source, PythonErrorStrategy.getLine(e));
119+
throw core.raise(SyntaxError, location, e.getMessage());
140120
}
141121
}
142122
return input;

0 commit comments

Comments
 (0)