Skip to content

Commit 95bac26

Browse files
committed
Raise indentation error on unexpected indent/dedent
1 parent 2e50c2a commit 95bac26

File tree

1 file changed

+33
-12
lines changed

1 file changed

+33
-12
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/parser/antlr/DescriptiveBailErrorListener.java

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -68,26 +68,47 @@ public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol,
6868

6969
String entireMessage = e == null || e.getMessage() == null ? "invalid syntax" : e.getMessage();
7070

71+
IntervalSet expectedTokens = null;
72+
if (e != null) {
73+
expectedTokens = e.getExpectedTokens();
74+
} else if (recognizer instanceof Python3Parser) {
75+
expectedTokens = ((Python3Parser) recognizer).getExpectedTokens();
76+
}
77+
7178
if (isInteractive(recognizer)) {
72-
PIncompleteSourceException handleRecognitionException = null;
73-
if (e != null) {
74-
handleRecognitionException = handleRecognitionException(e.getExpectedTokens(), entireMessage, e, line);
75-
} else if (recognizer instanceof Python3Parser) {
76-
handleRecognitionException = handleRecognitionException(((Python3Parser) recognizer).getExpectedTokens(), entireMessage, null, line);
79+
PIncompleteSourceException incompleteSourceException = null;
80+
if (expectedTokens != null) {
81+
incompleteSourceException = handleRecognitionException(expectedTokens, entireMessage, e, line);
7782
}
78-
if (handleRecognitionException == null) {
79-
handleRecognitionException = handleInteractiveException(recognizer, offendingSymbol);
83+
if (incompleteSourceException == null) {
84+
incompleteSourceException = handleInteractiveException(recognizer, offendingSymbol);
8085
}
81-
if (handleRecognitionException != null) {
82-
throw handleRecognitionException;
86+
if (incompleteSourceException != null) {
87+
throw incompleteSourceException;
8388
}
8489
}
90+
8591
if (offendingSymbol instanceof Token) {
8692
Token token = (Token) offendingSymbol;
8793
ErrorType errorType = ErrorType.Generic;
88-
if (token.getType() == Python3Parser.INDENT_ERROR) {
89-
entireMessage = "unindent does not match any outer indentation level";
90-
errorType = ErrorType.Indentation;
94+
switch (token.getType()) {
95+
case Python3Parser.INDENT_ERROR:
96+
entireMessage = "unindent does not match any outer indentation level";
97+
errorType = ErrorType.Indentation;
98+
break;
99+
case Python3Parser.INDENT:
100+
entireMessage = "unexpected indent";
101+
errorType = ErrorType.Indentation;
102+
break;
103+
case Python3Parser.DEDENT:
104+
entireMessage = "unexpected unindent";
105+
errorType = ErrorType.Indentation;
106+
break;
107+
default:
108+
if (expectedTokens != null && expectedTokens.contains(Python3Parser.INDENT)) {
109+
entireMessage = "expected an indented block";
110+
errorType = ErrorType.Indentation;
111+
}
91112
}
92113
throw new EmptyRecognitionException(errorType, entireMessage, recognizer, token);
93114
}

0 commit comments

Comments
 (0)