Search in sources :

Example 1 with ITermFactory

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

the class StrategoCommon method locationTerm.

@Override
public IStrategoString locationTerm(FileObject location) {
    final ITermFactory termFactory = termFactoryService.getGeneric();
    final String locationURI = location.getName().getURI();
    final IStrategoString locationTerm = termFactory.makeString(locationURI);
    return locationTerm;
}
Also used : IStrategoString(org.spoofax.interpreter.terms.IStrategoString) IStrategoString(org.spoofax.interpreter.terms.IStrategoString) ITermFactory(org.spoofax.interpreter.terms.ITermFactory)

Example 2 with ITermFactory

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

the class StrategoCommon method prettyPrint.

@Override
public IStrategoString prettyPrint(IStrategoTerm term) {
    final Context context = strategoRuntimeService.genericRuntime().getCompiledContext();
    final ITermFactory termFactory = termFactoryService.getGeneric();
    org.strategoxt.stratego_aterm.Main.init(context);
    term = aterm_escape_strings_0_0.instance.invoke(context, term);
    term = pp_aterm_box_0_0.instance.invoke(context, term);
    term = box2text_string_0_1.instance.invoke(context, term, termFactory.makeInt(120));
    return (IStrategoString) term;
}
Also used : Context(org.strategoxt.lang.Context) IContext(org.metaborg.core.context.IContext) IStrategoString(org.spoofax.interpreter.terms.IStrategoString) ITermFactory(org.spoofax.interpreter.terms.ITermFactory)

Example 3 with ITermFactory

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

the class StrategoRuntimeService method createPrototype.

private HybridInterpreter createPrototype(ILanguageComponent component) throws MetaborgException {
    logger.debug("Creating prototype runtime for {}", component);
    final ITermFactory termFactory = termFactoryService.get(component, null, false);
    final HybridInterpreter runtime = createNew(termFactory);
    loadFiles(runtime, component);
    prototypes.put(component, runtime);
    return runtime;
}
Also used : HybridInterpreter(org.strategoxt.HybridInterpreter) ITermFactory(org.spoofax.interpreter.terms.ITermFactory)

Example 4 with ITermFactory

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

the class JSGLRParseService method parse.

@Override
public ISpoofaxParseUnit parse(ISpoofaxInputUnit input, IProgress progress, ICancel cancel) throws ParseException {
    final FileObject source = input.source();
    final ILanguageImpl langImpl;
    final ILanguageImpl base;
    if (input.dialect() != null) {
        langImpl = input.dialect();
        base = input.langImpl();
    } else {
        langImpl = input.langImpl();
        base = null;
    }
    final String text = input.text();
    final ITermFactory termFactory = termFactoryService.get(langImpl, null, false);
    final IParserConfig config;
    JSGLRParserConfiguration parserConfig = input.config();
    if (parserConfig == null) {
        parserConfig = defaultParserConfig;
    }
    if (parserConfig.completion) {
        config = getCompletionParserConfig(langImpl, input);
    } else {
        config = getParserConfig(langImpl, input);
    }
    try {
        logger.trace("Parsing {}", source);
        JSGLRVersion version = jsglrVersion(input);
        final JSGLRI<?> parser;
        boolean dataDependentParsing = false;
        for (ILanguageComponent component : langImpl.components()) {
            if (component.config().dataDependent()) {
                dataDependentParsing = true;
            }
        }
        if (version == JSGLRVersion.v2) {
            if (dataDependentParsing) {
                parser = new JSGLR2I(config, termFactory, langImpl, null, source, text, true);
            } else {
                parser = new JSGLR2I(config, termFactory, langImpl, null, source, text, false);
            }
        } else {
            if (base != null) {
                parser = new JSGLR1I(config, termFactory, base, langImpl, source, text);
            } else {
                parser = new JSGLR1I(config, termFactory, langImpl, null, source, text);
            }
        }
        final ParseContrib contrib = parser.parse(parserConfig);
        if (version == JSGLRVersion.v2) {
            if (contrib.valid)
                logger.info("Valid JSGLR2 parse");
            else
                logger.info("Invalid JSGLR2 parse");
        }
        final ISpoofaxParseUnit unit = unitService.parseUnit(input, contrib);
        return unit;
    } catch (IOException | InvalidParseTableException | ParseTableReadException e) {
        throw new ParseException(input, e);
    }
}
Also used : ISpoofaxParseUnit(org.metaborg.spoofax.core.unit.ISpoofaxParseUnit) ParseContrib(org.metaborg.spoofax.core.unit.ParseContrib) JSGLRVersion(org.metaborg.core.config.JSGLRVersion) IOException(java.io.IOException) ILanguageImpl(org.metaborg.core.language.ILanguageImpl) FileObject(org.apache.commons.vfs2.FileObject) ParseException(org.metaborg.core.syntax.ParseException) ITermFactory(org.spoofax.interpreter.terms.ITermFactory) ILanguageComponent(org.metaborg.core.language.ILanguageComponent) InvalidParseTableException(org.spoofax.jsglr.client.InvalidParseTableException) ParseTableReadException(org.spoofax.jsglr2.parsetable.ParseTableReadException)

