001/*
002 * Copyright 2010-2013 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
017package org.jetbrains.jet.lang.psi;
018
019import com.intellij.lang.ASTNode;
020import com.intellij.psi.PsiElement;
021import com.intellij.psi.tree.IElementType;
022import org.jetbrains.annotations.NotNull;
023import org.jetbrains.annotations.Nullable;
024import org.jetbrains.jet.JetNodeTypes;
025
026public class JetBinaryExpression extends JetExpressionImpl implements JetOperationExpression {
027    public JetBinaryExpression(@NotNull ASTNode node) {
028        super(node);
029    }
030
031    @Override
032    public void accept(@NotNull JetVisitorVoid visitor) {
033        visitor.visitBinaryExpression(this);
034    }
035
036    @Override
037    public <R, D> R accept(@NotNull JetVisitor<R, D> visitor, D data) {
038        return visitor.visitBinaryExpression(this, data);
039    }
040
041    @Nullable @IfNotParsed
042    public JetExpression getLeft() {
043        ASTNode node = getOperationReference().getNode().getTreePrev();
044        while (node != null) {
045            PsiElement psi = node.getPsi();
046            if (psi instanceof JetExpression) {
047                return (JetExpression) psi;
048            }
049            node = node.getTreePrev();
050        }
051
052        return null;
053    }
054
055    @Nullable @IfNotParsed
056    public JetExpression getRight() {
057        ASTNode node = getOperationReference().getNode().getTreeNext();
058        while (node != null) {
059            PsiElement psi = node.getPsi();
060            if (psi instanceof JetExpression) {
061                return (JetExpression) psi;
062            }
063            node = node.getTreeNext();
064        }
065
066        return null;
067    }
068
069    @Override
070    @NotNull
071    public JetSimpleNameExpression getOperationReference() {
072        return (JetSimpleNameExpression) findChildByType(JetNodeTypes.OPERATION_REFERENCE);
073    }
074
075    public IElementType getOperationToken() {
076        return getOperationReference().getReferencedNameElementType();
077    }
078}