From e500402006160d51d6b2b8dd2a57fe40089bd91a Mon Sep 17 00:00:00 2001 From: Dmitry Jemerov Date: Tue, 22 Feb 2011 15:13:01 +0100 Subject: [PATCH] report colon with no following statement list as error (PY-2790) --- .../python/parsing/StatementParsing.java | 22 ++++++++++++++----- python/testData/psi/ColonBeforeEof.py | 1 + python/testData/psi/ColonBeforeEof.txt | 16 ++++++++++++++ .../jetbrains/python/PythonParsingTest.java | 4 ++++ 4 files changed, 37 insertions(+), 6 deletions(-) create mode 100644 python/testData/psi/ColonBeforeEof.py create mode 100644 python/testData/psi/ColonBeforeEof.txt diff --git a/python/src/com/jetbrains/python/parsing/StatementParsing.java b/python/src/com/jetbrains/python/parsing/StatementParsing.java index 8d49292437eb..f55955235d9c 100644 --- a/python/src/com/jetbrains/python/parsing/StatementParsing.java +++ b/python/src/com/jetbrains/python/parsing/StatementParsing.java @@ -714,8 +714,13 @@ public class StatementParsing extends Parsing implements ITokenTypeRemapper { } else { myBuilder.advanceLexer(); - while (!myBuilder.eof() && myBuilder.getTokenType() != PyTokenTypes.DEDENT) { - parseStatement(); + if (myBuilder.eof()) { + myBuilder.error("indented block expected"); + } + else { + while (!myBuilder.eof() && myBuilder.getTokenType() != PyTokenTypes.DEDENT) { + parseStatement(); + } } } @@ -734,11 +739,16 @@ public class StatementParsing extends Parsing implements ITokenTypeRemapper { } else { final PsiBuilder.Marker marker = myBuilder.mark(); - parseSimpleStatement(true); - while (matchToken(PyTokenTypes.SEMICOLON)) { - if (matchToken(PyTokenTypes.STATEMENT_BREAK)) - break; + if (myBuilder.eof()) { + myBuilder.error("statement expected"); + } + else { parseSimpleStatement(true); + while (matchToken(PyTokenTypes.SEMICOLON)) { + if (matchToken(PyTokenTypes.STATEMENT_BREAK)) + break; + parseSimpleStatement(true); + } } marker.done(PyElementTypes.STATEMENT_LIST); if (endMarker != null) { diff --git a/python/testData/psi/ColonBeforeEof.py b/python/testData/psi/ColonBeforeEof.py new file mode 100644 index 000000000000..f14e607a0ce0 --- /dev/null +++ b/python/testData/psi/ColonBeforeEof.py @@ -0,0 +1 @@ +for a in b: \ No newline at end of file diff --git a/python/testData/psi/ColonBeforeEof.txt b/python/testData/psi/ColonBeforeEof.txt new file mode 100644 index 000000000000..246f155b21dd --- /dev/null +++ b/python/testData/psi/ColonBeforeEof.txt @@ -0,0 +1,16 @@ +PyFile:ColonBeforeEof.py + PyForStatement + PyForPart + PsiElement(Py:FOR_KEYWORD)('for') + PsiWhiteSpace(' ') + PyTargetExpression: a + PsiElement(Py:IDENTIFIER)('a') + PsiWhiteSpace(' ') + PsiElement(Py:IN_KEYWORD)('in') + PsiWhiteSpace(' ') + PyReferenceExpression: b + PsiElement(Py:IDENTIFIER)('b') + PsiElement(Py:COLON)(':') + PyStatementList + PsiErrorElement:statement expected + \ No newline at end of file diff --git a/python/testSrc/com/jetbrains/python/PythonParsingTest.java b/python/testSrc/com/jetbrains/python/PythonParsingTest.java index 9ae3a2ee4312..9656fcd6290c 100644 --- a/python/testSrc/com/jetbrains/python/PythonParsingTest.java +++ b/python/testSrc/com/jetbrains/python/PythonParsingTest.java @@ -251,6 +251,10 @@ public class PythonParsingTest extends ParsingTestCase { doTest(); } + public void testColonBeforeEof() { // PY-2790 + doTest(); + } + public void doTest() { doTest(LanguageLevel.PYTHON25); }