Mark getters, that return null only in case of parsing error

This commit is contained in:
Maxim Shafirov
2011-01-11 01:18:30 +03:00
parent 04fbaa26fe
commit 6cf565dcf5
28 changed files with 67 additions and 32 deletions
@@ -34,6 +34,7 @@ public interface JetNodeTypes {
// TODO: Not sure if we really need separate PSI nodes for the class parameters?
JetNodeType PRIMARY_CONSTRUCTOR_PARAMETERS_LIST = new JetNodeType("PRIMARY_CONSTRUCTOR_PARAMETERS_LIST", JetParameterList.class);
JetNodeType PRIMARY_CONSTRUCTOR_PARAMETER = new JetNodeType("PRIMARY_CONSTRUCTOR_PARAMETER", JetParameter.class);
JetNodeType CLASS_BODY = new JetNodeType("CLASS_BODY", JetClassBody.class);
JetNodeType IMPORT_DIRECTIVE = new JetNodeType("IMPORT_DIRECTIVE", JetImportDirective.class);
JetNodeType IMPORTED = new JetNodeType("IMPORTED"); // TODO: ???
@@ -17,11 +17,12 @@ public class JetArgument extends JetElement {
visitor.visitArgument(this);
}
@Nullable
@Nullable(IF_NOT_PARSED)
public JetExpression getArgumentExpression() {
return findChildByClass(JetExpression.class);
}
@Nullable
public String getArgumentName() {
ASTNode firstChildNode = getNode().getFirstChildNode();
return firstChildNode.getElementType() == JetTokens.IDENTIFIER ? firstChildNode.getText() : null;
@@ -21,7 +21,7 @@ public class JetAttribute extends JetElement {
visitor.visitAttribute(this);
}
@Nullable
@Nullable(IF_NOT_PARSED)
public JetTypeReference getTypeReference() {
return (JetTypeReference) findChildByType(JetNodeTypes.TYPE_REFERENCE);
}
@@ -27,7 +27,7 @@ public class JetBinaryExpression extends JetExpression {
return left;
}
@Nullable
@Nullable(IF_NOT_PARSED)
public JetExpression getRight() {
ASTNode node = getOperationTokenNode();
while (node != null) {
@@ -48,7 +48,7 @@ public class JetBinaryExpression extends JetExpression {
return operationNode;
}
@Nullable
@NotNull
public JetToken getOperationSign() {
return (JetToken) getOperationTokenNode().getElementType();
}
@@ -27,7 +27,7 @@ public class JetBinaryExpressionWithTypeRHS extends JetExpression {
return left;
}
@Nullable
@Nullable(IF_NOT_PARSED)
public JetTypeReference getRight() {
ASTNode node = getOperationTokenNode();
while (node != null) {
@@ -48,7 +48,7 @@ public class JetBinaryExpressionWithTypeRHS extends JetExpression {
return operationNode;
}
@Nullable
@NotNull
public JetToken getOperationSign() {
return (JetToken) getOperationTokenNode().getElementType();
}
@@ -20,12 +20,12 @@ public class JetCatchSection extends JetElement {
visitor.visitCatchSection(this);
}
@Nullable
@Nullable(IF_NOT_PARSED)
public JetParameterList getParameterList() {
return (JetParameterList) findChildByType(JetNodeTypes.VALUE_PARAMETER_LIST);
}
@Nullable
@Nullable(IF_NOT_PARSED)
public JetParameter getCatchParameter() {
JetParameterList list = getParameterList();
if (list == null) return null;
@@ -34,6 +34,7 @@ public class JetCatchSection extends JetElement {
}
@Nullable(IF_NOT_PARSED)
public JetExpression getCatchBody() {
return findChildByClass(JetExpression.class);
}
@@ -2,7 +2,6 @@ package org.jetbrains.jet.lang.psi;
import com.intellij.lang.ASTNode;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* @author max
@@ -17,8 +16,10 @@ public class JetClassInitializer extends JetDeclaration {
visitor.visitAnonymousInitializer(this);
}
@Nullable
@NotNull
public JetExpression getBody() {
return findChildByClass(JetExpression.class);
JetExpression body = findChildByClass(JetExpression.class);
assert body != null;
return body;
}
}
@@ -21,7 +21,7 @@ public class JetConstructor extends JetDeclaration {
visitor.visitConstructor(this);
}
@Nullable
@Nullable(IF_NOT_PARSED)
public JetParameterList getParameterList() {
return (JetParameterList) findChildByType(JetNodeTypes.VALUE_PARAMETER_LIST);
}
@@ -21,7 +21,7 @@ public class JetDecomposer extends JetNamedDeclaration {
visitor.visitDecomposer(this);
}
@Nullable
@Nullable(IF_NOT_PARSED)
public JetDecomposerPropertyList getPropertyList() {
return (JetDecomposerPropertyList) findChildByType(JetNodeTypes.DECOMPOSER_PROPERTY_LIST);
}
@@ -18,11 +18,12 @@ public class JetDoWhileExpression extends JetExpression {
visitor.visitDoWhileExpression(this);
}
@Nullable
@Nullable(IF_NOT_PARSED)
public JetExpression getCondition() {
return findExpressionUnder(JetNodeTypes.CONDITION);
}
@Nullable(IF_NOT_PARSED)
public JetExpression getBody() {
return findExpressionUnder(JetNodeTypes.BODY);
}
@@ -15,6 +15,8 @@ public class JetElement extends ASTWrapperPsiElement {
super(node);
}
protected static final String IF_NOT_PARSED = "Returns null if has parsing error";
@NotNull
@Override
public Language getLanguage() {
@@ -21,6 +21,7 @@ public class JetExtension extends JetTypeParameterListOwner {
visitor.visitExtension(this);
}
@NotNull
public List<JetDeclaration> getDeclarations() {
JetClassBody body = (JetClassBody) findChildByType(JetNodeTypes.CLASS_BODY);
if (body == null) return Collections.emptyList();
@@ -28,7 +29,7 @@ public class JetExtension extends JetTypeParameterListOwner {
return body.getDeclarations();
}
@Nullable
@Nullable(IF_NOT_PARSED)
public JetTypeReference getTargetTypeRef() {
return (JetTypeReference) findChildByType(JetNodeTypes.TYPE_REFERENCE);
}
@@ -24,7 +24,7 @@ public class JetFunction extends JetTypeParameterListOwner {
visitor.visitFunction(this);
}
@Nullable
@Nullable(IF_NOT_PARSED)
public JetParameterList getParameterList() {
return (JetParameterList) findChildByType(JetNodeTypes.VALUE_PARAMETER_LIST);
}
@@ -18,15 +18,17 @@ public class JetIfExpression extends JetExpression {
visitor.visitIfExpression(this);
}
@Nullable
@Nullable(IF_NOT_PARSED)
public JetExpression getCondition() {
return findExpressionUnder(JetNodeTypes.CONDITION);
}
@Nullable(IF_NOT_PARSED)
public JetExpression getThen() {
return findExpressionUnder(JetNodeTypes.THEN);
}
@Nullable
public JetExpression getElse() {
return findExpressionUnder(JetNodeTypes.ELSE);
}
@@ -19,6 +19,7 @@ public class JetImportDirective extends JetElement {
visitor.visitImportDirective(this);
}
@NotNull
public String getImportedName() {
StringBuilder answer = new StringBuilder();
ASTNode childNode = getNode().getFirstChildNode();
@@ -25,7 +25,7 @@ public class JetMatchExpression extends JetExpression {
return left;
}
@Nullable
@Nullable(IF_NOT_PARSED)
public JetMatchBlock getMatchBlock() {
return (JetMatchBlock) findChildByType(JetNodeTypes.MATCH_BLOCK);
}
@@ -21,6 +21,7 @@ public class JetNewExpression extends JetExpression {
visitor.visitNewExpression(this);
}
@Nullable(IF_NOT_PARSED)
public JetTypeReference getTypeReference() {
return (JetTypeReference) findChildByType(JetNodeTypes.TYPE_REFERENCE);
}
@@ -35,6 +35,7 @@ public class JetObjectLiteralExpression extends JetExpression implements JetClas
}
@Override
@NotNull
public List<JetDeclaration> getDeclarations() {
JetClassBody body = (JetClassBody) findChildByType(JetNodeTypes.CLASS_BODY);
if (body == null) return Collections.emptyList();
@@ -19,8 +19,11 @@ public class JetPostfixExpression extends JetExpression {
visitor.visitPostfixExpression(this);
}
@NotNull
public JetExpression getBaseExpression() {
return findChildByClass(JetExpression.class);
JetExpression answer = findChildByClass(JetExpression.class);
assert answer != null;
return answer;
}
@NotNull
@@ -2,7 +2,6 @@ package org.jetbrains.jet.lang.psi;
import com.intellij.lang.ASTNode;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lexer.JetToken;
import org.jetbrains.jet.lexer.JetTokens;
@@ -19,8 +18,11 @@ public class JetPrefixExpression extends JetExpression {
visitor.visitPrefixExpression(this);
}
@NotNull
public JetExpression getBaseExpression() {
return findChildByClass(JetExpression.class);
JetExpression answer = findChildByClass(JetExpression.class);
assert answer != null;
return answer;
}
@NotNull
@@ -30,7 +32,7 @@ public class JetPrefixExpression extends JetExpression {
return operationNode;
}
@Nullable
@NotNull
public JetToken getOperationSign() {
return (JetToken) getOperationTokenNode().getElementType();
}
@@ -71,6 +71,7 @@ public class JetProperty extends JetNamedDeclaration {
return null;
}
@Nullable
public JetPropertyAccessor getSetter() {
for (JetPropertyAccessor accessor : getAccessors()) {
if (accessor.isSetter()) return accessor;
@@ -22,7 +22,7 @@ public abstract class JetQualifiedExpression extends JetExpression {
return left;
}
@Nullable
@Nullable(IF_NOT_PARSED)
public JetExpression getSelectorExpression() {
ASTNode node = getOperationTokenNode();
while (node != null) {
@@ -42,7 +42,7 @@ public abstract class JetQualifiedExpression extends JetExpression {
return operationNode;
}
@Nullable
@NotNull
public JetToken getOperationSign() {
return (JetToken) getOperationTokenNode().getElementType();
}
@@ -17,7 +17,7 @@ public class JetThrowExpression extends JetExpression {
visitor.visitThrowExpression(this);
}
@Nullable
@Nullable(IF_NOT_PARSED)
public JetExpression getThrownExpression() {
return findChildByClass(JetExpression.class);
}
@@ -25,7 +25,7 @@ public class JetTypeConstraint extends JetElement {
findChildByType(JetTokens.OBJECT_KEYWORD) != null;
}
@Nullable
@Nullable(IF_NOT_PARSED)
public JetTypeReference getSubjectTypeReference() {
ASTNode node = getNode().getFirstChildNode();
while (node != null) {
@@ -38,7 +38,7 @@ public class JetTypeConstraint extends JetElement {
return null;
}
@Nullable
@Nullable(IF_NOT_PARSED)
public JetTypeReference getExtendsTypeReference() {
boolean passedColon = false;
ASTNode node = getNode().getFirstChildNode();
@@ -21,6 +21,7 @@ public class JetTypeParameterListOwner extends JetNamedDeclaration {
return (JetTypeParameterList) findChildByType(JetNodeTypes.TYPE_PARAMETER_LIST);
}
@NotNull
public List<JetTypeParameter> getTypeParameters() {
JetTypeParameterList list = getTypeParameterList();
if (list == null) return Collections.emptyList();
@@ -18,7 +18,7 @@ public class JetTypedef extends JetTypeParameterListOwner {
visitor.visitTypedef(this);
}
@Nullable
@Nullable(IF_NOT_PARSED)
public JetTypeReference getTypeReference() {
return (JetTypeReference) findChildByType(JetNodeTypes.TYPE_REFERENCE);
}
@@ -18,11 +18,12 @@ public class JetWhileExpression extends JetExpression {
visitor.visitWhileExpression(this);
}
@Nullable
@Nullable(IF_NOT_PARSED)
public JetExpression getCondition() {
return findExpressionUnder(JetNodeTypes.CONDITION);
}
@Nullable(IF_NOT_PARSED)
public JetExpression getBody() {
return findExpressionUnder(JetNodeTypes.BODY);
}
@@ -4,22 +4,24 @@
package org.jetbrains.jet.parsing;
import com.intellij.openapi.application.PathManager;
import com.intellij.psi.PsiErrorElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.testFramework.ParsingTestCase;
import junit.framework.TestSuite;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.jet.lang.psi.JetBinaryExpression;
import org.jetbrains.jet.lang.psi.JetElement;
import org.jetbrains.jet.lang.psi.JetExpression;
import org.jetbrains.jet.lang.psi.JetVisitor;
import java.io.File;
import java.io.FileFilter;
import java.io.FilenameFilter;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class JetParsingTest extends ParsingTestCase {
@@ -49,7 +51,6 @@ public class JetParsingTest extends ParsingTestCase {
@Override
protected void checkResult(@NonNls String targetDataName, PsiFile file) throws IOException {
super.checkResult(targetDataName, file);
file.acceptChildren(new JetVisitor() {
@Override
public void visitJetElement(JetElement elem) {
@@ -60,7 +61,20 @@ public class JetParsingTest extends ParsingTestCase {
throw new RuntimeException(throwable);
}
}
@Override
public void visitBinaryExpression(JetBinaryExpression expression) {
super.visitBinaryExpression(expression);
JetExpression right = expression.getRight();
JetExpression left = expression.getLeft();
assertNotSame(left, right);
if (right == null) {
assertNotNull("Imcomplete binary operation in parsed OK test", PsiTreeUtil.findChildOfType(expression, PsiErrorElement.class) != null);
}
}
});
super.checkResult(targetDataName, file);
}
private void checkPsiGetters(JetElement elem) throws Throwable {