use of mb.nabl2.spoofax.analysis.IScopeGraphContext 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 mb.nabl2.spoofax.analysis.IScopeGraphContext in project nabl by metaborg.
the class ScopeGraphLogPrimitive method call.
@Override
public Optional<ITerm> call(IScopeGraphContext<?> context, ITerm term, List<ITerm> terms) throws InterpreterException {
final ITerm messageTerm = terms.get(0);
final String message = M.stringValue().match(messageTerm).orElseGet(() -> messageTerm.toString());
logger.log(level(context), "{}", message);
return fatal() ? Optional.empty() : Optional.of(term);
}
use of mb.nabl2.spoofax.analysis.IScopeGraphContext in project nabl by metaborg.
the class ScopeGraphContextPrimitive method call.
@Override
public final boolean call(IContext env, Strategy[] svars, IStrategoTerm[] tvars) throws InterpreterException {
final Object contextObj = env.contextObject();
if (contextObj == null) {
logger.warn("Context is null.");
return false;
}
if (!(contextObj instanceof IScopeGraphContext)) {
throw new InterpreterException("Context does not implement IScopeGraphContext");
}
final IScopeGraphContext<?> context = (IScopeGraphContext<?>) env.contextObject();
List<IStrategoTerm> termArgs = Arrays.asList(tvars);
Optional<? extends IStrategoTerm> result;
try (IClosableLock lock = context.guard()) {
result = call(context, env.current(), termArgs, env.getFactory());
}
return result.map(t -> {
env.setCurrent(t);
return true;
}).orElse(false);
}
use of mb.nabl2.spoofax.analysis.IScopeGraphContext in project nabl by metaborg.
the class ScopeGraphContextPrimitive method call.
public Optional<? extends IStrategoTerm> call(IScopeGraphContext<?> context, IStrategoTerm sterm, List<IStrategoTerm> sterms, ITermFactory factory) throws InterpreterException {
StrategoTerms strategoTerms = new StrategoTerms(factory);
ITerm term = ConstraintTerms.specialize(strategoTerms.fromStratego(sterm));
List<ITerm> terms = sterms.stream().map(strategoTerms::fromStratego).map(ConstraintTerms::specialize).collect(Collectors.toList());
Optional<? extends ITerm> result = call(context, term, terms);
return result.map(ConstraintTerms::explicate).map(strategoTerms::toStratego);
}
use of mb.nabl2.spoofax.analysis.IScopeGraphContext in project nabl by metaborg.
the class SG_get_resource_analysis method call.
@Override
public Optional<? extends IStrategoTerm> call(IScopeGraphContext<?> context, IStrategoTerm sterm, List<IStrategoTerm> sterms, ITermFactory factory) throws InterpreterException {
if (!Tools.isTermString(sterm)) {
throw new InterpreterException("Expect a resource path.");
}
String resource = Tools.asJavaString(sterm);
final IScopeGraphUnit unit = context.unit(resource);
return Optional.of(new StrategoBlob(unit));
}
Aggregations