From 6413691c33f13a70fa4e77b65e76aa0630d96e5a Mon Sep 17 00:00:00 2001 From: "Pavel V. Talanov" Date: Wed, 26 Mar 2014 17:29:55 +0400 Subject: [PATCH] Rewrite JetSimpleNameExpression and inheritors to Kotlin --- ...on.java => JetLabelReferenceExpression.kt} | 11 +- ...ion.java => JetNameReferenceExpression.kt} | 11 +- ...ava => JetOperationReferenceExpression.kt} | 11 +- .../jet/lang/psi/JetSimpleNameExpression.java | 127 ------------------ .../jet/lang/psi/JetSimpleNameExpression.kt | 110 +++++++++++++++ 5 files changed, 119 insertions(+), 151 deletions(-) rename compiler/frontend/src/org/jetbrains/jet/lang/psi/{JetNameReferenceExpression.java => JetLabelReferenceExpression.kt} (68%) rename compiler/frontend/src/org/jetbrains/jet/lang/psi/{JetLabelReferenceExpression.java => JetNameReferenceExpression.kt} (68%) rename compiler/frontend/src/org/jetbrains/jet/lang/psi/{JetOperationReferenceExpression.java => JetOperationReferenceExpression.kt} (67%) delete mode 100644 compiler/frontend/src/org/jetbrains/jet/lang/psi/JetSimpleNameExpression.java create mode 100644 compiler/frontend/src/org/jetbrains/jet/lang/psi/JetSimpleNameExpression.kt diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetNameReferenceExpression.java b/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetLabelReferenceExpression.kt similarity index 68% rename from compiler/frontend/src/org/jetbrains/jet/lang/psi/JetNameReferenceExpression.java rename to compiler/frontend/src/org/jetbrains/jet/lang/psi/JetLabelReferenceExpression.kt index bf0df184381..866652a6bee 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetNameReferenceExpression.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetLabelReferenceExpression.kt @@ -14,13 +14,8 @@ * limitations under the License. */ -package org.jetbrains.jet.lang.psi; +package org.jetbrains.jet.lang.psi -import com.intellij.lang.ASTNode; -import org.jetbrains.annotations.NotNull; +import com.intellij.lang.ASTNode -public class JetNameReferenceExpression extends JetSimpleNameExpression { - public JetNameReferenceExpression(@NotNull ASTNode node) { - super(node); - } -} +public class JetLabelReferenceExpression(node: ASTNode) : JetSimpleNameExpression(node) diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetLabelReferenceExpression.java b/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetNameReferenceExpression.kt similarity index 68% rename from compiler/frontend/src/org/jetbrains/jet/lang/psi/JetLabelReferenceExpression.java rename to compiler/frontend/src/org/jetbrains/jet/lang/psi/JetNameReferenceExpression.kt index 9a0655cf4ec..ec149760cfa 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetLabelReferenceExpression.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetNameReferenceExpression.kt @@ -14,13 +14,8 @@ * limitations under the License. */ -package org.jetbrains.jet.lang.psi; +package org.jetbrains.jet.lang.psi -import com.intellij.lang.ASTNode; -import org.jetbrains.annotations.NotNull; +import com.intellij.lang.ASTNode -public class JetLabelReferenceExpression extends JetSimpleNameExpression { - public JetLabelReferenceExpression(@NotNull ASTNode node) { - super(node); - } -} +public class JetNameReferenceExpression(node: ASTNode) : JetSimpleNameExpression(node) \ No newline at end of file diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetOperationReferenceExpression.java b/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetOperationReferenceExpression.kt similarity index 67% rename from compiler/frontend/src/org/jetbrains/jet/lang/psi/JetOperationReferenceExpression.java rename to compiler/frontend/src/org/jetbrains/jet/lang/psi/JetOperationReferenceExpression.kt index 531c649a3ef..f98c697e268 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetOperationReferenceExpression.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetOperationReferenceExpression.kt @@ -14,13 +14,8 @@ * limitations under the License. */ -package org.jetbrains.jet.lang.psi; +package org.jetbrains.jet.lang.psi -import com.intellij.lang.ASTNode; -import org.jetbrains.annotations.NotNull; +import com.intellij.lang.ASTNode -public class JetOperationReferenceExpression extends JetSimpleNameExpression { - public JetOperationReferenceExpression(@NotNull ASTNode node) { - super(node); - } -} +public class JetOperationReferenceExpression(node: ASTNode) : JetSimpleNameExpression(node) diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetSimpleNameExpression.java b/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetSimpleNameExpression.java deleted file mode 100644 index 5bc869badfa..00000000000 --- a/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetSimpleNameExpression.java +++ /dev/null @@ -1,127 +0,0 @@ -/* - * Copyright 2010-2013 JetBrains s.r.o. - * - * 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 org.jetbrains.jet.lang.psi; - -import com.intellij.lang.ASTNode; -import com.intellij.psi.PsiElement; -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.lang.parsing.JetExpressionParsing; -import org.jetbrains.jet.lang.resolve.name.Name; -import org.jetbrains.jet.lang.types.expressions.OperatorConventions; -import org.jetbrains.jet.lexer.JetTokens; - -import static org.jetbrains.jet.lexer.JetTokens.*; - -public abstract class JetSimpleNameExpression extends JetExpressionImpl implements JetReferenceExpression { - public static final TokenSet REFERENCE_TOKENS = TokenSet.create(LABEL_IDENTIFIER, IDENTIFIER, FIELD_IDENTIFIER, THIS_KEYWORD, SUPER_KEYWORD); - - public JetSimpleNameExpression(@NotNull ASTNode node) { - super(node); - } - - @Nullable - public JetExpression getReceiverExpression() { - PsiElement parent = getParent(); - if (parent instanceof JetQualifiedExpression && !isImportDirectiveExpression()) { - JetExpression receiverExpression = ((JetQualifiedExpression) parent).getReceiverExpression(); - // Name expression can't be receiver for itself - if (receiverExpression != this) { - return receiverExpression; - } - } - else if (parent instanceof JetCallExpression) { - //This is in case `a().b()` - JetCallExpression callExpression = (JetCallExpression) parent; - parent = callExpression.getParent(); - if (parent instanceof JetQualifiedExpression) { - JetQualifiedExpression qualifiedExpression = (JetQualifiedExpression) parent; - JetExpression parentsReceiver = qualifiedExpression.getReceiverExpression(); - if (parentsReceiver != callExpression) { - return parentsReceiver; - } - } - } - else if (parent instanceof JetBinaryExpression && ((JetBinaryExpression) parent).getOperationReference() == this) { - JetBinaryExpression expr = (JetBinaryExpression) parent; - //noinspection SuspiciousMethodCalls - return OperatorConventions.IN_OPERATIONS.contains(expr.getOperationToken()) - ? expr.getRight() - : expr.getLeft(); - } - else if (parent instanceof JetUnaryExpression && ((JetUnaryExpression) parent).getOperationReference() == this) { - return ((JetUnaryExpression) parent).getBaseExpression(); - } - else if (parent instanceof JetUserType) { - JetUserType qualifier = ((JetUserType) parent).getQualifier(); - if (qualifier != null) { - return qualifier.getReferenceExpression(); - } - } - return null; - } - - public boolean isImportDirectiveExpression() { - PsiElement parent = getParent(); - if (parent == null) return false; - else return parent instanceof JetImportDirective || - parent.getParent() instanceof JetImportDirective; - } - - @NotNull - public String getReferencedName() { - String text = getReferencedNameElement().getNode().getText(); - return JetPsiUtil.unquoteIdentifierOrFieldReference(text); - } - - @NotNull - public Name getReferencedNameAsName() { - String name = getReferencedName(); - return Name.identifierNoValidate(name); - } - - @NotNull - public PsiElement getReferencedNameElement() { - PsiElement element = findChildByType(REFERENCE_TOKENS); - if (element == null) { - element = findChildByType(JetExpressionParsing.ALL_OPERATIONS); - } - - if (element != null) { - return element; - } - - return this; - } - - @Nullable - public PsiElement getIdentifier() { - return findChildByType(JetTokens.IDENTIFIER); - } - - @Nullable @IfNotParsed - public IElementType getReferencedNameElementType() { - return getReferencedNameElement().getNode().getElementType(); - } - - @Override - public R accept(@NotNull JetVisitor visitor, D data) { - return visitor.visitSimpleNameExpression(this, data); - } -} diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetSimpleNameExpression.kt b/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetSimpleNameExpression.kt new file mode 100644 index 00000000000..5e0821a64f6 --- /dev/null +++ b/compiler/frontend/src/org/jetbrains/jet/lang/psi/JetSimpleNameExpression.kt @@ -0,0 +1,110 @@ +/* + * Copyright 2010-2014 JetBrains s.r.o. + * + * 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 org.jetbrains.jet.lang.psi + +import com.intellij.lang.ASTNode +import com.intellij.psi.PsiElement +import com.intellij.psi.tree.IElementType +import com.intellij.psi.tree.TokenSet +import org.jetbrains.annotations.Nullable +import org.jetbrains.jet.lang.parsing.JetExpressionParsing +import org.jetbrains.jet.lang.resolve.name.Name +import org.jetbrains.jet.lexer.JetTokens +import org.jetbrains.jet.lexer.JetTokens.* +import org.jetbrains.jet.lang.types.expressions.OperatorConventions + +public abstract class JetSimpleNameExpression(node: ASTNode) : JetExpressionImpl(node), JetReferenceExpression { + + public fun getReceiverExpression(): JetExpression? { + val parent = getParent() + when { + parent is JetQualifiedExpression && !isImportDirectiveExpression() -> { + val receiverExpression = parent.getReceiverExpression() + // Name expression can't be receiver for itself + if (receiverExpression != this) { + return receiverExpression + } + } + parent is JetCallExpression -> { + //This is in case `a().b()` + val callExpression = (parent as JetCallExpression) + val grandParent = callExpression.getParent() + if (grandParent is JetQualifiedExpression) { + val parentsReceiver = grandParent.getReceiverExpression() + if (parentsReceiver != callExpression) { + return parentsReceiver + } + } + } + parent is JetBinaryExpression && parent.getOperationReference() == this -> { + return if (parent.getOperationToken() in OperatorConventions.IN_OPERATIONS) parent.getRight() else parent.getLeft() + } + parent is JetUnaryExpression && parent.getOperationReference() == this -> { + return parent.getBaseExpression()!! + } + parent is JetUserType -> { + val qualifier = parent.getQualifier() + if (qualifier != null) { + return qualifier.getReferenceExpression()!! + } + } + } + return null + } + + public fun isImportDirectiveExpression(): Boolean { + val parent = getParent() + if (parent == null) { + return false + } + else { + return parent is JetImportDirective || parent.getParent() is JetImportDirective + } + } + + public fun getReferencedName(): String { + val text = getReferencedNameElement().getNode()!!.getText() + return JetPsiUtil.unquoteIdentifierOrFieldReference(text) + } + + public fun getReferencedNameAsName(): Name { + val name = getReferencedName() + return Name.identifierNoValidate(name) + } + + public fun getReferencedNameElement(): PsiElement { + return findChildByType(REFERENCE_TOKENS) ?: + findChildByType(JetExpressionParsing.ALL_OPERATIONS) ?: + this + } + + public fun getIdentifier(): PsiElement? { + return findChildByType(JetTokens.IDENTIFIER) + } + + public fun getReferencedNameElementType(): IElementType { + return getReferencedNameElement().getNode()!!.getElementType()!! + } + + override fun accept(visitor: JetVisitor, data: D?): R { + return visitor.visitSimpleNameExpression(this, data) as R + } + + class object { + public val REFERENCE_TOKENS: TokenSet = TokenSet.create(LABEL_IDENTIFIER, IDENTIFIER, FIELD_IDENTIFIER, THIS_KEYWORD, SUPER_KEYWORD) + } +}