Search in sources :

Example 11 with IStrategoString

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

the class GetContextualGrammarPrimitive method call.

@Override
public boolean call(IContext env, Strategy[] svars, IStrategoTerm[] tvars) throws InterpreterException {
    org.metaborg.core.context.IContext context = (org.metaborg.core.context.IContext) env.contextObject();
    String path = ((IStrategoString) tvars[0]).stringValue();
    ResourceService rs = context.injector().getInstance(ResourceService.class);
    FileObject fo = rs.resolve(path + "/target/metaborg/ctxgrammar.aterm");
    InputStream inputStream;
    String text = "";
    try {
        inputStream = fo.getContent().getInputStream();
        text = IOUtils.toString(inputStream, StandardCharsets.UTF_8.name());
    } catch (IOException e) {
        e.printStackTrace();
    }
    final ITermFactory tf = env.getFactory();
    env.setCurrent(tf.parseFromString(text));
    return true;
}
Also used : IContext(org.spoofax.interpreter.core.IContext) InputStream(java.io.InputStream) ResourceService(org.metaborg.core.resource.ResourceService) IStrategoString(org.spoofax.interpreter.terms.IStrategoString) IStrategoString(org.spoofax.interpreter.terms.IStrategoString) IOException(java.io.IOException) FileObject(org.apache.commons.vfs2.FileObject) ITermFactory(org.spoofax.interpreter.terms.ITermFactory)

Example 12 with IStrategoString

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

the class LanguageSpecificationPrimitive method call.

@Override
protected IStrategoTerm call(IStrategoTerm current, Strategy[] svars, IStrategoTerm[] tvars, ITermFactory factory, IContext context) throws MetaborgException {
    final IProject project = context.project();
    if (project == null) {
        return null;
    }
    if (languageSpecServiceProvider == null) {
        // Indicates that meta-Spoofax is not available (ISpoofaxLanguageSpecService cannot be injected), but this
        // should never happen because this primitive is inside meta-Spoofax. Check for null just in case.
        logger.error("Language specification service is not available; static injection failed");
        return null;
    }
    final ISpoofaxLanguageSpecService languageSpecService = languageSpecServiceProvider.get();
    if (!languageSpecService.available(project)) {
        return null;
    }
    final ISpoofaxLanguageSpec languageSpec;
    try {
        languageSpec = languageSpecService.get(project);
    } catch (ConfigException e) {
        throw new MetaborgException("Unable to get language specification configuration for " + project, e);
    }
    if (languageSpec == null) {
        return null;
    }
    final IStrategoString nameTerm = factory.makeString(languageSpec.config().name());
    final LanguageIdentifier id = languageSpec.config().identifier();
    final IStrategoString groupIdTerm = factory.makeString(id.groupId);
    final IStrategoString idTerm = factory.makeString(id.id);
    final IStrategoString versionTerm = factory.makeString(id.version.toString());
    final IStrategoString locationTerm = factory.makeString(languageSpec.location().getName().getURI());
    return factory.makeTuple(nameTerm, groupIdTerm, idTerm, versionTerm, locationTerm);
}
Also used : ISpoofaxLanguageSpec(org.metaborg.spoofax.meta.core.project.ISpoofaxLanguageSpec) LanguageIdentifier(org.metaborg.core.language.LanguageIdentifier) ISpoofaxLanguageSpecService(org.metaborg.spoofax.meta.core.project.ISpoofaxLanguageSpecService) MetaborgException(org.metaborg.core.MetaborgException) IStrategoString(org.spoofax.interpreter.terms.IStrategoString) ConfigException(org.metaborg.core.config.ConfigException) IProject(org.metaborg.core.project.IProject)

Example 13 with IStrategoString

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

the class PlaceholderCharsPrimitive method call.

