use of org.spoofax.interpreter.terms.IStrategoTerm in project spoofax by metaborg.
the class JSGLRCompletionService method getLeftRecursiveTerms.
private Iterable<IStrategoTerm> getLeftRecursiveTerms(int position, Iterable<IStrategoTerm> terms, Map<IStrategoTerm, Boolean> leftRecursiveTerms) {
Collection<IStrategoTerm> leftRecursive = Lists.newLinkedList();
for (IStrategoTerm term : terms) {
boolean isLeftRecursive = leftRecursiveTerms.containsKey(term);
IToken left = ImploderAttachment.getLeftToken(term);
IToken right = ImploderAttachment.getRightToken(term);
if (isLeftRecursive && position > right.getEndOffset()) {
leftRecursive.add(term);
} else if (term instanceof IStrategoList || left.getStartOffset() > right.getEndOffset()) {
continue;
} else {
break;
}
}
return leftRecursive;
}
use of org.spoofax.interpreter.terms.IStrategoTerm in project spoofax by metaborg.
the class JSGLRCompletionService method getOptionals.
@Nullable
private Iterable<IStrategoTerm> getOptionals(final Iterable<IStrategoTerm> terms, Map<IStrategoTerm, Boolean> leftRecursiveTerms, Map<IStrategoTerm, Boolean> rightRecursiveTerms) {
Collection<IStrategoTerm> optionals = Lists.newLinkedList();
for (IStrategoTerm term : terms) {
IToken left = ImploderAttachment.getLeftToken(term);
IToken right = ImploderAttachment.getRightToken(term);
if (!(term instanceof IStrategoList) && left.getStartOffset() > right.getEndOffset()) {
optionals.add(term);
} else if (term instanceof IStrategoList) {
continue;
// if term is not nullable, nor a list nor left or right recursive stop the search
} else {
boolean isLeftRecursive = leftRecursiveTerms.containsKey(term);
boolean isRightRecursive = rightRecursiveTerms.containsKey(term);
if (!isLeftRecursive && !isRightRecursive) {
break;
}
}
}
return optionals;
}
use of org.spoofax.interpreter.terms.IStrategoTerm 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.interpreter.terms.IStrategoTerm in project spoofax by metaborg.
the class JSGLRCompletionService method findNestedCompletionTerm.
private Collection<IStrategoTerm> findNestedCompletionTerm(final StrategoTerm ast, final boolean excludeIdTerm) {
final Collection<IStrategoTerm> completionTerms = Lists.newLinkedList();
final IStrategoTermVisitor visitor = new AStrategoTermVisitor() {
@Override
public boolean visit(IStrategoTerm term) {
ImploderAttachment ia = term.getAttachment(ImploderAttachment.TYPE);
if (excludeIdTerm && term.equals(ast)) {
return true;
}
if (ia.isNestedCompletion()) {
completionTerms.add(term);
return false;
}
return true;
}
};
StrategoTermVisitee.topdown(visitor, ast);
return completionTerms;
}
use of org.spoofax.interpreter.terms.IStrategoTerm in project spoofax by metaborg.
the class JSGLRCompletionService method getCompletionTermsFromAST.
private Collection<IStrategoTerm> getCompletionTermsFromAST(ISpoofaxParseUnit completionParseResult) {
if (completionParseResult == null) {
return Lists.newLinkedList();
}
StrategoTerm ast = (StrategoTerm) completionParseResult.ast();
if (ast == null) {
return Lists.newLinkedList();
}
Collection<IStrategoTerm> completionTerm = findCompletionTerm(ast);
return completionTerm;
}
Aggregations