Search in sources :

Example 1 with IScopeGraphUnit

use of mb.nabl2.spoofax.analysis.IScopeGraphUnit 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 IScopeGraphUnit

use of mb.nabl2.spoofax.analysis.IScopeGraphUnit in project nabl by metaborg.

the class AnalysisPrimitive method call.

public Optional<? extends IStrategoTerm> call(IScopeGraphUnit unit, IStrategoTerm sterm, List<IStrategoTerm> sterms, ITermFactory factory) throws InterpreterException {
    final StrategoTerms strategoTerms = new StrategoTerms(factory);
    final List<ITerm> terms = sterms.stream().skip(1).map(strategoTerms::fromStratego).map(ConstraintTerms::specialize).collect(Collectors.toList());
    final ITerm term = ConstraintTerms.specialize(strategoTerms.fromStratego(sterm));
    Optional<? extends ITerm> result = call(unit, term, terms);
    return result.map(ConstraintTerms::explicate).map(strategoTerms::toStratego);
}
Also used : StrategoTerms(mb.nabl2.stratego.StrategoTerms) ITerm(mb.nabl2.terms.ITerm)

Example 3 with IScopeGraphUnit

use of mb.nabl2.spoofax.analysis.IScopeGraphUnit in project nabl by metaborg.

the class AnalysisNoTermPrimitive method call.

@Override
public Optional<? extends IStrategoTerm> call(IScopeGraphUnit unit, IStrategoTerm sterm, List<IStrategoTerm> sterms, ITermFactory factory) throws InterpreterException {
    StrategoTerms strategoTerms = new StrategoTerms(factory);
    Optional<? extends ITerm> result = call(unit);
    return result.map(ConstraintTerms::explicate).map(strategoTerms::toStratego);
}
Also used : StrategoTerms(mb.nabl2.stratego.StrategoTerms)

Example 4 with IScopeGraphUnit

use of mb.nabl2.spoofax.analysis.IScopeGraphUnit in project nabl by metaborg.

the class SG_get_ref_resolution method call.

@Override
public Optional<? extends ITerm> call(IScopeGraphUnit unit, ITerm term, List<ITerm> terms) throws InterpreterException {
    return unit.solution().flatMap(s -> {
        return Occurrence.matcher().match(term, s.unifier()).<ITerm>flatMap(ref -> {
            return s.nameResolution().resolve(ref).map(paths -> {
                List<ITerm> pathTerms = Lists.newArrayListWithExpectedSize(paths.size());
                for (IResolutionPath<Scope, Label, Occurrence> path : paths) {
                    pathTerms.add(B.newTuple(path.getDeclaration(), Paths.toTerm(path)));
                }
                ITerm result = B.newList(pathTerms);
                return result;
            });
        });
    });
}
Also used : Scope(mb.nabl2.scopegraph.terms.Scope) Label(mb.nabl2.scopegraph.terms.Label) ITerm(mb.nabl2.terms.ITerm) Occurrence(mb.nabl2.scopegraph.terms.Occurrence)

Example 5 with IScopeGraphUnit

use of mb.nabl2.spoofax.analysis.IScopeGraphUnit 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 Label (mb.nabl2.scopegraph.terms.Label)2 IScopeGraphUnit (mb.nabl2.spoofax.analysis.IScopeGraphUnit)2 StrategoBlob (mb.nabl2.stratego.StrategoBlob)2 InterpreterException (org.spoofax.interpreter.core.InterpreterException)2 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Collectors (java.util.stream.Collectors)1 Occurrence (mb.nabl2.scopegraph.terms.Occurrence)1 Scope (mb.nabl2.scopegraph.terms.Scope)1 IScopeGraphContext (mb.nabl2.spoofax.analysis.IScopeGraphContext)1 ConstraintTerms (mb.nabl2.stratego.ConstraintTerms)1 StrategoTermIndices (mb.nabl2.stratego.StrategoTermIndices)1 Tools (org.spoofax.interpreter.core.Tools)1 IStrategoAppl (org.spoofax.interpreter.terms.IStrategoAppl)1 IStrategoTerm (org.spoofax.interpreter.terms.IStrategoTerm)1 ITermFactory (org.spoofax.interpreter.terms.ITermFactory)1 NotImplementedException (org.spoofax.terms.util.NotImplementedException)1