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