Search in sources :

Example 6 with IStrategoAppl

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

the class Typesmart method extractImports.

private void extractImports(IStrategoTerm importDecls, List<String> imports) {
    for (IStrategoTerm importDecl : importDecls) {
        String importName = ((IStrategoString) importDecl.getSubterm(0)).stringValue();
        String importDeclName = ((IStrategoAppl) importDecl).getName();
        if (importDeclName.equals("Import")) {
            imports.add(importName);
        } else {
            // wildcard import
            imports.add(importName + "/-");
        }
    }
}
Also used : IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) IStrategoAppl(org.spoofax.interpreter.terms.IStrategoAppl) IStrategoString(org.spoofax.interpreter.terms.IStrategoString) IStrategoString(org.spoofax.interpreter.terms.IStrategoString)

Example 7 with IStrategoAppl

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

the class Typesmart method processModule.

private List<String> processModule(IStrategoTerm module) {
    if (module == null) {
        return Collections.emptyList();
    }
    assert ((IStrategoAppl) module).getName().equals("Module");
    List<String> imports = new ArrayList<>();
    IStrategoList decls = (IStrategoList) module.getSubterm(1);
    for (IStrategoTerm decl : decls) {
        String declName = ((IStrategoAppl) decl).getName();
        if (declName.equals("Imports")) {
            extractImports(decl.getSubterm(0), imports);
        } else if (declName.equals("Signature")) {
            processSignature(decl.getSubterm(0));
        }
    }
    return imports;
}
Also used : IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) ArrayList(java.util.ArrayList) IStrategoAppl(org.spoofax.interpreter.terms.IStrategoAppl) IStrategoList(org.spoofax.interpreter.terms.IStrategoList) IStrategoString(org.spoofax.interpreter.terms.IStrategoString)

Example 8 with IStrategoAppl

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

the class Typesmart method processSignature.

private void processSignature(IStrategoTerm sigDecls) {
    for (IStrategoTerm decl : sigDecls) {
        String declName = ((IStrategoAppl) decl).getName();
        if (!declName.equals("Constructors")) {
            continue;
        }
        next_constr: for (IStrategoTerm constr : decl.getSubterm(0)) {
            String kind = ((IStrategoAppl) constr).getName();
            String cname;
            IStrategoAppl typeTerm;
            if (kind.equals("OpDeclInj") || kind.equals("ExtOpDeclInj")) {
                cname = "";
                typeTerm = (IStrategoAppl) constr.getSubterm(0);
            } else {
                cname = ((IStrategoString) constr.getSubterm(0)).stringValue();
                typeTerm = (IStrategoAppl) constr.getSubterm(1);
            }
            List<SortType> sortTypes;
            if (typeTerm.getName().equals("ConstType")) {
                // no constructor arguments
                sortTypes = new ArrayList<>(1);
                SortType t = extractSortType(typeTerm.getSubterm(0));
                if (t == null) {
                    continue next_constr;
                }
                sortTypes.add(t);
            } else if (typeTerm.getName().equals("FunType")) {
                IStrategoTerm[] argTypes = typeTerm.getSubterm(0).getAllSubterms();
                sortTypes = new ArrayList<>(argTypes.length + 1);
                for (IStrategoTerm argType : argTypes) {
                    SortType t = extractSortType(argType.getSubterm(0));
                    if (t == null) {
                        continue next_constr;
                    }
                    sortTypes.add(t);
                }
                SortType t = extractSortType(typeTerm.getSubterm(1).getSubterm(0));
                if (t == null) {
                    continue next_constr;
                }
                sortTypes.add(t);
            } else {
                throw new IllegalArgumentException("Found constructor declaration in unexpected format " + constr);
            }
            addConstructorSignature(cname, sortTypes);
        }
    }
}
Also used : SortType(org.spoofax.terms.typesmart.types.SortType) IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) IStrategoAppl(org.spoofax.interpreter.terms.IStrategoAppl) ArrayList(java.util.ArrayList) IStrategoString(org.spoofax.interpreter.terms.IStrategoString) ArrayList(java.util.ArrayList) TList(org.spoofax.terms.typesmart.types.TList) LinkedList(java.util.LinkedList) IStrategoList(org.spoofax.interpreter.terms.IStrategoList) List(java.util.List) IStrategoString(org.spoofax.interpreter.terms.IStrategoString)

Example 9 with IStrategoAppl

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

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

IStrategoAppl (org.spoofax.interpreter.terms.IStrategoAppl)42 IStrategoTerm (org.spoofax.interpreter.terms.IStrategoTerm)26 IStrategoString (org.spoofax.interpreter.terms.IStrategoString)21 ICompletion (org.metaborg.core.completion.ICompletion)13 Nullable (javax.annotation.Nullable)12 IStrategoList (org.spoofax.interpreter.terms.IStrategoList)9 ITermFactory (org.spoofax.interpreter.terms.ITermFactory)8 ListImploderAttachment (org.spoofax.jsglr.client.imploder.ListImploderAttachment)8 Completion (org.metaborg.core.completion.Completion)7 ITokens (org.spoofax.jsglr.client.imploder.ITokens)7 ImploderAttachment (org.spoofax.jsglr.client.imploder.ImploderAttachment)7 StrategoTerm (org.spoofax.terms.StrategoTerm)7 HybridInterpreter (org.strategoxt.HybridInterpreter)7 FileObject (org.apache.commons.vfs2.FileObject)6 MetaborgException (org.metaborg.core.MetaborgException)6 IStrategoTuple (org.spoofax.interpreter.terms.IStrategoTuple)6 CompletionKind (org.metaborg.core.completion.CompletionKind)5 StrategoAppl (org.spoofax.terms.StrategoAppl)5 IToken (org.spoofax.jsglr.client.imploder.IToken)4 ISourceRegion (org.metaborg.core.source.ISourceRegion)3