@Override
public boolean call(IContext env, Strategy[] svars, IStrategoTerm[] tvars) throws InterpreterException {
    org.metaborg.core.context.IContext context = (org.metaborg.core.context.IContext) env.contextObject();
    final FileObject location = context.location();
    final IProject project = projectService.get(location);
    if (project == null) {
        return false;
    }
    if (languageSpecServiceProvider == null) {
        // Indicates that meta-Spoofax is not available (ISpoofaxLanguageSpecService cannot be injected), but this
        // should never happen because this primitive is inside meta-Spoofax. Check for null just in case.
        logger.error("Language specification service is not available; static injection failed");
        return false;
    }
    final ISpoofaxLanguageSpecService languageSpecService = languageSpecServiceProvider.get();
    if (!languageSpecService.available(project)) {
        return false;
    }
    final ISpoofaxLanguageSpec languageSpec;
    try {
        languageSpec = languageSpecService.get(project);
    } catch (ConfigException e) {
        throw new InterpreterException("Unable to get language specification name for " + location, e);
    }
    if (languageSpec == null) {
        return false;
    }
    PlaceholderCharacters placeholderChars = languageSpec.config().placeholderChars();
    IStrategoString prefix = env.getFactory().makeString(placeholderChars.prefix);
    IStrategoString suffix = null;
    if (placeholderChars.suffix != null) {
        suffix = env.getFactory().makeString(placeholderChars.suffix);
    } else {
        suffix = env.getFactory().makeString("");
    }
    env.setCurrent(env.getFactory().makeTuple(prefix, suffix));
    return true;
}
Also used : IContext(org.spoofax.interpreter.core.IContext) InterpreterException(org.spoofax.interpreter.core.InterpreterException) IStrategoString(org.spoofax.interpreter.terms.IStrategoString) ConfigException(org.metaborg.core.config.ConfigException) PlaceholderCharacters(org.metaborg.spoofax.meta.core.config.PlaceholderCharacters) IProject(org.metaborg.core.project.IProject) ISpoofaxLanguageSpec(org.metaborg.spoofax.meta.core.project.ISpoofaxLanguageSpec) ISpoofaxLanguageSpecService(org.metaborg.spoofax.meta.core.project.ISpoofaxLanguageSpecService) FileObject(org.apache.commons.vfs2.FileObject)

Example 14 with IStrategoString

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

the class JSGLRCompletionService method recursiveCompletions.

private Collection<? extends ICompletion> recursiveCompletions(Iterable<IStrategoTerm> leftRecursive, Iterable<IStrategoTerm> rightRecursive, 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);
    for (IStrategoTerm term : leftRecursive) {
        IStrategoTerm sort = termFactory.makeString(ImploderAttachment.getSort(term));
        final IStrategoTerm strategoInput = termFactory.makeTuple(sort, term);
        IStrategoTerm proposals = null;
        try {
            proposals = strategoCommon.invoke(runtime, strategoInput, "get-proposals-left-recursive-" + languageName);
        } catch (Exception e) {
            logger.error("Getting proposals for {} failed", term);
            continue;
        }
        if (proposals == null) {
            logger.error("Getting proposals for {} failed", term);
            continue;
        }
        for (IStrategoTerm proposalTerm : proposals) {
            final IStrategoTuple tuple = (IStrategoTuple) proposalTerm;
            if (tuple.getSubtermCount() != 5 || !(tuple.getSubterm(0) instanceof IStrategoString) || !(tuple.getSubterm(1) instanceof IStrategoString) || !(tuple.getSubterm(2) instanceof IStrategoString) || !(tuple.getSubterm(3) instanceof IStrategoAppl) || !(tuple.getSubterm(4) instanceof IStrategoString)) {
                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);
            final String prefix = Tools.asJavaString(tuple.getSubterm(4));
            if (change.getConstructor().getName().contains("REPLACE_TERM")) {
                final ICompletion completion = createCompletionReplaceTerm(name, text, additionalInfo, change, false, prefix, "");
                if (completion == null) {
                    logger.error("Unexpected proposal term {}, skipping", proposalTerm);
                    continue;
                }
                completions.add(completion);
            }
        }
    }
    for (IStrategoTerm term : rightRecursive) {
        IStrategoTerm sort = termFactory.makeString(ImploderAttachment.getSort(term));
        final IStrategoTerm strategoInput = termFactory.makeTuple(sort, term);
        IStrategoTerm proposals = null;
        try {
            proposals = strategoCommon.invoke(runtime, strategoInput, "get-proposals-right-recursive-" + languageName);
        } catch (Exception e) {
            logger.error("Getting proposals for {} failed", term);
            continue;
        }
        if (proposals == null) {
            logger.error("Getting proposals for {} failed", term);
            continue;
        }
        for (IStrategoTerm proposalTerm : proposals) {
            final IStrategoTuple tuple = (IStrategoTuple) proposalTerm;
            if (tuple.getSubtermCount() != 5 || !(tuple.getSubterm(0) instanceof IStrategoString) || !(tuple.getSubterm(1) instanceof IStrategoString) || !(tuple.getSubterm(2) instanceof IStrategoString) || !(tuple.getSubterm(3) instanceof IStrategoAppl) || !(tuple.getSubterm(4) instanceof IStrategoString)) {
                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);
            final String suffix = Tools.asJavaString(tuple.getSubterm(4));
            if (change.getConstructor().getName().contains("REPLACE_TERM")) {
                final ICompletion completion = createCompletionReplaceTerm(name, text, additionalInfo, change, false, "", suffix);
                if (completion == null) {
                    logger.error("Unexpected proposal term {}, skipping", proposalTerm);
                    continue;
                }
                completions.add(completion);
            }
        }
    }
    return completions;
}
Also used : 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) MetaborgException(org.metaborg.core.MetaborgException)

