Skip to content

Commit 11f6d2c

Browse files
committed
Emit a parse error on mismatched dedent
1 parent 470818b commit 11f6d2c

File tree

1 file changed

+20
-2
lines changed
  • graalpython/com.oracle.graal.python/src/com/oracle/graal/python/parser/antlr

1 file changed

+20
-2
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/parser/antlr/Python3.g4

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ grammar Python3;
3535
// All comments that start with "///" are copy-pasted from
3636
// The Python Language Reference
3737

38-
tokens { INDENT, DEDENT }
38+
tokens { INDENT, DEDENT, INDENT_ERROR }
3939

4040
@lexer::members {
4141
// new version with semantic actions in parser
@@ -167,6 +167,20 @@ tokens { INDENT, DEDENT }
167167
return dedent;
168168
}
169169

170+
private Token createIndentError() {
171+
// For some reason, CPython sets the error position to the end of line
172+
int cur = getCharIndex();
173+
String s;
174+
do {
175+
s = _input.getText(new Interval(cur, cur));
176+
cur++;
177+
} while (!s.isEmpty() && s.charAt(0) != '\n');
178+
cur--;
179+
CommonToken error = new CommonToken(this._tokenFactorySourcePair, Python3Parser.INDENT_ERROR, DEFAULT_TOKEN_CHANNEL, cur, cur);
180+
error.setLine(this.lastToken.getLine());
181+
return error;
182+
}
183+
170184
private CommonToken commonToken(int type, String text) {
171185
int stop = Math.max(this.getCharIndex() - 1, 0);
172186
int start = Math.max(text.isEmpty() ? stop : stop - text.length() + 1, 0);
@@ -1773,10 +1787,14 @@ NEWLINE
17731787
}
17741788
else {
17751789
// Possibly emit more than 1 DEDENT token.
1776-
while(!indents.isEmpty() && indents.peek() > indent) {
1790+
while (!indents.isEmpty() && indents.peek() > indent) {
17771791
this.emit(createDedent());
17781792
indents.pop();
17791793
}
1794+
int expectedIndent = indents.empty() ? 0 : indents.peek();
1795+
if (expectedIndent != indent) {
1796+
this.emit(createIndentError());
1797+
}
17801798
}
17811799
}
17821800
}

0 commit comments

Comments
 (0)