use of org.spoofax.interpreter.terms.IStrategoTerm in project spoofax by metaborg.
the class JSGLRCompletionService method calculateDirectCompletionProposals.
private Collection<IStrategoTerm> calculateDirectCompletionProposals(IStrategoTerm nestedCompletionTerm, ITermFactory termFactory, IStrategoTerm completionAst, String languageName, HybridInterpreter runtime) throws MetaborgException {
Collection<IStrategoTerm> inputsStratego = Lists.newLinkedList();
Collection<IStrategoTerm> completionTerms = findCompletionTermInsideNested((StrategoTerm) nestedCompletionTerm);
for (IStrategoTerm completionTerm : completionTerms) {
final StrategoTerm topMostCompletionTerm = findTopMostCompletionNode((StrategoTerm) completionTerm);
final StrategoTerm topMostAmb = findTopMostAmbNode(topMostCompletionTerm);
IStrategoTerm parenthesized = parenthesizeTerm(topMostCompletionTerm, termFactory);
final IStrategoTerm inputStratego = termFactory.makeTuple(termFactory.makeString(ImploderAttachment.getElementSort(parenthesized)), completionAst, completionTerm, topMostAmb, parenthesized);
final IStrategoTerm proposalTerm = strategoCommon.invoke(runtime, inputStratego, "get-proposals-incorrect-programs-" + languageName);
if (proposalTerm == null) {
logger.error("Getting proposals for {} failed", inputStratego);
continue;
}
final IStrategoTerm replaceTermText = termFactory.makeAppl(new StrategoConstructor("REPLACE_TERM_TEXT", 2), topMostAmb, proposalTerm.getSubterm(1));
IStrategoTerm parenthesizedNested = parenthesizeTerm(nestedCompletionTerm, termFactory);
final IStrategoTerm inputStrategoNested = termFactory.makeTuple(termFactory.makeString(ImploderAttachment.getElementSort(parenthesizedNested)), completionAst, nestedCompletionTerm, proposalTerm.getSubterm(0), replaceTermText, parenthesizedNested);
inputsStratego.add(inputStrategoNested);
}
return inputsStratego;
}
use of org.spoofax.interpreter.terms.IStrategoTerm 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;
}
use of org.spoofax.interpreter.terms.IStrategoTerm in project spoofax by metaborg.
the class JSGLRCompletionService method tracingTermsCompletions.
private Iterable<IStrategoTerm> tracingTermsCompletions(final int position, Object result, final ISourceRegion region, final HybridInterpreter runtime, final ITermFactory termFactory, final String languageName, final Map<IStrategoTerm, Boolean> leftRecursiveTerms, final Map<IStrategoTerm, Boolean> rightRecursiveTerms) {
if (result == 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 = fromTokens(term, runtime, termFactory, position, languageName, leftRecursiveTerms, rightRecursiveTerms);
if (location != null && location.region().contains(region)) {
parsed.add(term);
return false;
}
return true;
}
};
StrategoTermVisitee.bottomup(visitor, (IStrategoTerm) result);
return parsed;
}
use of org.spoofax.interpreter.terms.IStrategoTerm in project spoofax by metaborg.
the class JSGLRCompletionService method calculateNestedCompletionProposals.
private Collection<IStrategoTerm> calculateNestedCompletionProposals(IStrategoTerm mainNestedCompletionTerm, IStrategoTerm nestedCompletionTerm, ITermFactory termFactory, IStrategoTerm completionAst, String languageName, HybridInterpreter runtime) throws MetaborgException {
Collection<IStrategoTerm> inputsStratego = Lists.newLinkedList();
Collection<IStrategoTerm> nestedCompletionTerms = findNestedCompletionTerm((StrategoTerm) nestedCompletionTerm, true);
for (IStrategoTerm innerNestedCompletionTerm : nestedCompletionTerms) {
Collection<IStrategoTerm> inputsStrategoInnerNested = calculateNestedCompletionProposals(nestedCompletionTerm, innerNestedCompletionTerm, termFactory, completionAst, languageName, runtime);
for (IStrategoTerm inputStrategoNested : inputsStrategoInnerNested) {
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 StrategoTerm topMostAmb = findTopMostAmbNode((StrategoTerm) nestedCompletionTerm);
final IStrategoTerm replaceTermText = termFactory.makeAppl(new StrategoConstructor("REPLACE_TERM_TEXT", 2), topMostAmb, proposalTermNested.getSubterm(1));
IStrategoTerm parenthesized = parenthesizeTerm(mainNestedCompletionTerm, termFactory);
final IStrategoTerm inputStrategoInnerNested = termFactory.makeTuple(termFactory.makeString(ImploderAttachment.getElementSort(parenthesized)), completionAst, mainNestedCompletionTerm, proposalTermNested.getSubterm(0), replaceTermText, parenthesized);
inputsStratego.add(inputStrategoInnerNested);
}
}
Collection<IStrategoTerm> inputsStrategoInner = calculateDirectCompletionProposals(nestedCompletionTerm, termFactory, completionAst, languageName, runtime);
for (IStrategoTerm inputStrategoNested : inputsStrategoInner) {
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 StrategoTerm topMostAmb = findTopMostAmbNode((StrategoTerm) nestedCompletionTerm);
final IStrategoTerm replaceTermText = termFactory.makeAppl(new StrategoConstructor("REPLACE_TERM_TEXT", 2), topMostAmb, proposalTermNested.getSubterm(1));
IStrategoTerm parenthesized = parenthesizeTerm(mainNestedCompletionTerm, termFactory);
final IStrategoTerm inputStrategoInnerNested = termFactory.makeTuple(termFactory.makeString(ImploderAttachment.getElementSort(parenthesized)), completionAst, mainNestedCompletionTerm, proposalTermNested.getSubterm(0), replaceTermText, parenthesized);
inputsStratego.add(inputStrategoInnerNested);
}
return inputsStratego;
}
use of org.spoofax.interpreter.terms.IStrategoTerm in project spoofax by metaborg.
the class JSGLRCompletionService method createCompletionInsertBeforeFixing.
private ICompletion createCompletionInsertBeforeFixing(String name, String text, String additionalInfo, String prefix, String suffix, StrategoAppl change, String completionKind) {
// expect two terms and 1st should be an element of a list
final StrategoTerm oldNode = (StrategoTerm) change.getSubterm(0);
final StrategoTerm newNode = (StrategoTerm) change.getSubterm(1);
final StrategoTerm oldList = (StrategoTerm) ParentAttachment.getParent(oldNode);
if (change.getSubtermCount() != 2 || !(oldNode instanceof IStrategoAppl) || !(newNode instanceof IStrategoAppl) || !(oldList instanceof IStrategoList)) {
return null;
}
final String sort = ImploderAttachment.getSort(oldNode);
int insertionPoint, suffixPoint;
IStrategoTerm[] subterms = ((IStrategoList) oldList).getAllSubterms();
int indexOfCompletion;
for (indexOfCompletion = 0; indexOfCompletion < subterms.length; indexOfCompletion++) {
if (subterms[indexOfCompletion] == oldNode)
break;
}
// if inserted element is first (only two elements)
if (indexOfCompletion == 1) {
// insert after the first non-layout token before the leftmost token of the list
ITokens tokenizer = ImploderAttachment.getTokenizer(oldList);
// to avoid keeping duplicate tokens due to ambiguity
IStrategoTerm topMostAmbOldList = findTopMostAmbNode(oldList);
final ImploderAttachment oldListIA = topMostAmbOldList.getAttachment(ImploderAttachment.TYPE);
int tokenPosition = oldListIA.getLeftToken().getIndex() - 1 > 0 ? oldListIA.getLeftToken().getIndex() - 1 : 0;
while ((checkEmptyOffset(tokenizer.getTokenAt(tokenPosition)) || tokenizer.getTokenAt(tokenPosition).getKind() == IToken.TK_LAYOUT || tokenizer.getTokenAt(tokenPosition).getKind() == IToken.TK_ERROR) && tokenPosition > 0) tokenPosition--;
insertionPoint = tokenizer.getTokenAt(tokenPosition).getEndOffset();
} else {
// if inserted element is not first
// insert after at end offset of the rightmost token of the element before the completion
StrategoTerm elementBefore = (StrategoTerm) oldList.getSubterm(indexOfCompletion - 2);
insertionPoint = elementBefore.getAttachment(ImploderAttachment.TYPE).getRightToken().getEndOffset();
}
// suffix point should be the first token of the next element
suffixPoint = oldNode.getAttachment(ImploderAttachment.TYPE).getLeftToken().getStartOffset();
CompletionKind kind;
if (completionKind.equals("recovery")) {
kind = CompletionKind.recovery;
} else if (completionKind.equals("expansionEditing")) {
kind = CompletionKind.expansionEditing;
} else {
kind = CompletionKind.expansion;
}
return new Completion(name, sort, text, additionalInfo, insertionPoint + 1, suffixPoint, kind, prefix, suffix);
}
Aggregations