Search in sources :

Example 1 with IdentifiedResource

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);
}
Also used : ILanguage(org.metaborg.core.language.ILanguage) IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) ILanguageImpl(org.metaborg.core.language.ILanguageImpl) MetaborgException(org.metaborg.core.MetaborgException) IdentifiedResource(org.metaborg.core.language.IdentifiedResource) IProject(org.metaborg.core.project.IProject)

Example 2 with IdentifiedResource

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);
    }
}
Also used : ISpoofaxParseUnit(org.metaborg.spoofax.core.unit.ISpoofaxParseUnit) ISpoofaxInputUnit(org.metaborg.spoofax.core.unit.ISpoofaxInputUnit) FileObject(org.apache.commons.vfs2.FileObject) ParseException(org.metaborg.core.syntax.ParseException) IOException(java.io.IOException) StrategoException(org.strategoxt.lang.StrategoException) IdentifiedResource(org.metaborg.core.language.IdentifiedResource)

Example 3 with IdentifiedResource

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);
        }
    }
}
Also used : FileSystemException(org.apache.commons.vfs2.FileSystemException) ILanguageImpl(org.metaborg.core.language.ILanguageImpl) FileSelector(org.apache.commons.vfs2.FileSelector) LanguagesFileSelector(org.metaborg.core.language.LanguagesFileSelector) FileObject(org.apache.commons.vfs2.FileObject) ResourceChange(org.metaborg.core.resource.ResourceChange) IdentifiedResourceChange(org.metaborg.core.resource.IdentifiedResourceChange) IdentifiedResource(org.metaborg.core.language.IdentifiedResource) IdentifiedResourceChange(org.metaborg.core.resource.IdentifiedResourceChange)

Example 4 with IdentifiedResource

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;
}
Also used : FileName(org.apache.commons.vfs2.FileName) FileObject(org.apache.commons.vfs2.FileObject) IdentifiedResourceChange(org.metaborg.core.resource.IdentifiedResourceChange) IdentifiedResource(org.metaborg.core.language.IdentifiedResource) IdentifiedResourceChange(org.metaborg.core.resource.IdentifiedResourceChange) ResourceChange(org.metaborg.core.resource.ResourceChange)

Example 5 with IdentifiedResource

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;
}
Also used : FileObject(org.apache.commons.vfs2.FileObject) IdentifiedResource(org.metaborg.core.language.IdentifiedResource)

Aggregations

IdentifiedResource (org.metaborg.core.language.IdentifiedResource)9 FileObject (org.apache.commons.vfs2.FileObject)6 ILanguageImpl (org.metaborg.core.language.ILanguageImpl)5 IdentifiedResourceChange (org.metaborg.core.resource.IdentifiedResourceChange)3 ResourceChange (org.metaborg.core.resource.ResourceChange)3 IOException (java.io.IOException)2 MetaborgException (org.metaborg.core.MetaborgException)2 ILanguage (org.metaborg.core.language.ILanguage)2 IProject (org.metaborg.core.project.IProject)2 ParseException (org.metaborg.core.syntax.ParseException)2 ISpoofaxInputUnit (org.metaborg.spoofax.core.unit.ISpoofaxInputUnit)2 ISpoofaxParseUnit (org.metaborg.spoofax.core.unit.ISpoofaxParseUnit)2 IStrategoTerm (org.spoofax.interpreter.terms.IStrategoTerm)2 FileName (org.apache.commons.vfs2.FileName)1 FileSelector (org.apache.commons.vfs2.FileSelector)1 FileSystemException (org.apache.commons.vfs2.FileSystemException)1 MetaborgRuntimeException (org.metaborg.core.MetaborgRuntimeException)1 ILanguageComponent (org.metaborg.core.language.ILanguageComponent)1 LanguagesFileSelector (org.metaborg.core.language.LanguagesFileSelector)1 IMessagePrinter (org.metaborg.core.messages.IMessagePrinter)1