Search in sources :

Example 1 with AnalysisFacet

use of org.metaborg.spoofax.core.analysis.AnalysisFacet 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 AnalysisFacet

use of org.metaborg.spoofax.core.analysis.AnalysisFacet in project spoofax by metaborg.

the class SpoofaxLanguageTest method discoverLanguage.

/**
 * The 'res:' filesystem redirects to resources inside the tests' JAR file or class file location, which are copied
 * to the class file location from src/test/resources by Maven. The binary files of the Entity language are located
 * in the resources to test language discovery.
 */
@Test
public void discoverLanguage() throws Exception {
    final FileObject location = resourceService.resolve("res:");
    final Iterable<ILanguageComponent> languages = languageDiscoveryService.discover(languageDiscoveryService.request(location));
    assertEquals(1, Iterables.size(languages));
    final ILanguageComponent component = Iterables.get(languages, 0);
    final ILanguageImpl impl = Iterables.get(component.contributesTo(), 0);
    final ILanguage language = impl.belongsTo();
    assertEquals("Entity", language.name());
    assertEquals(resourceService.resolve("res:Entity"), component.location());
    final IdentificationFacet identificationFacet = impl.facet(IdentificationFacet.class);
    assertTrue(identificationFacet.identify(resourceService.resolve("ram:///Entity/test.ent")));
    final SyntaxFacet syntaxFacet = impl.facet(SyntaxFacet.class);
    assertEquals(resourceService.resolve("res:Entity/target/metaborg/sdf.tbl"), syntaxFacet.parseTable);
    assertIterableEquals(syntaxFacet.startSymbols, "Start");
    final StrategoRuntimeFacet strategoFacet = impl.facet(StrategoRuntimeFacet.class);
    assertIterableEquals(strategoFacet.ctreeFiles, resourceService.resolve("res:Entity/target/metaborg/stratego.ctree"));
    assertIterableEquals(strategoFacet.jarFiles, resourceService.resolve("res:Entity/target/metaborg/stratego-javastrat.jar"));
    final AnalysisFacet analysisFacet = impl.facet(AnalysisFacet.class);
    assertEquals("editor-analyze", analysisFacet.strategyName);
}
Also used : ILanguage(org.metaborg.core.language.ILanguage) SyntaxFacet(org.metaborg.spoofax.core.syntax.SyntaxFacet) ILanguageImpl(org.metaborg.core.language.ILanguageImpl) IdentificationFacet(org.metaborg.core.language.IdentificationFacet) FileObject(org.apache.commons.vfs2.FileObject) AnalysisFacet(org.metaborg.spoofax.core.analysis.AnalysisFacet) ILanguageComponent(org.metaborg.core.language.ILanguageComponent) StrategoRuntimeFacet(org.metaborg.spoofax.core.stratego.StrategoRuntimeFacet) Test(org.junit.Test) LanguageServiceTest(org.metaborg.core.test.language.LanguageServiceTest)

Example 3 with AnalysisFacet

use of org.metaborg.spoofax.core.analysis.AnalysisFacet in project spoofax by metaborg.

the class AbstractConstraintAnalyzer method analyzeAll.

