Search in sources :

Example 1 with IStrategoAppl

use of org.spoofax.interpreter.terms.IStrategoAppl in project spoofax by metaborg.

the class LanguageComponentFactory method esvTerm.

private IStrategoAppl esvTerm(FileObject location, FileObject esvFile) throws ParseError, IOException, MetaborgException {
    final TermReader reader = new TermReader(termFactoryService.getGeneric().getFactoryWithStorageType(IStrategoTerm.MUTABLE));
    final IStrategoTerm term = reader.parseFromStream(esvFile.getContent().getInputStream());
    if (term.getTermType() != IStrategoTerm.APPL) {
        final String message = logger.format("Cannot discover language at {}, ESV file at {} does not contain a valid ESV term", location, esvFile);
        throw new MetaborgException(message);
    }
    return (IStrategoAppl) term;
}
Also used : IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) MetaborgException(org.metaborg.core.MetaborgException) IStrategoAppl(org.spoofax.interpreter.terms.IStrategoAppl) TermReader(org.spoofax.terms.io.binary.TermReader)

Example 2 with IStrategoAppl

use of org.spoofax.interpreter.terms.IStrategoAppl 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 3 with IStrategoAppl

use of org.spoofax.interpreter.terms.IStrategoAppl in project spoofax by metaborg.

the class OutlineFacetFromESV method create.

@Nullable
public static OutlineFacet create(IStrategoAppl esv) {
    final IStrategoAppl outlineTerm = ESVReader.findTerm(esv, "OutlineView");
    if (outlineTerm == null) {
        return null;
    }
    final String strategyName = ESVReader.termContents(outlineTerm.getSubterm(0));
    final int expandTo;
    final IStrategoAppl expandToTerm = ESVReader.findTerm(esv, "ExpandToLevel");
    if (expandToTerm == null) {
        expandTo = 0;
    } else {
        final IStrategoTerm expandToNumberTerm = expandToTerm.getSubterm(0);
        if (expandToNumberTerm instanceof IStrategoInt) {
            final IStrategoInt expandToNumberIntTerm = (IStrategoInt) expandToNumberTerm;
            expandTo = expandToNumberIntTerm.intValue();
        } else if (expandToNumberTerm instanceof IStrategoString) {
            final IStrategoString expandToNumberStringTerm = (IStrategoString) expandToNumberTerm;
            expandTo = Integer.parseInt(expandToNumberStringTerm.stringValue());
        } else {
            expandTo = 0;
        }
    }
    return new OutlineFacet(strategyName, expandTo);
}
Also used : IStrategoInt(org.spoofax.interpreter.terms.IStrategoInt) IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) IStrategoAppl(org.spoofax.interpreter.terms.IStrategoAppl) IStrategoString(org.spoofax.interpreter.terms.IStrategoString) IStrategoString(org.spoofax.interpreter.terms.IStrategoString) Nullable(javax.annotation.Nullable)

Example 4 with IStrategoAppl

use of org.spoofax.interpreter.terms.IStrategoAppl in project spoofax by metaborg.

the class OutlineService method toOutlineNode.

@Nullable
private IOutlineNode toOutlineNode(IStrategoTerm term, @Nullable IOutlineNode parent, FileObject location) {
    if (!(term instanceof IStrategoAppl)) {
        return null;
    }
    final IStrategoAppl appl = (IStrategoAppl) term;
    if (!Tools.hasConstructor(appl, "Node", 2)) {
        return null;
    }
    final IStrategoTerm labelTerm = appl.getSubterm(0);
    final String label = label(labelTerm);
    final FileObject icon = icon(labelTerm, location);
    final ISourceRegion region = region(labelTerm);
    final OutlineNode node = new OutlineNode(label, icon, region, parent);
    final IStrategoTerm nodesTerm = appl.getSubterm(1);
    for (IStrategoTerm nodeTerm : nodesTerm) {
        final IOutlineNode childNode = toOutlineNode(nodeTerm, node, location);
        if (childNode != null) {
            node.addChild(childNode);
        }
    }
    return node;
}
Also used : IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) ISourceRegion(org.metaborg.core.source.ISourceRegion) IStrategoAppl(org.spoofax.interpreter.terms.IStrategoAppl) OutlineNode(org.metaborg.core.outline.OutlineNode) IOutlineNode(org.metaborg.core.outline.IOutlineNode) IStrategoString(org.spoofax.interpreter.terms.IStrategoString) FileObject(org.apache.commons.vfs2.FileObject) IOutlineNode(org.metaborg.core.outline.IOutlineNode) Nullable(javax.annotation.Nullable)

Example 5 with IStrategoAppl

use of org.spoofax.interpreter.terms.IStrategoAppl in project spoofax by metaborg.

the class ResolverFacetFromESV method createResolver.

@Nullable
public static ResolverFacet createResolver(IStrategoAppl esv) {
    final IStrategoAppl resolver = ESVReader.findTerm(esv, "ReferenceRule");
    if (resolver == null) {
        return null;
    }
    final String strategyName = ESVReader.termContents(termAt(resolver, 1));
    if (strategyName == null) {
        logger.error("Could not get contents of ESV ReferenceRule {}, cannot create resolver facet", resolver);
        return null;
    }
    return new ResolverFacet(strategyName);
}
Also used : IStrategoAppl(org.spoofax.interpreter.terms.IStrategoAppl) Nullable(javax.annotation.Nullable)

Aggregations

IStrategoAppl (org.spoofax.interpreter.terms.IStrategoAppl)42 IStrategoTerm (org.spoofax.interpreter.terms.IStrategoTerm)26 IStrategoString (org.spoofax.interpreter.terms.IStrategoString)21 ICompletion (org.metaborg.core.completion.ICompletion)13 Nullable (javax.annotation.Nullable)12 IStrategoList (org.spoofax.interpreter.terms.IStrategoList)9 ITermFactory (org.spoofax.interpreter.terms.ITermFactory)8 ListImploderAttachment (org.spoofax.jsglr.client.imploder.ListImploderAttachment)8 Completion (org.metaborg.core.completion.Completion)7 ITokens (org.spoofax.jsglr.client.imploder.ITokens)7 ImploderAttachment (org.spoofax.jsglr.client.imploder.ImploderAttachment)7 StrategoTerm (org.spoofax.terms.StrategoTerm)7 HybridInterpreter (org.strategoxt.HybridInterpreter)7 FileObject (org.apache.commons.vfs2.FileObject)6 MetaborgException (org.metaborg.core.MetaborgException)6 IStrategoTuple (org.spoofax.interpreter.terms.IStrategoTuple)6 CompletionKind (org.metaborg.core.completion.CompletionKind)5 StrategoAppl (org.spoofax.terms.StrategoAppl)5 IToken (org.spoofax.jsglr.client.imploder.IToken)4 ISourceRegion (org.metaborg.core.source.ISourceRegion)3