Search in sources :

Example 1 with TermReader

use of org.spoofax.terms.io.binary.TermReader 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 TermReader

use of org.spoofax.terms.io.binary.TermReader in project spoofax by metaborg.

the class DialectIdentifier method identify.

@Override
public IdentifiedDialect identify(FileObject resource) throws MetaborgException {
    final ILanguage strategoLanguage = languageService.getLanguage(SpoofaxConstants.LANG_STRATEGO_NAME);
    if (strategoLanguage == null) {
        final String message = logger.format("Could not find Stratego language, Stratego dialects cannot be identified for resource: {}", resource);
        throw new MetaborgRuntimeException(message);
    }
    // GTODO: use identifier service instead, but that introduces a cyclic dependency. Could use a provider.
    final ILanguageImpl strategoImpl = strategoLanguage.activeImpl();
    if (strategoImpl == null) {
        return null;
    }
    // HACK: assuming single identification facet
    final IdentificationFacet facet = strategoImpl.facet(IdentificationFacet.class);
    if (facet == null || !facet.identify(resource)) {
        return null;
    }
    try {
        final FileObject metaResource = metaResource(resource);
        if (metaResource == null) {
            return null;
        }
        final TermReader termReader = new TermReader(termFactoryService.getGeneric());
        final IStrategoTerm term = termReader.parseFromStream(metaResource.getContent().getInputStream());
        final String name = getSyntaxName(term.getSubterm(0));
        if (name == null) {
            return null;
        }
        final ILanguageImpl dialect = dialectService.getDialect(name);
        if (dialect == null) {
            final String message = String.format("Resource %s requires dialect %s, but that dialect does not exist", resource, name);
            throw new MetaborgException(message);
        }
        final ILanguageImpl base = dialectService.getBase(dialect);
        return new IdentifiedDialect(dialect, base);
    } catch (ParseError | IOException e) {
        throw new MetaborgException("Unable to open or parse .meta file", e);
    }
}
Also used : MetaborgRuntimeException(org.metaborg.core.MetaborgRuntimeException) IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) IdentificationFacet(org.metaborg.core.language.IdentificationFacet) MetaborgException(org.metaborg.core.MetaborgException) IOException(java.io.IOException) ILanguage(org.metaborg.core.language.ILanguage) ILanguageImpl(org.metaborg.core.language.ILanguageImpl) ParseError(org.spoofax.terms.ParseError) FileObject(org.apache.commons.vfs2.FileObject) IdentifiedDialect(org.metaborg.core.language.dialect.IdentifiedDialect) TermReader(org.spoofax.terms.io.binary.TermReader)

Example 3 with TermReader

use of org.spoofax.terms.io.binary.TermReader in project spoofax by metaborg.

the class JSGLR1FileParseTableProvider method parseTable.

@Override
public ParseTable parseTable() throws IOException {
    if (parseTable != null) {
        return parseTable;
    }
    resource.refresh();
    if (!resource.exists()) {
        throw new IOException("Could not load parse table from " + resource + ", file does not exist");
    }
    try (final InputStream stream = resource.getContent().getInputStream()) {
        final TermReader termReader = new TermReader(termFactory);
        IStrategoTerm parseTableTerm = termReader.parseFromStream(stream);
        FileObject persistedTable = resource.getParent().resolveFile("table.bin");
        if (persistedTable.exists()) {
            parseTable = new ParseTable(parseTableTerm, termFactory, persistedTable);
        } else {
            parseTable = new ParseTable(parseTableTerm, termFactory);
        }
    } catch (Exception e) {
        throw new IOException("Could not load parse table from " + resource, e);
    }
    return parseTable;
}
Also used : ParseTable(org.spoofax.jsglr.client.ParseTable) IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) InputStream(java.io.InputStream) IOException(java.io.IOException) FileObject(org.apache.commons.vfs2.FileObject) IOException(java.io.IOException) TermReader(org.spoofax.terms.io.binary.TermReader)

Example 4 with TermReader

use of org.spoofax.terms.io.binary.TermReader in project spoofax by metaborg.

the class JSGLR1IncrementalParseTableProvider method parseTable.