Example 15 with IStrategoString

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

the class JSGLRCompletionService method listsCompletions.

public Collection<ICompletion> listsCompletions(int position, boolean blankLineCompletion, Iterable<IStrategoList> lists, String languageName, ILanguageComponent component, FileObject location) throws MetaborgException {
    Collection<ICompletion> completions = Lists.newLinkedList();
    final ITermFactory termFactory = termFactoryService.get(component, null, false);
    for (IStrategoList list : lists) {
        ListImploderAttachment attachment = list.getAttachment(null);
        String sort = attachment.getSort().substring(0, attachment.getSort().length() - 1);
        String placeholderName = sort + "-Plhdr";
        IStrategoAppl listPlaceholder = termFactory.makeAppl(termFactory.makeConstructor(placeholderName, 0));
        final IStrategoTerm strategoInput = termFactory.makeTuple(termFactory.makeString(sort), list, listPlaceholder, termFactory.makeInt(position));
        final HybridInterpreter runtime = strategoRuntimeService.runtime(component, location, false);
        final IStrategoTerm proposalsLists = strategoCommon.invoke(runtime, strategoInput, "get-proposals-list-" + languageName);
        if (proposalsLists == null) {
            logger.error("Getting proposals for {} failed", strategoInput);
            continue;
        }
        for (IStrategoTerm proposalTerm : proposalsLists) {
            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 the change is inserting at the end of a list
            if (change.getConstructor().getName().contains("INSERT_AT_END")) {
                final ICompletion completion = createCompletionInsertAtEnd(name, text, additionalInfo, change, blankLineCompletion);
                if (completion == null) {
                    logger.error("Unexpected proposal term {}, skipping", proposalTerm);
                    continue;
                }
                completions.add(completion);
            } else if (change.getConstructor().getName().contains("INSERT_BEFORE")) {
                final ICompletion completion = createCompletionInsertBefore(name, text, additionalInfo, change);
                if (completion == null) {
                    logger.error("Unexpected proposal term {}, skipping", proposalTerm);
                    continue;
                }
                completions.add(completion);
            }
        }
    }
    return completions;
}
Also used : ICompletion(org.metaborg.core.completion.ICompletion) IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) ListImploderAttachment(org.spoofax.jsglr.client.imploder.ListImploderAttachment) IStrategoAppl(org.spoofax.interpreter.terms.IStrategoAppl) HybridInterpreter(org.strategoxt.HybridInterpreter) IStrategoTuple(org.spoofax.interpreter.terms.IStrategoTuple) IStrategoString(org.spoofax.interpreter.terms.IStrategoString) IStrategoList(org.spoofax.interpreter.terms.IStrategoList) IStrategoString(org.spoofax.interpreter.terms.IStrategoString) ITermFactory(org.spoofax.interpreter.terms.ITermFactory) IStrategoAppl(org.spoofax.interpreter.terms.IStrategoAppl) StrategoAppl(org.spoofax.terms.StrategoAppl)

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