Search in sources :

Example 31 with IStrategoTerm

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

the class JSGLRCompletionService method getLeftRecursiveTerms.

private Iterable<IStrategoTerm> getLeftRecursiveTerms(int position, Iterable<IStrategoTerm> terms, Map<IStrategoTerm, Boolean> leftRecursiveTerms) {
    Collection<IStrategoTerm> leftRecursive = Lists.newLinkedList();
    for (IStrategoTerm term : terms) {
        boolean isLeftRecursive = leftRecursiveTerms.containsKey(term);
        IToken left = ImploderAttachment.getLeftToken(term);
        IToken right = ImploderAttachment.getRightToken(term);
        if (isLeftRecursive && position > right.getEndOffset()) {
            leftRecursive.add(term);
        } else if (term instanceof IStrategoList || left.getStartOffset() > right.getEndOffset()) {
            continue;
        } else {
            break;
        }
    }
    return leftRecursive;
}
Also used : IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) IToken(org.spoofax.jsglr.client.imploder.IToken) IStrategoList(org.spoofax.interpreter.terms.IStrategoList)

Example 32 with IStrategoTerm

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

the class JSGLRCompletionService method getOptionals.

@Nullable
private Iterable<IStrategoTerm> getOptionals(final Iterable<IStrategoTerm> terms, Map<IStrategoTerm, Boolean> leftRecursiveTerms, Map<IStrategoTerm, Boolean> rightRecursiveTerms) {
    Collection<IStrategoTerm> optionals = Lists.newLinkedList();
    for (IStrategoTerm term : terms) {
        IToken left = ImploderAttachment.getLeftToken(term);
        IToken right = ImploderAttachment.getRightToken(term);
        if (!(term instanceof IStrategoList) && left.getStartOffset() > right.getEndOffset()) {
            optionals.add(term);
        } else if (term instanceof IStrategoList) {
            continue;
        // if term is not nullable, nor a list nor left or right recursive stop the search
        } else {
            boolean isLeftRecursive = leftRecursiveTerms.containsKey(term);
            boolean isRightRecursive = rightRecursiveTerms.containsKey(term);
            if (!isLeftRecursive && !isRightRecursive) {
                break;
            }
        }
    }
    return optionals;
}
Also used : IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) IToken(org.spoofax.jsglr.client.imploder.IToken) IStrategoList(org.spoofax.interpreter.terms.IStrategoList) Nullable(javax.annotation.Nullable)

Example 33 with IStrategoTerm

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

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

the class JSGLRCompletionService method findNestedCompletionTerm.

private Collection<IStrategoTerm> findNestedCompletionTerm(final StrategoTerm ast, final boolean excludeIdTerm) {
    final Collection<IStrategoTerm> completionTerms = Lists.newLinkedList();
    final IStrategoTermVisitor visitor = new AStrategoTermVisitor() {

        @Override
        public boolean visit(IStrategoTerm term) {
            ImploderAttachment ia = term.getAttachment(ImploderAttachment.TYPE);
            if (excludeIdTerm && term.equals(ast)) {
                return true;
            }
            if (ia.isNestedCompletion()) {
                completionTerms.add(term);
                return false;
            }
            return true;
        }
    };
    StrategoTermVisitee.topdown(visitor, ast);
    return completionTerms;
}
Also used : IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) AStrategoTermVisitor(org.spoofax.terms.visitor.AStrategoTermVisitor) IStrategoTermVisitor(org.spoofax.terms.visitor.IStrategoTermVisitor) ImploderAttachment(org.spoofax.jsglr.client.imploder.ImploderAttachment) ListImploderAttachment(org.spoofax.jsglr.client.imploder.ListImploderAttachment)

Example 35 with IStrategoTerm

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

the class JSGLRCompletionService method getCompletionTermsFromAST.

private Collection<IStrategoTerm> getCompletionTermsFromAST(ISpoofaxParseUnit completionParseResult) {
    if (completionParseResult == null) {
        return Lists.newLinkedList();
    }
    StrategoTerm ast = (StrategoTerm) completionParseResult.ast();
    if (ast == null) {
        return Lists.newLinkedList();
    }
    Collection<IStrategoTerm> completionTerm = findCompletionTerm(ast);
    return completionTerm;
}
Also used : IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) StrategoTerm(org.spoofax.terms.StrategoTerm)

Aggregations

IStrategoTerm (org.spoofax.interpreter.terms.IStrategoTerm)82 IStrategoString (org.spoofax.interpreter.terms.IStrategoString)31 FileObject (org.apache.commons.vfs2.FileObject)27 IStrategoAppl (org.spoofax.interpreter.terms.IStrategoAppl)25 MetaborgException (org.metaborg.core.MetaborgException)19 ITermFactory (org.spoofax.interpreter.terms.ITermFactory)17 HybridInterpreter (org.strategoxt.HybridInterpreter)17 IStrategoList (org.spoofax.interpreter.terms.IStrategoList)16 ILanguageImpl (org.metaborg.core.language.ILanguageImpl)15 Nullable (javax.annotation.Nullable)14 ISourceRegion (org.metaborg.core.source.ISourceRegion)12 ICompletion (org.metaborg.core.completion.ICompletion)11 IStrategoTuple (org.spoofax.interpreter.terms.IStrategoTuple)10 IOException (java.io.IOException)9 IProject (org.metaborg.core.project.IProject)9 IMessage (org.metaborg.core.messages.IMessage)8 IToken (org.spoofax.jsglr.client.imploder.IToken)8 IContext (org.metaborg.core.context.IContext)7 ILanguageComponent (org.metaborg.core.language.ILanguageComponent)7 ListImploderAttachment (org.spoofax.jsglr.client.imploder.ListImploderAttachment)7