use of org.metaborg.core.language.ILanguageImpl in project spoofax by metaborg.
the class Builder method parse.
private Collection<P> parse(BuildInput input, ILanguageImpl langImpl, Iterable<IdentifiedResourceChange> changes, boolean pardoned, Collection<FileObject> changedResources, Set<FileName> removedResources, Collection<IMessage> extraMessages, RefBool success, IProgress progress, ICancel cancel) throws InterruptedException {
final int size = Iterables.size(changes);
progress.setWorkRemaining(size);
final Collection<P> allParseUnits = Lists.newArrayListWithCapacity(size);
if (size == 0) {
return allParseUnits;
}
progress.setDescription("Parsing " + size + " file(s) of " + langImpl.belongsTo().name());
logger.debug("Parsing {} resources", size);
for (IdentifiedResourceChange identifiedChange : changes) {
cancel.throwIfCancelled();
final ResourceChange change = identifiedChange.change;
final FileObject resource = change.resource;
final ILanguageImpl dialect = identifiedChange.dialect;
final ResourceChangeKind changeKind = change.kind;
try {
if (changeKind == ResourceChangeKind.Delete) {
parseResultUpdater.remove(resource);
removedResources.add(resource.getName());
// LEGACY: add empty parse result, to indicate to analysis that this resource was
// removed. There is special handling in updating the analysis result processor, the marker
// updater, and the compiler, to exclude removed resources.
final I inputUnit = unitService.emptyInputUnit(resource, langImpl, dialect);
final P emptyParseResult = unitService.emptyParseUnit(inputUnit);
allParseUnits.add(emptyParseResult);
// Don't add resource as changed when it has been deleted, because it does not exist any more.
progress.work(1);
} else {
final String sourceText = sourceTextService.text(resource);
parseResultUpdater.invalidate(resource);
final I inputUnit = unitService.inputUnit(resource, sourceText, langImpl, dialect);
final P parseResult = syntaxService.parse(inputUnit, progress.subProgress(1), cancel);
final boolean noErrors = printMessages(parseResult.messages(), "Parsing", input, pardoned);
success.and(noErrors);
allParseUnits.add(parseResult);
parseResultUpdater.update(resource, parseResult);
changedResources.add(resource);
}
} catch (ParseException e) {
final String message = logger.format("Parsing {} failed unexpectedly", resource);
final boolean noErrors = printMessage(resource, message, e, input, pardoned);
success.and(noErrors);
parseResultUpdater.error(resource, e);
extraMessages.add(MessageFactory.newParseErrorAtTop(resource, "Parsing failed unexpectedly", e));
changedResources.add(resource);
} catch (IOException e) {
final String message = logger.format("Getting source text for {} failed unexpectedly", resource);
final boolean noErrors = printMessage(resource, message, e, input, pardoned);
success.and(noErrors);
final I inputUnit = unitService.emptyInputUnit(resource, langImpl, dialect);
parseResultUpdater.error(resource, new ParseException(inputUnit, e));
extraMessages.add(MessageFactory.newParseErrorAtTop(resource, "Getting source text failed unexpectedly", e));
changedResources.add(resource);
}
}
return allParseUnits;
}
use of org.metaborg.core.language.ILanguageImpl in project spoofax by metaborg.
the class LanguageServiceTest method activeLowerVersion.
/**
* Add an implementation with a lower version number, assert that the other implementation stays active.
*/
@Test
public void activeLowerVersion() throws Exception {
final String id = "org.metaborg.lang.entity";
final LanguageVersion version1 = version(0, 1, 0);
final LanguageVersion version2 = version(0, 0, 1);
final LanguageIdentifier identifier1 = new LanguageIdentifier(groupId, id, version1);
final LanguageIdentifier identifier2 = new LanguageIdentifier(groupId, id, version2);
final FileObject location1 = createDir("ram:///Entity1/");
final FileObject location2 = createDir("ram:///Entity2/");
final String name = "Entity";
final ILanguageComponent component1 = language(identifier1, location1, name);
final ILanguageImpl impl1 = Iterables.get(component1.contributesTo(), 0);
final ILanguage lang = impl1.belongsTo();
assertSame(component1, languageService.getComponent(location1.getName()));
assertSame(impl1, languageService.getImpl(identifier1));
assertSame(impl1, lang.activeImpl());
assertSame(lang, languageService.getLanguage(name));
final ILanguageComponent component2 = language(identifier2, location2, name);
final ILanguageImpl impl2 = Iterables.get(component2.contributesTo(), 0);
// Language 1 with higher version number stays active.
assertSame(component1, languageService.getComponent(location1.getName()));
assertSame(component2, languageService.getComponent(location2.getName()));
assertSame(impl1, languageService.getImpl(identifier1));
assertSame(impl2, languageService.getImpl(identifier2));
assertSame(impl1, lang.activeImpl());
assertSame(lang, languageService.getLanguage(name));
assertSize(1, impl1.components());
assertSize(1, impl2.components());
assertSize(2, lang.impls());
assertSize(2, languageService.getAllComponents());
assertSize(2, languageService.getAllImpls());
assertSize(1, languageService.getAllLanguages());
}
use of org.metaborg.core.language.ILanguageImpl in project spoofax by metaborg.
the class LanguageServiceTest method activeMostRecent.
/**
* Add multiple implementations, assert that the active language is correct.
*/
@Test
public void activeMostRecent() throws Exception {
final String id1 = "org.metaborg.lang.entity1";
final String id2 = "org.metaborg.lang.entity2";
final String id3 = "org.metaborg.lang.entity3";
final String id4 = "org.metaborg.lang.entity4";
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 FileObject location1 = createDir("ram:///Entity1");
final FileObject location2 = createDir("ram:///Entity2");
final FileObject location3 = createDir("ram:///Entity3");
final FileObject location4 = createDir("ram:///Entity3");
final String name = "Entity";
final ILanguageComponent component1 = language(identifier1, location1, name);
final ILanguageImpl impl1 = Iterables.get(component1.contributesTo(), 0);
final ILanguage lang = impl1.belongsTo();
assertSame(impl1, lang.activeImpl());
final ILanguageComponent component2 = language(identifier2, location2, name);
final ILanguageImpl impl2 = Iterables.get(component2.contributesTo(), 0);
assertSame(impl2, lang.activeImpl());
final ILanguageComponent component3 = language(identifier3, location3, name);
final ILanguageImpl impl3 = Iterables.get(component3.contributesTo(), 0);
assertSame(impl3, lang.activeImpl());
languageService.remove(component3);
assertSame(impl2, lang.activeImpl());
languageService.remove(component1);
assertSame(impl2, lang.activeImpl());
final ILanguageComponent component4 = language(identifier4, location4, name);
final ILanguageImpl impl4 = Iterables.get(component4.contributesTo(), 0);
assertSame(impl4, lang.activeImpl());
languageService.remove(component4);
assertSame(impl2, lang.activeImpl());
languageService.remove(component2);
assertNull(lang.activeImpl());
assertSize(0, lang.impls());
assertNull(languageService.getLanguage(name));
}
use of org.metaborg.core.language.ILanguageImpl in project spoofax by metaborg.
the class LanguageServiceTest method implementationFacets.
/**
* Add multiple components with facets to a single implementation, assert correctness of returned facets.
*/
@Test
public void implementationFacets() throws Exception {
final String id1 = "org.metaborg.lang.entity.component1";
final String id2 = "org.metaborg.lang.entity.component2";
final LanguageVersion version = version(0, 0, 1);
final LanguageIdentifier identifier1 = new LanguageIdentifier(groupId, id1, version);
final LanguageIdentifier identifier2 = new LanguageIdentifier(groupId, id2, version);
final FileObject location1 = createDir("ram:///Entity1");
final FileObject location2 = createDir("ram:///Entity2");
final String implId = "org.metaborg.lang.entity.impl1";
final String name = "Entity";
final LanguageIdentifier implIdentifier = new LanguageIdentifier(groupId, implId, version);
final LanguageContributionIdentifier requestIdentifier = new LanguageContributionIdentifier(implIdentifier, name);
final DescriptionFacet facet1 = new DescriptionFacet("Component1", null);
final ResourceExtensionFacet facet2 = new ResourceExtensionFacet(Iterables2.singleton("com"));
final DescriptionFacet facet3 = new DescriptionFacet("Component2", null);
final ILanguageComponent component1 = language(identifier1, location1, requestIdentifier, facet1, facet2);
final ILanguageComponent component2 = language(identifier2, location2, requestIdentifier, facet3);
final ILanguageImpl impl = languageService.getImpl(implIdentifier);
assertContains(facet1, component1.facets());
assertContains(facet2, component1.facets());
assertContains(facet3, component2.facets());
assertSize(2, component1.facets());
assertSize(1, component2.facets());
assertContains(facet1, component1.facets(DescriptionFacet.class));
assertSame(facet1, component1.facet(DescriptionFacet.class));
assertSize(1, component1.facets(DescriptionFacet.class));
assertContains(facet2, component1.facets(ResourceExtensionFacet.class));
assertSame(facet2, component1.facet(ResourceExtensionFacet.class));
assertSize(1, component1.facets(ResourceExtensionFacet.class));
assertContains(facet3, component2.facets(DescriptionFacet.class));
assertSame(facet3, component2.facet(DescriptionFacet.class));
assertSize(1, component2.facets(DescriptionFacet.class));
assertContains(facet1, impl.facets());
assertContains(facet2, impl.facets());
assertContains(facet3, impl.facets());
assertSize(3, impl.facets());
assertContains(facet1, impl.facets(DescriptionFacet.class));
assertContains(facet2, impl.facets(ResourceExtensionFacet.class));
assertContains(facet3, impl.facets(DescriptionFacet.class));
assertSize(2, impl.facets(DescriptionFacet.class));
assertSize(1, impl.facets(ResourceExtensionFacet.class));
for (FacetContribution<IFacet> facetContribution : component1.facetContributions()) {
assertSame(facetContribution.contributor, component1);
}
assertSame(component1, component1.facetContribution(DescriptionFacet.class).contributor);
assertSame(facet1, component1.facetContribution(DescriptionFacet.class).facet);
assertSize(1, component1.facetContributions(DescriptionFacet.class));
assertSame(component1, component1.facetContribution(ResourceExtensionFacet.class).contributor);
assertSame(facet2, component1.facetContribution(ResourceExtensionFacet.class).facet);
assertSize(1, component1.facetContributions(ResourceExtensionFacet.class));
for (FacetContribution<IFacet> facetContribution : component2.facetContributions()) {
assertSame(facetContribution.contributor, component2);
}
assertSame(component2, component2.facetContribution(DescriptionFacet.class).contributor);
assertSame(facet3, component2.facetContribution(DescriptionFacet.class).facet);
assertSize(1, component2.facetContributions(DescriptionFacet.class));
for (FacetContribution<IFacet> facetContribution : impl.facetContributions()) {
final ILanguageComponent contributor = facetContribution.contributor;
final IFacet facet = facetContribution.facet;
if (facet.equals(facet1)) {
assertSame(component1, contributor);
} else if (facet.equals(facet2)) {
assertSame(component1, contributor);
} else if (facet.equals(facet3)) {
assertSame(component2, contributor);
} else {
fail("Facet does not equals any created facet");
}
}
}
use of org.metaborg.core.language.ILanguageImpl in project spoofax by metaborg.
the class LanguageServiceTest method languageServiceCorrectness.
/**
* Add multiple different language implementations, assert correctness of the language service.
*/
@Test
public void languageServiceCorrectness() throws Exception {
final String id1 = "org.metaborg.lang.entity1";
final String id2 = "org.metaborg.lang.entity2";
final String id3 = "org.metaborg.lang.entity3";
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 name1 = "Entity1";
final String name2 = "Entity2";
final String name3 = "Entity3";
final ILanguageComponent component1 = language(identifier1, location1, name1);
final ILanguageImpl impl1 = Iterables.get(component1.contributesTo(), 0);
final ILanguage lang1 = impl1.belongsTo();
final ILanguageComponent component2 = language(identifier2, location2, name2);
final ILanguageImpl impl2 = Iterables.get(component2.contributesTo(), 0);
final ILanguage lang2 = impl2.belongsTo();
final ILanguageComponent component3 = language(identifier3, location3, name3);
final ILanguageImpl impl3 = Iterables.get(component3.contributesTo(), 0);
final ILanguage lang3 = impl3.belongsTo();
assertEquals(component1, languageService.getComponent(location1.getName()));
assertSame(component1, languageService.getComponent(location1.getName()));
assertEquals(component2, languageService.getComponent(location2.getName()));
assertSame(component2, languageService.getComponent(location2.getName()));
assertEquals(component3, languageService.getComponent(location3.getName()));
assertSame(component3, languageService.getComponent(location3.getName()));
assertEquals(impl1, languageService.getImpl(identifier1));
assertSame(impl1, languageService.getImpl(identifier1));
assertEquals(impl2, languageService.getImpl(identifier2));
assertSame(impl2, languageService.getImpl(identifier2));
assertEquals(impl3, languageService.getImpl(identifier3));
assertSame(impl3, languageService.getImpl(identifier3));
assertEquals(impl1, lang1.activeImpl());
assertSame(impl1, lang1.activeImpl());
assertEquals(impl2, lang2.activeImpl());
assertSame(impl2, lang2.activeImpl());
assertEquals(impl3, lang3.activeImpl());
assertSame(impl3, lang3.activeImpl());
assertEquals(lang1, languageService.getLanguage(name1));
assertSame(lang1, languageService.getLanguage(name1));
assertEquals(lang2, languageService.getLanguage(name2));
assertSame(lang2, languageService.getLanguage(name2));
assertEquals(lang3, languageService.getLanguage(name3));
assertSame(lang3, languageService.getLanguage(name3));
assertSize(1, impl1.components());
assertSize(1, impl2.components());
assertSize(1, impl3.components());
assertSize(1, lang1.impls());
assertSize(1, lang2.impls());
assertSize(1, lang3.impls());
assertSize(3, languageService.getAllComponents());
assertSize(3, languageService.getAllImpls());
assertSize(3, languageService.getAllLanguages());
}
Aggregations