Search in sources :

Example 1 with LanguageIdentifier

use of org.metaborg.core.language.LanguageIdentifier in project spoofax by metaborg.

the class LanguageComponentFactory method createConfig.

@Override
public ComponentCreationConfig createConfig(IComponentCreationConfigRequest configRequest) throws MetaborgException {
    final ComponentFactoryRequest request = (ComponentFactoryRequest) configRequest;
    final FileObject location = request.location();
    if (!request.valid()) {
        throw new MetaborgException(request.toString());
    }
    final ILanguageComponentConfig componentConfig = request.config();
    logger.debug("Creating language component for {}", location);
    final LanguageIdentifier identifier = componentConfig.identifier();
    final Collection<LanguageContributionIdentifier> langContribs = request.config().langContribs();
    if (langContribs.isEmpty()) {
        langContribs.add(new LanguageContributionIdentifier(identifier, componentConfig.name()));
    }
    final ComponentCreationConfig config = new ComponentCreationConfig(identifier, location, langContribs, componentConfig);
    final SyntaxFacet syntaxFacet;
    if (componentConfig.sdfEnabled()) {
        syntaxFacet = request.syntaxFacet();
        if (syntaxFacet != null) {
            config.addFacet(syntaxFacet);
        }
    } else {
        syntaxFacet = null;
    }
    final StrategoRuntimeFacet strategoRuntimeFacet = request.strategoRuntimeFacet();
    if (strategoRuntimeFacet != null) {
        config.addFacet(strategoRuntimeFacet);
    }
    final IStrategoAppl esvTerm = request.esvTerm();
    if (esvTerm != null) {
        final String[] extensions = extensions(esvTerm);
        if (extensions.length != 0) {
            final Iterable<String> extensionsIterable = Iterables2.from(extensions);
            final IdentificationFacet identificationFacet = new IdentificationFacet(new ResourceExtensionsIdentifier(extensionsIterable));
            config.addFacet(identificationFacet);
            final ResourceExtensionFacet resourceExtensionsFacet = new ResourceExtensionFacet(extensionsIterable);
            config.addFacet(resourceExtensionsFacet);
        }
        if (ParseFacetFromESV.hasParser(esvTerm)) {
            config.addFacet(ParseFacetFromESV.create(esvTerm));
        } else if (syntaxFacet != null) {
            config.addFacet(new ParseFacet("jsglr"));
        }
        final boolean hasContext = ContextFacetFromESV.hasContext(esvTerm);
        final boolean hasAnalysis = AnalysisFacetFromESV.hasAnalysis(esvTerm);
        final IContextFactory contextFactory;
        final ISpoofaxAnalyzer analyzer;
        final AnalysisFacet analysisFacet;
        if (hasAnalysis) {
            final String analysisType = AnalysisFacetFromESV.type(esvTerm);
            assert analysisType != null : "Analyzer type cannot be null because hasAnalysis is true, no null check is needed.";
            analyzer = analyzers.get(analysisType);
            analysisFacet = AnalysisFacetFromESV.create(esvTerm);
            final String contextType = hasContext ? ContextFacetFromESV.type(esvTerm) : null;
            if (hasContext && contextType == null) {
                contextFactory = null;
            } else {
                final String analysisContextType;
                switch(analysisType) {
                    default:
                    case StrategoAnalyzer.name:
                        analysisContextType = LegacyContextFactory.name;
                        break;
                    case TaskEngineAnalyzer.name:
                        analysisContextType = IndexTaskContextFactory.name;
                        break;
                    case ConstraintSingleFileAnalyzer.name:
                        analysisContextType = SingleFileScopeGraphContextFactory.name;
                        break;
                    case ConstraintMultiFileAnalyzer.name:
                        analysisContextType = MultiFileScopeGraphContextFactory.name;
                        break;
                }
                if (hasContext && !analysisContextType.equals(contextType)) {
                    logger.warn("Ignoring explicit context type {}, because it is incompatible with analysis {}.", contextType, analysisType);
                }
                contextFactory = contextFactory(analysisContextType);
            }
        } else if (hasContext) {
            final String type = ContextFacetFromESV.type(esvTerm);
            contextFactory = contextFactory(type);
            analyzer = null;
            analysisFacet = null;
        } else {
            contextFactory = contextFactory(LegacyContextFactory.name);
            analyzer = null;
            analysisFacet = null;
        }
        if (contextFactory != null) {
            final IContextStrategy contextStrategy = contextStrategy(ProjectContextStrategy.name);
            config.addFacet(new ContextFacet(contextFactory, contextStrategy));
        }
        if (analyzer != null) {
            config.addFacet(new AnalyzerFacet<>(analyzer));
        }
        if (analysisFacet != null) {
            config.addFacet(analysisFacet);
        }
        final ActionFacet menusFacet = ActionFacetFromESV.create(esvTerm);
        if (menusFacet != null) {
            config.addFacet(menusFacet);
        }
        final StylerFacet stylerFacet = StylerFacetFromESV.create(esvTerm);
        if (stylerFacet != null) {
            config.addFacet(stylerFacet);
        }
        final ResolverFacet resolverFacet = ResolverFacetFromESV.createResolver(esvTerm);
        if (resolverFacet != null) {
            config.addFacet(resolverFacet);
        }
        final HoverFacet hoverFacet = ResolverFacetFromESV.createHover(esvTerm);
        if (hoverFacet != null) {
            config.addFacet(hoverFacet);
        }
        final OutlineFacet outlineFacet = OutlineFacetFromESV.create(esvTerm);
        if (outlineFacet != null) {
            config.addFacet(outlineFacet);
        }
        final ShellFacet shellFacet = ShellFacetFromESV.create(esvTerm);
        if (shellFacet != null) {
            config.addFacet(shellFacet);
        }
    }
    return config;
}
Also used : ResourceExtensionsIdentifier(org.metaborg.core.language.ResourceExtensionsIdentifier) ResolverFacet(org.metaborg.spoofax.core.tracing.ResolverFacet) MetaborgException(org.metaborg.core.MetaborgException) IStrategoAppl(org.spoofax.interpreter.terms.IStrategoAppl) LanguageContributionIdentifier(org.metaborg.core.language.LanguageContributionIdentifier) ParseFacet(org.metaborg.core.syntax.ParseFacet) ISpoofaxAnalyzer(org.metaborg.spoofax.core.analysis.ISpoofaxAnalyzer) IContextFactory(org.metaborg.core.context.IContextFactory) FileObject(org.apache.commons.vfs2.FileObject) StylerFacet(org.metaborg.spoofax.core.style.StylerFacet) ContextFacet(org.metaborg.core.context.ContextFacet) OutlineFacet(org.metaborg.spoofax.core.outline.OutlineFacet) ILanguageComponentConfig(org.metaborg.core.config.ILanguageComponentConfig) StrategoRuntimeFacet(org.metaborg.spoofax.core.stratego.StrategoRuntimeFacet) ShellFacet(org.metaborg.spoofax.core.shell.ShellFacet) SyntaxFacet(org.metaborg.spoofax.core.syntax.SyntaxFacet) IdentificationFacet(org.metaborg.core.language.IdentificationFacet) ActionFacet(org.metaborg.spoofax.core.action.ActionFacet) AnalysisFacet(org.metaborg.spoofax.core.analysis.AnalysisFacet) LanguageIdentifier(org.metaborg.core.language.LanguageIdentifier) IContextStrategy(org.metaborg.core.context.IContextStrategy) ResourceExtensionFacet(org.metaborg.core.language.ResourceExtensionFacet) ComponentCreationConfig(org.metaborg.core.language.ComponentCreationConfig) HoverFacet(org.metaborg.spoofax.core.tracing.HoverFacet)

