use of org.spoofax.interpreter.terms.IStrategoTerm in project spoofax by metaborg.
the class JSGLRCompletionService method getLists.
@Nullable
private Iterable<IStrategoList> getLists(final Iterable<IStrategoTerm> terms, Map<IStrategoTerm, Boolean> leftRecursiveTerms, Map<IStrategoTerm, Boolean> rightRecursiveTerms) {
Collection<IStrategoList> lists = Lists.newLinkedList();
for (IStrategoTerm term : terms) {
if (term instanceof IStrategoList) {
final IStrategoList list = (IStrategoList) term;
lists.add(list);
} else {
IToken left = ImploderAttachment.getLeftToken(term);
IToken right = ImploderAttachment.getRightToken(term);
// if term is not nullable, nor a list nor left or right recursive stop the search
if (left.getStartOffset() <= right.getEndOffset()) {
boolean isLeftRecursive = leftRecursiveTerms.containsKey(term);
boolean isRightRecursive = rightRecursiveTerms.containsKey(term);
if (!isLeftRecursive && !isRightRecursive) {
break;
}
}
}
}
return lists;
}
use of org.spoofax.interpreter.terms.IStrategoTerm in project spoofax by metaborg.
the class JSGLRCompletionService method createCompletionInsertBefore.
private ICompletion createCompletionInsertBefore(String name, String text, String additionalInfo, StrategoAppl change) {
final StrategoTerm oldNode = (StrategoTerm) change.getSubterm(0);
final StrategoTerm newNode = (StrategoTerm) change.getSubterm(1);
// expect two terms and 1st should be an element of a list
final StrategoTerm oldList = (StrategoTerm) ParentAttachment.getParent(oldNode);
if (change.getSubtermCount() != 2 || !(oldNode instanceof IStrategoAppl) || !(newNode instanceof IStrategoList) || !(oldList instanceof IStrategoList)) {
return null;
}
final String sort = ImploderAttachment.getSort(oldNode);
int insertionPoint, suffixPoint;
ITokens tokenizer = ImploderAttachment.getTokenizer(oldNode);
IStrategoTerm[] subterms = oldList.getAllSubterms();
int indexOfElement;
for (indexOfElement = 0; indexOfElement < subterms.length; indexOfElement++) {
if (subterms[indexOfElement] == oldNode)
break;
}
// if inserted element is first (only two elements)
if (indexOfElement == 0) {
// insert after the first non-layout token before the leftmost token of the
// completion node
final ImploderAttachment oldNodeIA = oldNode.getAttachment(ImploderAttachment.TYPE);
int tokenPosition = oldNodeIA.getLeftToken().getIndex() - 1 > 0 ? oldNodeIA.getLeftToken().getIndex() - 1 : 0;
while ((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(indexOfElement - 1);
insertionPoint = elementBefore.getAttachment(ImploderAttachment.TYPE).getRightToken().getEndOffset();
}
// if completion is separated by a newline, preserve indentation of the subsequent node
// else separation follows from the grammar
String separator = "";
for (int i = text.length() - 1; i >= 0; i--) {
if (text.charAt(i) == additionalInfo.charAt(additionalInfo.length() - 1)) {
break;
}
separator = text.charAt(i) + separator;
}
IToken checkToken = oldNode.getAttachment(ImploderAttachment.TYPE).getLeftToken();
int checkTokenIdx = oldNode.getAttachment(ImploderAttachment.TYPE).getLeftToken().getIndex();
suffixPoint = insertionPoint;
if (separator.contains("\n")) {
for (; checkTokenIdx >= 0; checkTokenIdx--) {
checkToken = tokenizer.getTokenAt(checkTokenIdx);
if (tokenizer.toString(checkToken, checkToken).contains("\n")) {
break;
}
suffixPoint = checkToken.getStartOffset();
}
} else {
suffixPoint = checkToken.getStartOffset();
}
return new Completion(name, sort, text, additionalInfo, insertionPoint + 1, suffixPoint, CompletionKind.expansion);
}
use of org.spoofax.interpreter.terms.IStrategoTerm in project spoofax by metaborg.
the class JSGLRCompletionService method getRightRecursiveTerms.
private Iterable<IStrategoTerm> getRightRecursiveTerms(int position, Iterable<IStrategoTerm> terms, Map<IStrategoTerm, Boolean> rightRecursiveTerms) {
Collection<IStrategoTerm> rightRecursive = Lists.newLinkedList();
for (IStrategoTerm term : terms) {
boolean isRightRecursive = rightRecursiveTerms.containsKey(term);
IToken left = ImploderAttachment.getLeftToken(term);
IToken right = ImploderAttachment.getRightToken(term);
if (isRightRecursive && position <= left.getStartOffset()) {
rightRecursive.add(term);
} else if (term instanceof IStrategoList || left.getStartOffset() > right.getEndOffset()) {
continue;
} else {
break;
}
}
return rightRecursive;
}
use of org.spoofax.interpreter.terms.IStrategoTerm 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.IStrategoTerm 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;
}
Aggregations