Search in sources :

Example 16 with ITermFactory

use of org.spoofax.interpreter.terms.ITermFactory 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 17 with ITermFactory

use of org.spoofax.interpreter.terms.ITermFactory in project metalib by softlang.

the class java_strategy_0_0 method invoke.

@Override
public IStrategoTerm invoke(Context context, IStrategoTerm current) {
    context.getIOAgent().printError("Input for java-strategy: " + current);
    ITermFactory factory = context.getFactory();
    return factory.makeString("Regards from java-strategy");
}
Also used : ITermFactory(org.spoofax.interpreter.terms.ITermFactory)

Example 18 with ITermFactory

use of org.spoofax.interpreter.terms.ITermFactory in project spoofax by metaborg.

the class StrategoCommon method resourceTerm.

@Override
public IStrategoString resourceTerm(FileObject resource, FileObject location) {
    final ITermFactory termFactory = termFactoryService.getGeneric();
    String resourceURI = ResourceUtils.relativeName(resource.getName(), location.getName(), false);
    final IStrategoString resourceTerm = termFactory.makeString(resourceURI);
    return resourceTerm;
}
Also used : IStrategoString(org.spoofax.interpreter.terms.IStrategoString) IStrategoString(org.spoofax.interpreter.terms.IStrategoString) ITermFactory(org.spoofax.interpreter.terms.ITermFactory)

Example 19 with ITermFactory

use of org.spoofax.interpreter.terms.ITermFactory 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)

Example 20 with ITermFactory

use of org.spoofax.interpreter.terms.ITermFactory in project spoofax by metaborg.

the class StrategoRuntimeService method clone.

private HybridInterpreter clone(HybridInterpreter prototype, FileObject workingLocation, ILanguageComponent component, @Nullable IProject project, boolean typesmart) {
    // TODO: this seems to copy operator registries, but they should be recreated to isolate interpreters?
    final HybridInterpreter runtime = new HybridInterpreter(prototype);
    final ResourceAgent agent = new ResourceAgent(resourceService);
    agent.setAbsoluteWorkingDir(workingLocation);
    agent.setAbsoluteDefinitionDir(component.location());
    runtime.setIOAgent(agent);
    runtime.getCompiledContext().getExceptionHandler().setEnabled(false);
    // Add primitive libraries again, to make sure that our libraries override any default ones.
    for (IOperatorRegistry library : strategoLibraries) {
        runtime.getCompiledContext().addOperatorRegistry(library);
    }
    final ITermFactory termFactory = termFactoryService.get(component, project, typesmart);
    runtime.getContext().setFactory(termFactory);
    runtime.getCompiledContext().setFactory(termFactory);
    runtime.init();
    return runtime;
}
Also used : HybridInterpreter(org.strategoxt.HybridInterpreter) ITermFactory(org.spoofax.interpreter.terms.ITermFactory) IOperatorRegistry(org.spoofax.interpreter.library.IOperatorRegistry)

Aggregations

ITermFactory (org.spoofax.interpreter.terms.ITermFactory)28 HybridInterpreter (org.strategoxt.HybridInterpreter)17 IStrategoTerm (org.spoofax.interpreter.terms.IStrategoTerm)15 IStrategoString (org.spoofax.interpreter.terms.IStrategoString)14 FileObject (org.apache.commons.vfs2.FileObject)11 ILanguageImpl (org.metaborg.core.language.ILanguageImpl)11 MetaborgException (org.metaborg.core.MetaborgException)9 ILanguageComponent (org.metaborg.core.language.ILanguageComponent)9 IStrategoAppl (org.spoofax.interpreter.terms.IStrategoAppl)9 ICompletion (org.metaborg.core.completion.ICompletion)8 IStrategoTuple (org.spoofax.interpreter.terms.IStrategoTuple)6 StrategoAppl (org.spoofax.terms.StrategoAppl)6 IContext (org.metaborg.core.context.IContext)5 ISourceRegion (org.metaborg.core.source.ISourceRegion)5 SourceRegion (org.metaborg.core.source.SourceRegion)5 IProject (org.metaborg.core.project.IProject)4 TermWithRegion (org.metaborg.spoofax.core.tracing.TracingCommon.TermWithRegion)4 IOException (java.io.IOException)3 AnalysisException (org.metaborg.core.analysis.AnalysisException)3 ParseException (org.metaborg.core.syntax.ParseException)3