Search in sources :

Example 16 with IStrategoString

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

the class JSGLRCompletionService method placeholderCompletions.

public Collection<ICompletion> placeholderCompletions(IStrategoAppl placeholder, String languageName, ILanguageComponent component, FileObject location) throws MetaborgException {
    Collection<ICompletion> completions = Lists.newLinkedList();
    // call Stratego part of the framework to compute change
    final HybridInterpreter runtime = strategoRuntimeService.runtime(component, location, false);
    final ITermFactory termFactory = termFactoryService.get(component, null, false);
    IStrategoTerm placeholderParent = ParentAttachment.getParent(placeholder);
    if (placeholderParent == null) {
        placeholderParent = placeholder;
    }
    IStrategoInt placeholderIdx = termFactory.makeInt(-1);
    for (int i = 0; i < placeholderParent.getSubtermCount(); i++) {
        if (placeholderParent.getSubterm(i) == placeholder) {
            placeholderIdx = termFactory.makeInt(i);
        }
    }
    final String sort = ImploderAttachment.getSort(placeholder);
    final IStrategoTerm strategoInput = termFactory.makeTuple(termFactory.makeString(sort), placeholder, placeholderParent, placeholderIdx);
    final IStrategoTerm proposalsPlaceholder = strategoCommon.invoke(runtime, strategoInput, "get-proposals-placeholder-" + languageName);
    if (proposalsPlaceholder == null) {
        logger.error("Getting proposals for {} failed", placeholder);
        return completions;
    }
    for (IStrategoTerm proposalTerm : proposalsPlaceholder) {
        if (!(proposalTerm instanceof IStrategoTuple)) {
            logger.error("Unexpected proposal term {}, skipping", proposalTerm);
            continue;
        }
        final IStrategoTuple tuple = (IStrategoTuple) proposalTerm;
        if (tuple.getSubtermCount() != 4 || !(tuple.getSubterm(0) instanceof IStrategoString) || !(tuple.getSubterm(1) instanceof IStrategoString) || !(tuple.getSubterm(2) instanceof IStrategoString) || !(tuple.getSubterm(3) instanceof IStrategoAppl)) {
            logger.error("Unexpected proposal term {}, skipping", proposalTerm);
            continue;
        }
        final String name = Tools.asJavaString(tuple.getSubterm(0));
        final String text = Tools.asJavaString(tuple.getSubterm(1));
        final String additionalInfo = Tools.asJavaString(tuple.getSubterm(2));
        final StrategoAppl change = (StrategoAppl) tuple.getSubterm(3);
        if (change.getConstructor().getName().contains("REPLACE_TERM")) {
            final ICompletion completion = createCompletionReplaceTerm(name, text, additionalInfo, change, false, "", "");
            if (completion == null) {
                logger.error("Unexpected proposal term {}, skipping", proposalTerm);
                continue;
            }
            completions.add(completion);
        }
    }
    return completions;
}
Also used : IStrategoInt(org.spoofax.interpreter.terms.IStrategoInt) ICompletion(org.metaborg.core.completion.ICompletion) IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) HybridInterpreter(org.strategoxt.HybridInterpreter) IStrategoAppl(org.spoofax.interpreter.terms.IStrategoAppl) IStrategoTuple(org.spoofax.interpreter.terms.IStrategoTuple) IStrategoString(org.spoofax.interpreter.terms.IStrategoString) IStrategoString(org.spoofax.interpreter.terms.IStrategoString) ITermFactory(org.spoofax.interpreter.terms.ITermFactory) IStrategoAppl(org.spoofax.interpreter.terms.IStrategoAppl) StrategoAppl(org.spoofax.terms.StrategoAppl)

Example 17 with IStrategoString

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

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

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

the class AbsolutePathPrimitive method call.

@Override
protected IStrategoTerm call(IStrategoTerm current, Strategy[] svars, IStrategoTerm[] tvars, ITermFactory factory, IContext context) {
    if (!(current instanceof IStrategoString)) {
        return null;
    }
    String path = Tools.asJavaString(current);
    IStrategoTerm basePathTerm = tvars[0];
    if (!(basePathTerm instanceof IStrategoString)) {
        return null;
    }
    String basePath = Tools.asJavaString(basePathTerm);
    final FileObject base = resourceService.resolve(basePath);
    final FileObject abs = resourceService.resolve(base, path);
    return factory.makeString(abs.getName().getURI());
}
Also used : IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) IStrategoString(org.spoofax.interpreter.terms.IStrategoString) IStrategoString(org.spoofax.interpreter.terms.IStrategoString) FileObject(org.apache.commons.vfs2.FileObject)

Example 20 with IStrategoString

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

the class LocalReplicatePrimitive method call.

@Override
protected IStrategoTerm call(IStrategoTerm current, Strategy[] svars, IStrategoTerm[] tvars, ITermFactory factory, IContext context) {
    if (!(current instanceof IStrategoString)) {
        return null;
    }
    final IStrategoString currentStr = (IStrategoString) current;
    final String path = currentStr.stringValue();
    final FileObject resource = resourceService.resolve(path);
    final File localFile = resourceService.localFile(resource);
    return factory.makeString(localFile.getPath());
}
Also used : IStrategoString(org.spoofax.interpreter.terms.IStrategoString) IStrategoString(org.spoofax.interpreter.terms.IStrategoString) FileObject(org.apache.commons.vfs2.FileObject) File(java.io.File)

Aggregations

IStrategoString (org.spoofax.interpreter.terms.IStrategoString)29 IStrategoTerm (org.spoofax.interpreter.terms.IStrategoTerm)16 IStrategoAppl (org.spoofax.interpreter.terms.IStrategoAppl)11 ITermFactory (org.spoofax.interpreter.terms.ITermFactory)11 FileObject (org.apache.commons.vfs2.FileObject)10 IStrategoTuple (org.spoofax.interpreter.terms.IStrategoTuple)10 MetaborgException (org.metaborg.core.MetaborgException)7 HybridInterpreter (org.strategoxt.HybridInterpreter)7 ICompletion (org.metaborg.core.completion.ICompletion)6 IStrategoList (org.spoofax.interpreter.terms.IStrategoList)6 StrategoAppl (org.spoofax.terms.StrategoAppl)5 ILanguageComponent (org.metaborg.core.language.ILanguageComponent)4 LanguageIdentifier (org.metaborg.core.language.LanguageIdentifier)4 Nullable (javax.annotation.Nullable)3 ILanguageImpl (org.metaborg.core.language.ILanguageImpl)3 IStrategoInt (org.spoofax.interpreter.terms.IStrategoInt)3 File (java.io.File)2 IOException (java.io.IOException)2 AnalysisException (org.metaborg.core.analysis.AnalysisException)2 ConfigException (org.metaborg.core.config.ConfigException)2