Search in sources :

Example 6 with MetaborgException

use of org.metaborg.core.MetaborgException 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 7 with MetaborgException

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

the class ParsePrimitive method call.

@Override
protected IStrategoTerm call(IStrategoTerm current, Strategy[] svars, IStrategoTerm[] tvars, ITermFactory factory, org.spoofax.interpreter.core.IContext strategoContext) throws MetaborgException, IOException {
    // Determine what to parse.
    if (!(current instanceof IStrategoString)) {
        throw new MetaborgException("Cannot parse, input string or file " + current + " is not a string");
    }
    final String stringOrFile = ((IStrategoString) current).stringValue();
    final String text;
    @Nullable final FileObject file;
    final IStrategoTerm isFileTerm = tvars[0];
    if (!(isFileTerm instanceof IStrategoInt)) {
        throw new MetaborgException("Cannot parse, input kind " + isFileTerm + " is not an integer");
    }
    if (((IStrategoInt) isFileTerm).intValue() == 1) {
        file = resourceService.resolve(stringOrFile);
        if (!file.exists() || !file.isFile()) {
            throw new MetaborgException("Cannot parse, input file " + file + " does not exist or is not a file");
        }
        text = sourceTextService.text(file);
    } else {
        file = null;
        text = stringOrFile;
    }
    // Determine which language to parse it with.
    final ILanguageImpl langImpl;
    final IStrategoTerm nameOrGroupIdTerm = tvars[1];
    final IStrategoTerm idTerm = tvars[2];
    final IStrategoTerm versionTerm = tvars[3];
    if (nameOrGroupIdTerm instanceof IStrategoTuple) {
        // No name, groupId, id, and version was set, auto detect language to parse with.
        if (file == null) {
            throw new MetaborgException("Cannot parse a string, no language to parse it with was given");
        }
        final IContext context = metaborgContext(strategoContext);
        if (context != null) {
            langImpl = languageIdentifierService.identify(file, context.project());
        } else {
            langImpl = languageIdentifierService.identify(file);
        }
        if (langImpl == null) {
            throw new MetaborgException("Cannot parse, language for " + file + " could not be identified");
        }
    } else if (idTerm instanceof IStrategoTuple) {
        // No id was set, name is set.
        if (!(nameOrGroupIdTerm instanceof IStrategoString)) {
            throw new MetaborgException("Cannot parse, language name " + nameOrGroupIdTerm + " is not a string");
        }
        final String name = ((IStrategoString) nameOrGroupIdTerm).stringValue();
        final ILanguage lang = languageService.getLanguage(name);
        if (lang == null) {
            throw new MetaborgException("Cannot parse, language " + nameOrGroupIdTerm + " does not exist");
        }
        langImpl = lang.activeImpl();
        if (langImpl == null) {
            throw new MetaborgException("Cannot parse, language " + lang + " has no implementation loaded");
        }
    } else {
        // A groupId, id, and version is set.
        if (!(nameOrGroupIdTerm instanceof IStrategoString)) {
            throw new MetaborgException("Cannot parse, language groupId " + nameOrGroupIdTerm + " is not a string");
        }
        final String groupId = ((IStrategoString) nameOrGroupIdTerm).stringValue();
        if (!(idTerm instanceof IStrategoString)) {
            throw new MetaborgException("Cannot parse, language id " + idTerm + " is not a string");
        }
        final String id = ((IStrategoString) idTerm).stringValue();
        if (!(versionTerm instanceof IStrategoString)) {
            throw new MetaborgException("Cannot parse, language version " + versionTerm + " is not a string");
        }
        final String versionStr = ((IStrategoString) versionTerm).stringValue();
        final LanguageVersion version = LanguageVersion.parse(versionStr);
        final LanguageIdentifier langId = new LanguageIdentifier(groupId, id, version);
        langImpl = languageService.getImpl(langId);
        if (langImpl == null) {
            throw new MetaborgException("Cannot parse, language implementation " + langId + " does not exist");
        }
    }
    // Parse the text.
    final ISpoofaxInputUnit input;
    if (file != null) {
        @Nullable ILanguageImpl dialect;
        try {
            final IdentifiedDialect identifierDialect = dialectIdentifier.identify(file);
            if (identifierDialect != null) {
                dialect = identifierDialect.dialect;
            } else {
                dialect = null;
            }
        } catch (MetaborgException | MetaborgRuntimeException e) {
            // Ignore
            dialect = null;
        }
        input = unitService.inputUnit(file, text, langImpl, dialect);
    } else {
        input = unitService.inputUnit(text, langImpl, null);
    }
    final ISpoofaxParseUnit result = syntaxService.parse(input);
    if (result.valid() && result.success()) {
        return result.ast();
    } else {
        return null;
    }
}
Also used : ISpoofaxParseUnit(org.metaborg.spoofax.core.unit.ISpoofaxParseUnit) MetaborgRuntimeException(org.metaborg.core.MetaborgRuntimeException) IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) IContext(org.metaborg.core.context.IContext) MetaborgException(org.metaborg.core.MetaborgException) IStrategoString(org.spoofax.interpreter.terms.IStrategoString) IStrategoTuple(org.spoofax.interpreter.terms.IStrategoTuple) IStrategoString(org.spoofax.interpreter.terms.IStrategoString) IStrategoInt(org.spoofax.interpreter.terms.IStrategoInt) ILanguage(org.metaborg.core.language.ILanguage) LanguageIdentifier(org.metaborg.core.language.LanguageIdentifier) ILanguageImpl(org.metaborg.core.language.ILanguageImpl) ISpoofaxInputUnit(org.metaborg.spoofax.core.unit.ISpoofaxInputUnit) LanguageVersion(org.metaborg.core.language.LanguageVersion) FileObject(org.apache.commons.vfs2.FileObject) IdentifiedDialect(org.metaborg.core.language.dialect.IdentifiedDialect) Nullable(javax.annotation.Nullable)

