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;
}
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;
}
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;
}
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;
}
Aggregations