Search in sources :

Example 1 with IStrategoList

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

the class LanguageComponentsPrimitive method call.

@Override
protected IStrategoTerm call(IStrategoTerm current, Strategy[] svars, IStrategoTerm[] tvars, ITermFactory factory, IContext context) throws MetaborgException, IOException {
    IStrategoList list = factory.makeList();
    for (ILanguageComponent component : context.language().components()) {
        final LanguageIdentifier id = component.id();
        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(component.location().getName().getURI());
        final IStrategoTuple tuple = factory.makeTuple(groupIdTerm, idTerm, versionTerm, locationTerm);
        list = factory.makeListCons(tuple, list);
    }
    return list;
}
Also used : LanguageIdentifier(org.metaborg.core.language.LanguageIdentifier) IStrategoString(org.spoofax.interpreter.terms.IStrategoString) IStrategoTuple(org.spoofax.interpreter.terms.IStrategoTuple) IStrategoList(org.spoofax.interpreter.terms.IStrategoList) ILanguageComponent(org.metaborg.core.language.ILanguageComponent)

Example 2 with IStrategoList

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

the class OutlineService method icon.

@Nullable
private FileObject icon(IStrategoTerm term, FileObject location) {
    final IStrategoList annos = term.getAnnotations();
    if (annos == null) {
        return null;
    }
    if (annos.getSubtermCount() != 1) {
        return null;
    }
    final IStrategoTerm iconTerm = annos.getSubterm(0);
    if (!(iconTerm instanceof IStrategoString)) {
        return null;
    }
    final IStrategoString iconTermString = (IStrategoString) iconTerm;
    final String iconLocation = iconTermString.stringValue();
    try {
        return location.resolveFile(iconLocation);
    } catch (FileSystemException e) {
        logger.error("Cannot resolve icon {} in {}", e, iconLocation, location);
        return null;
    }
}
Also used : FileSystemException(org.apache.commons.vfs2.FileSystemException) IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) IStrategoString(org.spoofax.interpreter.terms.IStrategoString) IStrategoList(org.spoofax.interpreter.terms.IStrategoList) IStrategoString(org.spoofax.interpreter.terms.IStrategoString) Nullable(javax.annotation.Nullable)

Example 3 with IStrategoList

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

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

the class JSGLRCompletionService method createCompletionInsertAtEnd.

private ICompletion createCompletionInsertAtEnd(String name, String text, String additionalInfo, StrategoAppl change, boolean blankLineCompletion) {
    final StrategoTerm oldNode = (StrategoTerm) change.getSubterm(0);
    final StrategoTerm newNode = (StrategoTerm) change.getSubterm(1);
    // expected two lists
    if (change.getSubtermCount() != 2 || !(oldNode instanceof IStrategoList) || !(newNode instanceof IStrategoList)) {
        return null;
    }
    final String sort = ImploderAttachment.getElementSort(oldNode);
    int insertionPoint, suffixPoint;
    ITokens tokenizer = ImploderAttachment.getTokenizer(oldNode);
    final ImploderAttachment oldListIA = oldNode.getAttachment(ImploderAttachment.TYPE);
    int tokenPosition;
    // node
    if (oldNode.getSubtermCount() == 0) {
        tokenPosition = oldListIA.getLeftToken().getIndex() - 1 > 0 ? oldListIA.getLeftToken().getIndex() - 1 : 0;
        while ((tokenizer.getTokenAt(tokenPosition).getKind() == IToken.TK_LAYOUT || tokenizer.getTokenAt(tokenPosition).getKind() == IToken.TK_ERROR) && tokenPosition > 0) tokenPosition--;
        insertionPoint = tokenizer.getTokenAt(tokenPosition).getEndOffset();
    } else {
        // if list is not empty
        // insert after at end offset of the rightmost token of the element before the
        // completion
        StrategoTerm elementBefore = (StrategoTerm) oldNode.getSubterm(oldNode.getAllSubterms().length - 1);
        int leftIdx = elementBefore.getAttachment(ImploderAttachment.TYPE).getLeftToken().getIndex();
        int rightIdx = elementBefore.getAttachment(ImploderAttachment.TYPE).getRightToken().getIndex();
        while ((tokenizer.getTokenAt(rightIdx).getKind() == IToken.TK_LAYOUT || tokenizer.getTokenAt(rightIdx).getLength() == 0) && rightIdx > leftIdx) {
            rightIdx--;
        }
        insertionPoint = tokenizer.getTokenAt(rightIdx).getEndOffset();
        tokenPosition = rightIdx;
    }
    suffixPoint = insertionPoint + 1;
    // if completion is triggered in an empty line, consume that line
    IToken checkToken;
    boolean blankLine = false;
    if (blankLineCompletion) {
        for (; tokenPosition < tokenizer.getTokenCount(); tokenPosition++) {
            checkToken = tokenizer.getTokenAt(tokenPosition);
            if (tokenizer.toString(checkToken, checkToken).contains("\n")) {
                suffixPoint = checkToken.getEndOffset();
                if (!blankLine) {
                    blankLine = true;
                } else {
                    break;
                }
            }
        }
    }
    return new Completion(name, sort, text, additionalInfo, insertionPoint + 1, suffixPoint, CompletionKind.expansion);
}
Also used : ICompletion(org.metaborg.core.completion.ICompletion) Completion(org.metaborg.core.completion.Completion) IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) StrategoTerm(org.spoofax.terms.StrategoTerm) IToken(org.spoofax.jsglr.client.imploder.IToken) IStrategoList(org.spoofax.interpreter.terms.IStrategoList) IStrategoString(org.spoofax.interpreter.terms.IStrategoString) ImploderAttachment(org.spoofax.jsglr.client.imploder.ImploderAttachment) ListImploderAttachment(org.spoofax.jsglr.client.imploder.ListImploderAttachment) ITokens(org.spoofax.jsglr.client.imploder.ITokens)

Example 5 with IStrategoList

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

IStrategoList (org.spoofax.interpreter.terms.IStrategoList)17 IStrategoTerm (org.spoofax.interpreter.terms.IStrategoTerm)16 IStrategoString (org.spoofax.interpreter.terms.IStrategoString)12 IStrategoAppl (org.spoofax.interpreter.terms.IStrategoAppl)8 ICompletion (org.metaborg.core.completion.ICompletion)7 IToken (org.spoofax.jsglr.client.imploder.IToken)7 ITokens (org.spoofax.jsglr.client.imploder.ITokens)5 ListImploderAttachment (org.spoofax.jsglr.client.imploder.ListImploderAttachment)5 StrategoTerm (org.spoofax.terms.StrategoTerm)5 Nullable (javax.annotation.Nullable)4 FileObject (org.apache.commons.vfs2.FileObject)4 Completion (org.metaborg.core.completion.Completion)4 ILanguageComponent (org.metaborg.core.language.ILanguageComponent)4 IStrategoTuple (org.spoofax.interpreter.terms.IStrategoTuple)4 ImploderAttachment (org.spoofax.jsglr.client.imploder.ImploderAttachment)4 HybridInterpreter (org.strategoxt.HybridInterpreter)4 ITermFactory (org.spoofax.interpreter.terms.ITermFactory)3 MetaborgException (org.metaborg.core.MetaborgException)2 CompletionKind (org.metaborg.core.completion.CompletionKind)2 ILanguageImpl (org.metaborg.core.language.ILanguageImpl)2