Search in sources :

Example 11 with HybridInterpreter

use of org.strategoxt.HybridInterpreter 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)

Example 12 with HybridInterpreter

use of org.strategoxt.HybridInterpreter 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 13 with HybridInterpreter

use of org.strategoxt.HybridInterpreter in project spoofax by metaborg.

the class JSGLRCompletionService method completionCorrectPrograms.

public Collection<ICompletion> completionCorrectPrograms(int position, boolean blankLineCompletion, ISpoofaxParseUnit parseResult) throws MetaborgException {
    Collection<ICompletion> completions = Sets.newHashSet();
    final FileObject location = parseResult.source();
    final ILanguageImpl language = parseResult.input().langImpl();
    final String languageName = language.belongsTo().name();
    for (ILanguageComponent component : language.components()) {
        final HybridInterpreter runtime = strategoRuntimeService.runtime(component, location, false);
        final ITermFactory termFactory = termFactoryService.get(component, null, false);
        final Map<IStrategoTerm, Boolean> leftRecursiveTerms = new HashMap<IStrategoTerm, Boolean>();
        final Map<IStrategoTerm, Boolean> rightRecursiveTerms = new HashMap<IStrategoTerm, Boolean>();
        final Iterable<IStrategoTerm> terms = tracingTermsCompletions(position, parseResult.ast(), new SourceRegion(position), runtime, termFactory, languageName, leftRecursiveTerms, rightRecursiveTerms);
        final IStrategoAppl placeholder = getPlaceholder(position, terms);
        final Iterable<IStrategoList> lists = getLists(terms, leftRecursiveTerms, rightRecursiveTerms);
        final Iterable<IStrategoTerm> optionals = getOptionals(terms, leftRecursiveTerms, rightRecursiveTerms);
        final Iterable<IStrategoTerm> leftRecursive = getLeftRecursiveTerms(position, terms, leftRecursiveTerms);
        final Iterable<IStrategoTerm> rightRecursive = getRightRecursiveTerms(position, terms, rightRecursiveTerms);
        if (placeholder != null) {
            completions.addAll(placeholderCompletions(placeholder, languageName, component, location));
        } else {
            if (Iterables.size(lists) != 0) {
                completions.addAll(listsCompletions(position, blankLineCompletion, lists, languageName, component, location));
            }
            if (Iterables.size(optionals) != 0) {
                completions.addAll(optionalCompletions(optionals, blankLineCompletion, languageName, component, location));
            }
        // TODO Improve recursive completions
        // if(Iterables.size(leftRecursive) != 0 || Iterables.size(rightRecursive) != 0) {
        // completions
        // .addAll(recursiveCompletions(leftRecursive, rightRecursive, languageName, component, location));
        // }
        }
    }
    return completions;
}
Also used : ICompletion(org.metaborg.core.completion.ICompletion) IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) HashMap(java.util.HashMap) HybridInterpreter(org.strategoxt.HybridInterpreter) IStrategoAppl(org.spoofax.interpreter.terms.IStrategoAppl) IStrategoList(org.spoofax.interpreter.terms.IStrategoList) IStrategoString(org.spoofax.interpreter.terms.IStrategoString) SourceRegion(org.metaborg.core.source.SourceRegion) ISourceRegion(org.metaborg.core.source.ISourceRegion) ILanguageImpl(org.metaborg.core.language.ILanguageImpl) FileObject(org.apache.commons.vfs2.FileObject) ILanguageComponent(org.metaborg.core.language.ILanguageComponent) ITermFactory(org.spoofax.interpreter.terms.ITermFactory)

Example 14 with HybridInterpreter

use of org.strategoxt.HybridInterpreter in project spoofax by metaborg.

the class StrategoRuntimeService method runtime.

@Override
public HybridInterpreter runtime(ILanguageComponent component, FileObject location, boolean typesmart) throws MetaborgException {
    HybridInterpreter prototype = prototypes.get(component);
    if (prototype == null) {
        prototype = createPrototype(component);
    }
    final IProject project = projectService.get(location);
    final HybridInterpreter runtime = clone(prototype, location, component, project, typesmart);
    return runtime;
}
Also used : HybridInterpreter(org.strategoxt.HybridInterpreter) IProject(org.metaborg.core.project.IProject)

Example 15 with HybridInterpreter

use of org.strategoxt.HybridInterpreter in project spoofax by metaborg.

the class StrategoRuntimeService method clone.

private HybridInterpreter clone(HybridInterpreter prototype, FileObject workingLocation, ILanguageComponent component, @Nullable IProject project, boolean typesmart) {
    // TODO: this seems to copy operator registries, but they should be recreated to isolate interpreters?
    final HybridInterpreter runtime = new HybridInterpreter(prototype);
    final ResourceAgent agent = new ResourceAgent(resourceService);
    agent.setAbsoluteWorkingDir(workingLocation);
    agent.setAbsoluteDefinitionDir(component.location());
    runtime.setIOAgent(agent);
    runtime.getCompiledContext().getExceptionHandler().setEnabled(false);
    // Add primitive libraries again, to make sure that our libraries override any default ones.
    for (IOperatorRegistry library : strategoLibraries) {
        runtime.getCompiledContext().addOperatorRegistry(library);
    }
    final ITermFactory termFactory = termFactoryService.get(component, project, typesmart);
    runtime.getContext().setFactory(termFactory);
    runtime.getCompiledContext().setFactory(termFactory);
    runtime.init();
    return runtime;
}
Also used : HybridInterpreter(org.strategoxt.HybridInterpreter) ITermFactory(org.spoofax.interpreter.terms.ITermFactory) IOperatorRegistry(org.spoofax.interpreter.library.IOperatorRegistry)

Aggregations

HybridInterpreter (org.strategoxt.HybridInterpreter)26 IStrategoTerm (org.spoofax.interpreter.terms.IStrategoTerm)17 ITermFactory (org.spoofax.interpreter.terms.ITermFactory)17 MetaborgException (org.metaborg.core.MetaborgException)14 ILanguageImpl (org.metaborg.core.language.ILanguageImpl)13 IStrategoString (org.spoofax.interpreter.terms.IStrategoString)12 FileObject (org.apache.commons.vfs2.FileObject)11 ICompletion (org.metaborg.core.completion.ICompletion)8 IStrategoAppl (org.spoofax.interpreter.terms.IStrategoAppl)8 ILanguageComponent (org.metaborg.core.language.ILanguageComponent)7 IStrategoTuple (org.spoofax.interpreter.terms.IStrategoTuple)7 AnalysisException (org.metaborg.core.analysis.AnalysisException)6 StrategoAppl (org.spoofax.terms.StrategoAppl)6 ISourceRegion (org.metaborg.core.source.ISourceRegion)5 SourceRegion (org.metaborg.core.source.SourceRegion)5 ISpoofaxAnalyzeResults (org.metaborg.spoofax.core.analysis.ISpoofaxAnalyzeResults)5 SpoofaxAnalyzeResults (org.metaborg.spoofax.core.analysis.SpoofaxAnalyzeResults)5 IContext (org.metaborg.core.context.IContext)4 IProject (org.metaborg.core.project.IProject)4 AnalysisFacet (org.metaborg.spoofax.core.analysis.AnalysisFacet)4