Search in sources :

Example 6 with IdentifiedResource

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);
}
Also used : ILanguageImpl(org.metaborg.core.language.ILanguageImpl) ILanguageComponent(org.metaborg.core.language.ILanguageComponent) IdentifiedResource(org.metaborg.core.language.IdentifiedResource)

Example 7 with IdentifiedResource

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);
}
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 8 with IdentifiedResource

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

Example 9 with IdentifiedResource

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