use of org.spoofax.interpreter.terms.IStrategoTerm in project spoofax by metaborg.
the class JSGLRCompletionService method findCompletionTermInsideNested.
private Collection<IStrategoTerm> findCompletionTermInsideNested(final StrategoTerm ast) {
final Collection<IStrategoTerm> completionTerms = Lists.newLinkedList();
final IStrategoTermVisitor visitor = new AStrategoTermVisitor() {
@Override
public boolean visit(IStrategoTerm term) {
ImploderAttachment ia = term.getAttachment(ImploderAttachment.TYPE);
if (ia.isNestedCompletion() && !term.equals(ast)) {
return false;
}
if (ia.isCompletion()) {
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 completionErroneousPrograms.
public Collection<ICompletion> completionErroneousPrograms(int cursorPosition, Iterable<IStrategoTerm> completionTerms, 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();
final Collection<IStrategoTerm> proposalsTerm = Lists.newLinkedList();
for (ILanguageComponent component : language.components()) {
final ITermFactory termFactory = termFactoryService.get(component, null, false);
for (IStrategoTerm completionTerm : completionTerms) {
IStrategoTerm completionAst = completionParseResult.ast();
final StrategoTerm topMostAmb = findTopMostAmbNode((StrategoTerm) completionTerm);
if (ImploderAttachment.get(completionTerm).isSinglePlaceholderCompletion()) {
Collection<IStrategoTerm> placeholders = Lists.newLinkedList();
placeholders.addAll(findPlaceholderTerms(completionTerm));
if (placeholders.size() != 1) {
logger.error("Getting proposals for {} failed", completionTerm);
continue;
}
IStrategoAppl placeholderTerm = (IStrategoAppl) Iterables.get(placeholders, 0);
IStrategoAppl placeholder = termFactory.makeAppl(termFactory.makeConstructor(placeholderTerm.getConstructor().getName(), 0));
IStrategoTerm parenthesized = parenthesizeTerm(completionTerm, termFactory);
final IStrategoTerm inputStratego = termFactory.makeTuple(termFactory.makeString(ImploderAttachment.getElementSort(parenthesized)), completionAst, completionTerm, topMostAmb, parenthesized, placeholder, placeholderTerm);
final HybridInterpreter runtime = strategoRuntimeService.runtime(component, location, false);
final IStrategoTerm proposalTerm = strategoCommon.invoke(runtime, inputStratego, "get-proposals-incorrect-programs-single-placeholder-" + languageName);
if (proposalTerm == null || !(proposalTerm instanceof IStrategoList)) {
logger.error("Getting proposals for {} failed", completionTerm);
continue;
}
for (IStrategoTerm proposalPlaceholder : proposalTerm) {
proposalsTerm.add(proposalPlaceholder);
}
} else {
IStrategoTerm parenthesized = parenthesizeTerm(completionTerm, termFactory);
final IStrategoTerm inputStratego = termFactory.makeTuple(termFactory.makeString(ImploderAttachment.getElementSort(parenthesized)), completionAst, completionTerm, topMostAmb, parenthesized);
final HybridInterpreter runtime = strategoRuntimeService.runtime(component, location, false);
final IStrategoTerm proposalTerm = strategoCommon.invoke(runtime, inputStratego, "get-proposals-incorrect-programs-" + languageName);
if (proposalTerm == null) {
logger.error("Getting proposals for {} failed", completionTerm);
continue;
}
proposalsTerm.add(proposalTerm);
}
}
for (IStrategoTerm proposalTerm : proposalsTerm) {
if (!(proposalTerm instanceof IStrategoTuple)) {
logger.error("Unexpected proposal term {}, skipping", proposalTerm);
continue;
}
final IStrategoTuple tuple = (IStrategoTuple) proposalTerm;
if (tuple.getSubtermCount() != 6 || !(tuple.getSubterm(0) instanceof IStrategoString) || !(tuple.getSubterm(1) instanceof IStrategoString) || !(tuple.getSubterm(2) instanceof IStrategoString) || !(tuple.getSubterm(3) instanceof IStrategoAppl) || (tuple.getSubterm(4) == null) || !(tuple.getSubterm(5) instanceof IStrategoString)) {
logger.error("Unexpected proposal term {}, skipping", proposalTerm);
continue;
}
final String name = Tools.asJavaString(tuple.getSubterm(0));
String text = Tools.asJavaString(tuple.getSubterm(1));
String additionalInfo = Tools.asJavaString(tuple.getSubterm(2));
final StrategoAppl change = (StrategoAppl) tuple.getSubterm(3);
final StrategoTerm completionTerm = (StrategoTerm) tuple.getSubterm(4);
final String completionKind = Tools.asJavaString(tuple.getSubterm(5));
String prefix = calculatePrefix(cursorPosition, completionTerm);
String suffix = calculateSuffix(cursorPosition, completionTerm);
// 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", proposalTerm);
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", proposalTerm);
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", proposalTerm);
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", proposalTerm);
continue;
}
completions.add(completion);
}
}
}
return completions;
}
use of org.spoofax.interpreter.terms.IStrategoTerm in project spoofax by metaborg.
the class AnalysisCommon method messages.
public Collection<IMessage> messages(FileObject resource, MessageSeverity severity, IStrategoTerm messagesTerm) {
final Collection<IMessage> messages = Lists.newArrayListWithExpectedSize(messagesTerm.getSubtermCount());
for (IStrategoTerm term : messagesTerm.getAllSubterms()) {
final IStrategoTerm originTerm;
final String message;
if (term.getSubtermCount() == 2) {
originTerm = term.getSubterm(0);
message = toString(term.getSubterm(1));
} else {
originTerm = term;
message = toString(term) + " (no tree node indicated)";
}
if (originTerm != null) {
final ISourceLocation location = tracingService.location(originTerm);
if (location != null) {
final ISourceRegion region = location.region();
messages.add(message(resource, region, message, severity));
} else {
messages.add(message(resource, message, severity));
}
} else {
messages.add(message(resource, message, severity));
}
}
return messages;
}
use of org.spoofax.interpreter.terms.IStrategoTerm in project spoofax by metaborg.
the class AnalysisCommon method ambiguityMessages.
public Collection<IMessage> ambiguityMessages(final FileObject resource, IStrategoTerm ast) {
final Collection<IMessage> messages = Lists.newLinkedList();
final TermVisitor termVisitor = new TermVisitor() {
private IStrategoTerm ambStart;
@Override
public void preVisit(IStrategoTerm term) {
if (ambStart == null && "amb".equals(Term.tryGetName(term))) {
final String text = "Fragment is ambiguous: " + ambToString(term);
final ISourceLocation location = tracingService.location(term);
if (location != null) {
final ISourceRegion region = location.region();
messages.add(message(resource, region, text, MessageSeverity.WARNING));
} else {
messages.add(message(resource, text, MessageSeverity.WARNING));
}
ambStart = term;
}
}
@Override
public void postVisit(IStrategoTerm term) {
if (term == ambStart) {
ambStart = null;
}
}
private String ambToString(IStrategoTerm amb) {
final String result = amb.toString();
return result.length() > 5000 ? result.substring(0, 5000) + "..." : result;
}
};
termVisitor.visit(ast);
return messages;
}
use of org.spoofax.interpreter.terms.IStrategoTerm in project spoofax by metaborg.
the class HoverService method hover.
@Override
public Hover hover(int offset, ISpoofaxAnalyzeUnit result) throws MetaborgException {
if (!result.valid() || !result.hasAst()) {
return null;
}
final FileObject source = result.source();
final IContext context = result.context();
final ILanguageImpl language = context.language();
final FacetContribution<HoverFacet> facetContrib = facet(language);
final HoverFacet facet = facetContrib.facet;
final String strategy = facet.strategyName;
try {
final IProject project = context.project();
final ITermFactory termFactory = termFactoryService.get(facetContrib.contributor, project, true);
final HybridInterpreter interpreter = strategoRuntimeService.runtime(facetContrib.contributor, context, true);
final Iterable<IStrategoTerm> inRegion = tracingService.fragments(result, new SourceRegion(offset));
final TermWithRegion tuple;
try (IClosableLock lock = context.read()) {
tuple = common.outputs(termFactory, interpreter, context.location(), source, result.ast(), inRegion, strategy);
}
return hover(tuple);
} catch (MetaborgException e) {
throw new MetaborgException("Getting hover tooltip information failed unexpectedly", e);
}
}
Aggregations