use of org.spoofax.interpreter.terms.IStrategoTerm in project spoofax by metaborg.
the class OutlineService method outline.
@Override
public IOutline outline(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<OutlineFacet> facetContrib = facet(langImpl);
final OutlineFacet facet = facetContrib.facet;
final ILanguageComponent contributor = facetContrib.contributor;
final String strategy = facet.strategyName;
try {
final HybridInterpreter interpreter;
if (context == null) {
interpreter = strategoRuntimeService.runtime(contributor, source, true);
} else {
interpreter = strategoRuntimeService.runtime(contributor, context, true);
}
final IStrategoTerm input = common.builderInputTerm(result.ast(), source, source);
final IStrategoTerm outlineTerm = common.invoke(interpreter, input, strategy);
if (outlineTerm == null) {
return null;
}
final IOutline outline = toOutline(outlineTerm, facet.expandTo, contributor.location());
return outline;
} catch (MetaborgException e) {
throw new MetaborgException("Creating outline failed", e);
}
}
use of org.spoofax.interpreter.terms.IStrategoTerm in project spoofax by metaborg.
the class ASpoofaxPrimitive method call.
@Override
public boolean call(IContext context, Strategy[] svars, IStrategoTerm[] tvars) throws InterpreterException {
final IStrategoTerm current = context.current();
final ITermFactory factory = context.getFactory();
try {
final IStrategoTerm newCurrent = call(current, svars, tvars, factory, context);
if (newCurrent != null) {
context.setCurrent(newCurrent);
return true;
}
} catch (MetaborgException | IOException e) {
throw new InterpreterException("Executing primitive " + name + " failed unexpectedly", e);
}
return false;
}
use of org.spoofax.interpreter.terms.IStrategoTerm in project spoofax by metaborg.
the class Sdf2ParenthesizeStamper method stampOf.
@Override
public Stamp stampOf(File file) {
if (!FileCommands.exists(file)) {
return new ValueStamp<>(this, null);
}
final IStrategoTerm term;
try {
term = context.parse(file);
} catch (ParseException | IOException e) {
return LastModifiedStamper.instance.stampOf(file);
}
if (term == null) {
return LastModifiedStamper.instance.stampOf(file);
}
final ParenExtractor parenExtractor = new ParenExtractor(context.termFactory());
parenExtractor.visit(term);
return new ValueStamp<>(this, Pair.create(parenExtractor.getRelevantProds(), parenExtractor.getPriorities()));
}
use of org.spoofax.interpreter.terms.IStrategoTerm in project spoofax by metaborg.
the class StrategoAnalyzer method analyze.
private ISpoofaxAnalyzeUnit analyze(ISpoofaxParseUnit input, IContext context, HybridInterpreter runtime, String strategy, ITermFactory termFactory) throws AnalysisException {
final FileObject source = input.source();
final IStrategoString contextPath = strategoCommon.locationTerm(context.location());
final IStrategoString resourcePath = strategoCommon.resourceTerm(source, context.location());
final IStrategoTuple inputTerm = termFactory.makeTuple(input.ast(), resourcePath, contextPath);
try {
logger.trace("Analysing {}", source);
final Timer timer = new Timer(true);
final IStrategoTerm resultTerm = strategoCommon.invoke(runtime, inputTerm, strategy);
final long duration = timer.stop();
if (resultTerm == null) {
logger.trace("Analysis for {} failed", source);
return result(analysisCommon.analysisFailedMessage(runtime), input, context, null, duration);
} else if (!(resultTerm instanceof IStrategoTuple)) {
logger.trace("Analysis for {} has unexpected result, not a tuple", source);
final String message = logger.format("Unexpected results from analysis {}", resultTerm);
return result(message, input, context, null, duration);
} else if (resultTerm.getSubtermCount() == 4) {
logger.trace("Analysis for {} done", source);
return result(resultTerm, input, context, duration);
} else if (resultTerm.getSubtermCount() == 3) {
logger.trace("Analysis for {} done", source);
return resultNoAst(resultTerm, input, context, duration);
} else {
logger.trace("Analysis for {} has unexpected result; tuple with more than 4 or less than 2 elements", source);
final String message = logger.format("Unexpected results from analysis {}", resultTerm);
return result(message, input, context, null, duration);
}
} catch (MetaborgException e) {
final String message = logger.format("Analysis for {} failed", source);
logger.trace(message, e);
throw new AnalysisException(context, message, e);
}
}
use of org.spoofax.interpreter.terms.IStrategoTerm in project spoofax by metaborg.
the class TaskEngineAnalyzer method analyzeAll.
private ISpoofaxAnalyzeResults analyzeAll(Iterable<ISpoofaxParseUnit> inputs, IContext context, HybridInterpreter runtime, String strategy, ITermFactory termFactory) throws AnalysisException {
final Map<String, ISpoofaxParseUnit> inputsPerSource = Maps.newHashMap();
int detachedCounter = 0;
final Collection<IStrategoAppl> analysisInputs = Lists.newArrayList();
for (ISpoofaxParseUnit input : inputs) {
if (!input.valid()) {
logger.warn("Parse result for {} is invalid, cannot analyze", input.source());
continue;
}
final String pathString;
if (input.detached()) {
pathString = "detached-source-" + detachedCounter++;
logger.debug("Parse input is detached, using '{}' as path", pathString);
} else {
pathString = input.source().getName().getURI();
}
inputsPerSource.put(pathString, input);
final IStrategoString pathTerm = termFactory.makeString(pathString);
final IStrategoReal durationTerm = termFactory.makeReal(input.duration());
analysisInputs.add(termFactory.makeAppl(fileCons, pathTerm, input.ast(), durationTerm));
}
final IStrategoTerm inputTerm = termFactory.makeList(analysisInputs);
logger.trace("Invoking {} strategy", strategy);
final IStrategoTerm resultTerm;
try {
resultTerm = strategoCommon.invoke(runtime, inputTerm, strategy);
} catch (MetaborgException e) {
final String message = analysisCommon.analysisFailedMessage(runtime);
throw new AnalysisException(context, message, e);
}
if (resultTerm == null) {
final String message = analysisCommon.analysisFailedMessage(runtime);
throw new AnalysisException(context, message);
}
if (!(resultTerm instanceof IStrategoAppl) || resultTerm.getSubtermCount() != 5) {
final String message = logger.format("Unexpected results from analysis {}, expected 5-tuple", resultTerm);
throw new AnalysisException(context, message);
}
final IStrategoTerm resultsTerm = resultTerm.getSubterm(0);
final IStrategoTerm updateResultsTerm = resultTerm.getSubterm(1);
final Collection<ISpoofaxAnalyzeUnit> fileResults = Lists.newArrayListWithCapacity(resultsTerm.getSubtermCount());
for (IStrategoTerm result : resultsTerm) {
// HACK: analysis duration per parse unit is unknown, pass -1 as duration.
final ISpoofaxAnalyzeUnit fileResult = result(result, inputsPerSource, context, -1);
if (fileResult == null) {
continue;
}
fileResults.add(fileResult);
}
final Collection<ISpoofaxAnalyzeUnitUpdate> updateResults = Lists.newArrayListWithCapacity(updateResultsTerm.getSubtermCount());
for (IStrategoTerm result : updateResultsTerm) {
final ISpoofaxAnalyzeUnitUpdate updateResult = updateResult(result, context);
if (updateResult == null) {
continue;
}
updateResults.add(updateResult);
}
return new SpoofaxAnalyzeResults(fileResults, updateResults, context);
}
Aggregations