@SuppressWarnings("unchecked")
@Override
public ISpoofaxAnalyzeResults analyzeAll(Iterable<ISpoofaxParseUnit> inputs, IContext genericContext, IProgress progress, ICancel cancel) throws AnalysisException {
    C context;
    try {
        context = (C) genericContext;
    } catch (ClassCastException ex) {
        throw new AnalysisException(genericContext, "Scope graph context required for constraint analysis.", ex);
    }
    final ILanguageImpl langImpl = context.language();
    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;
    final HybridInterpreter runtime;
    try {
        runtime = runtimeService.runtime(facetContribution.contributor, context, false);
    } catch (MetaborgException e) {
        throw new AnalysisException(context, "Failed to get Stratego runtime", e);
    }
    Map<String, ISpoofaxParseUnit> changed = Maps.newHashMap();
    Set<String> removed = Sets.newHashSet();
    for (ISpoofaxParseUnit input : inputs) {
        final String source;
        if (input.detached()) {
            source = "detached-" + UUID.randomUUID().toString();
        } else {
            source = resource(input.source(), context);
        }
        if (!input.valid() || isEmptyAST(input.ast())) {
            removed.add(source);
        } else {
            changed.put(source, input);
        }
    }
    return analyzeAll(changed, removed, context, runtime, facet.strategyName, progress, cancel);
}
Also used : ISpoofaxAnalyzeResults(org.metaborg.spoofax.core.analysis.ISpoofaxAnalyzeResults) SpoofaxAnalyzeResults(org.metaborg.spoofax.core.analysis.SpoofaxAnalyzeResults) ISpoofaxParseUnit(org.metaborg.spoofax.core.unit.ISpoofaxParseUnit) MetaborgException(org.metaborg.core.MetaborgException) HybridInterpreter(org.strategoxt.HybridInterpreter) AnalysisFacet(org.metaborg.spoofax.core.analysis.AnalysisFacet) ILanguageImpl(org.metaborg.core.language.ILanguageImpl) AnalysisException(org.metaborg.core.analysis.AnalysisException)

Example 4 with AnalysisFacet

use of org.metaborg.spoofax.core.analysis.AnalysisFacet in project spoofax by metaborg.

the class StrategoAnalyzer method analyze.

@Override
public ISpoofaxAnalyzeResult analyze(ISpoofaxParseUnit input, IContext context, IProgress progress, ICancel cancel) throws AnalysisException, InterruptedException {
    cancel.throwIfCancelled();
    if (!input.valid()) {
        final String message = logger.format("Parse input for {} is invalid, cannot analyze", input.source());
        throw new AnalysisException(context, message);
    }
    final ILanguageImpl language = context.language();
    final ITermFactory termFactory = termFactoryService.getGeneric();
    final FacetContribution<AnalysisFacet> facetContribution = language.facetContribution(AnalysisFacet.class);
    if (facetContribution == null) {
        logger.debug("No analysis required for {}", language);
        final ISpoofaxAnalyzeUnit emptyUnit = unitService.emptyAnalyzeUnit(input, context);
        return new SpoofaxAnalyzeResult(emptyUnit, context);
    }
    final AnalysisFacet facet = facetContribution.facet;
    cancel.throwIfCancelled();
    final HybridInterpreter runtime;
    try {
        runtime = runtimeService.runtime(facetContribution.contributor, context, true);
    } catch (MetaborgException e) {
        throw new AnalysisException(context, "Failed to get Stratego runtime", e);
    }
    cancel.throwIfCancelled();
    final ISpoofaxAnalyzeUnit result = analyze(input, context, runtime, facet.strategyName, termFactory);
    return new SpoofaxAnalyzeResult(result, context);
}
Also used : ILanguageImpl(org.metaborg.core.language.ILanguageImpl) MetaborgException(org.metaborg.core.MetaborgException) AnalysisException(org.metaborg.core.analysis.AnalysisException) HybridInterpreter(org.strategoxt.HybridInterpreter) SpoofaxAnalyzeResult(org.metaborg.spoofax.core.analysis.SpoofaxAnalyzeResult) ISpoofaxAnalyzeResult(org.metaborg.spoofax.core.analysis.ISpoofaxAnalyzeResult) IStrategoString(org.spoofax.interpreter.terms.IStrategoString) AnalysisFacet(org.metaborg.spoofax.core.analysis.AnalysisFacet) ITermFactory(org.spoofax.interpreter.terms.ITermFactory) ISpoofaxAnalyzeUnit(org.metaborg.spoofax.core.unit.ISpoofaxAnalyzeUnit)

Example 5 with AnalysisFacet

use of org.metaborg.spoofax.core.analysis.AnalysisFacet in project spoofax by metaborg.

the class StrategoAnalyzer method analyzeAll.

