001 /* 002 * Copyright 2010-2015 JetBrains s.r.o. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017 package org.jetbrains.kotlin.psi; 018 019 import com.intellij.lang.ASTNode; 020 import com.intellij.psi.PsiElement; 021 import com.intellij.psi.tree.IElementType; 022 import org.jetbrains.annotations.NotNull; 023 import org.jetbrains.annotations.Nullable; 024 import org.jetbrains.kotlin.KtNodeTypes; 025 026 public class KtBinaryExpression extends KtExpressionImpl implements KtOperationExpression { 027 public KtBinaryExpression(@NotNull ASTNode node) { 028 super(node); 029 } 030 031 @Override 032 public <R, D> R accept(@NotNull KtVisitor<R, D> visitor, D data) { 033 return visitor.visitBinaryExpression(this, data); 034 } 035 036 @Nullable @IfNotParsed 037 public KtExpression getLeft() { 038 ASTNode node = getOperationReference().getNode().getTreePrev(); 039 while (node != null) { 040 PsiElement psi = node.getPsi(); 041 if (psi instanceof KtExpression) { 042 return (KtExpression) psi; 043 } 044 node = node.getTreePrev(); 045 } 046 047 return null; 048 } 049 050 @Nullable @IfNotParsed 051 public KtExpression getRight() { 052 ASTNode node = getOperationReference().getNode().getTreeNext(); 053 while (node != null) { 054 PsiElement psi = node.getPsi(); 055 if (psi instanceof KtExpression) { 056 return (KtExpression) psi; 057 } 058 node = node.getTreeNext(); 059 } 060 061 return null; 062 } 063 064 @Override 065 @NotNull 066 public KtSimpleNameExpression getOperationReference() { 067 return (KtSimpleNameExpression) findChildByType(KtNodeTypes.OPERATION_REFERENCE); 068 } 069 070 @NotNull 071 public IElementType getOperationToken() { 072 return getOperationReference().getReferencedNameElementType(); 073 } 074 }