Search in sources :

Example 21 with ITermFactory

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

the class ASpoofaxPrimitive method call.

@Override
public boolean call(IContext context, Strategy[] svars, IStrategoTerm[] tvars) throws InterpreterException {
    final IStrategoTerm current = context.current();
    final ITermFactory factory = context.getFactory();
    try {
        final IStrategoTerm newCurrent = call(current, svars, tvars, factory, context);
        if (newCurrent != null) {
            context.setCurrent(newCurrent);
            return true;
        }
    } catch (MetaborgException | IOException e) {
        throw new InterpreterException("Executing primitive " + name + " failed unexpectedly", e);
    }
    return false;
}
Also used : IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) InterpreterException(org.spoofax.interpreter.core.InterpreterException) MetaborgException(org.metaborg.core.MetaborgException) IOException(java.io.IOException) ITermFactory(org.spoofax.interpreter.terms.ITermFactory)

Example 22 with ITermFactory

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

the class TaskEngineAnalyzer method analyzeAll.

@Override
public ISpoofaxAnalyzeResults analyzeAll(Iterable<ISpoofaxParseUnit> inputs, IContext context, IProgress progress, ICancel cancel) throws AnalysisException, InterruptedException {
    cancel.throwIfCancelled();
    final ILanguageImpl langImpl = context.language();
    final ITermFactory termFactory = termFactoryService.getGeneric();
    final FacetContribution<AnalysisFacet> facetContribution = langImpl.facetContribution(AnalysisFacet.class);
    if (facetContribution == null) {
        logger.debug("No analysis required for {}", langImpl);
        return new SpoofaxAnalyzeResults(context);
    }
    final AnalysisFacet facet = facetContribution.facet;
    cancel.throwIfCancelled();
    final HybridInterpreter runtime;
    try {
        runtime = runtimeService.runtime(facetContribution.contributor, context, false);
    } catch (MetaborgException e) {
        throw new AnalysisException(context, "Failed to get Stratego runtime", e);
    }
    cancel.throwIfCancelled();
    return analyzeAll(inputs, context, runtime, facet.strategyName, termFactory);
}
Also used : ISpoofaxAnalyzeResults(org.metaborg.spoofax.core.analysis.ISpoofaxAnalyzeResults) SpoofaxAnalyzeResults(org.metaborg.spoofax.core.analysis.SpoofaxAnalyzeResults) ILanguageImpl(org.metaborg.core.language.ILanguageImpl) MetaborgException(org.metaborg.core.MetaborgException) HybridInterpreter(org.strategoxt.HybridInterpreter) AnalysisException(org.metaborg.core.analysis.AnalysisException) AnalysisFacet(org.metaborg.spoofax.core.analysis.AnalysisFacet) ITermFactory(org.spoofax.interpreter.terms.ITermFactory)

Example 23 with ITermFactory

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

the class JSGLRCompletionService method completionErroneousProgramsNested.

