use of org.spoofax.terms.StrategoAppl in project spoofax by metaborg.
the class JSGLRCompletionService method recursiveCompletions.
private Collection<? extends ICompletion> recursiveCompletions(Iterable<IStrategoTerm> leftRecursive, Iterable<IStrategoTerm> rightRecursive, String languageName, ILanguageComponent component, FileObject location) throws MetaborgException {
Collection<ICompletion> completions = Lists.newLinkedList();
// 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 (IStrategoTerm term : leftRecursive) {
IStrategoTerm sort = termFactory.makeString(ImploderAttachment.getSort(term));
final IStrategoTerm strategoInput = termFactory.makeTuple(sort, term);
IStrategoTerm proposals = null;
try {
proposals = strategoCommon.invoke(runtime, strategoInput, "get-proposals-left-recursive-" + languageName);
} catch (Exception e) {
logger.error("Getting proposals for {} failed", term);
continue;
}
if (proposals == null) {
logger.error("Getting proposals for {} failed", term);
continue;
}
for (IStrategoTerm proposalTerm : proposals) {
final IStrategoTuple tuple = (IStrategoTuple) proposalTerm;
if (tuple.getSubtermCount() != 5 || !(tuple.getSubterm(0) instanceof IStrategoString) || !(tuple.getSubterm(1) instanceof IStrategoString) || !(tuple.getSubterm(2) instanceof IStrategoString) || !(tuple.getSubterm(3) instanceof IStrategoAppl) || !(tuple.getSubterm(4) 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(2));
final StrategoAppl change = (StrategoAppl) tuple.getSubterm(3);
final String prefix = Tools.asJavaString(tuple.getSubterm(4));
if (change.getConstructor().getName().contains("REPLACE_TERM")) {
final ICompletion completion = createCompletionReplaceTerm(name, text, additionalInfo, change, false, prefix, "");
if (completion == null) {
logger.error("Unexpected proposal term {}, skipping", proposalTerm);
continue;
}
completions.add(completion);
}
}
}
for (IStrategoTerm term : rightRecursive) {
IStrategoTerm sort = termFactory.makeString(ImploderAttachment.getSort(term));
final IStrategoTerm strategoInput = termFactory.makeTuple(sort, term);
IStrategoTerm proposals = null;
try {
proposals = strategoCommon.invoke(runtime, strategoInput, "get-proposals-right-recursive-" + languageName);
} catch (Exception e) {
logger.error("Getting proposals for {} failed", term);
continue;
}
if (proposals == null) {
logger.error("Getting proposals for {} failed", term);
continue;
}
for (IStrategoTerm proposalTerm : proposals) {
final IStrategoTuple tuple = (IStrategoTuple) proposalTerm;
if (tuple.getSubtermCount() != 5 || !(tuple.getSubterm(0) instanceof IStrategoString) || !(tuple.getSubterm(1) instanceof IStrategoString) || !(tuple.getSubterm(2) instanceof IStrategoString) || !(tuple.getSubterm(3) instanceof IStrategoAppl) || !(tuple.getSubterm(4) 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(2));
final StrategoAppl change = (StrategoAppl) tuple.getSubterm(3);
final String suffix = Tools.asJavaString(tuple.getSubterm(4));
if (change.getConstructor().getName().contains("REPLACE_TERM")) {
final ICompletion completion = createCompletionReplaceTerm(name, text, additionalInfo, change, false, "", suffix);
if (completion == null) {
logger.error("Unexpected proposal term {}, skipping", proposalTerm);
continue;
}
completions.add(completion);
}
}
}
return completions;
}
use of org.spoofax.terms.StrategoAppl in project spoofax by metaborg.
the class JSGLRCompletionService method getNestedCompletionTermsFromAST.
private Collection<IStrategoTerm> getNestedCompletionTermsFromAST(ISpoofaxParseUnit completionParseResult) {
if (completionParseResult == null) {
return Lists.newLinkedList();
}
StrategoAppl ast = (StrategoAppl) completionParseResult.ast();
if (ast == null) {
return Lists.newLinkedList();
}
Collection<IStrategoTerm> completionTerm = findNestedCompletionTerm(ast, false);
return completionTerm;
}
use of org.spoofax.terms.StrategoAppl in project spoofax by metaborg.
the class JSGLRCompletionService method listsCompletions.
public Collection<ICompletion> listsCompletions(int position, boolean blankLineCompletion, Iterable<IStrategoList> lists, String languageName, ILanguageComponent component, FileObject location) throws MetaborgException {
Collection<ICompletion> completions = Lists.newLinkedList();
final ITermFactory termFactory = termFactoryService.get(component, null, false);
for (IStrategoList list : lists) {
ListImploderAttachment attachment = list.getAttachment(null);
String sort = attachment.getSort().substring(0, attachment.getSort().length() - 1);
String placeholderName = sort + "-Plhdr";
IStrategoAppl listPlaceholder = termFactory.makeAppl(termFactory.makeConstructor(placeholderName, 0));
final IStrategoTerm strategoInput = termFactory.makeTuple(termFactory.makeString(sort), list, listPlaceholder, termFactory.makeInt(position));
final HybridInterpreter runtime = strategoRuntimeService.runtime(component, location, false);
final IStrategoTerm proposalsLists = strategoCommon.invoke(runtime, strategoInput, "get-proposals-list-" + languageName);
if (proposalsLists == null) {
logger.error("Getting proposals for {} failed", strategoInput);
continue;
}
for (IStrategoTerm proposalTerm : proposalsLists) {
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 the change is inserting at the end of a list
if (change.getConstructor().getName().contains("INSERT_AT_END")) {
final ICompletion completion = createCompletionInsertAtEnd(name, text, additionalInfo, change, blankLineCompletion);
if (completion == null) {
logger.error("Unexpected proposal term {}, skipping", proposalTerm);
continue;
}
completions.add(completion);
} else if (change.getConstructor().getName().contains("INSERT_BEFORE")) {
final ICompletion completion = createCompletionInsertBefore(name, text, additionalInfo, change);
if (completion == null) {
logger.error("Unexpected proposal term {}, skipping", proposalTerm);
continue;
}
completions.add(completion);
}
}
}
return completions;
}
use of org.spoofax.terms.StrategoAppl in project spoofax by metaborg.
the class JSGLRCompletionService method placeholderCompletions.
public Collection<ICompletion> placeholderCompletions(IStrategoAppl placeholder, String languageName, ILanguageComponent component, FileObject location) throws MetaborgException {
Collection<ICompletion> completions = Lists.newLinkedList();
// 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);
IStrategoTerm placeholderParent = ParentAttachment.getParent(placeholder);
if (placeholderParent == null) {
placeholderParent = placeholder;
}
IStrategoInt placeholderIdx = termFactory.makeInt(-1);
for (int i = 0; i < placeholderParent.getSubtermCount(); i++) {
if (placeholderParent.getSubterm(i) == placeholder) {
placeholderIdx = termFactory.makeInt(i);
}
}
final String sort = ImploderAttachment.getSort(placeholder);
final IStrategoTerm strategoInput = termFactory.makeTuple(termFactory.makeString(sort), placeholder, placeholderParent, placeholderIdx);
final IStrategoTerm proposalsPlaceholder = strategoCommon.invoke(runtime, strategoInput, "get-proposals-placeholder-" + languageName);
if (proposalsPlaceholder == null) {
logger.error("Getting proposals for {} failed", placeholder);
return completions;
}
for (IStrategoTerm proposalTerm : proposalsPlaceholder) {
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, false, "", "");
if (completion == null) {
logger.error("Unexpected proposal term {}, skipping", proposalTerm);
continue;
}
completions.add(completion);
}
}
return completions;
}
use of org.spoofax.terms.StrategoAppl 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;
}
Aggregations