Search in sources :

Example 6 with ISourceLocation

use of org.metaborg.core.source.ISourceLocation 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)

Example 7 with ISourceLocation

use of org.metaborg.core.source.ISourceLocation in project spoofax by metaborg.

the class TracingCommon method outputs.

public TermWithRegion outputs(ITermFactory termFactory, HybridInterpreter runtime, FileObject location, FileObject resource, IStrategoTerm result, Iterable<IStrategoTerm> inRegion, String strategy) throws MetaborgException {
    for (IStrategoTerm term : inRegion) {
        final IStrategoTerm inputTerm = common.builderInputTerm(term, resource, location);
        final IStrategoTerm output = common.invoke(runtime, inputTerm, strategy);
        if (output == null) {
            continue;
        }
        final ISourceLocation highlightLocation = tracingService.location(term);
        if (highlightLocation == null) {
            logger.debug("Cannot get source region for {}", term);
            continue;
        }
        return new TermWithRegion(output, highlightLocation.region());
    }
    return null;
}
Also used : IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) ISourceLocation(org.metaborg.core.source.ISourceLocation)

Example 8 with ISourceLocation

use of org.metaborg.core.source.ISourceLocation in project spoofax by metaborg.

the class TracingService method toTermsWithin.

private Iterable<IStrategoTerm> toTermsWithin(IStrategoTerm ast, final ISourceRegion region) {
    if (ast == null || region == null) {
        return Iterables2.empty();
    }
    final Collection<IStrategoTerm> parsed = Lists.newLinkedList();
    final IStrategoTermVisitor visitor = new AStrategoTermVisitor() {

        @Override
        public boolean visit(IStrategoTerm term) {
            final ISourceLocation location = location(term);
            if (location != null && region.contains(location.region())) {
                parsed.add(term);
                // no need to check the children, as this term is already within the given region
                return false;
            }
            return true;
        }
    };
    StrategoTermVisitee.topdown(visitor, ast);
    return parsed;
}
Also used : IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) AStrategoTermVisitor(org.spoofax.terms.visitor.AStrategoTermVisitor) IStrategoTermVisitor(org.spoofax.terms.visitor.IStrategoTermVisitor) ISourceLocation(org.metaborg.core.source.ISourceLocation)

Example 9 with ISourceLocation

use of org.metaborg.core.source.ISourceLocation in project spoofax by metaborg.

the class TracingService method toTerms.

private Iterable<IStrategoTerm> toTerms(IStrategoTerm ast, final ISourceRegion region) {
    if (ast == null || region == null) {
        return Iterables2.empty();
    }
    final Collection<IStrategoTerm> parsed = Lists.newLinkedList();
    final IStrategoTermVisitor visitor = new AStrategoTermVisitor() {

        @Override
        public boolean visit(IStrategoTerm term) {
            final ISourceLocation location = location(term);
            if (location != null && location.region().contains(region)) {
                parsed.add(term);
                return false;
            }
            return true;
        }
    };
    StrategoTermVisitee.bottomup(visitor, ast);
    return parsed;
}
Also used : IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) AStrategoTermVisitor(org.spoofax.terms.visitor.AStrategoTermVisitor) IStrategoTermVisitor(org.spoofax.terms.visitor.IStrategoTermVisitor) ISourceLocation(org.metaborg.core.source.ISourceLocation)

Aggregations

ISourceLocation (org.metaborg.core.source.ISourceLocation)9 IStrategoTerm (org.spoofax.interpreter.terms.IStrategoTerm)8 ISourceRegion (org.metaborg.core.source.ISourceRegion)5 IStrategoString (org.spoofax.interpreter.terms.IStrategoString)3 AStrategoTermVisitor (org.spoofax.terms.visitor.AStrategoTermVisitor)3 IStrategoTermVisitor (org.spoofax.terms.visitor.IStrategoTermVisitor)3 FileObject (org.apache.commons.vfs2.FileObject)2 IMessage (org.metaborg.core.messages.IMessage)2 SourceLocation (org.metaborg.core.source.SourceLocation)2 IToken (org.spoofax.jsglr.client.imploder.IToken)2 Nullable (javax.annotation.Nullable)1 MetaborgException (org.metaborg.core.MetaborgException)1 Resolution (org.metaborg.core.tracing.Resolution)1 IStrategoAppl (org.spoofax.interpreter.terms.IStrategoAppl)1 IStrategoList (org.spoofax.interpreter.terms.IStrategoList)1 ITokens (org.spoofax.jsglr.client.imploder.ITokens)1 TermVisitor (org.spoofax.terms.TermVisitor)1