Search in sources :

Example 1 with ParseTable

use of org.metaborg.sdf2table.parsetable.ParseTable 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)

Example 2 with ParseTable

use of org.metaborg.sdf2table.parsetable.ParseTable 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 3 with ParseTable

use of org.metaborg.sdf2table.parsetable.ParseTable in project spoofax by metaborg.

the class Sdf2Parenthesize method build.

@Override
public OutputPersisted<File> build(Input input) throws IOException {
    require(input.inputFile);
    boolean status = true;
    try {
        InputStream out = Files.asByteSource(input.inputFile).openStream();
        ObjectInputStream ois = new ObjectInputStream(out);
        // read persisted normalized grammar
        ParseTable table = (ParseTable) ois.readObject();
        Parenthesizer.generateParenthesizer(input.inputModule, input.outputFile, table);
        ois.close();
        out.close();
    } catch (Exception e) {
        System.out.println("Failed to generate parenthesizer");
        e.printStackTrace();
        status = false;
    }
    provide(input.outputFile);
    setState(State.finished(status));
    return OutputPersisted.of(input.outputFile);
}
Also used : ParseTable(org.metaborg.sdf2table.parsetable.ParseTable) ObjectInputStream(java.io.ObjectInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) ObjectInputStream(java.io.ObjectInputStream)

Aggregations

ParseTable (org.metaborg.sdf2table.parsetable.ParseTable)3 FileObject (org.apache.commons.vfs2.FileObject)2 FileSystemException (org.apache.commons.vfs2.FileSystemException)2 ILanguageComponent (org.metaborg.core.language.ILanguageComponent)2 ParseException (org.metaborg.core.syntax.ParseException)2 ITermFactory (org.spoofax.interpreter.terms.ITermFactory)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 ObjectInputStream (java.io.ObjectInputStream)1 JSGLRVersion (org.metaborg.core.config.JSGLRVersion)1