@Override
public ISpoofaxAnalyzeResults analyzeAll(Iterable<ISpoofaxParseUnit> inputs, IContext context, IProgress progress, ICancel cancel) throws AnalysisException, InterruptedException {
    cancel.throwIfCancelled();
    final ILanguageImpl language = context.language();
    final ITermFactory termFactory = termFactoryService.getGeneric();
    final FacetContribution<AnalysisFacet> facetContribution = language.facetContribution(AnalysisFacet.class);
    if (facetContribution == null) {
        logger.debug("No analysis required for {}", language);
        return new SpoofaxAnalyzeResults(context);
    }
    final AnalysisFacet facet = facetContribution.facet;
    cancel.throwIfCancelled();
    final HybridInterpreter runtime;
    try {
        runtime = runtimeService.runtime(facetContribution.contributor, context, true);
    } catch (MetaborgException e) {
        throw new AnalysisException(context, "Failed to get Stratego runtime", e);
    }
    final int size = Iterables.size(inputs);
    progress.setWorkRemaining(size);
    final Collection<ISpoofaxAnalyzeUnit> results = Lists.newArrayListWithCapacity(size);
    for (ISpoofaxParseUnit input : inputs) {
        cancel.throwIfCancelled();
        if (!input.valid()) {
            logger.warn("Parse input for {} is invalid, cannot analyze", input.source());
            // TODO: throw exception instead?
            progress.work(1);
            continue;
        }
        final ISpoofaxAnalyzeUnit result = analyze(input, context, runtime, facet.strategyName, termFactory);
        results.add(result);
        progress.work(1);
    }
    return new SpoofaxAnalyzeResults(results, context);
}
Also used : ISpoofaxAnalyzeResults(org.metaborg.spoofax.core.analysis.ISpoofaxAnalyzeResults) SpoofaxAnalyzeResults(org.metaborg.spoofax.core.analysis.SpoofaxAnalyzeResults) ISpoofaxParseUnit(org.metaborg.spoofax.core.unit.ISpoofaxParseUnit) 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) ISpoofaxAnalyzeUnit(org.metaborg.spoofax.core.unit.ISpoofaxAnalyzeUnit)

Aggregations

AnalysisFacet (org.metaborg.spoofax.core.analysis.AnalysisFacet)6 MetaborgException (org.metaborg.core.MetaborgException)5 ILanguageImpl (org.metaborg.core.language.ILanguageImpl)5 AnalysisException (org.metaborg.core.analysis.AnalysisException)4 HybridInterpreter (org.strategoxt.HybridInterpreter)4 ISpoofaxAnalyzeResults (org.metaborg.spoofax.core.analysis.ISpoofaxAnalyzeResults)3 SpoofaxAnalyzeResults (org.metaborg.spoofax.core.analysis.SpoofaxAnalyzeResults)3 ITermFactory (org.spoofax.interpreter.terms.ITermFactory)3 FileObject (org.apache.commons.vfs2.FileObject)2 IdentificationFacet (org.metaborg.core.language.IdentificationFacet)2 StrategoRuntimeFacet (org.metaborg.spoofax.core.stratego.StrategoRuntimeFacet)2 SyntaxFacet (org.metaborg.spoofax.core.syntax.SyntaxFacet)2 ISpoofaxAnalyzeUnit (org.metaborg.spoofax.core.unit.ISpoofaxAnalyzeUnit)2 ISpoofaxParseUnit (org.metaborg.spoofax.core.unit.ISpoofaxParseUnit)2 Test (org.junit.Test)1 ILanguageComponentConfig (org.metaborg.core.config.ILanguageComponentConfig)1 ContextFacet (org.metaborg.core.context.ContextFacet)1 IContextFactory (org.metaborg.core.context.IContextFactory)1 IContextStrategy (org.metaborg.core.context.IContextStrategy)1 ComponentCreationConfig (org.metaborg.core.language.ComponentCreationConfig)1