use of org.metaborg.core.language.LanguageVersion in project spoofax by metaborg.
the class ParsePrimitive method call.
@Override
protected IStrategoTerm call(IStrategoTerm current, Strategy[] svars, IStrategoTerm[] tvars, ITermFactory factory, org.spoofax.interpreter.core.IContext strategoContext) throws MetaborgException, IOException {
// Determine what to parse.
if (!(current instanceof IStrategoString)) {
throw new MetaborgException("Cannot parse, input string or file " + current + " is not a string");
}
final String stringOrFile = ((IStrategoString) current).stringValue();
final String text;
@Nullable final FileObject file;
final IStrategoTerm isFileTerm = tvars[0];
if (!(isFileTerm instanceof IStrategoInt)) {
throw new MetaborgException("Cannot parse, input kind " + isFileTerm + " is not an integer");
}
if (((IStrategoInt) isFileTerm).intValue() == 1) {
file = resourceService.resolve(stringOrFile);
if (!file.exists() || !file.isFile()) {
throw new MetaborgException("Cannot parse, input file " + file + " does not exist or is not a file");
}
text = sourceTextService.text(file);
} else {
file = null;
text = stringOrFile;
}
// Determine which language to parse it with.
final ILanguageImpl langImpl;
final IStrategoTerm nameOrGroupIdTerm = tvars[1];
final IStrategoTerm idTerm = tvars[2];
final IStrategoTerm versionTerm = tvars[3];
if (nameOrGroupIdTerm instanceof IStrategoTuple) {
// No name, groupId, id, and version was set, auto detect language to parse with.
if (file == null) {
throw new MetaborgException("Cannot parse a string, no language to parse it with was given");
}
final IContext context = metaborgContext(strategoContext);
if (context != null) {
langImpl = languageIdentifierService.identify(file, context.project());
} else {
langImpl = languageIdentifierService.identify(file);
}
if (langImpl == null) {
throw new MetaborgException("Cannot parse, language for " + file + " could not be identified");
}
} else if (idTerm instanceof IStrategoTuple) {
// No id was set, name is set.
if (!(nameOrGroupIdTerm instanceof IStrategoString)) {
throw new MetaborgException("Cannot parse, language name " + nameOrGroupIdTerm + " is not a string");
}
final String name = ((IStrategoString) nameOrGroupIdTerm).stringValue();
final ILanguage lang = languageService.getLanguage(name);
if (lang == null) {
throw new MetaborgException("Cannot parse, language " + nameOrGroupIdTerm + " does not exist");
}
langImpl = lang.activeImpl();
if (langImpl == null) {
throw new MetaborgException("Cannot parse, language " + lang + " has no implementation loaded");
}
} else {
// A groupId, id, and version is set.
if (!(nameOrGroupIdTerm instanceof IStrategoString)) {
throw new MetaborgException("Cannot parse, language groupId " + nameOrGroupIdTerm + " is not a string");
}
final String groupId = ((IStrategoString) nameOrGroupIdTerm).stringValue();
if (!(idTerm instanceof IStrategoString)) {
throw new MetaborgException("Cannot parse, language id " + idTerm + " is not a string");
}
final String id = ((IStrategoString) idTerm).stringValue();
if (!(versionTerm instanceof IStrategoString)) {
throw new MetaborgException("Cannot parse, language version " + versionTerm + " is not a string");
}
final String versionStr = ((IStrategoString) versionTerm).stringValue();
final LanguageVersion version = LanguageVersion.parse(versionStr);
final LanguageIdentifier langId = new LanguageIdentifier(groupId, id, version);
langImpl = languageService.getImpl(langId);
if (langImpl == null) {
throw new MetaborgException("Cannot parse, language implementation " + langId + " does not exist");
}
}
// Parse the text.
final ISpoofaxInputUnit input;
if (file != null) {
@Nullable ILanguageImpl dialect;
try {
final IdentifiedDialect identifierDialect = dialectIdentifier.identify(file);
if (identifierDialect != null) {
dialect = identifierDialect.dialect;
} else {
dialect = null;
}
} catch (MetaborgException | MetaborgRuntimeException e) {
// Ignore
dialect = null;
}
input = unitService.inputUnit(file, text, langImpl, dialect);
} else {
input = unitService.inputUnit(text, langImpl, null);
}
final ISpoofaxParseUnit result = syntaxService.parse(input);
if (result.valid() && result.success()) {
return result.ast();
} else {
return null;
}
}
use of org.metaborg.core.language.LanguageVersion in project spoofax by metaborg.
the class LanguageServiceTest method multipleUnexpectedFacets.
/**
* Try to get a single facet, but have multiple. Assert that exception is thrown.
*/
@Test(expected = MetaborgRuntimeException.class)
public void multipleUnexpectedFacets() throws Exception {
final LanguageVersion version = version(0, 0, 1);
final FileObject location = createDir("ram:///");
final ILanguageComponent component = language(groupId, "org.metaborg.lang.entity", version, location, "Entity", new DescriptionFacet("Entity language", null), new DescriptionFacet("Entity language", null));
component.facet(DescriptionFacet.class);
}
use of org.metaborg.core.language.LanguageVersion in project spoofax by metaborg.
the class LanguageServiceTest method activeHigherVersion.
/**
* Add an implementation with a higher version number, assert that the newer implementation becomes active.
*/
@Test
public void activeHigherVersion() throws Exception {
final String id = "org.metaborg.lang.entity";
final LanguageVersion version1 = version(0, 0, 1);
final LanguageVersion version2 = version(0, 1, 0);
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 2 with higher version number becomes active.
assertSame(component1, languageService.getComponent(location1.getName()));
assertSame(component2, languageService.getComponent(location2.getName()));
assertSame(impl1, languageService.getImpl(identifier1));
assertSame(impl2, languageService.getImpl(identifier2));
assertSame(impl2, 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.LanguageVersion in project spoofax by metaborg.
the class LanguageServiceTest method conflictingContributionNames.
/**
* Add contributions iwth conflicting language names, assert that this throws an error.
*/
@Test(expected = IllegalStateException.class)
public void conflictingContributionNames() throws Exception {
final String id = "org.metaborg.lang.entity";
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 identifier = new LanguageIdentifier(groupId, id, version);
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 name1 = "Entity1";
final String name2 = "Entity2";
language(identifier1, location1, new LanguageContributionIdentifier(identifier, name1));
language(identifier2, location2, new LanguageContributionIdentifier(identifier, name2));
}
use of org.metaborg.core.language.LanguageVersion 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);
}
Aggregations