Search in sources :

Example 16 with ILanguageComponent

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

the class LanguageServiceTest method reloadComponent.

/**
 * Reload a single component, assert that it was reloaded and its implementations and language stays the same.
 */
@Test
public void reloadComponent() throws Exception {
    final String id = "org.metaborg.lang.entity";
    final LanguageVersion version = version(0, 0, 1);
    final FileObject location = createDir("ram:///");
    final String name = "Entity";
    // GTODO: test multiple contributing components
    final ILanguageComponent componentBefore = language(groupId, id, version, location, name);
    final ILanguageImpl implBefore = Iterables.get(componentBefore.contributesTo(), 0);
    final ILanguage langBefore = implBefore.belongsTo();
    assertSame(componentBefore, languageService.getComponent(location.getName()));
    final ILanguageComponent componentAfter = language(groupId, id, version, location, name);
    final ILanguageImpl implAfter = Iterables.get(componentAfter.contributesTo(), 0);
    final ILanguage langAfter = implAfter.belongsTo();
    // Before components are equal, but not the same object, since they are re-created.
    assertEquals(componentBefore, languageService.getComponent(location.getName()));
    assertNotSame(componentBefore, languageService.getComponent(location.getName()));
    assertEquals(componentBefore, componentAfter);
    assertNotSame(componentBefore, componentAfter);
    assertSame(componentAfter, languageService.getComponent(location.getName()));
    assertSame(implBefore, implAfter);
    assertSame(langBefore, langAfter);
}
Also used : ILanguage(org.metaborg.core.language.ILanguage) 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 17 with ILanguageComponent

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

the class LanguageServiceTest method languageChanges.

/**
 * Add/reload/remove components, assert change events correctness.
 */
