Search in sources :

Example 1 with IScopeGraphContext

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);
}
Also used : NotImplementedException(org.spoofax.terms.util.NotImplementedException) ITerm(mb.nabl2.terms.ITerm) IStrategoAppl(org.spoofax.interpreter.terms.IStrategoAppl) StrategoTerms(mb.nabl2.stratego.StrategoTerms) Tools(org.spoofax.interpreter.core.Tools) ITermFactory(org.spoofax.interpreter.terms.ITermFactory) IScopeGraphContext(mb.nabl2.spoofax.analysis.IScopeGraphContext) Collectors(java.util.stream.Collectors) List(java.util.List) StrategoTermIndices(mb.nabl2.stratego.StrategoTermIndices) InterpreterException(org.spoofax.interpreter.core.InterpreterException) ConstraintTerms(mb.nabl2.stratego.ConstraintTerms) StrategoBlob(mb.nabl2.stratego.StrategoBlob) Optional(java.util.Optional) IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) IScopeGraphUnit(mb.nabl2.spoofax.analysis.IScopeGraphUnit) IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) IScopeGraphUnit(mb.nabl2.spoofax.analysis.IScopeGraphUnit)

Example 2 with IScopeGraphContext

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);
}
Also used : ITerm(mb.nabl2.terms.ITerm)

Example 3 with IScopeGraphContext

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);
}
Also used : Arrays(java.util.Arrays) ITerm(mb.nabl2.terms.ITerm) StrategoTerms(mb.nabl2.stratego.StrategoTerms) ITermFactory(org.spoofax.interpreter.terms.ITermFactory) IScopeGraphContext(mb.nabl2.spoofax.analysis.IScopeGraphContext) Collectors(java.util.stream.Collectors) IContext(org.spoofax.interpreter.core.IContext) List(java.util.List) Strategy(org.spoofax.interpreter.stratego.Strategy) InterpreterException(org.spoofax.interpreter.core.InterpreterException) ConstraintTerms(mb.nabl2.stratego.ConstraintTerms) Optional(java.util.Optional) LoggerUtils(org.metaborg.util.log.LoggerUtils) AbstractPrimitive(org.spoofax.interpreter.library.AbstractPrimitive) IClosableLock(org.metaborg.util.concurrent.IClosableLock) ILogger(org.metaborg.util.log.ILogger) IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) IScopeGraphContext(mb.nabl2.spoofax.analysis.IScopeGraphContext) IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) InterpreterException(org.spoofax.interpreter.core.InterpreterException) IClosableLock(org.metaborg.util.concurrent.IClosableLock)

Example 4 with IScopeGraphContext

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);
}
Also used : StrategoTerms(mb.nabl2.stratego.StrategoTerms) ITerm(mb.nabl2.terms.ITerm)

Example 5 with IScopeGraphContext

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));
}
Also used : InterpreterException(org.spoofax.interpreter.core.InterpreterException) IScopeGraphUnit(mb.nabl2.spoofax.analysis.IScopeGraphUnit) StrategoBlob(mb.nabl2.stratego.StrategoBlob)

Aggregations

ITerm (mb.nabl2.terms.ITerm)4 StrategoTerms (mb.nabl2.stratego.StrategoTerms)3 InterpreterException (org.spoofax.interpreter.core.InterpreterException)3 List (java.util.List)2 Optional (java.util.Optional)2 Collectors (java.util.stream.Collectors)2 IScopeGraphContext (mb.nabl2.spoofax.analysis.IScopeGraphContext)2 IScopeGraphUnit (mb.nabl2.spoofax.analysis.IScopeGraphUnit)2 ConstraintTerms (mb.nabl2.stratego.ConstraintTerms)2 StrategoBlob (mb.nabl2.stratego.StrategoBlob)2 IStrategoTerm (org.spoofax.interpreter.terms.IStrategoTerm)2 ITermFactory (org.spoofax.interpreter.terms.ITermFactory)2 Arrays (java.util.Arrays)1 StrategoTermIndices (mb.nabl2.stratego.StrategoTermIndices)1 IClosableLock (org.metaborg.util.concurrent.IClosableLock)1 ILogger (org.metaborg.util.log.ILogger)1 LoggerUtils (org.metaborg.util.log.LoggerUtils)1 IContext (org.spoofax.interpreter.core.IContext)1 Tools (org.spoofax.interpreter.core.Tools)1 AbstractPrimitive (org.spoofax.interpreter.library.AbstractPrimitive)1