Search in sources :

Example 46 with ILanguageImpl

use of org.metaborg.core.language.ILanguageImpl in project spoofax by metaborg.

the class LanguageServiceTest method componentContributions.

/**
 * Add multiple components that contribute to multiple implementations, assert correctness of language service and
 * objects. Delete some components and assert correctness again.
 */
@Test
public void componentContributions() throws Exception {
    final String id1 = "org.metaborg.lang.entity.component1";
    final String id2 = "org.metaborg.lang.entity.component2";
    final String id3 = "org.metaborg.lang.entity.component3";
    final String id4 = "org.metaborg.lang.entity.component4";
    final String id5 = "org.metaborg.lang.entity.component5";
    final LanguageVersion version = version(0, 0, 1);
    final LanguageIdentifier identifier1 = new LanguageIdentifier(groupId, id1, version);
    final LanguageIdentifier identifier2 = new LanguageIdentifier(groupId, id2, version);
    final LanguageIdentifier identifier3 = new LanguageIdentifier(groupId, id3, version);
    final LanguageIdentifier identifier4 = new LanguageIdentifier(groupId, id4, version);
    final LanguageIdentifier identifier5 = new LanguageIdentifier(groupId, id5, version);
    final FileObject location1 = createDir("ram:///Entity1");
    final FileObject location2 = createDir("ram:///Entity2");
    final FileObject location3 = createDir("ram:///Entity3");
    final FileObject location4 = createDir("ram:///Entity4");
    final FileObject location5 = createDir("ram:///Entity5");
    final String implId1 = "org.metaborg.lang.entity.impl1";
    final String implId2 = "org.metaborg.lang.entity.impl2";
    final String name = "Entity";
    final LanguageIdentifier implIdentifier1 = new LanguageIdentifier(groupId, implId1, version);
    final LanguageIdentifier implIdentifier2 = new LanguageIdentifier(groupId, implId2, version);
    final LanguageContributionIdentifier requestIdentifier1 = new LanguageContributionIdentifier(implIdentifier1, name);
    final LanguageContributionIdentifier requestIdentifier2 = new LanguageContributionIdentifier(implIdentifier2, name);
    final ILanguageComponent component1 = language(identifier1, location1, requestIdentifier1);
    final ILanguageComponent component2 = language(identifier2, location2, requestIdentifier1);
    final ILanguageComponent component3 = language(identifier3, location3, requestIdentifier2);
    final ILanguageComponent component4 = language(identifier4, location4, requestIdentifier1, requestIdentifier2);
    final ILanguageComponent component5 = language(identifier5, location5, requestIdentifier1, requestIdentifier2);
    final ILanguageImpl impl1 = languageService.getImpl(implIdentifier1);
    final ILanguageImpl impl2 = languageService.getImpl(implIdentifier2);
    final ILanguage lang = languageService.getLanguage(name);
    assertSame(component1, languageService.getComponent(location1.getName()));
    assertSame(component2, languageService.getComponent(location2.getName()));
    assertSame(component3, languageService.getComponent(location3.getName()));
    assertSame(component4, languageService.getComponent(location4.getName()));
    assertSame(component5, languageService.getComponent(location5.getName()));
    assertSame(impl1, languageService.getImpl(implIdentifier1));
    assertSame(impl2, languageService.getImpl(implIdentifier2));
    assertSame(lang, languageService.getLanguage(name));
    assertSize(5, languageService.getAllComponents());
    assertSize(2, languageService.getAllImpls());
    assertSize(1, languageService.getAllLanguages());
    assertContains(impl1, component1.contributesTo());
    assertNotContains(impl2, component1.contributesTo());
    assertContains(impl1, component2.contributesTo());
    assertNotContains(impl2, component2.contributesTo());
    assertNotContains(impl1, component3.contributesTo());
    assertContains(impl2, component3.contributesTo());
    assertContains(impl1, component4.contributesTo());
    assertContains(impl2, component4.contributesTo());
    assertContains(impl1, component5.contributesTo());
    assertContains(impl2, component5.contributesTo());
    assertContains(component1, impl1.components());
    assertContains(component2, impl1.components());
    assertNotContains(component3, impl1.components());
    assertContains(component4, impl1.components());
    assertContains(component5, impl1.components());
    assertNotContains(component1, impl2.components());
    assertNotContains(component2, impl2.components());
    assertContains(component3, impl2.components());
    assertContains(component4, impl2.components());
    assertContains(component5, impl2.components());
    assertSame(lang, impl1.belongsTo());
    assertSame(lang, impl2.belongsTo());
    assertContains(impl1, lang.impls());
    assertContains(impl2, lang.impls());
    languageService.remove(component3);
    assertNull(languageService.getComponent(location3.getName()));
    assertSize(4, languageService.getAllComponents());
    assertSize(2, languageService.getAllImpls());
    assertSize(1, languageService.getAllLanguages());
    assertEmpty(component3.contributesTo());
    assertNotContains(component3, impl1.components());
    languageService.remove(component4);
    languageService.remove(component5);
    // Also removes implementation 2, since all its components have been removed.
    assertNull(languageService.getImpl(implIdentifier2));
    assertSize(2, languageService.getAllComponents());
    assertSize(1, languageService.getAllImpls());
    assertSize(1, languageService.getAllLanguages());
    assertEmpty(component4.contributesTo());
    assertEmpty(component5.contributesTo());
    assertNotContains(component4, impl1.components());
    assertNotContains(component5, impl1.components());
    assertEmpty(impl2.components());
    assertNotContains(impl2, lang.impls());
}
Also used : ILanguage(org.metaborg.core.language.ILanguage) LanguageIdentifier(org.metaborg.core.language.LanguageIdentifier) ILanguageImpl(org.metaborg.core.language.ILanguageImpl) LanguageVersion(org.metaborg.core.language.LanguageVersion) FileObject(org.apache.commons.vfs2.FileObject) ILanguageComponent(org.metaborg.core.language.ILanguageComponent) LanguageContributionIdentifier(org.metaborg.core.language.LanguageContributionIdentifier) Test(org.junit.Test) MetaborgTest(org.metaborg.core.test.MetaborgTest)

