@@ -0,0 +1,2023 @@
+package org.metaborg.meta.lang.dynsem.interpreter;%0A%0Aimport java.io.File;%0Aimport java.io.FileInputStream;%0Aimport java.io.IOException;%0Aimport java.io.InputStream;%0Aimport java.nio.file.Path;%0A%0Aimport org.apache.commons.io.IOUtils;%0Aimport org.spoofax.interpreter.terms.IStrategoTerm;%0Aimport org.spoofax.jsglr.client.ParseTable;%0Aimport org.spoofax.jsglr.client.SGLR;%0Aimport org.spoofax.jsglr.client.SGLRParseResult;%0Aimport org.spoofax.jsglr.client.imploder.TermTreeFactory;%0Aimport org.spoofax.jsglr.client.imploder.TreeBuilder;%0Aimport org.spoofax.jsglr.shared.SGLRException;%0Aimport org.spoofax.terms.TermFactory;%0Aimport org.spoofax.terms.attachments.ParentTermFactory;%0Aimport org.spoofax.terms.io.binary.TermReader;%0A%0Aimport com.oracle.truffle.api.source.Source;%0A%0Apublic class DynSemLanguageParser %7B%0A%09private Path parsetable;%0A%09private SGLR parser;%0A%0A%09public DynSemLanguageParser(Path parsetable) %7B%0A%09%09this.parsetable = parsetable;%0A%09%7D%0A%0A%09public IStrategoTerm parse(Source src, String startSymbol) %7B%0A%09%09if (parser == null) %7B%0A%09%09%09createParser();%0A%09%09%7D%0A%0A%09%09try %7B%0A%09%09%09SGLRParseResult parseResult = parser.parse(%0A%09%09%09%09%09IOUtils.toString(src.getInputStream()), src.getName(),%0A%09%09%09%09%09startSymbol);%0A%09%09%09IStrategoTerm term = (IStrategoTerm) parseResult.output;%0A%09%09%09return term;%0A%09%09%7D catch (SGLRException %7C InterruptedException %7C IOException e) %7B%0A%09%09%09throw new IllegalStateException(%22File failed to parse%22, e);%0A%09%09%7D%0A%0A%09%7D%0A%0A%09private void createParser() %7B%0A%09%09TreeBuilder treebuilder = new TreeBuilder(new TermTreeFactory(%0A%09%09%09%09new ParentTermFactory(new TermFactory())));%0A%0A%09%09parser = new SGLR(treebuilder, loadPT());%0A%09%09parser.setUseStructureRecovery(false);%0A%09%7D%0A%0A%09private ParseTable loadPT() %7B%0A%09%09TermFactory factory = new TermFactory();%0A%09%09try (InputStream stream = new FileInputStream(new File(%0A%09%09%09%09parsetable.toUri()));) %7B%0A%09%09%09TermReader termReader = new TermReader(factory);%0A%09%09%09IStrategoTerm parseTableTerm = termReader.parseFromStream(stream);%0A%0A%09%09%09return new ParseTable(parseTableTerm, factory);%0A%09%09%7D catch (Exception e) %7B%0A%09%09%09throw new RuntimeException(e);%0A%09%09%7D%0A%09%7D%0A%7D%0A

Extract language-independent object language parser implementation into the DynSem interpreter