use of org.metaborg.core.language.IdentifiedResource in project spoofax by metaborg.
the class LanguageIncludeFilesPrimitive 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> includeFiles = languagePathService.includeFiles(project, impl);
final List<IStrategoTerm> terms = Lists.newArrayList();
for (IdentifiedResource includeFile : includeFiles) {
terms.add(factory.makeString(includeFile.resource.getName().getURI()));
}
return factory.makeList(terms);
}
use of org.metaborg.core.language.IdentifiedResource in project spoofax by metaborg.
the class ParseFileStrategy method invoke.
@Override
public IStrategoTerm invoke(Context context, IStrategoTerm current) {
if (!Tools.isTermString(current))
return null;
try {
final String path = Tools.asJavaString(current);
final FileObject resource = resourceService.resolve(path);
if (resource.getType() != FileType.FILE) {
return null;
}
final IdentifiedResource identifiedResource = languageIdentifierService.identifyToResource(resource);
if (identifiedResource == null) {
return null;
}
final String text = sourceTextService.text(resource);
final ISpoofaxInputUnit input = unitService.inputUnit(resource, text, identifiedResource.language, identifiedResource.dialect);
final ISpoofaxParseUnit result = syntaxService.parse(input);
return result.ast();
} catch (ParseException | IOException e) {
throw new StrategoException("Parsing failed unexpectedly", e);
}
}
use of org.metaborg.core.language.IdentifiedResource in project spoofax by metaborg.
the class Builder method identifyResources.
private void identifyResources(Iterable<ResourceChange> changes, BuildInput input, Multimap<ILanguageImpl, IdentifiedResourceChange> identifiedChanges, ICancel cancel) throws InterruptedException {
final Iterable<ILanguageImpl> languages = input.buildOrder.languages();
final FileSelector selector = input.selector;
final FileObject location = input.project.location();
for (ResourceChange change : changes) {
cancel.throwIfCancelled();
final FileObject resource = change.resource;
if (selector != null) {
try {
if (!FileSelectorUtils.include(selector, resource, location)) {
continue;
}
} catch (FileSystemException e) {
logger.error("Error determining if {} should be ignored from the build, including it", e, resource);
}
}
final IdentifiedResource identifiedResource = languageIdentifier.identifyToResource(resource, languages);
if (identifiedResource != null) {
final IdentifiedResourceChange identifiedChange = new IdentifiedResourceChange(change, identifiedResource);
identifiedChanges.put(identifiedChange.language, identifiedChange);
}
}
}
use of org.metaborg.core.language.IdentifiedResource in project spoofax by metaborg.
the class FilesBuildState method includeDiff.
private Iterable<IdentifiedResourceChange> includeDiff(LanguageBuildState newState, Iterable<IdentifiedResource> newFiles) {
final Collection<IdentifiedResourceChange> changes = Lists.newLinkedList();
final Set<FileName> existingFiles = Sets.newHashSet(include.files);
for (IdentifiedResource identifiedResource : newFiles) {
final FileObject resource = identifiedResource.resource;
final FileName name = resource.getName();
final long newModification = newState.include.add(resource);
existingFiles.remove(name);
if (include.files.contains(name)) {
final long existingModification = include.modification.get(name);
if (existingModification != newModification) {
changes.add(new IdentifiedResourceChange(new ResourceChange(resource, ResourceChangeKind.Modify), identifiedResource));
}
} else {
changes.add(new IdentifiedResourceChange(new ResourceChange(resource, ResourceChangeKind.Create), identifiedResource));
}
}
for (FileName name : existingFiles) {
newState.include.remove(name);
final FileObject resource = resourceService.resolve(name.getURI());
final IdentifiedResource identifiedResource = languageIdentifierService.identifyToResource(resource, Iterables2.singleton(language));
if (identifiedResource != null) {
changes.add(new IdentifiedResourceChange(new ResourceChange(resource, ResourceChangeKind.Delete), identifiedResource));
}
}
return changes;
}
use of org.metaborg.core.language.IdentifiedResource in project spoofax by metaborg.
the class LanguagePathService method toFiles.
@Override
public Collection<IdentifiedResource> toFiles(Iterable<FileObject> paths, ILanguageImpl language) {
final Collection<FileObject> files = ResourceUtils.expand(paths);
final Collection<IdentifiedResource> identifiedFiles = Lists.newArrayListWithExpectedSize(files.size());
for (FileObject file : files) {
final IdentifiedResource identifiedFile = languageIdentifierService.identifyToResource(file, Iterables2.singleton(language));
if (identifiedFile != null) {
identifiedFiles.add(identifiedFile);
}
}
return identifiedFiles;
}
Aggregations