use of org.spoofax.interpreter.terms.IStrategoString in project spoofax by metaborg.
the class DigestPrimitive method call.
@Override
protected IStrategoTerm call(IStrategoTerm current, Strategy[] svars, IStrategoTerm[] tvars, ITermFactory factory, IContext context) {
if (!(current instanceof IStrategoString)) {
return null;
}
final String str = Tools.asJavaString(current);
final String hash = Hashing.sha256().hashString(str, StandardCharsets.UTF_8).toString();
return factory.makeString(hash);
}
use of org.spoofax.interpreter.terms.IStrategoString in project spoofax by metaborg.
the class Typesmart method extractSortType.
private SortType extractSortType(IStrategoTerm sort) {
String kind = ((IStrategoAppl) sort).getName();
if (kind.equals("SortList") || kind.equals("SortListTl") || kind.equals("SortVar")) {
logger.error("Unsupported Stratego signature: " + sort);
return TAny.instance;
} else if (kind.equals("SortTuple")) {
IStrategoTerm[] kids = sort.getSubterm(0).getAllSubterms();
SortType[] sorts = new SortType[kids.length];
for (int i = 0; i < kids.length; i++) {
sorts[i] = extractSortType(kids[i]);
}
return new TTuple(sorts);
}
if (sort.getSubterm(0).getTermType() != IStrategoTerm.STRING) {
throw new IllegalArgumentException("Found type in unexpected format " + sort);
}
String sortName = ((IStrategoString) sort.getSubterm(0)).stringValue();
if (kind.equals("SortNoArgs") && sortName.equals(SortType.LEXICAL_SORT)) {
return TLexical.instance;
} else if (kind.equals("SortNoArgs") && sortName.equals(SortType.ANY_SORT)) {
return TAny.instance;
} else if (kind.equals("SortNoArgs")) {
return new TSort(sortName);
} else if (kind.equals("Sort") && sortName.equals("List")) {
SortType t = extractSortType(sort.getSubterm(1).getSubterm(0));
return t == null ? null : new TList(t);
} else if (kind.equals("Sort") && sortName.equals("Option")) {
SortType t = extractSortType(sort.getSubterm(1).getSubterm(0));
return t == null ? null : new TOption(t);
} else if (kind.equals("SortVar")) {
return null;
} else {
throw new IllegalArgumentException("Found type in unexpected format " + sort);
}
}
use of org.spoofax.interpreter.terms.IStrategoString 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.IStrategoString in project spoofax by metaborg.
the class TaskEngineAnalyzer method analyzeAll.
private ISpoofaxAnalyzeResults analyzeAll(Iterable<ISpoofaxParseUnit> inputs, IContext context, HybridInterpreter runtime, String strategy, ITermFactory termFactory) throws AnalysisException {
final Map<String, ISpoofaxParseUnit> inputsPerSource = Maps.newHashMap();
int detachedCounter = 0;
final Collection<IStrategoAppl> analysisInputs = Lists.newArrayList();
for (ISpoofaxParseUnit input : inputs) {
if (!input.valid()) {
logger.warn("Parse result for {} is invalid, cannot analyze", input.source());
continue;
}
final String pathString;
if (input.detached()) {
pathString = "detached-source-" + detachedCounter++;
logger.debug("Parse input is detached, using '{}' as path", pathString);
} else {
pathString = input.source().getName().getURI();
}
inputsPerSource.put(pathString, input);
final IStrategoString pathTerm = termFactory.makeString(pathString);
final IStrategoReal durationTerm = termFactory.makeReal(input.duration());
analysisInputs.add(termFactory.makeAppl(fileCons, pathTerm, input.ast(), durationTerm));
}
final IStrategoTerm inputTerm = termFactory.makeList(analysisInputs);
logger.trace("Invoking {} strategy", strategy);
final IStrategoTerm resultTerm;
try {
resultTerm = strategoCommon.invoke(runtime, inputTerm, strategy);
} catch (MetaborgException e) {
final String message = analysisCommon.analysisFailedMessage(runtime);
throw new AnalysisException(context, message, e);
}
if (resultTerm == null) {
final String message = analysisCommon.analysisFailedMessage(runtime);
throw new AnalysisException(context, message);
}
if (!(resultTerm instanceof IStrategoAppl) || resultTerm.getSubtermCount() != 5) {
final String message = logger.format("Unexpected results from analysis {}, expected 5-tuple", resultTerm);
throw new AnalysisException(context, message);
}
final IStrategoTerm resultsTerm = resultTerm.getSubterm(0);
final IStrategoTerm updateResultsTerm = resultTerm.getSubterm(1);
final Collection<ISpoofaxAnalyzeUnit> fileResults = Lists.newArrayListWithCapacity(resultsTerm.getSubtermCount());
for (IStrategoTerm result : resultsTerm) {
// HACK: analysis duration per parse unit is unknown, pass -1 as duration.
final ISpoofaxAnalyzeUnit fileResult = result(result, inputsPerSource, context, -1);
if (fileResult == null) {
continue;
}
fileResults.add(fileResult);
}
final Collection<ISpoofaxAnalyzeUnitUpdate> updateResults = Lists.newArrayListWithCapacity(updateResultsTerm.getSubtermCount());
for (IStrategoTerm result : updateResultsTerm) {
final ISpoofaxAnalyzeUnitUpdate updateResult = updateResult(result, context);
if (updateResult == null) {
continue;
}
updateResults.add(updateResult);
}
return new SpoofaxAnalyzeResults(fileResults, updateResults, context);
}
use of org.spoofax.interpreter.terms.IStrategoString 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