Search in sources :

Example 41 with IStrategoTerm

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;
}
Also used : IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) IStrategoAppl(org.spoofax.interpreter.terms.IStrategoAppl) Nullable(javax.annotation.Nullable)

Example 42 with IStrategoTerm

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);
}
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 43 with IStrategoTerm

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;
}
Also used : B(mb.nabl2.terms.build.TermBuild.B) Arrays(java.util.Arrays) IStrategoInt(org.spoofax.interpreter.terms.IStrategoInt) Function1(org.metaborg.util.functions.Function1) ListTerms(mb.nabl2.terms.ListTerms) Builder(com.google.common.collect.ImmutableClassToInstanceMap.Builder) ITerm(mb.nabl2.terms.ITerm) IStrategoAppl(org.spoofax.interpreter.terms.IStrategoAppl) ITermFactory(org.spoofax.interpreter.terms.ITermFactory) IStrategoTuple(org.spoofax.interpreter.terms.IStrategoTuple) Collectors(java.util.stream.Collectors) IStrategoList(org.spoofax.interpreter.terms.IStrategoList) IStrategoReal(org.spoofax.interpreter.terms.IStrategoReal) List(java.util.List) Terms(mb.nabl2.terms.Terms) Lists(com.google.common.collect.Lists) IListTerm(mb.nabl2.terms.IListTerm) Optional(java.util.Optional) LinkedList(java.util.LinkedList) IStrategoString(org.spoofax.interpreter.terms.IStrategoString) ImmutableClassToInstanceMap(com.google.common.collect.ImmutableClassToInstanceMap) IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) IStrategoList(org.spoofax.interpreter.terms.IStrategoList) List(java.util.List) LinkedList(java.util.LinkedList)

Example 44 with IStrategoTerm

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();
}
Also used : IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm)

Example 45 with IStrategoTerm

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);
}
Also used : IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) IStrategoString(org.spoofax.interpreter.terms.IStrategoString) IStrategoString(org.spoofax.interpreter.terms.IStrategoString) ITermFactory(org.spoofax.interpreter.terms.ITermFactory)

Aggregations

IStrategoTerm (org.spoofax.interpreter.terms.IStrategoTerm)82 IStrategoString (org.spoofax.interpreter.terms.IStrategoString)31 FileObject (org.apache.commons.vfs2.FileObject)27 IStrategoAppl (org.spoofax.interpreter.terms.IStrategoAppl)25 MetaborgException (org.metaborg.core.MetaborgException)19 ITermFactory (org.spoofax.interpreter.terms.ITermFactory)17 HybridInterpreter (org.strategoxt.HybridInterpreter)17 IStrategoList (org.spoofax.interpreter.terms.IStrategoList)16 ILanguageImpl (org.metaborg.core.language.ILanguageImpl)15 Nullable (javax.annotation.Nullable)14 ISourceRegion (org.metaborg.core.source.ISourceRegion)12 ICompletion (org.metaborg.core.completion.ICompletion)11 IStrategoTuple (org.spoofax.interpreter.terms.IStrategoTuple)10 IOException (java.io.IOException)9 IProject (org.metaborg.core.project.IProject)9 IMessage (org.metaborg.core.messages.IMessage)8 IToken (org.spoofax.jsglr.client.imploder.IToken)8 IContext (org.metaborg.core.context.IContext)7 ILanguageComponent (org.metaborg.core.language.ILanguageComponent)7 ListImploderAttachment (org.spoofax.jsglr.client.imploder.ListImploderAttachment)7