Search in sources :

Example 36 with MetaborgException

use of org.metaborg.core.MetaborgException in project spoofax by metaborg.

the class ASpoofaxPrimitive method call.

@Override
public boolean call(IContext context, Strategy[] svars, IStrategoTerm[] tvars) throws InterpreterException {
    final IStrategoTerm current = context.current();
    final ITermFactory factory = context.getFactory();
    try {
        final IStrategoTerm newCurrent = call(current, svars, tvars, factory, context);
        if (newCurrent != null) {
            context.setCurrent(newCurrent);
            return true;
        }
    } catch (MetaborgException | IOException e) {
        throw new InterpreterException("Executing primitive " + name + " failed unexpectedly", e);
    }
    return false;
}
Also used : IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) InterpreterException(org.spoofax.interpreter.core.InterpreterException) MetaborgException(org.metaborg.core.MetaborgException) IOException(java.io.IOException) ITermFactory(org.spoofax.interpreter.terms.ITermFactory)

Example 37 with MetaborgException

use of org.metaborg.core.MetaborgException in project spoofax by metaborg.

the class SimpleProjectService method create.

@Override
public IProject create(FileObject location) throws MetaborgException {
    final FileName name = location.getName();
    for (FileName projectName : projects.keySet()) {
        if (name.equals(projectName) || name.isAncestor(projectName)) {
            final String message = String.format("Location %s is equal to or nested in project %s", name, projectName);
            throw new MetaborgException(message);
        }
    }
    final ConfigRequest<? extends IProjectConfig> configRequest = projectConfigService.get(location);
    if (!configRequest.valid()) {
        logger.error("Errors occurred when retrieving project configuration from project directory {}", location);
        configRequest.reportErrors(new StreamMessagePrinter(sourceTextService, false, false, logger));
    }
    final IProjectConfig config;
    if (configRequest.config() != null) {
        config = configRequest.config();
    } else {
        logger.debug("Using default configuration for project at {}", location);
        config = projectConfigService.defaultConfig(location);
    }
    final IProject project = new Project(location, config);
    if (projects.putIfAbsent(name, project) != null) {
        final String message = String.format("Project with location %s already exists", name);
        throw new MetaborgException(message);
    }
    return project;
}
Also used : StreamMessagePrinter(org.metaborg.core.messages.StreamMessagePrinter) FileName(org.apache.commons.vfs2.FileName) MetaborgException(org.metaborg.core.MetaborgException) IProjectConfig(org.metaborg.core.config.IProjectConfig)

Example 38 with MetaborgException

use of org.metaborg.core.MetaborgException in project spoofax by metaborg.

the class LanguageSpecPpNamePrimitive method call.

@Override
protected IStrategoTerm call(IStrategoTerm current, Strategy[] svars, IStrategoTerm[] tvars, ITermFactory factory, IContext context) throws MetaborgException {
    final FileObject location = context.location();
    final IProject project = projectService.get(location);
    if (project == null) {
        return null;
    }
    if (languageSpecServiceProvider == null) {
        // Indicates that meta-Spoofax is not available (ISpoofaxLanguageSpecService cannot be injected), but this
        // should never happen because this primitive is inside meta-Spoofax. Check for null just in case.
        logger.error("Language specification service is not available; static injection failed");
        return null;
    }
    final ISpoofaxLanguageSpecService languageSpecService = languageSpecServiceProvider.get();
    if (!languageSpecService.available(project)) {
        return null;
    }
    final ISpoofaxLanguageSpec languageSpec;
    try {
        languageSpec = languageSpecService.get(project);
    } catch (ConfigException e) {
        throw new MetaborgException("Unable to get language specification name for " + location, e);
    }
    if (languageSpec == null) {
        return null;
    }
    return factory.makeString(languageSpec.config().prettyPrintLanguage());
}
Also used : ISpoofaxLanguageSpec(org.metaborg.spoofax.meta.core.project.ISpoofaxLanguageSpec) ISpoofaxLanguageSpecService(org.metaborg.spoofax.meta.core.project.ISpoofaxLanguageSpecService) MetaborgException(org.metaborg.core.MetaborgException) ConfigException(org.metaborg.core.config.ConfigException) FileObject(org.apache.commons.vfs2.FileObject) IProject(org.metaborg.core.project.IProject)

Example 39 with MetaborgException

use of org.metaborg.core.MetaborgException in project spoofax by metaborg.

the class StrategoAnalyzer method analyze.

