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.JetNodeTypes;
025    
026    public class JetBinaryExpression extends JetExpressionImpl implements JetOperationExpression {
027        public JetBinaryExpression(@NotNull ASTNode node) {
028            super(node);
029        }
030    
031        @Override
032        public <R, D> R accept(@NotNull JetVisitor<R, D> visitor, D data) {
033            return visitor.visitBinaryExpression(this, data);
034        }
035    
036        @Nullable @IfNotParsed
037        public JetExpression getLeft() {
038            ASTNode node = getOperationReference().getNode().getTreePrev();
039            while (node != null) {
040                PsiElement psi = node.getPsi();
041                if (psi instanceof JetExpression) {
042                    return (JetExpression) psi;
043                }
044                node = node.getTreePrev();
045            }
046    
047            return null;
048        }
049    
050        @Nullable @IfNotParsed
051        public JetExpression getRight() {
052            ASTNode node = getOperationReference().getNode().getTreeNext();
053            while (node != null) {
054                PsiElement psi = node.getPsi();
055                if (psi instanceof JetExpression) {
056                    return (JetExpression) psi;
057                }
058                node = node.getTreeNext();
059            }
060    
061            return null;
062        }
063    
064        @Override
065        @NotNull
066        public JetSimpleNameExpression getOperationReference() {
067            return (JetSimpleNameExpression) findChildByType(JetNodeTypes.OPERATION_REFERENCE);
068        }
069    
070        public IElementType getOperationToken() {
071            return getOperationReference().getReferencedNameElementType();
072        }
073    }