public Collection<? extends ICompletion> completionErroneousProgramsNested(int cursorPosition, Collection<IStrategoTerm> nestedCompletionTerms, ISpoofaxParseUnit completionParseResult) throws MetaborgException {
    final FileObject location = completionParseResult.source();
    final ILanguageImpl language = completionParseResult.input().langImpl();
    final String languageName = language.belongsTo().name();
    final Collection<ICompletion> completions = Lists.newLinkedList();
    IStrategoTerm completionAst = completionParseResult.ast();
    for (ILanguageComponent component : language.components()) {
        final ITermFactory termFactory = termFactoryService.get(component, null, false);
        for (IStrategoTerm nestedCompletionTerm : nestedCompletionTerms) {
            final HybridInterpreter runtime = strategoRuntimeService.runtime(component, location, false);
            Collection<IStrategoTerm> inputsStrategoNested = Lists.newLinkedList();
            // calculate direct proposals
            inputsStrategoNested.addAll(calculateDirectCompletionProposals(nestedCompletionTerm, termFactory, completionAst, languageName, runtime));
            // calculate inner nested proposals
            Collection<IStrategoTerm> innerNestedCompletionTerms = findNestedCompletionTerm((StrategoTerm) nestedCompletionTerm, true);
            for (IStrategoTerm innerNestedCompletionTerm : innerNestedCompletionTerms) {
                inputsStrategoNested.addAll(calculateNestedCompletionProposals(nestedCompletionTerm, innerNestedCompletionTerm, termFactory, completionAst, languageName, runtime));
            }
            for (IStrategoTerm inputStrategoNested : inputsStrategoNested) {
                final IStrategoTerm proposalTermNested = strategoCommon.invoke(runtime, inputStrategoNested, "get-proposals-incorrect-programs-nested-" + languageName);
                if (proposalTermNested == null) {
                    logger.error("Getting proposals for {} failed", inputStrategoNested);
                    continue;
                }
                final String name = Tools.asJavaString(proposalTermNested.getSubterm(0));
                final String text = Tools.asJavaString(proposalTermNested.getSubterm(1));
                final String additionalInfo = Tools.asJavaString(proposalTermNested.getSubterm(2));
                final StrategoAppl change = (StrategoAppl) proposalTermNested.getSubterm(3);
                final StrategoTerm completionTerm = (StrategoTerm) proposalTermNested.getSubterm(4);
                String prefix = calculatePrefix(cursorPosition, completionTerm);
                String suffix = calculateSuffix(cursorPosition, completionTerm);
                String completionKind = "recovery";
                // if the change is inserting at the end of a list
                if (change.getConstructor().getName().contains("INSERT_AT_END")) {
                    // calls a different method because now, the program has errors that should be fixed
                    final ICompletion completion = createCompletionInsertAtEndFixing(name, text, additionalInfo, prefix, suffix, change, completionKind);
                    if (completion == null) {
                        logger.error("Unexpected proposal term {}, skipping", proposalTermNested);
                        continue;
                    }
                    completions.add(completion);
                } else if (change.getConstructor().getName().contains("INSERT_BEFORE")) {
                    final ICompletion completion = createCompletionInsertBeforeFixing(name, text, additionalInfo, prefix, suffix, change, completionKind);
                    if (completion == null) {
                        logger.error("Unexpected proposal term {}, skipping", proposalTermNested);
                        continue;
                    }
                    completions.add(completion);
                } else if (change.getConstructor().getName().contains("INSERTION_TERM")) {
                    final ICompletion completion = createCompletionInsertionTermFixing(name, text, additionalInfo, prefix, suffix, change, completionKind);
                    if (completion == null) {
                        logger.error("Unexpected proposal term {}, skipping", proposalTermNested);
                        continue;
                    }
                    completions.add(completion);
                } else if (change.getConstructor().getName().contains("REPLACE_TERM")) {
                    final ICompletion completion = createCompletionReplaceTermFixing(name, text, additionalInfo, prefix, suffix, change, completionKind);
                    if (completion == null) {
                        logger.error("Unexpected proposal term {}, skipping", proposalTermNested);
                        continue;
                    }
                    completions.add(completion);
                }
            }
        }
    }
    return completions;
}
Also used : ICompletion(org.metaborg.core.completion.ICompletion) IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) ILanguageImpl(org.metaborg.core.language.ILanguageImpl) IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) StrategoTerm(org.spoofax.terms.StrategoTerm) HybridInterpreter(org.strategoxt.HybridInterpreter) FileObject(org.apache.commons.vfs2.FileObject) IStrategoString(org.spoofax.interpreter.terms.IStrategoString) ILanguageComponent(org.metaborg.core.language.ILanguageComponent) ITermFactory(org.spoofax.interpreter.terms.ITermFactory) IStrategoAppl(org.spoofax.interpreter.terms.IStrategoAppl) StrategoAppl(org.spoofax.terms.StrategoAppl)

Example 24 with ITermFactory

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

the class JSGLRCompletionService method optionalCompletions.

