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
036 public abstract static class PsiErrorElementVisitor extends KtTreeVisitorVoid {
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 throwExceptionOnErrors(bindingContext.getDiagnostics());
064 }
065
066 public static void throwExceptionOnErrors(Diagnostics diagnostics) {
067 for (Diagnostic diagnostic : diagnostics) {
068 DiagnosticSink.THROW_EXCEPTION.report(diagnostic);
069 }
070 }
071
072 // --------------------------------------------------------------------------------------------------------------------------
073
074 public static String formDebugNameForBindingTrace(@NotNull String debugName, @Nullable Object resolutionSubjectForMessage) {
075 StringBuilder debugInfo = new StringBuilder(debugName);
076 if (resolutionSubjectForMessage instanceof KtElement) {
077 KtElement element = (KtElement) resolutionSubjectForMessage;
078 debugInfo.append(" ").append(DebugTextUtilKt.getDebugText(element));
079 debugInfo.append(" in ").append(element.getContainingFile().getName());
080 }
081 else if (resolutionSubjectForMessage != null) {
082 debugInfo.append(" ").append(resolutionSubjectForMessage);
083 }
084 return debugInfo.toString();
085 }
086 }