Example 2 with LanguageIdentifier

use of org.metaborg.core.language.LanguageIdentifier in project spoofax by metaborg.

the class LanguageComponentsPrimitive method call.

@Override
protected IStrategoTerm call(IStrategoTerm current, Strategy[] svars, IStrategoTerm[] tvars, ITermFactory factory, IContext context) throws MetaborgException, IOException {
    IStrategoList list = factory.makeList();
    for (ILanguageComponent component : context.language().components()) {
        final LanguageIdentifier id = component.id();
        final IStrategoString groupIdTerm = factory.makeString(id.groupId);
        final IStrategoString idTerm = factory.makeString(id.id);
        final IStrategoString versionTerm = factory.makeString(id.version.toString());
        final IStrategoString locationTerm = factory.makeString(component.location().getName().getURI());
        final IStrategoTuple tuple = factory.makeTuple(groupIdTerm, idTerm, versionTerm, locationTerm);
        list = factory.makeListCons(tuple, list);
    }
    return list;
}
Also used : LanguageIdentifier(org.metaborg.core.language.LanguageIdentifier) IStrategoString(org.spoofax.interpreter.terms.IStrategoString) IStrategoTuple(org.spoofax.interpreter.terms.IStrategoTuple) IStrategoList(org.spoofax.interpreter.terms.IStrategoList) ILanguageComponent(org.metaborg.core.language.ILanguageComponent)