@Test
public void languageChanges() 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 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 FileObject location1 = createDir("ram:///Entity1");
    final FileObject location2 = createDir("ram:///Entity2");
    final FileObject location3 = createDir("ram:///Entity3");
    final String implId1 = "org.metaborg.lang.entity.impl1";
    final String implId2 = "org.metaborg.lang.entity.impl2";
    final String implId3 = "org.metaborg.lang.entity.impl3";
    final String name = "Entity";
    final LanguageIdentifier implIdentifier1 = new LanguageIdentifier(groupId, implId1, version);
    final LanguageIdentifier implIdentifier2 = new LanguageIdentifier(groupId, implId2, version);
    final LanguageIdentifier implIdentifier3 = new LanguageIdentifier(groupId, implId3, version);
    final LanguageContributionIdentifier requestIdentifier1 = new LanguageContributionIdentifier(implIdentifier1, name);
    final LanguageContributionIdentifier requestIdentifier2 = new LanguageContributionIdentifier(implIdentifier2, name);
    final LanguageContributionIdentifier requestIdentifier3 = new LanguageContributionIdentifier(implIdentifier3, name);
    final ITestableObserver<LanguageComponentChange> compObs = new TestableObserver<LanguageComponentChange>();
    languageService.componentChanges().subscribe(compObs);
    final ITestableObserver<LanguageImplChange> implObs = new TestableObserver<LanguageImplChange>();
    languageService.implChanges().subscribe(implObs);
    // Add component1 to impl1, expect component add, impl add
    final ILanguageComponent component1 = language(identifier1, location1, requestIdentifier1);
    final ILanguageImpl impl1 = languageService.getImpl(implIdentifier1);
    assertOnNext(new LanguageComponentChange(LanguageComponentChange.Kind.Add, null, component1), compObs);
    assertOnNext(new LanguageImplChange(LanguageImplChange.Kind.Add, impl1), implObs);
    // Add component2 to impl1, expect component2 add, impl1 reload
    final ILanguageComponent component2 = language(identifier2, location2, requestIdentifier1);
    assertOnNext(new LanguageComponentChange(LanguageComponentChange.Kind.Add, null, component2), compObs);
    assertOnNext(new LanguageImplChange(LanguageImplChange.Kind.Reload, impl1), implObs);
    // Add component3 to impl1, impl2, impl3, expect component add, [impl1 reload, impl2 add, impl3 add] (order
    // unknown)
    final ILanguageComponent component3 = language(identifier3, location3, requestIdentifier1, requestIdentifier2, requestIdentifier3);
    final ILanguageImpl impl2 = languageService.getImpl(implIdentifier2);
    final ILanguageImpl impl3 = languageService.getImpl(implIdentifier3);
    assertOnNext(new LanguageComponentChange(LanguageComponentChange.Kind.Add, null, component3), compObs);
    {
        final Iterable<LanguageImplChange> changes = Iterables2.from(new LanguageImplChange(LanguageImplChange.Kind.Reload, impl1), new LanguageImplChange(LanguageImplChange.Kind.Add, impl2), new LanguageImplChange(LanguageImplChange.Kind.Add, impl3));
        assertOnNext(changes, implObs);
        assertOnNext(changes, implObs);
        assertOnNext(changes, implObs);
    }
    // Remove component1, expect component1 remove, impl1 reload
    languageService.remove(component1);
    assertOnNext(new LanguageComponentChange(LanguageComponentChange.Kind.Remove, component1, null), compObs);
    assertOnNext(new LanguageImplChange(LanguageImplChange.Kind.Reload, impl1), implObs);
    // Reload component2, contribute to impl2 now, expect component2 reload, [impl1 reload, impl2 reload] (order
    // unknown)
    final ILanguageComponent component2Reload = language(identifier2, location2, requestIdentifier2);
    assertOnNext(new LanguageComponentChange(LanguageComponentChange.Kind.Reload, component2, component2Reload), compObs);
    {
        final Iterable<LanguageImplChange> changes = Iterables2.from(new LanguageImplChange(LanguageImplChange.Kind.Reload, impl1), new LanguageImplChange(LanguageImplChange.Kind.Reload, impl2));
        assertOnNext(changes, implObs);
        assertOnNext(changes, implObs);
    }
    // Remove component3, expect component3 remove, [impl1 remove, impl2 reload, impl3 remove] (order unknown)
    languageService.remove(component3);
    assertOnNext(new LanguageComponentChange(LanguageComponentChange.Kind.Remove, component3, null), compObs);
    {
        final Iterable<LanguageImplChange> changes = Iterables2.from(new LanguageImplChange(LanguageImplChange.Kind.Remove, impl3), new LanguageImplChange(LanguageImplChange.Kind.Reload, impl2), new LanguageImplChange(LanguageImplChange.Kind.Remove, impl1));
        assertOnNext(changes, implObs);
        assertOnNext(changes, implObs);
        assertOnNext(changes, implObs);
    }
    // Remove component2, expect component2 remove, impl2 remove
    languageService.remove(component2);
    assertOnNext(new LanguageComponentChange(LanguageComponentChange.Kind.Remove, component2, null), compObs);
    assertOnNext(new LanguageImplChange(LanguageImplChange.Kind.Remove, impl2), implObs);
    assertEmpty(compObs);
    assertEmpty(implObs);
}
Also used : ITestableObserver(org.metaborg.util.observable.ITestableObserver) TestableObserver(org.metaborg.util.observable.TestableObserver) LanguageImplChange(org.metaborg.core.language.LanguageImplChange) LanguageContributionIdentifier(org.metaborg.core.language.LanguageContributionIdentifier) LanguageComponentChange(org.metaborg.core.language.LanguageComponentChange) 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) Test(org.junit.Test) MetaborgTest(org.metaborg.core.test.MetaborgTest)

Example 18 with ILanguageComponent

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

the class DependencyPathProvider method sourcePaths.

