Some getters for expression nodes

This commit is contained in:
Maxim Shafirov
2011-01-08 23:32:07 +03:00
parent 1d6317356a
commit 2ce071dd38
22 changed files with 319 additions and 33 deletions
+9 -9
View File
@@ -59,7 +59,6 @@ public interface JetNodeTypes {
JetNodeType PROPERTY_ACCESSOR = new JetNodeType("PROPERTY_ACCESSOR");
JetNodeType INITIALIZER_LIST = new JetNodeType("INITIALIZER_LIST");
JetNodeType THIS_CALL = new JetNodeType("THIS_CALL");
JetNodeType BLOCK = new JetNodeType("BLOCK");
JetNodeType TYPE_CONSTRAINT_LIST = new JetNodeType("TYPE_CONSTRAINT_LIST");
JetNodeType TYPE_CONSTRAINT = new JetNodeType("TYPE_CONSTRAINT");
@@ -81,19 +80,20 @@ public interface JetNodeTypes {
JetNodeType CONTINUE = new JetNodeType("CONTINUE", JetContinueExpression.class);
JetNodeType BREAK = new JetNodeType("BREAK", JetBreakExpression.class);
JetNodeType IF = new JetNodeType("IF", JetIfExpression.class);
JetNodeType CONDITION = new JetNodeType("CONDITION");
JetNodeType THEN = new JetNodeType("THEN");
JetNodeType ELSE = new JetNodeType("ELSE");
JetNodeType CONDITION = new JetNodeType("CONDITION"); //TODO: discuss, this can be omitted
JetNodeType THEN = new JetNodeType("THEN"); //TODO: discuss, this can be omitted
JetNodeType ELSE = new JetNodeType("ELSE"); //TODO: discuss, this can be omitted
JetNodeType TRY = new JetNodeType("TRY", JetTryExpression.class);
JetNodeType CATCH = new JetNodeType("CATCH");
JetNodeType FINALLY = new JetNodeType("FINALLY");
JetNodeType CATCH = new JetNodeType("CATCH", JetCatchSection.class);
JetNodeType FINALLY = new JetNodeType("FINALLY", JetFinallySection.class);
JetNodeType FOR = new JetNodeType("FOR", JetForExpression.class);
JetNodeType WHILE = new JetNodeType("WHILE", JetWhileExpression.class);
JetNodeType DO_WHILE = new JetNodeType("DO_WHILE", JetDoWhileExpression.class);
JetNodeType LOOP_PARAMETER = new JetNodeType("LOOP_PARAMETER");
JetNodeType LOOP_RANGE = new JetNodeType("LOOP_RANGE");
JetNodeType BODY = new JetNodeType("BODY");
JetNodeType LOOP_PARAMETER = new JetNodeType("LOOP_PARAMETER", JetParameter.class); // TODO: Do we need separate type?
JetNodeType LOOP_RANGE = new JetNodeType("LOOP_RANGE"); //TODO: discuss, this can be omitted
JetNodeType BODY = new JetNodeType("BODY"); //TODO: discuss, this can be omitted
JetNodeType RECEIVER_TYPE = new JetNodeType("RECEIVER_TYPE");
JetNodeType BLOCK = new JetNodeType("BLOCK", JetBlockExpression.class);
JetNodeType FUNCTION_LITERAL = new JetNodeType("FUNCTION_LITERAL", JetFunctionLiteralExpression.class);
JetNodeType ANNOTATED_EXPRESSION = new JetNodeType("ANNOTATED_EXPRESSION", JetAnnotatedExpression.class);
JetNodeType REFERENCE_EXPRESSION = new JetNodeType("REFERENCE_EXPRESSION", JetReferenceExpression.class);
@@ -2,6 +2,11 @@ package org.jetbrains.jet.lang.psi;
import com.intellij.lang.ASTNode;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.JetNodeTypes;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* @author max
@@ -19,4 +24,17 @@ public class JetAnnotatedExpression extends JetExpression {
public JetExpression getBaseExpression() {
return findChildByClass(JetExpression.class);
}
public List<JetAttributeAnnotation> getAttributeAnnotations() {
return findChildrenByType(JetNodeTypes.ATTRIBUTE_ANNOTATION);
}
public List<JetAttribute> getAttributes() {
List<JetAttribute> answer = null;
for (JetAttributeAnnotation annotation : getAttributeAnnotations()) {
if (answer == null) answer = new ArrayList<JetAttribute>();
answer.addAll(annotation.getAttributes());
}
return answer != null ? answer : Collections.<JetAttribute>emptyList();
}
}
@@ -2,16 +2,11 @@ package org.jetbrains.jet.lang.psi;
import com.intellij.lang.ASTNode;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiErrorElement;
import com.intellij.psi.tree.IElementType;
import com.intellij.psi.tree.TokenSet;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lexer.JetToken;
import org.jetbrains.jet.lexer.JetTokens;
import static org.jetbrains.jet.lexer.JetTokens.*;
/**
* @author max
*/
@@ -46,26 +41,15 @@ public class JetBinaryExpression extends JetExpression {
return null;
}
@NotNull
public ASTNode getOperationTokenNode() {
PsiElement child = getLeft().getNextSibling();
while (child != null) {
IElementType tt = child.getNode().getElementType();
if (JetTokens.WHITE_SPACE_OR_COMMENT_BIT_SET.contains(tt) || child instanceof PsiErrorElement) {
child = child.getNextSibling();
}
else {
return child.getNode();
}
}
return null;
ASTNode operationNode = getNode().findChildByType(JetTokens.OPERATIONS);
assert operationNode != null;
return operationNode;
}
@Nullable
public JetToken getOperationSign() {
ASTNode node = getOperationTokenNode();
return node != null ? (JetToken) node.getElementType() : null;
return (JetToken) getOperationTokenNode().getElementType();
}
}
@@ -0,0 +1,18 @@
package org.jetbrains.jet.lang.psi;
import com.intellij.lang.ASTNode;
import org.jetbrains.annotations.NotNull;
/**
* @author max
*/
public class JetBlockExpression extends JetExpression {
public JetBlockExpression(@NotNull ASTNode node) {
super(node);
}
@Override
public void accept(JetVisitor visitor) {
visitor.visitBlockExpression(this);
}
}
@@ -1,7 +1,10 @@
package org.jetbrains.jet.lang.psi;
import com.intellij.lang.ASTNode;
import com.intellij.psi.PsiElement;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lexer.JetTokens;
/**
* @author max
@@ -15,4 +18,10 @@ public class JetBreakExpression extends JetExpression {
public void accept(JetVisitor visitor) {
visitor.visitBreakExpression(this);
}
@Nullable
public String getLabelName() {
PsiElement id = findChildByType(JetTokens.IDENTIFIER);
return id != null ? id.getText() : null;
}
}
@@ -0,0 +1,40 @@
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.JetNodeTypes;
import java.util.List;
/**
* @author max
*/
public class JetCatchSection extends JetElement {
public JetCatchSection(@NotNull ASTNode node) {
super(node);
}
@Override
public void accept(JetVisitor visitor) {
visitor.visitCatchSection(this);
}
@Nullable
public JetParameterList getParameterList() {
return (JetParameterList) findChildByType(JetNodeTypes.VALUE_PARAMETER_LIST);
}
@Nullable
public JetParameter getCatchParameter() {
JetParameterList list = getParameterList();
if (list == null) return null;
List<JetParameter> parameters = list.getParameters();
return parameters.size() == 1 ? parameters.get(0) : null;
}
public JetExpression getCatchBody() {
return findChildByClass(JetExpression.class);
}
}
@@ -1,7 +1,10 @@
package org.jetbrains.jet.lang.psi;
import com.intellij.lang.ASTNode;
import com.intellij.psi.PsiElement;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lexer.JetTokens;
/**
* @author max
@@ -15,4 +18,10 @@ public class JetContinueExpression extends JetExpression {
public void accept(JetVisitor visitor) {
visitor.visitContinueExpression(this);
}
@Nullable
public String getLabelName() {
PsiElement id = findChildByType(JetTokens.IDENTIFIER);
return id != null ? id.getText() : null;
}
}
@@ -2,6 +2,8 @@ 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.JetNodeTypes;
/**
* @author max
@@ -15,4 +17,13 @@ public class JetDoWhileExpression extends JetExpression {
public void accept(JetVisitor visitor) {
visitor.visitDoWhileExpression(this);
}
@Nullable
public JetExpression getCondition() {
return findExpressionUnder(JetNodeTypes.CONDITION);
}
public JetExpression getBody() {
return findExpressionUnder(JetNodeTypes.THEN);
}
}
@@ -1,7 +1,10 @@
package org.jetbrains.jet.lang.psi;
import com.intellij.lang.ASTNode;
import com.intellij.psi.PsiElement;
import com.intellij.psi.util.PsiTreeUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.JetNodeType;
/**
* @author max
@@ -15,4 +18,12 @@ public class JetExpression extends JetElement {
public void accept(JetVisitor visitor) {
visitor.visitExpression(this);
}
JetExpression findExpressionUnder(JetNodeType type) {
PsiElement containerNode = findChildByType(type);
if (containerNode == null) return null;
return PsiTreeUtil.findChildOfType(containerNode, JetExpression.class);
}
}
@@ -0,0 +1,22 @@
package org.jetbrains.jet.lang.psi;
import com.intellij.lang.ASTNode;
import org.jetbrains.annotations.NotNull;
/**
* @author max
*/
public class JetFinallySection extends JetElement {
public JetFinallySection(@NotNull ASTNode node) {
super(node);
}
@Override
public void accept(JetVisitor visitor) {
visitor.visitFinallySection(this);
}
public JetExpression getFinalExpression() {
return findChildByClass(JetExpression.class);
}
}
@@ -2,6 +2,7 @@ package org.jetbrains.jet.lang.psi;
import com.intellij.lang.ASTNode;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.JetNodeTypes;
/**
* @author max
@@ -15,4 +16,16 @@ public class JetForExpression extends JetExpression {
public void accept(JetVisitor visitor) {
visitor.visitForExpression(this);
}
public JetParameter getLoopParameter() {
return (JetParameter) findChildByType(JetNodeTypes.LOOP_PARAMETER);
}
public JetExpression getLoopRange() {
return findExpressionUnder(JetNodeTypes.LOOP_RANGE);
}
public JetExpression getBody() {
return findExpressionUnder(JetNodeTypes.THEN);
}
}
@@ -2,6 +2,8 @@ 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.JetNodeTypes;
/**
* @author max
@@ -15,4 +17,17 @@ public class JetIfExpression extends JetExpression {
public void accept(JetVisitor visitor) {
visitor.visitIfExpression(this);
}
@Nullable
public JetExpression getCondition() {
return findExpressionUnder(JetNodeTypes.CONDITION);
}
public JetExpression getThen() {
return findExpressionUnder(JetNodeTypes.THEN);
}
public JetExpression getElse() {
return findExpressionUnder(JetNodeTypes.ELSE);
}
}
@@ -2,6 +2,11 @@ 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.JetNodeTypes;
import java.util.Collections;
import java.util.List;
/**
* @author max
@@ -15,4 +20,19 @@ public class JetNewExpression extends JetExpression {
public void accept(JetVisitor visitor) {
visitor.visitNewExpression(this);
}
public JetTypeReference getTypeReference() {
return (JetTypeReference) findChildByType(JetNodeTypes.TYPE_REFERENCE);
}
@Nullable
public JetArgumentList getArgumentList() {
return (JetArgumentList) findChildByType(JetNodeTypes.VALUE_ARGUMENT_LIST);
}
@NotNull
public List<JetArgument> getArguments() {
JetArgumentList list = getArgumentList();
return list != null ? list.getArguments() : Collections.<JetArgument>emptyList();
}
}
@@ -2,6 +2,9 @@ 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;
/**
* @author max
@@ -19,4 +22,16 @@ public class JetPostfixExpression extends JetExpression {
public JetExpression getBaseExpression() {
return findChildByClass(JetExpression.class);
}
@NotNull
public ASTNode getOperationTokenNode() {
ASTNode operationNode = getNode().findChildByType(JetTokens.OPERATIONS);
assert operationNode != null;
return operationNode;
}
@Nullable
public JetToken getOperationSign() {
return (JetToken) getOperationTokenNode().getElementType();
}
}
@@ -1,10 +1,10 @@
package org.jetbrains.jet.lang.psi;
import com.intellij.lang.ASTNode;
import com.intellij.psi.util.PsiTreeUtil;
import org.jetbrains.annotations.NotNull;
import java.util.List;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lexer.JetToken;
import org.jetbrains.jet.lexer.JetTokens;
/**
* @author max
@@ -22,4 +22,16 @@ public class JetPrefixExpression extends JetExpression {
public JetExpression getBaseExpression() {
return findChildByClass(JetExpression.class);
}
@NotNull
public ASTNode getOperationTokenNode() {
ASTNode operationNode = getNode().findChildByType(JetTokens.OPERATIONS);
assert operationNode != null;
return operationNode;
}
@Nullable
public JetToken getOperationSign() {
return (JetToken) getOperationTokenNode().getElementType();
}
}
@@ -1,7 +1,12 @@
package org.jetbrains.jet.lang.psi;
import com.intellij.lang.ASTNode;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiErrorElement;
import com.intellij.psi.tree.IElementType;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lexer.JetTokens;
/**
* @author max
@@ -10,4 +15,42 @@ public abstract class JetQualifiedExpression extends JetExpression {
public JetQualifiedExpression(@NotNull ASTNode node) {
super(node);
}
@NotNull
public JetExpression getReceiverExpression() {
JetExpression left = findChildByClass(JetExpression.class);
assert left != null;
return left;
}
@Nullable
public JetExpression getSelectorExpression() {
ASTNode node = getOperationTokenNode();
while (node != null) {
PsiElement psi = node.getPsi();
if (psi instanceof JetExpression) {
return (JetExpression) psi;
}
node = node.getTreeNext();
}
return null;
}
public ASTNode getOperationTokenNode() {
PsiElement child = getReceiverExpression().getNextSibling();
while (child != null) {
IElementType tt = child.getNode().getElementType();
if (JetTokens.WHITE_SPACE_OR_COMMENT_BIT_SET.contains(tt) || child instanceof PsiErrorElement) {
child = child.getNextSibling();
}
else {
return child.getNode();
}
}
return null;
}
}
@@ -2,6 +2,7 @@ package org.jetbrains.jet.lang.psi;
import com.intellij.lang.ASTNode;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* @author max
@@ -15,4 +16,9 @@ public class JetReturnExpression extends JetExpression {
public void accept(JetVisitor visitor) {
visitor.visitReturnExpression(this);
}
@Nullable
public JetExpression getReturnedExpression() {
return findChildByClass(JetExpression.class);
}
}
@@ -2,6 +2,7 @@ package org.jetbrains.jet.lang.psi;
import com.intellij.lang.ASTNode;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* @author max
@@ -15,4 +16,9 @@ public class JetThrowExpression extends JetExpression {
public void accept(JetVisitor visitor) {
visitor.visitThrowExpression(this);
}
@Nullable
public JetExpression getThrownExpression() {
return findChildByClass(JetExpression.class);
}
}
@@ -15,4 +15,10 @@ public class JetTryExpression extends JetExpression {
public void accept(JetVisitor visitor) {
visitor.visitTryExpression(this);
}
public JetExpression getBody() {
return findChildByClass(JetExpression.class);
}
}
@@ -241,4 +241,16 @@ public class JetVisitor extends PsiElementVisitor {
public void visitRootNamespaceExpression(JetRootNamespaceExpression expression) {
visitExpression(expression);
}
public void visitBlockExpression(JetBlockExpression expression) {
visitExpression(expression);
}
public void visitCatchSection(JetCatchSection catchSection) {
visitJetElement(catchSection);
}
public void visitFinallySection(JetFinallySection finallySection) {
visitJetElement(finallySection);
}
}
@@ -2,6 +2,8 @@ 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.JetNodeTypes;
/**
* @author max
@@ -15,4 +17,13 @@ public class JetWhileExpression extends JetExpression {
public void accept(JetVisitor visitor) {
visitor.visitWhileExpression(this);
}
@Nullable
public JetExpression getCondition() {
return findExpressionUnder(JetNodeTypes.CONDITION);
}
public JetExpression getBody() {
return findExpressionUnder(JetNodeTypes.THEN);
}
}
@@ -155,4 +155,9 @@ public interface JetTokens {
TokenSet WHITESPACES = TokenSet.create(TokenType.WHITE_SPACE);
TokenSet COMMENTS = TokenSet.create(EOL_COMMENT, BLOCK_COMMENT, DOC_COMMENT);
TokenSet STRINGS = TokenSet.create(CHARACTER_LITERAL, STRING_LITERAL, RAW_STRING_LITERAL);
TokenSet OPERATIONS = TokenSet.create(AS_KEYWORD, IS_KEYWORD, IN_KEYWORD, DOT, PLUSPLUS, MINUSMINUS, MUL, PLUS,
MINUS, EXCL, DIV, PERC, LT, GT, LTEQ, GTEQ, EQEQEQ, ARROW, EXCLEQEQEQ, EQEQ, EXCLEQ, ANDAND, OROR,
SAFE_ACCESS, ELVIS, MAP, FILTER, QUEST, COLON, SEMICOLON, RANGE, EQ, MULTEQ, DIVEQ, PERCEQ, PLUSEQ, MINUSEQ,
NOT_IN, NOT_IS, HASH, IDENTIFIER, MATCH_KEYWORD);
}