Example 3 with LanguageIdentifier

use of org.metaborg.core.language.LanguageIdentifier in project spoofax by metaborg.

the class LanguageImplementationPrimitive method call.

@Override
protected IStrategoTerm call(IStrategoTerm current, Strategy[] svars, IStrategoTerm[] tvars, ITermFactory factory, IContext context) throws MetaborgException, IOException {
    final ILanguageImpl langImpl = context.language();
    final LanguageIdentifier langId = langImpl.id();
    final IStrategoString groupIdTerm = factory.makeString(langId.groupId);
    final IStrategoString idTerm = factory.makeString(langId.id);
    final IStrategoString versionTerm = factory.makeString(langId.version.toString());
    return factory.makeTuple(groupIdTerm, idTerm, versionTerm);
}
Also used : LanguageIdentifier(org.metaborg.core.language.LanguageIdentifier) ILanguageImpl(org.metaborg.core.language.ILanguageImpl) IStrategoString(org.spoofax.interpreter.terms.IStrategoString)

Example 4 with LanguageIdentifier

use of org.metaborg.core.language.LanguageIdentifier 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 5 with LanguageIdentifier

use of org.metaborg.core.language.LanguageIdentifier in project spoofax by metaborg.

the class LanguageSpecBuilder method archiveBuilderInput.

private ArchiveBuilder.Input archiveBuilderInput(LanguageSpecBuildInput input, Origin origin) {
    final ISpoofaxLanguageSpec languageSpec = input.languageSpec();
    final ISpoofaxLanguageSpecConfig config = languageSpec.config();
    final FileObject baseLoc = input.languageSpec().location();
    final SpoofaxLangSpecCommonPaths paths = new SpoofaxLangSpecCommonPaths(baseLoc);
    final FileObject buildInfoLoc = paths.plutoBuildInfoDir();
    final SpoofaxContext context = new SpoofaxContext(baseLoc, buildInfoLoc);
    final Iterable<IExportConfig> exports = config.exports();
    final LanguageIdentifier languageIdentifier = config.identifier();
    return new ArchiveBuilder.Input(context, origin, exports, languageIdentifier);
}
Also used : IExportConfig(org.metaborg.core.config.IExportConfig) BuildInput(org.metaborg.core.build.BuildInput) ISpoofaxLanguageSpec(org.metaborg.spoofax.meta.core.project.ISpoofaxLanguageSpec) LanguageIdentifier(org.metaborg.core.language.LanguageIdentifier) SpoofaxContext(org.metaborg.spoofax.meta.core.pluto.SpoofaxContext) ISpoofaxLanguageSpecConfig(org.metaborg.spoofax.meta.core.config.ISpoofaxLanguageSpecConfig) FileObject(org.apache.commons.vfs2.FileObject)

Aggregations

LanguageIdentifier (org.metaborg.core.language.LanguageIdentifier)21 FileObject (org.apache.commons.vfs2.FileObject)11 ILanguageComponent (org.metaborg.core.language.ILanguageComponent)11 ILanguageImpl (org.metaborg.core.language.ILanguageImpl)11 LanguageVersion (org.metaborg.core.language.LanguageVersion)9 Test (org.junit.Test)8 MetaborgTest (org.metaborg.core.test.MetaborgTest)8 LanguageContributionIdentifier (org.metaborg.core.language.LanguageContributionIdentifier)7 ILanguage (org.metaborg.core.language.ILanguage)6 IStrategoString (org.spoofax.interpreter.terms.IStrategoString)4 MetaborgException (org.metaborg.core.MetaborgException)3 ILanguageComponentConfig (org.metaborg.core.config.ILanguageComponentConfig)3 Nullable (javax.annotation.Nullable)2 IExportConfig (org.metaborg.core.config.IExportConfig)2 ComponentCreationConfig (org.metaborg.core.language.ComponentCreationConfig)2 IFacet (org.metaborg.core.language.IFacet)2 IdentificationFacet (org.metaborg.core.language.IdentificationFacet)2 ResourceExtensionFacet (org.metaborg.core.language.ResourceExtensionFacet)2 ISpoofaxLanguageSpecConfig (org.metaborg.spoofax.meta.core.config.ISpoofaxLanguageSpecConfig)2 ISpoofaxLanguageSpec (org.metaborg.spoofax.meta.core.project.ISpoofaxLanguageSpec)2