use of org.spoofax.interpreter.terms.IStrategoTerm in project spoofax by metaborg.
the class AnalysisFacetFromESV method type.
@Nullable
public static String type(IStrategoAppl esv) {
final IStrategoAppl strategy = ESVReader.findTerm(esv, "SemanticObserver");
if (strategy == null) {
return null;
}
final IStrategoTerm annotations = strategy.getSubterm(1);
boolean multifile = false;
boolean constraint = false;
for (IStrategoTerm annotation : annotations) {
multifile |= Tools.hasConstructor((IStrategoAppl) annotation, "MultiFile", 0);
constraint |= Tools.hasConstructor((IStrategoAppl) annotation, "Constraint", 0);
}
if (constraint) {
return multifile ? ConstraintMultiFileAnalyzer.name : ConstraintSingleFileAnalyzer.name;
} else if (multifile) {
return TaskEngineAnalyzer.name;
}
return StrategoAnalyzer.name;
}
use of org.spoofax.interpreter.terms.IStrategoTerm in project nabl by metaborg.
the class AnalysisPrimitive method call.
@Override
public Optional<? extends IStrategoTerm> call(IScopeGraphContext<?> context, IStrategoTerm sterm, List<IStrategoTerm> sterms, ITermFactory factory) throws InterpreterException {
if (sterms.size() < 1) {
throw new IllegalArgumentException("Expected as first term argument: analysis");
}
final IStrategoTerm analysisTerm = sterms.get(0);
final IScopeGraphUnit analysis;
final Optional<IScopeGraphUnit> maybeAnalysis = StrategoBlob.match(analysisTerm, IScopeGraphUnit.class);
if (maybeAnalysis.isPresent()) {
analysis = maybeAnalysis.get();
} else if (Tools.isTermAppl(analysisTerm) && Tools.hasConstructor((IStrategoAppl) analysisTerm, "AnalysisToken", 0)) {
// TODO Remove legacy case after bootstrapping
analysis = StrategoTermIndices.get(analysisTerm).map(idx -> context.unit(idx.getResource())).orElseThrow(() -> new IllegalArgumentException("Not a valid analysis term."));
} else {
throw new IllegalArgumentException("Not a valid analysis term.");
}
return call(analysis, sterm, sterms, factory);
}
use of org.spoofax.interpreter.terms.IStrategoTerm in project nabl by metaborg.
the class StrategoTerms method toStratego.
// to
public IStrategoTerm toStratego(ITerm term) {
IStrategoTerm strategoTerm = term.match(Terms.cases(// @formatter:off
appl -> {
List<IStrategoTerm> args = appl.getArgs().stream().map(arg -> toStratego(arg)).collect(Collectors.toList());
IStrategoTerm[] argArray = args.toArray(new IStrategoTerm[args.size()]);
return appl.getOp().equals(Terms.TUPLE_OP) ? termFactory.makeTuple(argArray) : termFactory.makeAppl(termFactory.makeConstructor(appl.getOp(), appl.getArity()), argArray);
}, list -> toStrategoList(list), string -> termFactory.makeString(string.getValue()), integer -> termFactory.makeInt(integer.getValue()), blob -> new StrategoBlob(blob.getValue()), var -> {
throw new IllegalArgumentException("Cannot convert specialized terms to Stratego.");
}));
strategoTerm = putAttachments(strategoTerm, term.getAttachments());
return strategoTerm;
}
use of org.spoofax.interpreter.terms.IStrategoTerm in project spoofax by metaborg.
the class StrategoCommon method traceToString.
private String traceToString(IStrategoList trace) {
final StringBuilder sb = new StringBuilder();
sb.append("Stratego trace:");
final int depth = trace.getSubtermCount();
for (int i = 0; i < depth; i++) {
final IStrategoTerm t = trace.getSubterm(depth - i - 1);
sb.append("\n\t");
sb.append(t.getTermType() == IStrategoTerm.STRING ? Tools.asJavaString(t) : t);
}
return sb.toString();
}
use of org.spoofax.interpreter.terms.IStrategoTerm in project spoofax by metaborg.
the class StrategoCommon method builderInputTerm.
@Override
public IStrategoTerm builderInputTerm(IStrategoTerm ast, FileObject resource, FileObject location) {
final ITermFactory termFactory = termFactoryService.getGeneric();
// TODO: support selected node
final IStrategoTerm node = ast;
// TODO: support position
final IStrategoTerm position = termFactory.makeList();
final String locationURI = location.getName().getURI();
final IStrategoString locationTerm = termFactory.makeString(locationURI);
String resourceURI = ResourceUtils.relativeName(resource.getName(), location.getName(), false);
final IStrategoString resourceTerm = termFactory.makeString(resourceURI);
return termFactory.makeTuple(node, position, ast, resourceTerm, locationTerm);
}
Aggregations