Search in sources :

Example 6 with ITermFactory

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

the class JSGLRParseService method getCompletionParserConfig.

public IParserConfig getCompletionParserConfig(ILanguageImpl lang, ISpoofaxInputUnit input) throws ParseException {
    IParserConfig config = completionParserConfigs.get(lang);
    if (config == null) {
        final ITermFactory termFactory = termFactoryService.getGeneric().getFactoryWithStorageType(IStrategoTerm.MUTABLE);
        final SyntaxFacet facet = lang.facet(SyntaxFacet.class);
        FileObject completionParseTable = null;
        boolean incrementalPTGen = false;
        for (ILanguageComponent component : lang.components()) {
            if (component.config().sdfEnabled() && component.config().sdf2tableVersion() == Sdf2tableVersion.incremental) {
                incrementalPTGen = true;
            }
        }
        if (facet.completionParseTable == null) {
            try {
                boolean multipleTables = false;
                for (ILanguageComponent component : lang.components()) {
                    if (component.config().sdfEnabled()) {
                        if (component.config().completionsParseTable() != null) {
                            if (multipleTables) {
                                logger.error("Different components are specifying multiple completion parse tables.");
                                throw new ParseException(input);
                            }
                            completionParseTable = component.location().resolveFile(component.config().completionsParseTable());
                            multipleTables = true;
                        }
                    }
                }
            } catch (FileSystemException e) {
                logger.error("Completion parse table not found or sdf is not enabled for this language.");
                throw new ParseException(input, e);
            }
        } else {
            completionParseTable = facet.completionParseTable;
        }
        try {
            if (completionParseTable == null || !completionParseTable.exists()) {
                logger.error("Completion parse table not found or sdf is not enabled for this language.");
                throw new ParseException(input);
            }
        } catch (FileSystemException e) {
            logger.error("Completion parse table not found or sdf is not enabled for this language.");
            throw new ParseException(input, e);
        }
        final IParseTableProvider provider;
        final ParseTable referenceParseTable = referenceCompletionParseTables.get(lang);
        if (referenceParseTable != null && incrementalPTGen) {
            provider = new JSGLR1IncrementalParseTableProvider(completionParseTable, termFactory, referenceParseTable);
        } else {
            provider = new JSGLR1FileParseTableProvider(completionParseTable, termFactory);
        }
        config = new ParserConfig(Iterables.get(facet.startSymbols, 0), provider);
        completionParserConfigs.put(lang, config);
    }
    return config;
}
Also used : ParseTable(org.metaborg.sdf2table.parsetable.ParseTable) FileSystemException(org.apache.commons.vfs2.FileSystemException) FileObject(org.apache.commons.vfs2.FileObject) ParseException(org.metaborg.core.syntax.ParseException) ITermFactory(org.spoofax.interpreter.terms.ITermFactory) ILanguageComponent(org.metaborg.core.language.ILanguageComponent)

Example 7 with ITermFactory

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

the class HoverService method hover.

@Override
public Hover hover(int offset, ISpoofaxParseUnit result) throws MetaborgException {
    if (!result.valid()) {
        return null;
    }
    final FileObject source = result.source();
    final IProject project = projectService.get(source);
    final ILanguageImpl langImpl = result.input().langImpl();
    @Nullable IContext context;
    if (project == null) {
        context = null;
    } else {
        try {
            context = contextService.get(source, project, langImpl);
        } catch (ContextException | MetaborgRuntimeException e) {
            // Failed to get a context, ignore and use the source file to get a stratego runtime later.
            context = null;
        }
    }
    final FacetContribution<HoverFacet> facetContrib = facet(langImpl);
    final HoverFacet facet = facetContrib.facet;
    final ILanguageComponent contributor = facetContrib.contributor;
    final String strategy = facet.strategyName;
    try {
        final ITermFactory termFactory = termFactoryService.get(contributor, project, true);
        final HybridInterpreter interpreter;
        if (context == null) {
            interpreter = strategoRuntimeService.runtime(contributor, source, true);
        } else {
            interpreter = strategoRuntimeService.runtime(contributor, context, true);
        }
        final Iterable<IStrategoTerm> inRegion = tracingService.fragments(result, new SourceRegion(offset));
        final TermWithRegion tuple = common.outputs(termFactory, interpreter, context.location(), source, result.ast(), inRegion, strategy);
        return hover(tuple);
    } catch (MetaborgException e) {
        throw new MetaborgException("Getting hover tooltip information failed unexpectedly", e);
    }
}
Also used : MetaborgRuntimeException(org.metaborg.core.MetaborgRuntimeException) IContext(org.metaborg.core.context.IContext) IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) MetaborgException(org.metaborg.core.MetaborgException) HybridInterpreter(org.strategoxt.HybridInterpreter) ISourceRegion(org.metaborg.core.source.ISourceRegion) SourceRegion(org.metaborg.core.source.SourceRegion) IProject(org.metaborg.core.project.IProject) ContextException(org.metaborg.core.context.ContextException) TermWithRegion(org.metaborg.spoofax.core.tracing.TracingCommon.TermWithRegion) ILanguageImpl(org.metaborg.core.language.ILanguageImpl) FileObject(org.apache.commons.vfs2.FileObject) ILanguageComponent(org.metaborg.core.language.ILanguageComponent) ITermFactory(org.spoofax.interpreter.terms.ITermFactory) Nullable(javax.annotation.Nullable)