public Collection<ICompletion> optionalCompletions(Iterable<IStrategoTerm> optionals, boolean blankLineCompletion, String languageName, ILanguageComponent component, FileObject location) throws MetaborgException {
    Collection<ICompletion> completions = Lists.newLinkedList();
    final ITermFactory termFactory = termFactoryService.get(component, null, false);
    for (IStrategoTerm optional : optionals) {
        ImploderAttachment attachment = optional.getAttachment(ImploderAttachment.TYPE);
        String sort = attachment.getSort().substring(0, attachment.getSort().length());
        String placeholderName = sort + "-Plhdr";
        IStrategoAppl optionalPlaceholder = termFactory.makeAppl(termFactory.makeConstructor(placeholderName, 0));
        final IStrategoTerm strategoInput = termFactory.makeTuple(termFactory.makeString(sort), optional, optionalPlaceholder);
        // call Stratego part of the framework to compute change
        final HybridInterpreter runtime = strategoRuntimeService.runtime(component, location, false);
        final IStrategoTerm proposalsOptional = strategoCommon.invoke(runtime, strategoInput, "get-proposals-optional-" + languageName);
        if (proposalsOptional == null) {
            logger.error("Getting proposals for {} failed", strategoInput);
            continue;
        }
        for (IStrategoTerm proposalTerm : proposalsOptional) {
            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, blankLineCompletion, "", "");
                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) IStrategoAppl(org.spoofax.interpreter.terms.IStrategoAppl) HybridInterpreter(org.strategoxt.HybridInterpreter) IStrategoTuple(org.spoofax.interpreter.terms.IStrategoTuple) IStrategoString(org.spoofax.interpreter.terms.IStrategoString) ImploderAttachment(org.spoofax.jsglr.client.imploder.ImploderAttachment) ListImploderAttachment(org.spoofax.jsglr.client.imploder.ListImploderAttachment) IStrategoString(org.spoofax.interpreter.terms.IStrategoString) ITermFactory(org.spoofax.interpreter.terms.ITermFactory) IStrategoAppl(org.spoofax.interpreter.terms.IStrategoAppl) StrategoAppl(org.spoofax.terms.StrategoAppl)

Example 25 with ITermFactory

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

the class JSGLRCompletionService method completionEmptyProgram.

public Collection<? extends ICompletion> completionEmptyProgram(Iterable<String> startSymbols, int endOffset, ILanguageImpl language, FileObject location) throws MetaborgException {
    Collection<ICompletion> completions = Lists.newLinkedList();
    final String languageName = language.belongsTo().name();
    for (ILanguageComponent component : language.components()) {
        // 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 (String startSymbol : startSymbols) {
            String placeholderName = startSymbol + "-Plhdr";
            IStrategoAppl placeholder = termFactory.makeAppl(termFactory.makeConstructor(placeholderName, 0));
            IStrategoTuple input = termFactory.makeTuple(termFactory.makeString(startSymbol), placeholder);
            final IStrategoTerm proposalsPlaceholder = strategoCommon.invoke(runtime, input, "get-proposals-empty-program-" + languageName);
            if (proposalsPlaceholder == null) {
                logger.error("Getting proposals for {} failed", placeholder);
                continue;
            }
            for (IStrategoTerm proposalTerm : proposalsPlaceholder) {
                if (!(proposalTerm instanceof IStrategoTuple)) {
                    logger.error("Unexpected proposal term {}, skipping", proposalTerm);
                    continue;
                }
                final IStrategoTuple tuple = (IStrategoTuple) proposalTerm;
                if (tuple.getSubtermCount() != 2 || !(tuple.getSubterm(0) instanceof IStrategoString) || !(tuple.getSubterm(1) 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(1));
                completions.add(new Completion(name, startSymbol, text, additionalInfo, 0, endOffset, CompletionKind.expansion));
            }
        }
    }
    return completions;
}
Also used : ICompletion(org.metaborg.core.completion.ICompletion) IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) ICompletion(org.metaborg.core.completion.ICompletion) Completion(org.metaborg.core.completion.Completion) 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) ILanguageComponent(org.metaborg.core.language.ILanguageComponent) ITermFactory(org.spoofax.interpreter.terms.ITermFactory)

Aggregations

ITermFactory (org.spoofax.interpreter.terms.ITermFactory)28 HybridInterpreter (org.strategoxt.HybridInterpreter)17 IStrategoTerm (org.spoofax.interpreter.terms.IStrategoTerm)15 IStrategoString (org.spoofax.interpreter.terms.IStrategoString)14 FileObject (org.apache.commons.vfs2.FileObject)11 ILanguageImpl (org.metaborg.core.language.ILanguageImpl)11 MetaborgException (org.metaborg.core.MetaborgException)9 ILanguageComponent (org.metaborg.core.language.ILanguageComponent)9 IStrategoAppl (org.spoofax.interpreter.terms.IStrategoAppl)9 ICompletion (org.metaborg.core.completion.ICompletion)8 IStrategoTuple (org.spoofax.interpreter.terms.IStrategoTuple)6 StrategoAppl (org.spoofax.terms.StrategoAppl)6 IContext (org.metaborg.core.context.IContext)5 ISourceRegion (org.metaborg.core.source.ISourceRegion)5 SourceRegion (org.metaborg.core.source.SourceRegion)5 IProject (org.metaborg.core.project.IProject)4 TermWithRegion (org.metaborg.spoofax.core.tracing.TracingCommon.TermWithRegion)4 IOException (java.io.IOException)3 AnalysisException (org.metaborg.core.analysis.AnalysisException)3 ParseException (org.metaborg.core.syntax.ParseException)3