use of org.spoofax.interpreter.terms.ITermFactory 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.ITermFactory in project spoofax by metaborg.
the class TaskEngineAnalyzer method analyzeAll.
@Override
public ISpoofaxAnalyzeResults analyzeAll(Iterable<ISpoofaxParseUnit> inputs, IContext context, IProgress progress, ICancel cancel) throws AnalysisException, InterruptedException {
cancel.throwIfCancelled();
final ILanguageImpl langImpl = context.language();
final ITermFactory termFactory = termFactoryService.getGeneric();
final FacetContribution<AnalysisFacet> facetContribution = langImpl.facetContribution(AnalysisFacet.class);
if (facetContribution == null) {
logger.debug("No analysis required for {}", langImpl);
return new SpoofaxAnalyzeResults(context);
}
final AnalysisFacet facet = facetContribution.facet;
cancel.throwIfCancelled();
final HybridInterpreter runtime;
try {
runtime = runtimeService.runtime(facetContribution.contributor, context, false);
} catch (MetaborgException e) {
throw new AnalysisException(context, "Failed to get Stratego runtime", e);
}
cancel.throwIfCancelled();
return analyzeAll(inputs, context, runtime, facet.strategyName, termFactory);
}
use of org.spoofax.interpreter.terms.ITermFactory in project spoofax by metaborg.
the class JSGLRCompletionService method completionErroneousProgramsNested.
public Collection<? extends ICompletion> completionErroneousProgramsNested(int cursorPosition, Collection<IStrategoTerm> nestedCompletionTerms, ISpoofaxParseUnit completionParseResult) throws MetaborgException {
final FileObject location = completionParseResult.source();
final ILanguageImpl language = completionParseResult.input().langImpl();
final String languageName = language.belongsTo().name();
final Collection<ICompletion> completions = Lists.newLinkedList();
IStrategoTerm completionAst = completionParseResult.ast();
for (ILanguageComponent component : language.components()) {
final ITermFactory termFactory = termFactoryService.get(component, null, false);
for (IStrategoTerm nestedCompletionTerm : nestedCompletionTerms) {
final HybridInterpreter runtime = strategoRuntimeService.runtime(component, location, false);
Collection<IStrategoTerm> inputsStrategoNested = Lists.newLinkedList();
// calculate direct proposals
inputsStrategoNested.addAll(calculateDirectCompletionProposals(nestedCompletionTerm, termFactory, completionAst, languageName, runtime));
// calculate inner nested proposals
Collection<IStrategoTerm> innerNestedCompletionTerms = findNestedCompletionTerm((StrategoTerm) nestedCompletionTerm, true);
for (IStrategoTerm innerNestedCompletionTerm : innerNestedCompletionTerms) {
inputsStrategoNested.addAll(calculateNestedCompletionProposals(nestedCompletionTerm, innerNestedCompletionTerm, termFactory, completionAst, languageName, runtime));
}
for (IStrategoTerm inputStrategoNested : inputsStrategoNested) {
final IStrategoTerm proposalTermNested = strategoCommon.invoke(runtime, inputStrategoNested, "get-proposals-incorrect-programs-nested-" + languageName);
if (proposalTermNested == null) {
logger.error("Getting proposals for {} failed", inputStrategoNested);
continue;
}
final String name = Tools.asJavaString(proposalTermNested.getSubterm(0));
final String text = Tools.asJavaString(proposalTermNested.getSubterm(1));
final String additionalInfo = Tools.asJavaString(proposalTermNested.getSubterm(2));
final StrategoAppl change = (StrategoAppl) proposalTermNested.getSubterm(3);
final StrategoTerm completionTerm = (StrategoTerm) proposalTermNested.getSubterm(4);
String prefix = calculatePrefix(cursorPosition, completionTerm);
String suffix = calculateSuffix(cursorPosition, completionTerm);
String completionKind = "recovery";
// if the change is inserting at the end of a list
if (change.getConstructor().getName().contains("INSERT_AT_END")) {
// calls a different method because now, the program has errors that should be fixed
final ICompletion completion = createCompletionInsertAtEndFixing(name, text, additionalInfo, prefix, suffix, change, completionKind);
if (completion == null) {
logger.error("Unexpected proposal term {}, skipping", proposalTermNested);
continue;
}
completions.add(completion);
} else if (change.getConstructor().getName().contains("INSERT_BEFORE")) {
final ICompletion completion = createCompletionInsertBeforeFixing(name, text, additionalInfo, prefix, suffix, change, completionKind);
if (completion == null) {
logger.error("Unexpected proposal term {}, skipping", proposalTermNested);
continue;
}
completions.add(completion);
} else if (change.getConstructor().getName().contains("INSERTION_TERM")) {
final ICompletion completion = createCompletionInsertionTermFixing(name, text, additionalInfo, prefix, suffix, change, completionKind);
if (completion == null) {
logger.error("Unexpected proposal term {}, skipping", proposalTermNested);
continue;
}
completions.add(completion);
} else if (change.getConstructor().getName().contains("REPLACE_TERM")) {
final ICompletion completion = createCompletionReplaceTermFixing(name, text, additionalInfo, prefix, suffix, change, completionKind);
if (completion == null) {
logger.error("Unexpected proposal term {}, skipping", proposalTermNested);
continue;
}
completions.add(completion);
}
}
}
}
return completions;
}
use of org.spoofax.interpreter.terms.ITermFactory in project spoofax by metaborg.
the class JSGLRCompletionService method optionalCompletions.
public Collection<ICompletion> optionalCompletions(Iterable<IStrategoTerm> optionals, boolean blankLineCompletion, String languageName, ILanguageComponent component, FileObject location) throws MetaborgException {
Collection<ICompletion> completions = Lists.newLinkedList();
final ITermFactory termFactory = termFactoryService.get(component, null, false);
for (IStrategoTerm optional : optionals) {
ImploderAttachment attachment = optional.getAttachment(ImploderAttachment.TYPE);
String sort = attachment.getSort().substring(0, attachment.getSort().length());
String placeholderName = sort + "-Plhdr";
IStrategoAppl optionalPlaceholder = termFactory.makeAppl(termFactory.makeConstructor(placeholderName, 0));
final IStrategoTerm strategoInput = termFactory.makeTuple(termFactory.makeString(sort), optional, optionalPlaceholder);
// call Stratego part of the framework to compute change
final HybridInterpreter runtime = strategoRuntimeService.runtime(component, location, false);
final IStrategoTerm proposalsOptional = strategoCommon.invoke(runtime, strategoInput, "get-proposals-optional-" + languageName);
if (proposalsOptional == null) {
logger.error("Getting proposals for {} failed", strategoInput);
continue;
}
for (IStrategoTerm proposalTerm : proposalsOptional) {
if (!(proposalTerm instanceof IStrategoTuple)) {
logger.error("Unexpected proposal term {}, skipping", proposalTerm);
continue;
}
final IStrategoTuple tuple = (IStrategoTuple) proposalTerm;
if (tuple.getSubtermCount() != 4 || !(tuple.getSubterm(0) instanceof IStrategoString) || !(tuple.getSubterm(1) instanceof IStrategoString) || !(tuple.getSubterm(2) instanceof IStrategoString) || !(tuple.getSubterm(3) instanceof IStrategoAppl)) {
logger.error("Unexpected proposal term {}, skipping", proposalTerm);
continue;
}
final String name = Tools.asJavaString(tuple.getSubterm(0));
final String text = Tools.asJavaString(tuple.getSubterm(1));
final String additionalInfo = Tools.asJavaString(tuple.getSubterm(2));
final StrategoAppl change = (StrategoAppl) tuple.getSubterm(3);
if (change.getConstructor().getName().contains("REPLACE_TERM")) {
final ICompletion completion = createCompletionReplaceTerm(name, text, additionalInfo, change, blankLineCompletion, "", "");
if (completion == null) {
logger.error("Unexpected proposal term {}, skipping", proposalTerm);
continue;
}
completions.add(completion);
}
}
}
return completions;
}
use of org.spoofax.interpreter.terms.ITermFactory in project spoofax by metaborg.
the class JSGLRCompletionService method completionEmptyProgram.
public Collection<? extends ICompletion> completionEmptyProgram(Iterable<String> startSymbols, int endOffset, ILanguageImpl language, FileObject location) throws MetaborgException {
Collection<ICompletion> completions = Lists.newLinkedList();
final String languageName = language.belongsTo().name();
for (ILanguageComponent component : language.components()) {
// call Stratego part of the framework to compute change
final HybridInterpreter runtime = strategoRuntimeService.runtime(component, location, false);
final ITermFactory termFactory = termFactoryService.get(component, null, false);
for (String startSymbol : startSymbols) {
String placeholderName = startSymbol + "-Plhdr";
IStrategoAppl placeholder = termFactory.makeAppl(termFactory.makeConstructor(placeholderName, 0));
IStrategoTuple input = termFactory.makeTuple(termFactory.makeString(startSymbol), placeholder);
final IStrategoTerm proposalsPlaceholder = strategoCommon.invoke(runtime, input, "get-proposals-empty-program-" + languageName);
if (proposalsPlaceholder == null) {
logger.error("Getting proposals for {} failed", placeholder);
continue;
}
for (IStrategoTerm proposalTerm : proposalsPlaceholder) {
if (!(proposalTerm instanceof IStrategoTuple)) {
logger.error("Unexpected proposal term {}, skipping", proposalTerm);
continue;
}
final IStrategoTuple tuple = (IStrategoTuple) proposalTerm;
if (tuple.getSubtermCount() != 2 || !(tuple.getSubterm(0) instanceof IStrategoString) || !(tuple.getSubterm(1) instanceof IStrategoString)) {
logger.error("Unexpected proposal term {}, skipping", proposalTerm);
continue;
}
final String name = Tools.asJavaString(tuple.getSubterm(0));
final String text = Tools.asJavaString(tuple.getSubterm(1));
final String additionalInfo = Tools.asJavaString(tuple.getSubterm(1));
completions.add(new Completion(name, startSymbol, text, additionalInfo, 0, endOffset, CompletionKind.expansion));
}
}
}
return completions;
}
Aggregations