Example 5 with ITermFactory

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

the class JSGLRParseService method getParserConfig.

public IParserConfig getParserConfig(ILanguageImpl lang, ISpoofaxInputUnit input) throws ParseException {
    IParserConfig config = parserConfigs.get(lang);
    if (config == null) {
        final ITermFactory termFactory = termFactoryService.getGeneric().getFactoryWithStorageType(IStrategoTerm.MUTABLE);
        final SyntaxFacet facet = lang.facet(SyntaxFacet.class);
        FileObject parseTable = null;
        boolean incrementalPTGen = false;
        for (ILanguageComponent component : lang.components()) {
            if (component.config().sdfEnabled() && component.config().sdf2tableVersion() == Sdf2tableVersion.incremental) {
                incrementalPTGen = true;
            }
        }
        if (facet.parseTable == null) {
            try {
                boolean multipleTables = false;
                for (ILanguageComponent component : lang.components()) {
                    if (component.config().sdfEnabled()) {
                        if (component.config().parseTable() != null) {
                            if (multipleTables) {
                                logger.error("Different components are specifying multiple parse tables.");
                                throw new ParseException(input);
                            }
                            parseTable = component.location().resolveFile(component.config().parseTable());
                            multipleTables = true;
                        }
                    }
                }
            } catch (FileSystemException e) {
                logger.error("Parse table not found or sdf is not enabled for this language.");
                throw new ParseException(input, e);
            }
        } else {
            parseTable = facet.parseTable;
        }
        try {
            if (parseTable == null || !parseTable.exists()) {
                logger.error("Parse table not found or sdf is not enabled for this language.");
                throw new ParseException(input);
            }
        } catch (FileSystemException e) {
            logger.error("Parse table not found or sdf is not enabled for this language.");
            throw new ParseException(input, e);
        }
        final IParseTableProvider provider;
        JSGLRVersion version = jsglrVersion(input);
        if (version == JSGLRVersion.v2) {
            provider = new JSGLR2FileParseTableProvider(parseTable, termFactory);
        } else {
            final ParseTable referenceParseTable = referenceParseTables.get(lang);
            if (referenceParseTable != null && incrementalPTGen) {
                provider = new JSGLR1IncrementalParseTableProvider(parseTable, termFactory, referenceParseTable);
            } else {
                provider = new JSGLR1FileParseTableProvider(parseTable, termFactory);
            }
        }
        config = new ParserConfig(Iterables.get(facet.startSymbols, 0), provider);
        parserConfigs.put(lang, config);
    }
    return config;
}
Also used : ParseTable(org.metaborg.sdf2table.parsetable.ParseTable) JSGLRVersion(org.metaborg.core.config.JSGLRVersion) 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)

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