use of org.metaborg.core.language.IdentifiedResource 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);
}
use of org.metaborg.core.language.IdentifiedResource in project spoofax by metaborg.
the class LanguageSourceFilesPrimitive method call.
@Override
protected IStrategoTerm call(IStrategoTerm current, Strategy[] svars, IStrategoTerm[] tvars, ITermFactory factory, IContext context) throws MetaborgException {
if (!Tools.isTermString(tvars[0])) {
return null;
}
final IProject project = projectService.get(context.location());
if (project == null) {
return factory.makeList();
}
// GTODO: require language identifier instead of language name
final String languageName = Tools.asJavaString(tvars[0]);
final ILanguage language = languageService.getLanguage(languageName);
if (language == null) {
final String message = String.format("Getting include files for %s failed, language could not be found", languageName);
throw new MetaborgException(message);
}
final ILanguageImpl impl = language.activeImpl();
if (impl == null) {
final String message = String.format("Getting include files for %s failed, no active language implementation could be found", languageName);
throw new MetaborgException(message);
}
final Iterable<IdentifiedResource> sourceFiles = languagePathService.sourceFiles(project, impl);
final List<IStrategoTerm> terms = Lists.newArrayList();
for (IdentifiedResource sourceFile : sourceFiles) {
terms.add(factory.makeString(sourceFile.resource.getName().getURI()));
}
return factory.makeList(terms);
}
use of org.metaborg.core.language.IdentifiedResource in project spoofax by metaborg.
the class Builder method build.
@Override
public IBuildOutput<P, A, AU, T> build(BuildInput input, IProgress progress, ICancel cancel) throws InterruptedException {
cancel.throwIfCancelled();
final Multimap<ILanguageImpl, IdentifiedResourceChange> changes = ArrayListMultimap.create();
identifyResources(input.sourceChanges, input, changes, cancel);
if (changes.size() == 0) {
// When there are no source changes, keep the old state and skip building.
final IBuildOutputInternal<P, A, AU, T> buildOutput = buildOutputProvider.get();
buildOutput.setState(input.state);
return buildOutput;
}
cancel.throwIfCancelled();
logger.info("Building " + input.project.location());
final BuildState newState = new BuildState();
final IBuildOutputInternal<P, A, AU, T> buildOutput = buildOutputProvider.get();
buildOutput.setState(newState);
final Iterable<ILanguageImpl> buildOrder = input.buildOrder.buildOrder();
progress.setWorkRemaining(Iterables.size(buildOrder));
for (ILanguageImpl language : buildOrder) {
cancel.throwIfCancelled();
final LanguageBuildState languageState = input.state.get(resourceService, languageIdentifier, language);
final Collection<IdentifiedResourceChange> sourceChanges = changes.get(language);
if (sourceChanges.size() == 0) {
// When there are no source changes for this language, keep the old state and don't build.
newState.add(language, languageState);
continue;
}
final Iterable<FileObject> includePaths = input.includePaths.get(language);
final Iterable<IdentifiedResource> includeFiles = languagePathService.toFiles(includePaths, language);
final LanguageBuildDiff diff = languageState.diff(changes.get(language), includeFiles);
final boolean pardoned = input.pardonedLanguages.contains(language);
final Collection<FileObject> newResources = updateLanguageResources(input, language, diff, buildOutput, pardoned, progress.subProgress(1), cancel);
final Iterable<ResourceChange> newResourceChanges = ResourceUtils.toChanges(newResources, ResourceChangeKind.Create);
identifyResources(newResourceChanges, input, changes, cancel);
newState.add(language, diff.newState);
}
final IMessagePrinter printer = input.messagePrinter;
if (printer != null) {
printer.printSummary();
}
return buildOutput;
}
use of org.metaborg.core.language.IdentifiedResource in project spoofax by metaborg.
the class LegacyParseFilePrimitive method call.
@Override
public boolean call(IContext env, Strategy[] strategies, IStrategoTerm[] terms) throws InterpreterException {
if (!Tools.isTermString(terms[0]))
return false;
if (!Tools.isTermString(terms[3]))
return false;
try {
final String pathOrInput = Tools.asJavaString(terms[0]);
final String pathOrInput2 = Tools.asJavaString(terms[3]);
FileObject resource;
String text;
try {
resource = resourceService.resolve(pathOrInput);
if (!resource.exists() || resource.getType() != FileType.FILE) {
resource = resourceService.resolve(pathOrInput2);
text = pathOrInput;
} else {
text = sourceTextService.text(resource);
}
} catch (MetaborgRuntimeException | IOException e) {
resource = resourceService.resolve(pathOrInput2);
text = pathOrInput;
}
if (resource.getType() != FileType.FILE) {
return false;
}
final IdentifiedResource identifiedResource = languageIdentifierService.identifyToResource(resource);
if (identifiedResource == null) {
return false;
}
final ISpoofaxInputUnit input = unitService.inputUnit(resource, text, identifiedResource.language, identifiedResource.dialect);
final ISpoofaxParseUnit result = syntaxService.parse(input);
if (result.valid() && result.success()) {
env.setCurrent(result.ast());
} else {
return false;
}
} catch (ParseException | IOException e) {
throw new InterpreterException("Parsing failed unexpectedly", e);
}
return true;
}
Aggregations