Search in sources :

Example 1 with ResourceChangeKind

use of org.metaborg.core.resource.ResourceChangeKind in project spoofax by metaborg.

the class DialectProcessor method update.

@Override
public void update(FileObject location, Iterable<ResourceChange> changes) {
    final int numChanges = Iterables.size(changes);
    if (numChanges == 0) {
        return;
    }
    final ILanguage strategoLanguage = languageService.getLanguage(SpoofaxConstants.LANG_STRATEGO_NAME);
    if (strategoLanguage == null) {
        logger.debug("Could not find Stratego language, Stratego dialects cannot be updated");
        return;
    }
    final ILanguageImpl strategoImpl = strategoLanguage.activeImpl();
    if (strategoImpl == null) {
        logger.debug("Could not find active Stratego language implementation, " + "Stratego dialects cannot be updated");
        return;
    }
    logger.debug("Processing dialect updates for {}", location);
    // HACK: assuming single syntax facet
    final SyntaxFacet baseFacet = strategoImpl.facet(SyntaxFacet.class);
    if (baseFacet == null) {
        logger.debug("Active Stratego language implementation has no syntax facet, " + "Stratego dialects cannot be updated");
        return;
    }
    for (ResourceChange change : changes) {
        final FileObject resource = change.resource;
        try {
            if (!FileSelectorUtils.include(selector, resource, location)) {
                continue;
            }
        } catch (FileSystemException e) {
            continue;
        }
        final String fileName = FilenameUtils.getBaseName(resource.getName().getBaseName());
        final SyntaxFacet newFacet = new SyntaxFacet(resource, baseFacet.completionParseTable, baseFacet.startSymbols, baseFacet.singleLineCommentPrefixes, baseFacet.multiLineCommentCharacters, baseFacet.fenceCharacters);
        final ResourceChangeKind changeKind = change.kind;
        try {
            switch(changeKind) {
                case Create:
                    add(fileName, resource, strategoImpl, newFacet);
                    break;
                case Delete:
                    remove(fileName, resource);
                    break;
                case Modify:
                    update(fileName, resource, newFacet);
                    break;
                case Rename:
                    if (change.from != null) {
                        remove(fileName, resource);
                    }
                    if (change.to != null) {
                        add(fileName, resource, strategoImpl, newFacet);
                    }
                    break;
                case Copy:
                    if (change.to != null) {
                        add(fileName, resource, strategoImpl, newFacet);
                    }
                    break;
                default:
                    logger.error("Unhandled resource change kind {}", changeKind);
                    break;
            }
        } catch (MetaborgRuntimeException e) {
            logger.error("Failed to update dialect", e);
        }
    }
}
Also used : ILanguage(org.metaborg.core.language.ILanguage) MetaborgRuntimeException(org.metaborg.core.MetaborgRuntimeException) SyntaxFacet(org.metaborg.spoofax.core.syntax.SyntaxFacet) FileSystemException(org.apache.commons.vfs2.FileSystemException) ILanguageImpl(org.metaborg.core.language.ILanguageImpl) ResourceChangeKind(org.metaborg.core.resource.ResourceChangeKind) FileObject(org.apache.commons.vfs2.FileObject) ResourceChange(org.metaborg.core.resource.ResourceChange)

Example 2 with ResourceChangeKind

use of org.metaborg.core.resource.ResourceChangeKind 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;
}
Also used : IOException(java.io.IOException) IdentifiedResourceChange(org.metaborg.core.resource.IdentifiedResourceChange) ILanguageImpl(org.metaborg.core.language.ILanguageImpl) ResourceChangeKind(org.metaborg.core.resource.ResourceChangeKind) FileObject(org.apache.commons.vfs2.FileObject) ParseException(org.metaborg.core.syntax.ParseException) ResourceChange(org.metaborg.core.resource.ResourceChange) IdentifiedResourceChange(org.metaborg.core.resource.IdentifiedResourceChange)

Aggregations

FileObject (org.apache.commons.vfs2.FileObject)2 ILanguageImpl (org.metaborg.core.language.ILanguageImpl)2 ResourceChange (org.metaborg.core.resource.ResourceChange)2 ResourceChangeKind (org.metaborg.core.resource.ResourceChangeKind)2 IOException (java.io.IOException)1 FileSystemException (org.apache.commons.vfs2.FileSystemException)1 MetaborgRuntimeException (org.metaborg.core.MetaborgRuntimeException)1 ILanguage (org.metaborg.core.language.ILanguage)1 IdentifiedResourceChange (org.metaborg.core.resource.IdentifiedResourceChange)1 ParseException (org.metaborg.core.syntax.ParseException)1 SyntaxFacet (org.metaborg.spoofax.core.syntax.SyntaxFacet)1