@Override
public Iterable<FileObject> sourcePaths(IProject project, String languageName) throws MetaborgException {
    final Iterable<ILanguageComponent> dependencies = dependencyService.compileDeps(project);
    final Collection<FileObject> sources = Lists.newArrayList();
    for (ILanguageComponent dependency : dependencies) {
        final Collection<IGenerateConfig> generates = dependency.config().generates();
        for (IGenerateConfig generate : generates) {
            if (languageName.equals(generate.languageName())) {
                resolve(project.location(), Iterables2.singleton(generate.directory()), sources);
            }
        }
    }
    return sources;
}
Also used : IGenerateConfig(org.metaborg.core.config.IGenerateConfig) FileObject(org.apache.commons.vfs2.FileObject) ILanguageComponent(org.metaborg.core.language.ILanguageComponent)

Example 19 with ILanguageComponent

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

the class BuildInputBuilder method build.

/**
 * Builds a build input object from the current state.
 *
 * @throws MetaborgException
 *             When {@link IDependencyService#compileDeps} throws.
 */
public BuildInput build(IDependencyService dependencyService, ILanguagePathService languagePathService) throws MetaborgException {
    if (state == null) {
        state = new BuildState();
    }
    if (addDependencyLanguages) {
        final Iterable<ILanguageComponent> compileComponents = dependencyService.compileDeps(this.project);
        final Iterable<ILanguageImpl> compileImpls = LanguageUtils.toImpls(compileComponents);
        addLanguages(compileImpls);
    }
    if (addDefaultIncludePaths) {
        for (ILanguageImpl language : languages) {
            addIncludePaths(language, languagePathService.includePaths(this.project, language.belongsTo().name()));
        }
    }
    if (addSourcesFromDefaultSourceLocations) {
        for (ILanguageImpl language : languages) {
            final Iterable<IdentifiedResource> sources = languagePathService.sourceFiles(this.project, language);
            addIdentifiedSources(sources);
        }
    }
    for (ILanguageImpl language : languages) {
        if (pardonedLanguageStrings.contains(language.belongsTo().name())) {
            addPardonedLanguage(language);
        }
    }
    return new BuildInput(state, this.project, sourceChanges, includePaths, new BuildOrder(languages), selector, analyze, analyzeSelector, transform, transformSelector, transformGoals, messagePrinter, throwOnErrors, pardonedLanguages);
}
Also used : ILanguageImpl(org.metaborg.core.language.ILanguageImpl) ILanguageComponent(org.metaborg.core.language.ILanguageComponent) IdentifiedResource(org.metaborg.core.language.IdentifiedResource)

Example 20 with ILanguageComponent

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

Aggregations

ILanguageComponent (org.metaborg.core.language.ILanguageComponent)40 FileObject (org.apache.commons.vfs2.FileObject)26 ILanguageImpl (org.metaborg.core.language.ILanguageImpl)24 Test (org.junit.Test)12 LanguageIdentifier (org.metaborg.core.language.LanguageIdentifier)11 LanguageVersion (org.metaborg.core.language.LanguageVersion)11 MetaborgTest (org.metaborg.core.test.MetaborgTest)11 IStrategoTerm (org.spoofax.interpreter.terms.IStrategoTerm)9 ITermFactory (org.spoofax.interpreter.terms.ITermFactory)9 HybridInterpreter (org.strategoxt.HybridInterpreter)9 IStrategoString (org.spoofax.interpreter.terms.IStrategoString)8 ILanguage (org.metaborg.core.language.ILanguage)7 MetaborgException (org.metaborg.core.MetaborgException)6 IContext (org.metaborg.core.context.IContext)5 Nullable (javax.annotation.Nullable)4 MetaborgRuntimeException (org.metaborg.core.MetaborgRuntimeException)4 ICompletion (org.metaborg.core.completion.ICompletion)4 IStrategoList (org.spoofax.interpreter.terms.IStrategoList)4 IOException (java.io.IOException)3 FileSystemException (org.apache.commons.vfs2.FileSystemException)3