Good code is red: nested '​if​'​s in list comprehension (PY-322)

This commit is contained in:
Dmitry Jemerov
2010-01-15 22:16:53 +03:00
parent 01ee9b0d69
commit 445b18d1fa
4 changed files with 61 additions and 23 deletions
@@ -1,34 +1,15 @@
/*
* Copyright 2005 Pythonid Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS"; BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.jetbrains.python.parsing;
import com.intellij.lang.PsiBuilder;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.psi.tree.IElementType;
import static com.jetbrains.python.PyBundle.message;
import com.jetbrains.python.PyElementTypes;
import com.jetbrains.python.PyTokenTypes;
import static com.jetbrains.python.PyBundle.message;
/**
* Created by IntelliJ IDEA.
* User: yole
* Date: 29.05.2005
* Time: 10:26:06
* To change this template use File | Settings | File Templates.
* @author yole
*/
public class ExpressionParsing extends Parsing {
private static final Logger LOG = Logger.getInstance("#ru.yole.pythonlanguage.parsing.ExpressionParsing");
@@ -130,7 +111,7 @@ public class ExpressionParsing extends Parsing {
}
while (myBuilder.getTokenType() == PyTokenTypes.IF_KEYWORD) {
myBuilder.advanceLexer();
parseExpression();
parseOldExpression();
}
if (myBuilder.getTokenType() == endToken) {
myBuilder.advanceLexer();
@@ -432,6 +413,13 @@ public class ExpressionParsing extends Parsing {
return parseTestExpression(false, isTargetExpression);
}
public boolean parseOldExpression() {
if (myBuilder.getTokenType() == PyTokenTypes.LAMBDA_KEYWORD) {
return parseLambdaExpression(false);
}
return parseORTestExpression(myBuilder, false, false);
}
private boolean parseTestExpression(boolean stopOnIn, boolean isTargetExpression) {
if (myBuilder.getTokenType() == PyTokenTypes.LAMBDA_KEYWORD) {
return parseLambdaExpression(false);
@@ -0,0 +1 @@
[x for x in range(10) if x % 2 if x % 3]
@@ -0,0 +1,45 @@
PyFile:ListComprehensionNestedIf.py
PyExpressionStatement
PyListCompExpression
PsiElement(Py:LBRACKET)('[')
PyReferenceExpression: x
PsiElement(Py:IDENTIFIER)('x')
PsiWhiteSpace(' ')
PsiElement(Py:FOR_KEYWORD)('for')
PsiWhiteSpace(' ')
PyTargetExpression: x
PsiElement(Py:IDENTIFIER)('x')
PsiWhiteSpace(' ')
PsiElement(Py:IN_KEYWORD)('in')
PsiWhiteSpace(' ')
PyCallExpression: range
PyReferenceExpression: range
PsiElement(Py:IDENTIFIER)('range')
PyArgumentList
PsiElement(Py:LPAR)('(')
PyNumericLiteralExpression
PsiElement(Py:INTEGER_LITERAL)('10')
PsiElement(Py:RPAR)(')')
PsiWhiteSpace(' ')
PsiElement(Py:IF_KEYWORD)('if')
PsiWhiteSpace(' ')
PyBinaryExpression
PyReferenceExpression: x
PsiElement(Py:IDENTIFIER)('x')
PsiWhiteSpace(' ')
PsiElement(Py:PERC)('%')
PsiWhiteSpace(' ')
PyNumericLiteralExpression
PsiElement(Py:INTEGER_LITERAL)('2')
PsiWhiteSpace(' ')
PsiElement(Py:IF_KEYWORD)('if')
PsiWhiteSpace(' ')
PyBinaryExpression
PyReferenceExpression: x
PsiElement(Py:IDENTIFIER)('x')
PsiWhiteSpace(' ')
PsiElement(Py:PERC)('%')
PsiWhiteSpace(' ')
PyNumericLiteralExpression
PsiElement(Py:INTEGER_LITERAL)('3')
PsiElement(Py:RBRACKET)(']')
@@ -112,6 +112,10 @@ public class PythonParsingTest extends ParsingTestCase {
doTest();
}
public void testListComprehensionNestedIf() throws Exception { // PY-322
doTest();
}
public void doTest() throws Exception {
doTest(LanguageLevel.PYTHON25);
}