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