Example 47 with ILanguageImpl

use of org.metaborg.core.language.ILanguageImpl in project spoofax by metaborg.

the class LanguageIdentitificationTest method identification.

@Test
public void identification() throws Exception {
    final LanguageVersion version = version(0, 0, 1);
    final FileObject location1 = createDir("ram:///Entity1");
    final FileObject location2 = createDir("ram:///Entity2");
    final ILanguageComponent component1 = language(groupId, "org.metaborg.lang.entity1", version, location1, "Entity1", "ent1");
    final ILanguageImpl impl1 = Iterables.get(component1.contributesTo(), 0);
    final ILanguageComponent component2 = language(groupId, "org.metaborg.lang.entity2", version, location2, "Entity2", "ent2");
    final ILanguageImpl impl2 = Iterables.get(component2.contributesTo(), 0);
    assertTrue(languageIdentifierService.identify(resourceService.resolve("ram:///Entity1/test.ent1"), impl1));
    assertFalse(languageIdentifierService.identify(resourceService.resolve("ram:///Entity2/test.ent2"), impl1));
    assertTrue(languageIdentifierService.identify(resourceService.resolve("ram:///Entity2/test.ent2"), impl2));
    assertFalse(languageIdentifierService.identify(resourceService.resolve("ram:///Entity1/test.ent1"), impl2));
}
Also used : ILanguageImpl(org.metaborg.core.language.ILanguageImpl) LanguageVersion(org.metaborg.core.language.LanguageVersion) FileObject(org.apache.commons.vfs2.FileObject) ILanguageComponent(org.metaborg.core.language.ILanguageComponent) Test(org.junit.Test) MetaborgTest(org.metaborg.core.test.MetaborgTest)

Example 48 with ILanguageImpl

use of org.metaborg.core.language.ILanguageImpl in project spoofax by metaborg.

the class AnalysisService method analyze.

@Override
public IAnalyzeResult<A, AU> analyze(P input, IContext context, IProgress progress, ICancel cancel) throws AnalysisException, InterruptedException {
    final ILanguageImpl langImpl = context.language();
    final AnalyzerFacet<P, A, AU> facet = facet(langImpl);
    if (facet == null) {
        final String message = logger.format("Cannot get an analyzer for {}", langImpl);
        throw new AnalysisException(context, message);
    }
    final IAnalyzer<P, A, AU> analyzer = facet.analyzer;
    return analyzer.analyze(input, context, progress, cancel);
}
Also used : ILanguageImpl(org.metaborg.core.language.ILanguageImpl)

Example 49 with ILanguageImpl

use of org.metaborg.core.language.ILanguageImpl 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 50 with ILanguageImpl

use of org.metaborg.core.language.ILanguageImpl 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)

Aggregations

ILanguageImpl (org.metaborg.core.language.ILanguageImpl)53 FileObject (org.apache.commons.vfs2.FileObject)32 ILanguageComponent (org.metaborg.core.language.ILanguageComponent)24 MetaborgException (org.metaborg.core.MetaborgException)17 IStrategoTerm (org.spoofax.interpreter.terms.IStrategoTerm)15 ILanguage (org.metaborg.core.language.ILanguage)14 HybridInterpreter (org.strategoxt.HybridInterpreter)13 LanguageIdentifier (org.metaborg.core.language.LanguageIdentifier)11 ITermFactory (org.spoofax.interpreter.terms.ITermFactory)11 Test (org.junit.Test)10 IContext (org.metaborg.core.context.IContext)10 LanguageVersion (org.metaborg.core.language.LanguageVersion)10 MetaborgRuntimeException (org.metaborg.core.MetaborgRuntimeException)9 MetaborgTest (org.metaborg.core.test.MetaborgTest)9 IStrategoString (org.spoofax.interpreter.terms.IStrategoString)9 IProject (org.metaborg.core.project.IProject)8 Nullable (javax.annotation.Nullable)6 ISpoofaxParseUnit (org.metaborg.spoofax.core.unit.ISpoofaxParseUnit)6 IdentifiedResource (org.metaborg.core.language.IdentifiedResource)5 ISourceRegion (org.metaborg.core.source.ISourceRegion)5