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.resolve;
018
019import com.intellij.psi.PsiElement;
020import com.intellij.psi.PsiElementVisitor;
021import com.intellij.psi.PsiErrorElement;
022import org.jetbrains.annotations.NotNull;
023import org.jetbrains.annotations.Nullable;
024import org.jetbrains.jet.lang.diagnostics.Diagnostic;
025import org.jetbrains.jet.lang.diagnostics.DiagnosticHolder;
026import org.jetbrains.jet.lang.diagnostics.DiagnosticUtils;
027import org.jetbrains.jet.lang.psi.JetIdeTemplate;
028import org.jetbrains.jet.lang.psi.JetTreeVisitorVoid;
029import org.jetbrains.jet.lang.psi.JetVisitorVoid;
030
031import java.util.ArrayList;
032import java.util.List;
033
034public class AnalyzingUtils {
035
036    public abstract static class PsiErrorElementVisitor extends JetTreeVisitorVoid {
037        @Override
038        public abstract void visitErrorElement(PsiErrorElement element);
039    }
040
041
042    public static void checkForSyntacticErrors(@NotNull PsiElement root) {
043        root.acceptChildren(new PsiErrorElementVisitor() {
044            @Override
045            public void visitErrorElement(PsiErrorElement element) {
046                throw new IllegalArgumentException(element.getErrorDescription() + "; looking at " + element.getNode().getElementType() + " '" + element.getText() + DiagnosticUtils.atLocation(element));
047            }
048        });
049    }
050    
051    public static List<PsiErrorElement> getSyntaxErrorRanges(@NotNull PsiElement root) {
052        final ArrayList<PsiErrorElement> r = new ArrayList<PsiErrorElement>();
053        root.acceptChildren(new PsiErrorElementVisitor() {
054            @Override
055            public void visitErrorElement(PsiErrorElement element) {
056                r.add(element);
057            }
058        });
059        return r;
060    }
061
062    public static void throwExceptionOnErrors(BindingContext bindingContext) {
063        for (Diagnostic diagnostic : bindingContext.getDiagnostics()) {
064            DiagnosticHolder.THROW_EXCEPTION.report(diagnostic);
065        }
066    }
067
068    // --------------------------------------------------------------------------------------------------------------------------
069
070    public static String formDebugNameForBindingTrace(@NotNull String debugName, @Nullable Object resolutionSubjectForMessage) {
071        if (resolutionSubjectForMessage instanceof PsiElement) {
072            resolutionSubjectForMessage = ((PsiElement) resolutionSubjectForMessage).getText();
073        }
074        if (resolutionSubjectForMessage != null) {
075            return debugName + " '" + resolutionSubjectForMessage + "'";
076        }
077        return debugName;
078    }
079}