@Override
public ParseTable parseTable() throws IOException {
    if (parseTable != null) {
        return parseTable;
    }
    resource.refresh();
    if (!resource.exists()) {
        throw new IOException("Could not load parse table from " + resource + ", file does not exist");
    }
    try (final InputStream stream = resource.getContent().getInputStream()) {
        final TermReader termReader = new TermReader(termFactory);
        final IStrategoTerm parseTableTerm = termReader.parseFromStream(stream);
        FileObject persistedTable = resource.getParent().resolveFile("table.bin");
        if (persistedTable.exists()) {
            if (referenceTable != null) {
                parseTable = new ParseTable(parseTableTerm, termFactory, persistedTable, referenceTable);
            } else {
                parseTable = new ParseTable(parseTableTerm, termFactory, persistedTable);
            }
        } else {
            parseTable = new ParseTable(parseTableTerm, termFactory);
        }
    } catch (Exception e) {
        throw new IOException("Could not load parse table from " + resource, e);
    }
    return parseTable;
}
Also used : ParseTable(org.spoofax.jsglr.client.ParseTable) IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) InputStream(java.io.InputStream) IOException(java.io.IOException) FileObject(org.apache.commons.vfs2.FileObject) IOException(java.io.IOException) TermReader(org.spoofax.terms.io.binary.TermReader)

Example 5 with TermReader

use of org.spoofax.terms.io.binary.TermReader in project spoofax by metaborg.

the class JSGLR2FileParseTableProvider method parseTable.

@Override
public IParseTable parseTable() throws IOException {
    if (parseTable != null) {
        return parseTable;
    }
    resource.refresh();
    if (!resource.exists()) {
        throw new IOException("Could not load parse table from " + resource + ", file does not exist");
    }
    try (final InputStream stream = resource.getContent().getInputStream()) {
        final TermReader termReader = new TermReader(termFactory);
        IStrategoTerm parseTableTerm = termReader.parseFromStream(stream);
        FileObject persistedTable = resource.getParent().resolveFile("table.bin");
        if (!persistedTable.exists()) {
            parseTable = new ParseTableReader(new CharacterClassFactory(true, true), new ActionsFactory(true), new StateFactory()).read(parseTableTerm);
        } else {
            parseTable = new ParseTableReader(new CharacterClassFactory(true, true), new ActionsFactory(true), new StateFactory()).read(parseTableTerm, persistedTable);
        }
    } catch (Exception e) {
        throw new IOException("Could not load parse table from " + resource, e);
    }
    return parseTable;
}
Also used : ParseTableReader(org.spoofax.jsglr2.parsetable.ParseTableReader) IStrategoTerm(org.spoofax.interpreter.terms.IStrategoTerm) StateFactory(org.spoofax.jsglr2.states.StateFactory) CharacterClassFactory(org.metaborg.characterclasses.CharacterClassFactory) InputStream(java.io.InputStream) IOException(java.io.IOException) FileObject(org.apache.commons.vfs2.FileObject) IOException(java.io.IOException) TermReader(org.spoofax.terms.io.binary.TermReader) ActionsFactory(org.spoofax.jsglr2.actions.ActionsFactory)

Aggregations

IStrategoTerm (org.spoofax.interpreter.terms.IStrategoTerm)5 TermReader (org.spoofax.terms.io.binary.TermReader)5 IOException (java.io.IOException)4 FileObject (org.apache.commons.vfs2.FileObject)4 InputStream (java.io.InputStream)3 MetaborgException (org.metaborg.core.MetaborgException)2 ParseTable (org.spoofax.jsglr.client.ParseTable)2 CharacterClassFactory (org.metaborg.characterclasses.CharacterClassFactory)1 MetaborgRuntimeException (org.metaborg.core.MetaborgRuntimeException)1 ILanguage (org.metaborg.core.language.ILanguage)1 ILanguageImpl (org.metaborg.core.language.ILanguageImpl)1 IdentificationFacet (org.metaborg.core.language.IdentificationFacet)1 IdentifiedDialect (org.metaborg.core.language.dialect.IdentifiedDialect)1 IStrategoAppl (org.spoofax.interpreter.terms.IStrategoAppl)1 ActionsFactory (org.spoofax.jsglr2.actions.ActionsFactory)1 ParseTableReader (org.spoofax.jsglr2.parsetable.ParseTableReader)1 StateFactory (org.spoofax.jsglr2.states.StateFactory)1 ParseError (org.spoofax.terms.ParseError)1