Example 8 with ITermFactory

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

the class ResolverService method resolve.

@Override
public Resolution resolve(int offset, ISpoofaxAnalyzeUnit result) throws MetaborgException {
    if (!result.valid() || !result.hasAst()) {
        return null;
    }
    final FileObject source = result.source();
    final IContext context = result.context();
    final IProject project = context.project();
    final ILanguageImpl language = context.language();
    final FacetContribution<ResolverFacet> facetContrib = facet(language);
    final ResolverFacet facet = facetContrib.facet;
    final String strategy = facet.strategyName;
    try {
        final ITermFactory termFactory = termFactoryService.get(facetContrib.contributor, project, true);
        final HybridInterpreter interpreter = strategoRuntimeService.runtime(facetContrib.contributor, context, true);
        final Iterable<IStrategoTerm> inRegion = tracingService.fragments(result, new SourceRegion(offset));
        final TermWithRegion tuple;
        try (IClosableLock lock = context.read()) {
            tuple = common.outputs(termFactory, interpreter, source, source, result.ast(), inRegion, strategy);
        }
        return resolve(tuple);
    } catch (MetaborgException e) {
        throw new MetaborgException("Reference resolution failed", e);
    }
}
Also used : IContext(org.metaborg.core.context.IContext) IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) MetaborgException(org.metaborg.core.MetaborgException) HybridInterpreter(org.strategoxt.HybridInterpreter) ISourceRegion(org.metaborg.core.source.ISourceRegion) SourceRegion(org.metaborg.core.source.SourceRegion) IProject(org.metaborg.core.project.IProject) TermWithRegion(org.metaborg.spoofax.core.tracing.TracingCommon.TermWithRegion) ILanguageImpl(org.metaborg.core.language.ILanguageImpl) FileObject(org.apache.commons.vfs2.FileObject) IClosableLock(org.metaborg.util.concurrent.IClosableLock) ITermFactory(org.spoofax.interpreter.terms.ITermFactory)

Example 9 with ITermFactory

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

the class GetContextualGrammarPrimitive method call.

@Override
public boolean call(IContext env, Strategy[] svars, IStrategoTerm[] tvars) throws InterpreterException {
    org.metaborg.core.context.IContext context = (org.metaborg.core.context.IContext) env.contextObject();
    String path = ((IStrategoString) tvars[0]).stringValue();
    ResourceService rs = context.injector().getInstance(ResourceService.class);
    FileObject fo = rs.resolve(path + "/target/metaborg/ctxgrammar.aterm");
    InputStream inputStream;
    String text = "";
    try {
        inputStream = fo.getContent().getInputStream();
        text = IOUtils.toString(inputStream, StandardCharsets.UTF_8.name());
    } catch (IOException e) {
        e.printStackTrace();
    }
    final ITermFactory tf = env.getFactory();
    env.setCurrent(tf.parseFromString(text));
    return true;
}
Also used : IContext(org.spoofax.interpreter.core.IContext) InputStream(java.io.InputStream) ResourceService(org.metaborg.core.resource.ResourceService) IStrategoString(org.spoofax.interpreter.terms.IStrategoString) IStrategoString(org.spoofax.interpreter.terms.IStrategoString) IOException(java.io.IOException) FileObject(org.apache.commons.vfs2.FileObject) ITermFactory(org.spoofax.interpreter.terms.ITermFactory)

Example 10 with ITermFactory

use of org.spoofax.interpreter.terms.ITermFactory 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)

Aggregations

ITermFactory (org.spoofax.interpreter.terms.ITermFactory)28 HybridInterpreter (org.strategoxt.HybridInterpreter)17 IStrategoTerm (org.spoofax.interpreter.terms.IStrategoTerm)15 IStrategoString (org.spoofax.interpreter.terms.IStrategoString)14 FileObject (org.apache.commons.vfs2.FileObject)11 ILanguageImpl (org.metaborg.core.language.ILanguageImpl)11 MetaborgException (org.metaborg.core.MetaborgException)9 ILanguageComponent (org.metaborg.core.language.ILanguageComponent)9 IStrategoAppl (org.spoofax.interpreter.terms.IStrategoAppl)9 ICompletion (org.metaborg.core.completion.ICompletion)8 IStrategoTuple (org.spoofax.interpreter.terms.IStrategoTuple)6 StrategoAppl (org.spoofax.terms.StrategoAppl)6 IContext (org.metaborg.core.context.IContext)5 ISourceRegion (org.metaborg.core.source.ISourceRegion)5 SourceRegion (org.metaborg.core.source.SourceRegion)5 IProject (org.metaborg.core.project.IProject)4 TermWithRegion (org.metaborg.spoofax.core.tracing.TracingCommon.TermWithRegion)4 IOException (java.io.IOException)3 AnalysisException (org.metaborg.core.analysis.AnalysisException)3 ParseException (org.metaborg.core.syntax.ParseException)3