Example 8 with MetaborgException

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

the class CLIUtils method getLanguage.

/**
 * Get a already loaded language by language name
 */
public ILanguageImpl getLanguage(String languageName) throws MetaborgException {
    final ILanguage lang = spoofax.languageService.getLanguage(languageName);
    if (lang == null) {
        throw new MetaborgException("Cannot find language " + languageName);
    }
    final ILanguageImpl langImpl = lang.activeImpl();
    if (langImpl == null) {
        throw new MetaborgException("Language " + languageName + " has no active implementation");
    }
    return langImpl;
}
Also used : ILanguage(org.metaborg.core.language.ILanguage) ILanguageImpl(org.metaborg.core.language.ILanguageImpl) MetaborgException(org.metaborg.core.MetaborgException)

Example 9 with MetaborgException

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

the class LegacyForeignCallPrimitive method call.

@Override
protected IStrategoTerm call(IStrategoTerm current, Strategy[] svars, IStrategoTerm[] tvars, ITermFactory factory, IContext currentContext) throws MetaborgException {
    final String languageName = Tools.asJavaString(tvars[0]);
    final String strategyName = Tools.asJavaString(tvars[1]);
    // GTODO: require language identifier instead of language name
    final ILanguage language = languageService.getLanguage(languageName);
    if (language == null) {
        final String message = String.format("Stratego strategy call of '%s' into language %s failed, language could not be found", strategyName, languageName);
        throw new MetaborgException(message);
    }
    final ILanguageImpl activeImpl = language.activeImpl();
    if (activeImpl == null) {
        final String message = String.format("Stratego strategy call of '%s' into language %s failed, no active language implementation could be found", strategyName, languageName);
        throw new MetaborgException(message);
    }
    try {
        final IProject project = projectService.get(currentContext.location());
        IContext context = contextService.get(currentContext.location(), project, activeImpl);
        return common.invoke(activeImpl, context, current, strategyName);
    } catch (MetaborgException e) {
        final String message = String.format("Stratego strategy call of '%s' into language %s failed unexpectedly", strategyName, languageName);
        throw new MetaborgException(message, e);
    }
}
Also used : ILanguage(org.metaborg.core.language.ILanguage) IContext(org.metaborg.core.context.IContext) ILanguageImpl(org.metaborg.core.language.ILanguageImpl) MetaborgException(org.metaborg.core.MetaborgException) IProject(org.metaborg.core.project.IProject)

Example 10 with MetaborgException

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

the class LanguageSpecBuilder method pkg.

public void pkg(LanguageSpecBuildInput input) throws MetaborgException {
    logger.debug("Packaging language implementation for {}", input.languageSpec().location());
    initPluto();
    try {
        final Origin origin = GenerateSourcesBuilder.origin(generateSourcesBuilderInput(input));
        final String path = path(input);
        plutoBuild(PackageBuilder.request(packageBuilderInput(input, origin)), path);
    } catch (RequiredBuilderFailed e) {
        if (e.getMessage().contains("no rebuild of failing builder")) {
            throw new MetaborgException(failingRebuildMessage);
        }
        throw new MetaborgException();
    } catch (RuntimeException e) {
        throw e;
    } catch (Throwable e) {
        throw new MetaborgException(e);
    }
    for (IBuildStep buildStep : buildSteps) {
        buildStep.execute(LanguageSpecBuildPhase.pkg, input);
    }
}
Also used : Origin(build.pluto.dependency.Origin) MetaborgException(org.metaborg.core.MetaborgException) RequiredBuilderFailed(build.pluto.builder.RequiredBuilderFailed)

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