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