private ISpoofaxAnalyzeUnit analyze(ISpoofaxParseUnit input, IContext context, HybridInterpreter runtime, String strategy, ITermFactory termFactory) throws AnalysisException {
    final FileObject source = input.source();
    final IStrategoString contextPath = strategoCommon.locationTerm(context.location());
    final IStrategoString resourcePath = strategoCommon.resourceTerm(source, context.location());
    final IStrategoTuple inputTerm = termFactory.makeTuple(input.ast(), resourcePath, contextPath);
    try {
        logger.trace("Analysing {}", source);
        final Timer timer = new Timer(true);
        final IStrategoTerm resultTerm = strategoCommon.invoke(runtime, inputTerm, strategy);
        final long duration = timer.stop();
        if (resultTerm == null) {
            logger.trace("Analysis for {} failed", source);
            return result(analysisCommon.analysisFailedMessage(runtime), input, context, null, duration);
        } else if (!(resultTerm instanceof IStrategoTuple)) {
            logger.trace("Analysis for {} has unexpected result, not a tuple", source);
            final String message = logger.format("Unexpected results from analysis {}", resultTerm);
            return result(message, input, context, null, duration);
        } else if (resultTerm.getSubtermCount() == 4) {
            logger.trace("Analysis for {} done", source);
            return result(resultTerm, input, context, duration);
        } else if (resultTerm.getSubtermCount() == 3) {
            logger.trace("Analysis for {} done", source);
            return resultNoAst(resultTerm, input, context, duration);
        } else {
            logger.trace("Analysis for {} has unexpected result; tuple with more than 4 or less than 2 elements", source);
            final String message = logger.format("Unexpected results from analysis {}", resultTerm);
            return result(message, input, context, null, duration);
        }
    } catch (MetaborgException e) {
        final String message = logger.format("Analysis for {} failed", source);
        logger.trace(message, e);
        throw new AnalysisException(context, message, e);
    }
}
Also used : IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) Timer(org.metaborg.util.time.Timer) MetaborgException(org.metaborg.core.MetaborgException) AnalysisException(org.metaborg.core.analysis.AnalysisException) IStrategoString(org.spoofax.interpreter.terms.IStrategoString) IStrategoTuple(org.spoofax.interpreter.terms.IStrategoTuple) FileObject(org.apache.commons.vfs2.FileObject) IStrategoString(org.spoofax.interpreter.terms.IStrategoString)

Example 40 with MetaborgException

use of org.metaborg.core.MetaborgException in project spoofax by metaborg.

the class TaskEngineAnalyzer method analyzeAll.

@Override
public ISpoofaxAnalyzeResults analyzeAll(Iterable<ISpoofaxParseUnit> inputs, IContext context, IProgress progress, ICancel cancel) throws AnalysisException, InterruptedException {
    cancel.throwIfCancelled();
    final ILanguageImpl langImpl = context.language();
    final ITermFactory termFactory = termFactoryService.getGeneric();
    final FacetContribution<AnalysisFacet> facetContribution = langImpl.facetContribution(AnalysisFacet.class);
    if (facetContribution == null) {
        logger.debug("No analysis required for {}", langImpl);
        return new SpoofaxAnalyzeResults(context);
    }
    final AnalysisFacet facet = facetContribution.facet;
    cancel.throwIfCancelled();
    final HybridInterpreter runtime;
    try {
        runtime = runtimeService.runtime(facetContribution.contributor, context, false);
    } catch (MetaborgException e) {
        throw new AnalysisException(context, "Failed to get Stratego runtime", e);
    }
    cancel.throwIfCancelled();
    return analyzeAll(inputs, context, runtime, facet.strategyName, termFactory);
}
Also used : ISpoofaxAnalyzeResults(org.metaborg.spoofax.core.analysis.ISpoofaxAnalyzeResults) SpoofaxAnalyzeResults(org.metaborg.spoofax.core.analysis.SpoofaxAnalyzeResults) ILanguageImpl(org.metaborg.core.language.ILanguageImpl) MetaborgException(org.metaborg.core.MetaborgException) HybridInterpreter(org.strategoxt.HybridInterpreter) AnalysisException(org.metaborg.core.analysis.AnalysisException) AnalysisFacet(org.metaborg.spoofax.core.analysis.AnalysisFacet) ITermFactory(org.spoofax.interpreter.terms.ITermFactory)

Aggregations

MetaborgException (org.metaborg.core.MetaborgException)49 FileObject (org.apache.commons.vfs2.FileObject)25 IStrategoTerm (org.spoofax.interpreter.terms.IStrategoTerm)19 ILanguageImpl (org.metaborg.core.language.ILanguageImpl)17 HybridInterpreter (org.strategoxt.HybridInterpreter)14 IProject (org.metaborg.core.project.IProject)12 IStrategoString (org.spoofax.interpreter.terms.IStrategoString)11 IOException (java.io.IOException)9 IContext (org.metaborg.core.context.IContext)9 ITermFactory (org.spoofax.interpreter.terms.ITermFactory)9 Nullable (javax.annotation.Nullable)8 AnalysisException (org.metaborg.core.analysis.AnalysisException)8 FileSystemException (org.apache.commons.vfs2.FileSystemException)7 MetaborgRuntimeException (org.metaborg.core.MetaborgRuntimeException)7 ISpoofaxAnalyzeResults (org.metaborg.spoofax.core.analysis.ISpoofaxAnalyzeResults)6 SpoofaxAnalyzeResults (org.metaborg.spoofax.core.analysis.SpoofaxAnalyzeResults)6 IStrategoAppl (org.spoofax.interpreter.terms.IStrategoAppl)6 ILanguage (org.metaborg.core.language.ILanguage)5 ISourceRegion (org.metaborg.core.source.ISourceRegion)5 ISpoofaxParseUnit (org.metaborg.spoofax.core.unit.ISpoofaxParseUnit)5