use of org.spoofax.interpreter.terms.IStrategoTerm in project spoofax by metaborg.
the class ResolverService method resolve.
@Override
public Resolution resolve(int offset, ISpoofaxParseUnit result) throws MetaborgException {
if (!result.valid()) {
return null;
}
final FileObject source = result.source();
final IProject project = projectService.get(source);
final ILanguageImpl langImpl = result.input().langImpl();
@Nullable IContext context;
if (project == null) {
context = null;
} else {
try {
context = contextService.get(source, project, langImpl);
} catch (ContextException | MetaborgRuntimeException e) {
// Failed to get a context, ignore and use the source file to get a stratego runtime later.
context = null;
}
}
final FacetContribution<ResolverFacet> facetContrib = facet(langImpl);
final ResolverFacet facet = facetContrib.facet;
final ILanguageComponent contributor = facetContrib.contributor;
final String strategy = facet.strategyName;
try {
final ITermFactory termFactory = termFactoryService.get(contributor, project, true);
final HybridInterpreter interpreter;
if (context == null) {
interpreter = strategoRuntimeService.runtime(contributor, source, true);
} else {
interpreter = strategoRuntimeService.runtime(contributor, context, true);
}
final Iterable<IStrategoTerm> inRegion = tracingService.fragments(result, new SourceRegion(offset));
final TermWithRegion tuple = common.outputs(termFactory, interpreter, source, source, result.ast(), inRegion, strategy);
return resolve(tuple);
} catch (MetaborgException e) {
throw new MetaborgException("Reference resolution failed", e);
}
}
use of org.spoofax.interpreter.terms.IStrategoTerm 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.spoofax.interpreter.terms.IStrategoTerm 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.spoofax.interpreter.terms.IStrategoTerm 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;
}
use of org.spoofax.interpreter.terms.IStrategoTerm in project spoofax by metaborg.
the class StrategoTransformer method transform.
private <I extends IUnit> ISpoofaxTransformUnit<I> transform(I input, IContext context, TransformActionContrib actionContribution, FileObject source, IStrategoTerm term, ITransformConfig config) throws TransformException {
final FileObject location = context.location();
final ILanguageComponent component = actionContribution.contributor;
final TransformAction action = action(actionContribution.action);
// Get input term
final IStrategoTerm inputTerm = common.builderInputTerm(term, source, location);
// Get Stratego runtime
final HybridInterpreter runtime;
try {
runtime = strategoRuntimeService.runtime(component, context, true);
} catch (MetaborgException e) {
throw new TransformException("Transformation failed unexpectedly; cannot get Stratego interpreter", e);
}
// Transform
logger.debug("Transforming {} with '{}'", source, action.name);
final Timer timer = new Timer(true);
final IStrategoTerm outputTerm;
try {
outputTerm = common.invoke(runtime, inputTerm, action.strategy);
} catch (MetaborgException e) {
throw new TransformException(e.getMessage(), e.getCause());
}
final long duration = timer.stop();
if (outputTerm == null) {
final String message = logger.format("Invoking Stratego strategy {} failed", action.strategy);
throw new TransformException(message);
}
// Get the result and, if allowed and required, write to file
List<TransformOutput> outputs;
IStrategoTerm resultTerm;
if (outputTerm.getSubtermCount() == 2 && (outputTerm instanceof IStrategoTuple)) {
final IStrategoTerm resourceTerm = outputTerm.getSubterm(0);
final IStrategoTerm contentTerm = outputTerm.getSubterm(1);
try {
if (resourceTerm instanceof IStrategoString) {
resultTerm = contentTerm;
outputs = Lists.newArrayList(output(resourceTerm, contentTerm, location, config));
} else if (resourceTerm instanceof IStrategoList) {
if (!(contentTerm instanceof IStrategoList) || resourceTerm.getSubtermCount() != contentTerm.getSubtermCount()) {
logger.error("List of terms does not match list of file names, cannot write to file.");
resultTerm = null;
outputs = Collections.emptyList();
} else {
outputs = Lists.newArrayListWithExpectedSize(resourceTerm.getSubtermCount());
for (int i = 0; i < resourceTerm.getSubtermCount(); i++) {
outputs.add(output(resourceTerm.getSubterm(i), contentTerm.getSubterm(i), location, config));
}
resultTerm = resourceTerm.getSubtermCount() == 1 ? resourceTerm.getSubterm(0) : null;
}
} else {
logger.error("First term of result tuple {} is neither a string, nor a list, cannot write output file", resourceTerm);
resultTerm = null;
outputs = Collections.emptyList();
}
} catch (MetaborgException ex) {
resultTerm = null;
outputs = Collections.emptyList();
}
} else {
resultTerm = outputTerm;
outputs = Collections.emptyList();
}
// Open editor
if (action.flags.openEditor) {
List<FileObject> resources = Lists.newArrayListWithExpectedSize(outputs.size());
for (TransformOutput output : outputs) {
if (output.resource != null) {
resources.add(output.resource);
}
}
editorRegistry.open(resources, context.project());
}
// Return result
final TransformContrib contrib = new TransformContrib(resultTerm != null || !Iterables.isEmpty(outputs), true, resultTerm, outputs, Iterables2.<IMessage>empty(), duration);
return unitService.transformUnit(input, contrib, context, actionContribution);
}
Aggregations