use of org.spoofax.interpreter.terms.IStrategoTuple in project spoofax by metaborg.
the class StrategoAnalyzer method analyze.
private ISpoofaxAnalyzeUnit analyze(ISpoofaxParseUnit input, IContext context, HybridInterpreter runtime, String strategy, ITermFactory termFactory) throws AnalysisException {
final FileObject source = input.source();
final IStrategoString contextPath = strategoCommon.locationTerm(context.location());
final IStrategoString resourcePath = strategoCommon.resourceTerm(source, context.location());
final IStrategoTuple inputTerm = termFactory.makeTuple(input.ast(), resourcePath, contextPath);
try {
logger.trace("Analysing {}", source);
final Timer timer = new Timer(true);
final IStrategoTerm resultTerm = strategoCommon.invoke(runtime, inputTerm, strategy);
final long duration = timer.stop();
if (resultTerm == null) {
logger.trace("Analysis for {} failed", source);
return result(analysisCommon.analysisFailedMessage(runtime), input, context, null, duration);
} else if (!(resultTerm instanceof IStrategoTuple)) {
logger.trace("Analysis for {} has unexpected result, not a tuple", source);
final String message = logger.format("Unexpected results from analysis {}", resultTerm);
return result(message, input, context, null, duration);
} else if (resultTerm.getSubtermCount() == 4) {
logger.trace("Analysis for {} done", source);
return result(resultTerm, input, context, duration);
} else if (resultTerm.getSubtermCount() == 3) {
logger.trace("Analysis for {} done", source);
return resultNoAst(resultTerm, input, context, duration);
} else {
logger.trace("Analysis for {} has unexpected result; tuple with more than 4 or less than 2 elements", source);
final String message = logger.format("Unexpected results from analysis {}", resultTerm);
return result(message, input, context, null, duration);
}
} catch (MetaborgException e) {
final String message = logger.format("Analysis for {} failed", source);
logger.trace(message, e);
throw new AnalysisException(context, message, e);
}
}
use of org.spoofax.interpreter.terms.IStrategoTuple 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;
}
use of org.spoofax.interpreter.terms.IStrategoTuple 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.IStrategoTuple 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.IStrategoTuple in project spoofax by metaborg.
the class StrategoTransformer method transform.
private <I extends IUnit> ISpoofaxTransformUnit<I> transform(I input, IContext context, TransformActionContrib actionContribution, FileObject source, IStrategoTerm term, ITransformConfig config) throws TransformException {
final FileObject location = context.location();
final ILanguageComponent component = actionContribution.contributor;
final TransformAction action = action(actionContribution.action);
// Get input term
final IStrategoTerm inputTerm = common.builderInputTerm(term, source, location);
// Get Stratego runtime
final HybridInterpreter runtime;
try {
runtime = strategoRuntimeService.runtime(component, context, true);
} catch (MetaborgException e) {
throw new TransformException("Transformation failed unexpectedly; cannot get Stratego interpreter", e);
}
// Transform
logger.debug("Transforming {} with '{}'", source, action.name);
final Timer timer = new Timer(true);
final IStrategoTerm outputTerm;
try {
outputTerm = common.invoke(runtime, inputTerm, action.strategy);
} catch (MetaborgException e) {
throw new TransformException(e.getMessage(), e.getCause());
}
final long duration = timer.stop();
if (outputTerm == null) {
final String message = logger.format("Invoking Stratego strategy {} failed", action.strategy);
throw new TransformException(message);
}
// Get the result and, if allowed and required, write to file
List<TransformOutput> outputs;
IStrategoTerm resultTerm;
if (outputTerm.getSubtermCount() == 2 && (outputTerm instanceof IStrategoTuple)) {
final IStrategoTerm resourceTerm = outputTerm.getSubterm(0);
final IStrategoTerm contentTerm = outputTerm.getSubterm(1);
try {
if (resourceTerm instanceof IStrategoString) {
resultTerm = contentTerm;
outputs = Lists.newArrayList(output(resourceTerm, contentTerm, location, config));
} else if (resourceTerm instanceof IStrategoList) {
if (!(contentTerm instanceof IStrategoList) || resourceTerm.getSubtermCount() != contentTerm.getSubtermCount()) {
logger.error("List of terms does not match list of file names, cannot write to file.");
resultTerm = null;
outputs = Collections.emptyList();
} else {
outputs = Lists.newArrayListWithExpectedSize(resourceTerm.getSubtermCount());
for (int i = 0; i < resourceTerm.getSubtermCount(); i++) {
outputs.add(output(resourceTerm.getSubterm(i), contentTerm.getSubterm(i), location, config));
}
resultTerm = resourceTerm.getSubtermCount() == 1 ? resourceTerm.getSubterm(0) : null;
}
} else {
logger.error("First term of result tuple {} is neither a string, nor a list, cannot write output file", resourceTerm);
resultTerm = null;
outputs = Collections.emptyList();
}
} catch (MetaborgException ex) {
resultTerm = null;
outputs = Collections.emptyList();
}
} else {
resultTerm = outputTerm;
outputs = Collections.emptyList();
}
// Open editor
if (action.flags.openEditor) {
List<FileObject> resources = Lists.newArrayListWithExpectedSize(outputs.size());
for (TransformOutput output : outputs) {
if (output.resource != null) {
resources.add(output.resource);
}
}
editorRegistry.open(resources, context.project());
}
// Return result
final TransformContrib contrib = new TransformContrib(resultTerm != null || !Iterables.isEmpty(outputs), true, resultTerm, outputs, Iterables2.<IMessage>empty(), duration);
return unitService.transformUnit(input, contrib, context, actionContribution);
}
Aggregations