Search in sources :

Example 1 with TermVisitor

use of org.spoofax.terms.TermVisitor in project spoofax by metaborg.

the class AnalysisCommon method ambiguityMessages.

public Collection<IMessage> ambiguityMessages(final FileObject resource, IStrategoTerm ast) {
    final Collection<IMessage> messages = Lists.newLinkedList();
    final TermVisitor termVisitor = new TermVisitor() {

        private IStrategoTerm ambStart;

        @Override
        public void preVisit(IStrategoTerm term) {
            if (ambStart == null && "amb".equals(Term.tryGetName(term))) {
                final String text = "Fragment is ambiguous: " + ambToString(term);
                final ISourceLocation location = tracingService.location(term);
                if (location != null) {
                    final ISourceRegion region = location.region();
                    messages.add(message(resource, region, text, MessageSeverity.WARNING));
                } else {
                    messages.add(message(resource, text, MessageSeverity.WARNING));
                }
                ambStart = term;
            }
        }

        @Override
        public void postVisit(IStrategoTerm term) {
            if (term == ambStart) {
                ambStart = null;
            }
        }

        private String ambToString(IStrategoTerm amb) {
            final String result = amb.toString();
            return result.length() > 5000 ? result.substring(0, 5000) + "..." : result;
        }
    };
    termVisitor.visit(ast);
    return messages;
}
Also used : IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) IMessage(org.metaborg.core.messages.IMessage) ISourceRegion(org.metaborg.core.source.ISourceRegion) TermVisitor(org.spoofax.terms.TermVisitor) IStrategoString(org.spoofax.interpreter.terms.IStrategoString) ISourceLocation(org.metaborg.core.source.ISourceLocation)

Aggregations

IMessage (org.metaborg.core.messages.IMessage)1 ISourceLocation (org.metaborg.core.source.ISourceLocation)1 ISourceRegion (org.metaborg.core.source.ISourceRegion)1 IStrategoString (org.spoofax.interpreter.terms.IStrategoString)1 IStrategoTerm (org.spoofax.interpreter.terms.IStrategoTerm)1 TermVisitor (org.spoofax.terms.TermVisitor)1